我很难理解索引的用法 通过PosrgreSQL / PostGIS。
我有两个空间数据集:构建轮廓和泛洪多边形。 有4.700个洪水多边形非常复杂(许多顶点)和1.100.000建筑轮廓。我使用QGIS将它们导入数据库。
两个数据集的几何类型都是POLYGON,SRID也设置为相同的数字。
我在两个数据集上都建立了空间索引:
CREATE INDEX ON outlines USING gist (geom)
CREATE INDEX ON flood USING gist (geom);
CLUSTER outlines USING outlines_geom_idx;
CLUSTER flood USING flood_geom_idx;
VACUUM ANALYZE outlines
VACUUM ANALYZE flood
然后我运行一个简单的查询(仅限4000个轮廓):
SELECT b.id, a.id FROM flood b, outlines a
WHERE st_intersects(a.geom, b.geom) AND a.id < 4000
然后我在该查询中使用了EXPLAIN ANALYZE:
"Nested Loop (cost=79.68..17890.72 rows=21467 width=8) (actual
time=28.817..10367.845 rows=46 loops=1)"
" -> Bitmap Heap Scan on outlines a (cost=79.53..11103.17 rows=4013
width=163) (actual time=0.366..7.821 rows=3999 loops=1)"
" Recheck Cond: (id < 4000)"
" Heap Blocks: exact=795"
" -> Bitmap Index Scan on ks_obrisi_mp_pkey (cost=0.00..78.52
rows=4013 width=0) (actual time=0.284..0.284 rows=3999 loops=1)"
" Index Cond: (id < 4000)"
" -> Index Scan using flood_geom_idx on flood b (cost=0.15..1.68 rows=1
width=6161) (actual time=2.589..2.590 rows=0 loops=3999)"
" Index Cond: (a.geom && geom)"
" Filter: _st_intersects(a.geom, geom)"
" Rows Removed by Filter: 0"
"Planning time: 1.735 ms"
"Execution time: 10368.131 ms"
但是当我在洪水数据集上删除索引时,查询速度要快得多:
"Nested Loop (cost=0.29..431597.69 rows=21467 width=8) (actual
time=42.442..4610.522 rows=46 loops=1)"
" -> Seq Scan on flood b (cost=0.00..1298.78 rows=9278 width=6161) (actual
time=0.002..5.514 rows=9278 loops=1)"
" -> Index Scan using outlines_geom_idx on outlines a (cost=0.29..46.37
rows=1
width=163) (actual time=0.459..0.490 rows=0 loops=9278)"
" Index Cond: (geom && b.geom)"
" Filter: ((id < 4000) AND _st_intersects(geom, b.geom))"
" Rows Removed by Filter: 18"
"Planning time: 1.785 ms"
"Execution time: 4610.564 ms"
我看到选择的过程完全不同,而且那个 使用不同的索引。为何如此区别?我只找到线程说话 关于索引没有提高查询速度。在这种情况下,空间索引会将速度降低2倍。是否可以告诉规划者使用哪个索引?
感谢您的帮助!