为什么我们的Postgres查询性能会变慢?

时间:2018-01-23 15:52:55

标签: postgresql postgresql-performance

针对9.6和10运行。

我们开始注意到我们可以解决的查询时间问题。通过故障排除,我们发现即使是简单的查询也很慢。以下查询大约需要4秒才能返回计数。添加另一个左连接可将其最多踢9秒。

select count(1)
from sales_lead sl  
inner join dealership d
   on d.id = sl.dealership_id
inner join dealership_group dg
   on d.dealership_group_id = dg.id 
left join customer_email ce
   on sl.customer_id = ce.customer_id

所有连接列都有索引。所有表都是VACUUM ANALYZE。每个表的列在5到12之间。表计数大致如下:

  • sales_lead - ' 2113057'
  • customer_email - ' 2270856'
  • dealership_group - 4
  • 经销商 - 23

我从数据库统计数据中注意到,临时文件似乎异常高,它们的大小也是如此。数据库本身可能是400 MB的数据,但临时文件是117GB?这些数字似乎永远不会减少。

以下是解释的片段: enter image description here

文本版本的解释(分析,缓冲)

'Aggregate  (cost=297026.78..297026.79 rows=1 width=8) (actual time=5959.018..5959.018 rows=1 loops=1)'
'  Buffers: shared hit=1697364 read=193415'
'  ->  Hash Join  (cost=3.78..291505.59 rows=2208477 width=0) (actual time=0.106..5735.227 rows=2298433 loops=1)'
'        Hash Cond: (sl.dealership_id = d.id)'
'        Buffers: shared hit=1697364 read=193415'
'        ->  Merge Left Join  (cost=0.86..261136.11 rows=2208477 width=8) (actual time=0.030..5216.924 rows=2298433 loops=1)'
'              Merge Cond: (sl.customer_id = ce.customer_id)'
'              Buffers: shared hit=1697362 read=193415'
'              ->  Index Scan using fki_fk_customer_id on sales_lead sl  (cost=0.43..169112.10 rows=2113129 width=16) (actual time=0.010..3929.479 rows=2113057 loops=1)'
'                    Buffers: shared hit=1695917 read=187211'
'              ->  Index Only Scan using idx_customer_email_customer_id on customer_email ce  (cost=0.43..58979.27 rows=2270856 width=4) (actual time=0.016..390.700 rows=2792048 loops=1)'
'                    Heap Fetches: 0'
'                    Buffers: shared hit=1445 read=6204'
'        ->  Hash  (cost=2.64..2.64 rows=23 width=4) (actual time=0.056..0.056 rows=23 loops=1)'
'              Buckets: 1024  Batches: 1  Memory Usage: 9kB'
'              Buffers: shared hit=2'
'              ->  Hash Join  (cost=1.09..2.64 rows=23 width=4) (actual time=0.046..0.051 rows=23 loops=1)'
'                    Hash Cond: (d.dealership_group_id = dg.id)'
'                    Buffers: shared hit=2'
'                    ->  Seq Scan on dealership d  (cost=0.00..1.23 rows=23 width=8) (actual time=0.011..0.012 rows=23 loops=1)'
'                          Buffers: shared hit=1'
'                    ->  Hash  (cost=1.04..1.04 rows=4 width=4) (actual time=0.026..0.026 rows=4 loops=1)'
'                          Buckets: 1024  Batches: 1  Memory Usage: 9kB'
'                          Buffers: shared hit=1'
'                          ->  Seq Scan on dealership_group dg  (cost=0.00..1.04 rows=4 width=4) (actual time=0.005..0.006 rows=4 loops=1)'
'                                Buffers: shared hit=1'
'Planning time: 1.417 ms'
'Execution time: 5959.196 ms'

以下是数据库统计信息的片段: enter image description here

0 个答案:

没有答案