我有一张很大的 table 。所以我对这张表进行了分区。并将所有记录从主表复制到子表。然后我从主表中删除了所有记录。现在我的主表是空的。对子表进行查询非常快。但是查询master表还是很慢的。有什么问题吗?
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”。
关于performance - 为什么在主表上查询很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342196/