简体   繁体   English

减少我在 postgresql 中查询的执行时间

[英]Reduce execution time of my query in postgresql

Below is my query.以下是我的查询。 It takes 9387.430 ms to execute, which is certainly too long for such a request.执行需要 9387.430 毫秒,对于这样的请求来说肯定是太长了。 I would like to be able to reduce this execution time.我希望能够减少此执行时间。 Can you please help me on this?你能帮我解决这个问题吗? I also provided my analyze output.我还提供了我的分析 output。

EXPLAIN ANALYZE 
SELECT a.artist, b.artist, COUNT(*) 
FROM release_has_artist a, release_has_artist b 
WHERE a.release = b.release AND a.artist <> b.artist 
GROUP BY(a.artist,b.artist) 
ORDER BY (a.artist,b.artist);;

Output of EXPLAIN ANALYZE: EXPLAIN ANALYZE的Output:

     Sort  (cost=1696482.86..1707588.14 rows=4442112 width=48) (actual time=9253.474..9314.510 rows=461386 loops=1)
   Sort Key: (ROW(a.artist, b.artist))
   Sort Method: external sort  Disk: 24832kB
   ->  Finalize GroupAggregate  (cost=396240.32..932717.19 rows=4442112 width=48) (actual time=1928.058..2911.463 rows=461386 loops=1)
         Group Key: a.artist, b.artist
         ->  Gather Merge  (cost=396240.32..860532.87 rows=3701760 width=16) (actual time=1928.049..2494.638 rows=566468 loops=1)
               Workers Planned: 2
               Workers Launched: 2
               ->  Partial GroupAggregate  (cost=395240.29..432257.89 rows=1850880 width=16) (actual time=1912.809..2156.951 rows=188823 loops=3)
                     Group Key: a.artist, b.artist
                     ->  Sort  (cost=395240.29..399867.49 rows=1850880 width=8) (actual time=1912.794..2003.776 rows=271327 loops=3)
                           Sort Key: a.artist, b.artist
                           Sort Method: external merge  Disk: 4848kB
                           ->  Merge Join  (cost=0.85..177260.72 rows=1850880 width=8) (actual time=2.143..1623.628 rows=271327 loops=3)
                                 Merge Cond: (a.release = b.release)
                                 Join Filter: (a.artist <> b.artist)
                                 Rows Removed by Join Filter: 687597
                                 ->  Parallel Index Only Scan using release_has_artist_pkey on release_has_artist a  (cost=0.43..67329.73 rows=859497 width=8) (actual time=0.059..240.998 rows=687597 loops=3)
                                       Heap Fetches: 711154
                                 ->  Index Only Scan using release_has_artist_pkey on release_has_artist b  (cost=0.43..79362.68 rows=2062792 width=8) (actual time=0.072..798.402 rows=2329742 loops=3)
                                       Heap Fetches: 2335683
 Planning time: 2.101 ms
 Execution time: 9387.430 ms

In your EXPLAIN ANALYZE output, there are two Sort Method: external merge Disk: ####kB , indicating that the sort spilled out to disk and not in memory, due to an insufficiently-sized work_mem .在您的EXPLAIN ANALYZE output 中,有两种Sort Method: external merge Disk: ####kB ,表示排序溢出到磁盘而不是work_mem ,因为 work_mem 大小不足。 Try increasing your work_mem up to 32MB (30 might be ok, but I like multiples of 8), and try again尝试将你的work_mem增加到 32MB(30 可能没问题,但我喜欢 8 的倍数),然后再试一次

Note that you can set work_mem on a per-session basis, as a global change in work_mem could potentially have negative side-effects, such as running out of memory, because postgresql.conf -configured work_mem is allocated for each session (basically, it has a multiplicative effect). Note that you can set work_mem on a per-session basis, as a global change in work_mem could potentially have negative side-effects, such as running out of memory, because postgresql.conf -configured work_mem is allocated for each session (basically, it有乘数效应)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM