【问题标题】:Why is my Postgresql instance not using bitmap index scanning?为什么我的 Postgresql 实例不使用位图索引扫描?
【发布时间】:2019-03-23 15:26:55
【问题描述】:

我有两个 Postgresql 实例正在运行,一个在我的本地机器上运行版本 10.5,另一个版本在我的生产机器上运行 9.5.10。我在本地机器上创建表并使用 pg_dump 和 pr_restore 将它们移动到生产机器上。当我在本地机器上运行以下命令时,大约需要 100 毫秒:

CREATE TABLE test_point AS
    SELECT a.*, b.*, a.total_score + b.total_score_table_2 AS total_score_all
    FROM master_enigma_table_designations b,
      ST_Transform(ST_SetSRID(ST_Makepoint(-408601.4826183041,6707237.695265564), 3857), 27700) dropped_pin LEFT JOIN
    master_enigma_table a
    ON ST_Within(dropped_pin, a.wkb_geometry)
    WHERE a.poly_id = b.poly_id_new;

当我运行 EXPLAIN ANALYZE 时,我得到以下输出:

"Nested Loop  (cost=1119.13..180619.12 rows=9594 width=4224) (actual time=0.157..0.225 rows=1 loops=1)"
"  Buffers: shared hit=22"
"  ->  Nested Loop  (cost=1118.69..118339.83 rows=9594 width=2444) (actual time=0.126..0.189 rows=1 loops=1)"
"        Buffers: shared hit=18"
"        ->  Function Scan on dropped_pin  (cost=0.00..0.01 rows=1 width=32) (actual time=0.004..0.006 rows=1 loops=1)"
"        ->  Bitmap Heap Scan on master_enigma_table a  (cost=1118.69..118243.88 rows=9594 width=2444) (actual time=0.108..0.167 rows=1 loops=1)"
"              Recheck Cond: (wkb_geometry ~ dropped_pin.dropped_pin)"
"              Filter: _st_contains(wkb_geometry, dropped_pin.dropped_pin)"
"              Rows Removed by Filter: 2"
"              Heap Blocks: exact=3"
"              Buffers: shared hit=18"
"              ->  Bitmap Index Scan on master_enigma_table_gist_index  (cost=0.00..1116.29 rows=28783 width=0) (actual time=0.089..0.090 rows=3 loops=1)"
"                    Index Cond: (wkb_geometry ~ dropped_pin.dropped_pin)"
"                    Buffers: shared hit=8"
"  ->  Index Scan using master_enigma_table_designations_poly_id on master_enigma_table_designations b  (cost=0.44..6.48 rows=1 width=1772) (actual time=0.021..0.024 rows=1 loops=1)"
"        Index Cond: (poly_id_new = a.poly_id)"
"        Buffers: shared hit=4"
"Planning time: 1.397 ms"
"Execution time: 10.058 ms"

当我在生产机器上运行完全相同的查询时,需要 8 分钟。当我运行 EXPLAIN ANALYZE 时,我得到:

"Nested Loop  (cost=0.44..15399024.56 rows=9594 width=4208) (actual time=326842.620..478541.379 rows=1 loops=1)"
"  Buffers: shared hit=1314092 read=6890152"
"  ->  Nested Loop  (cost=0.00..15323938.18 rows=9594 width=2425) (actual time=326842.576..478541.332 rows=1 loops=1)"
"        Join Filter: ((a.wkb_geometry ~ dropped_pin.dropped_pin) AND _st_contains(a.wkb_geometry, dropped_pin.dropped_pin))"
"        Rows Removed by Join Filter: 28783093"
"        Buffers: shared hit=1314088 read=6890152"
"        ->  Function Scan on dropped_pin  (cost=0.00..0.01 rows=1 width=32) (actual time=0.003..0.004 rows=1 loops=1)"
"        ->  Seq Scan on master_enigma_table a  (cost=0.00..7768445.30 rows=28782830 width=2425) (actual time=0.018..458071.770 rows=28783094 loops=1)"
"              Buffers: shared hit=590465 read=6890152"
"  ->  Index Scan using master_enigma_table_designations_new_poly_id on master_enigma_table_designations b  (cost=0.44..7.81 rows=1 width=1783) (actual time=0.012..0.013 rows=1 loops=1)"
"        Index Cond: (poly_id_new = a.poly_id)"
"        Buffers: shared hit=4"
"Planning time: 26.628 ms"
"Execution time: 478582.199 ms"

我的生产机器似乎没有使用位图索引扫描,而我的本地机器是。两个实例都有相同的表和索引,我已经在所有表上运行了 ANALYZE。我已经运行了 SHOW ALL 并且位图扫描也被设置了。

有没有人对我可以做些什么来解决我的问题有任何建议。

【问题讨论】:

    标签: postgresql indexing explain


    【解决方案1】:

    不同版本的 Postgres 意味着您也使用不同版本的 PostGIS。 9.5 的旧版本没有在 st_within() 函数上使用索引。在 9.5 中,您必须添加 st_dwithin(dropped_pin, a.wkb_geometry,0) 才能使用索引值。

    CREATE TABLE test_point AS
    SELECT a.*, b.*, a.total_score + b.total_score_table_2 AS total_score_all
      FROM master_enigma_table_designations b,
           ST_Transform(ST_SetSRID(ST_Makepoint(-408601.4826183041,6707237.695265564), 3857), 27700) dropped_pin 
      LEFT JOIN master_enigma_table a ON ST_Within(dropped_pin, a.wkb_geometry)
                                      and ST_DWithin(dropped_pin, a.wkb_geometry,0)
    WHERE a.poly_id = b.poly_id_new;
    

    简单的建议 - 在两台机器上使用相同版本的 Postgres 和 PostGIS,否则您将遇到更多此类不兼容问题。

    【讨论】:

    • 在生产服务器上升级 10.5 解决了这个问题。
    猜你喜欢
    • 2019-09-03
    • 2020-09-06
    • 2020-02-14
    • 2015-06-10
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2021-05-23
    • 2021-03-19
    相关资源
    最近更新 更多