对2个相同DB的相同查询-不同的执行计划

时间:2019-02-25 13:55:46

标签: sql postgresql-9.6 pg-dump

我正在将Postgres数据库移动到另一台服务器。在完善数据(与pg_dump一起转储)之后,我检查了性能,发现相同的查询在两个DB上产生了不同的查询计划(假设DBMS版本,数据库结构和数据本身相同):

查询是:

explain analyse select * from common.composite where id = 0176200005519000087

生产数据库的查询计划:

Index Scan using composite_id_idx on composite  (cost=0.43..8.45 rows=1 width=222) (actual time=0.070..0.071 rows=1 loops=1)
  Index Cond: (id = '176200005519000087'::bigint)
Planning time: 0.502 ms
Execution time: 0.102 ms

对于新的:

Bitmap Heap Scan on composite  (cost=581.08..54325.66 rows=53916 width=76) (actual time=0.209..0.210 rows=1 loops=1)
  Recheck Cond: (id = '176200005519000087'::bigint)
  Heap Blocks: exact=1
  ->  Bitmap Index Scan on composite_id_idx  (cost=0.00..567.61 rows=53916 width=0) (actual time=0.187..0.187 rows=1 loops=1)
        Index Cond: (id = '176200005519000087'::bigint)
Planning time: 0.428 ms
Execution time: 0.305 ms

很明显,两个数据库中都有一个ID的btree索引。 据我所知,新版本出于某种原因使用了位图索引,而btree是从转储导入的。这会导致复杂查询的巨大延迟(最多30倍)。

导入索引/依赖项是否有问题,或者有一种方法可以指出计划者应该使用哪些索引?

谢谢。

0 个答案:

没有答案
相关问题