对于返回相同结果的不同查询,我有两个查询计划 我想知道是否有人可以告诉我哪个“更好”,以及为什么。
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)"
第二个陈述和计划:
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)"
最佳答案
它不足以产生任何真正的不同。我会使用第二个,因为第一个的语法相当不规则,这使得它更难维护。略。
你也可以试试这个:
select b.*
from (select id
from users
order by ranking desc
limit 1) u
join bids b on b.runner_id = u.id
limit 1
最后的限制 1 可能是多余的。
关于sql - 哪个查询计划更快/更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29764386/