最初发布:https : //stackoverflow.com/questions/11173717/expensive-query-on-select-distinct-with-multiple-inner-join-in-postgres
歌曲表只有大约 4k 行,帖子和电台更少。运行查询而不DISTINCT ON修复它。
在 Mac OS X Lion 上运行 Postgres。
Song Load (7358.2ms)
EXPLAIN (426.2ms)
EXPLAIN for:
SELECT DISTINCT ON (songs.rank, songs.shared_id) songs.*,
songs.*,
posts.url as post_url,
posts.excerpt as post_excerpt,
stations.title as station_title,
stations.slug as station_slug
FROM "songs"
INNER JOIN "posts" ON "posts"."id" = "songs"."post_id"
inner join stations on stations.blog_id = songs.blog_id
WHERE "songs"."processed" = 't'
AND "songs"."working" = 't'
ORDER BY songs.rank desc
LIMIT 24 OFFSET 0
QUERY PLAN
------------------------------------------------------------------------------------------------
Limit (cost=546147.28..546159.16 …Run Code Online (Sandbox Code Playgroud)