为什么在主表上查询非常慢?
问题描述:
我有一张很大的桌子。所以我划分了这张桌子。并将主表中的所有记录复制到子表中。然后我删除了主表中的所有记录。现在我的主表是空的。对子表进行查询的速度非常快。但是在主表上执行查询仍然很慢。它出什么问题了?为什么在主表上查询非常慢?
postgres=# select count(*) from only cdr;
count
-------
0
(1 row)
postgres=# explain select count(*) from only cdr;
QUERY PLAN
-----------------------------------------------------------------------
Aggregate (cost=2045094.31..2045094.32 rows=1 width=0)
-> Seq Scan on cdr (cost=0.00..2044867.85 rows=90585 width=0)
(2 rows)
postgres=# EXPLAIN ANALYZE select count(*) from only cdr;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
-----
Aggregate (cost=2045094.31..2045094.32 rows=1 width=0) (actual time=168385.356..168385.356 rows=1 loops=1)
-> Seq Scan on cdr (cost=0.00..2044867.85 rows=90585 width=0) (actual time=168385.351..168385.351 rows=0 loop
s=1)
Total runtime: 168385.404 ms
(3 rows)
我也在主表上运行真空分析。但仍然没有运气。
postgres =#vacuum analyze cdr; 真空
我发现一些其他人也有同样的问题。也许根本原因是,虽然master表中的旧记录已被删除,但psql仍以某种方式扫描这些垃圾数据。一种解决方案是创建一个新的主表,并放弃旧的主表。但是这个解决方案并不适合我。任何其他解决方案没有停机?谢谢!
答
我解决了这个问题。只需要在主表上运行“vacuum full”或“cluster”。
您是否在主控上运行过“真空分析”? –
我已经运行“真空分析cdr;”。但现在它仍然很慢。 –