为什么在 master table 上查询很慢?

why is it very slow to do query on master table?

我有一个非常大的table。所以我对这个table进行了分区。并将所有记录从 master table 复制到 child tables。然后我删除了 master table 中的所有记录。现在我的主人table是空的。在childtable上做查询是非常快的。但是在mastertable上查询还是很慢。怎么了?

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)

我还 运行 对 master table 进行真空分析。但是还是不行。

postgres=# vacuum analyze cdr; 真空

我发现其他一些人也遇到了同样的问题。可能根本原因是虽然 master table 中的旧记录已被删除,但 psql 仍以某种方式扫描这些垃圾数据。一种解决方案是创建一个新的 master table,并放弃旧的 master table。但是这个解决方案对我来说不是 suitable 。没有停机的任何其他解决方案?谢谢!

我解决了这个问题。只需要 运行 "vacuum full" 或 "cluster" 在 master table.