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

问题描述 投票:0回答:1

下面是我的查询。执行需要9387.430毫秒,对于这样的请求,这肯定太长了。我希望能够减少执行时间。你能帮我吗?我还提供了我的分析输出。

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);;

EXPLAIN ANALYZE的输出:

     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
postgresql query-performance
1个回答
0
投票

您的种类正在溢出到磁盘上,因此会占用更多时间。尝试将work_mem增大到32MB(30可以,但是我喜欢8的倍数),然后重试

© www.soinside.com 2019 - 2024. All rights reserved.