PG 数据库某一张表查询异常

该表数据3w左右,执行非索引查询耗时很长,今天开始,该表非索引查询非常慢。
异常explain:
EXPLAIN (analyze,buffers,verbose) select count(*) from user_order where status>6;

Aggregate (cost=2084738.25..2084738.26 rows=1 width=8) (actual time=72474.226..72474.227 rows=1 loops=1)
Output: count(*)
Buffers: shared hit=1369866 read=714388
-> Seq Scan on public.user_order (cost=0.00..2084671.73 rows=26609 width=0) (actual time=1678.079..72471.900 rows=33169 loops=1)

     Filter: (user_order.status > 6)
     Rows Removed by Filter: 6268
     Buffers: shared hit=1369866 read=714388

Planning time: 0.069 ms
Execution time: 72474.265 ms

正常explain

EXPLAIN (analyze,buffers,verbose) select count(*) from user_order where status>6;

Aggregate (cost=6113.74..6113.76 rows=1 width=8) (actual time=36.143..36.143 rows=1 loops=1)
Output: count(*)
Buffers: shared hit=5362
-> Seq Scan on public.user_order (cost=0.00..5999.59 rows=45663 width=0) (actual time=0.017..32.729 rows=46022 loops=1)

     Filter: (user_order.status > 6)
     Rows Removed by Filter: 5219
     Buffers: shared hit=5362

Planning time: 0.393 ms
Execution time: 36.260 ms

根据你提供的信息,可以看出查询的执行计划发生了变化,从正常情况下的6,113.74到异常情况下的2,084,738.25,这导致了非索引查询变得非常缓慢。异常情况下,查询计划中的Seq Scan操作扫描了整个表,而正常情况下只需要扫描部分行。这表明,异常情况下可能存在一些问题,例如表的数据分布不均匀、统计信息不准确、表中存在大量无用数据等。

为了解决该问题,可以采用以下几个方法:

1.优化查询语句,通过添加索引或优化查询条件来加速查询。如果可能的话,可以尝试将查询分解成多个步骤或使用JOIN。

2.通过更新统计信息或重建索引等方式优化表。可以考虑使用VACUUM,ANALYZE和REINDEX命令进行操作。

3.优化服务器设置,例如增加内存或调整其他设置,以提高服务器性能。

不同情况下的查询计划和耗时差距非常大,因此我们需要进一步排查问题的原因,并采取适当的措施来解决该问题。