小编Nat*_*ert的帖子

具有多个连接的 DISTINCT ON 查询真的很慢

最初发布: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)

postgresql join

2
推荐指数
1
解决办法
8793
查看次数

标签 统计

join ×1

postgresql ×1