PostgreSQL SELECT太慢

时间:2019-02-26 10:27:46

标签: postgresql database-performance

我正在寻找一种优化查询的方法。

当前,我有一个4M行的表,我只想检索引用的最后1000行:

SELECT * 
FROM customers_material_events 
WHERE reference = 'XXXXXX' 
ORDER BY date DESC 
LIMIT 1000;

这是执行计划:

Limit  (cost=12512155.48..12512272.15 rows=1000 width=6807) (actual time=8953.545..9013.658 rows=1000 loops=1)
   Buffers: shared hit=16153 read=30342
   ->  Gather Merge  (cost=12512155.48..12840015.90 rows=2810036 width=6807) (actual time=8953.543..9013.613 rows=1000 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         Buffers: shared hit=16153 read=30342
         ->  Sort  (cost=12511155.46..12514668.00 rows=1405018 width=6807) (actual time=8865.186..8865.208 rows=632 loops=3)
               Sort Key: date DESC
               Sort Method: top-N heapsort  Memory: 330kB
               Worker 0:  Sort Method: top-N heapsort  Memory: 328kB
               Worker 1:  Sort Method: top-N heapsort  Memory: 330kB
               Buffers: shared hit=16153 read=30342
               ->  Parallel Seq Scan on customers_material_events  (cost=0.00..64165.96 rows=1405018 width=6807) (actual time=0.064..944.029 rows=1117807 loops=3)
                     Filter: ((reference)::text = 'FFFEEE'::text)
                     Rows Removed by Filter: 17188
                     Buffers: shared hit=16091 read=30342
 Planning Time: 0.189 ms
 Execution Time: 9013.834 ms
(18 rows)

我看到执行时间非常慢...

1 个答案:

答案 0 :(得分:2)

此查询的理想索引为:

CREATE INDEX ON customers_material_events (reference, date);

这将使您能够快速找到由reference自动排序的某个date的值,因此不需要额外的排序步骤。

相关问题