mardi 21 avril 2015

Which query plan is faster/better

I have two query plans for different queries that return the same results I'm wondering if someone can tell me which one is "better", and why.

SELECT * 
FROM bids order by (select ranking from users where users.id = runner_id) DESC limit 1


"Limit  (cost=17.62..17.62 rows=1 width=568)"
"  ->  Sort  (cost=17.62..17.62 rows=2 width=568)"
"        Sort Key: ((SubPlan 1))"
"        ->  Seq Scan on bids  (cost=0.00..17.61 rows=2 width=568)"
"              SubPlan 1"
"                ->  Index Scan using users_pkey on users  (cost=0.28..8.29 rows=1 width=4)"
"                      Index Cond: (id = bids.runner_id)"

Second statement and plan:

SELECT  "bids".* 
FROM "bids" inner join users u on bids.runner_id=u.id  ORDER BY u.ranking DESC LIMIT 1

"Limit  (cost=17.64..17.64 rows=1 width=572)"
"  ->  Sort  (cost=17.64..17.64 rows=2 width=572)"
"        Sort Key: u.ranking"
"        ->  Nested Loop  (cost=0.28..17.63 rows=2 width=572)"
"              ->  Seq Scan on bids  (cost=0.00..1.02 rows=2 width=568)"
"              ->  Index Scan using users_pkey on users u  (cost=0.28..8.29 rows=1 width=8)"
"                    Index Cond: (id = bids.runner_id)"

Aucun commentaire:

Enregistrer un commentaire