重新调整磁盘大小后,PostgreSQL查询非常慢

时间:2019-06-07 08:55:29

标签: sql postgresql query-performance postgresql-9.5

我将PostgreSQL DB用于生产数据库,并且最近已调整了生产服务器的磁盘卷大小。

我已经注意到对特定表(〜10K记录)的查询非常慢,

    EXPLAIN (analyze, buffers, timing) SELECT count(id) FROM markets_sales WHERE volume>0;
                                                             QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=1844791.17..1844791.18 rows=1 width=4) (actual time=79842.776..79842.777 rows=1 loops=1)
   Buffers: shared hit=2329 read=1842313
   ->  Seq Scan on markets_sales  (cost=0.00..1844782.68 rows=3399 width=4) (actual time=8139.929..79842.043 rows=6731 loops=1)
         Filter: (volume > '0'::double precision)
         Rows Removed by Filter: 4523
         Buffers: shared hit=2329 read=1842313
 Planning time: 0.110 ms
 Execution time: 79842.809 ms

但是对另一个表(约2K条记录)的类似查询是完美的。

EXPLAIN ANALYZE SELECT count(id) FROM markets_volume WHERE percent>0;
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=1368.87..1368.88 rows=1 width=4) (actual time=1.866..1.866 rows=1 loops=1)
   ->  Seq Scan on markets_volume  (cost=0.00..1365.59 rows=1312 width=4) (actual time=0.023..1.751 rows=1313 loops=1)
         Filter: (h24_change > '0'::double precision)
         Rows Removed by Filter: 1614
 Planning time: 0.093 ms
 Execution time: 1.903 ms
(6 rows)

2 个答案:

答案 0 :(得分:3)

仅11254行的缓冲区(=从磁盘读取的块)的数量太高了。

因此,您的表很可能是bloated。可以使用以下方法纠正此问题:

...

请注意,该语句将需要对表进行独占锁定(从而阻止任何读取或写入访问)。

答案 1 :(得分:0)

尝试使用PostgreSQL的慢查询日志。在默认配置中,慢速查询日志是不活动的。您应该从postgresql.conf文件中打开它。它为您提供了更详细的问题所在。然后您可以对问题采取行动。