【问题标题】:Reduce execution time of my query in postgresql减少我在 postgresql 中查询的执行时间
【发布时间】:2019-11-17 20:41:41
【问题描述】:

以下是我的查询。执行需要 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

【问题讨论】:

  • 你在release_has_artist上有哪些索引?
  • Ser More value to work_mem(配置参数),这避免使用排序方法:外部合并磁盘,在执行该查询之前将此参数设置为 40 或 50 MB

标签: postgresql query-performance


【解决方案1】:

在您的EXPLAIN ANALYZE 输出中,有两个Sort Method: external merge Disk: ####kB,表示由于work_mem 的大小不足,排序溢出到磁盘而不是内存中。尝试将您的 work_mem 增加到 32MB(30 可能没问题,但我喜欢 8 的倍数),然后重试

请注意,您可以在每个会话的基础上设置work_mem,因为work_mem 的全局更改可能会产生负面影响,例如内存不足,因为postgresql.conf-configured work_mem为每个会话分配(基本上,它具有乘法效应)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多