首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有可能加速这个查询吗?

有可能加速这个查询吗?
EN

Stack Overflow用户
提问于 2019-01-01 00:11:20
回答 1查看 41关注 0票数 0

我有下面的查询,执行起来有点太长了。我已经发布了查询的EXPLAIN ANALYZE。我能做些什么来提高速度吗?

代码语言:javascript
复制
EXPLAIN analyze SELECT c.*, match.user_json FROM match INNER JOIN conversation c 
ON match.match_id = c.match_id WHERE c.from_id <> 142822281 AND c.to_id = 
142822281 AND c.unix_timestamp = (SELECT max( unix_timestamp ) FROM conversation 
WHERE match_id = c.match_id GROUP BY match_id) 

解释分析结果

代码语言:javascript
复制
Nested Loop  (cost=0.00..16183710.79 rows=2 width=805) (actual time=2455.133..2502.781 rows=34 loops=1)
  Join Filter: (match.match_id = c.match_id)
  Rows Removed by Join Filter: 71502
  ->  Seq Scan on match  (cost=0.00..268.51 rows=2151 width=723) (actual time=0.006..4.973 rows=2104 loops=1)
  ->  Materialize  (cost=0.00..16183377.75 rows=2 width=90) (actual time=0.034..1.168 rows=34 loops=2104)
        ->  Seq Scan on conversation c  (cost=0.00..16183377.74 rows=2 width=90) (actual time=70.972..2421.949 rows=34 loops=1)
              Filter: ((from_id <> 142822281) AND (to_id = 142822281) AND (unix_timestamp = (SubPlan 1)))
              Rows Removed by Filter: 22010
              SubPlan 1
                ->  GroupAggregate  (cost=0.00..739.64 rows=10 width=16) (actual time=5.358..5.358 rows=1 loops=450)
                      Group Key: conversation.match_id
                      ->  Seq Scan on conversation  (cost=0.00..739.49 rows=10 width=16) (actual time=3.355..5.320 rows=17 loops=450)
                            Filter: (match_id = c.match_id)
                            Rows Removed by Filter: 22027
Planning Time: 1.132 ms
Execution Time: 2502.926 ms
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-01 00:19:23

这是您的查询:

代码语言:javascript
复制
SELECT c.*, m.user_json
FROM match m INNER JOIN
     conversation c 
     ON m.match_id = c.match_id
WHERE c.from_id <> 142822281 AND 
      c.to_id = 142822281 AND
      c.unix_timestamp = (SELECT max( c2.unix_timestamp )
                          FROM conversation c2
                          WHERE c2.match_id = c.match_id
                          GROUP BY c2.match_id
                         );

我建议把它写成:

代码语言:javascript
复制
SELECT DISTINCT ON (c.match_id) c.*, m.user_json
FROM match m INNER JOIN
     conversation c 
     ON m.match_id = c.match_id
WHERE c.from_id <> 142822281 AND 
      c.to_id = 142822281 AND
ORDER BY c.match_id, c.unix_timestamp DESC;

然后在:conversation(to_id, from_id, match_id)上尝试一个索引。我假设您有一个match(match_id)索引。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53992252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档