UPDATE tbl
SET city=s.city_name
FROM shp AS s
WHERE
ST_CONTAINS(s.city_geom,geom);
使用上面的代码,我可以将精确的城市添加到 GPS 点。 它在 5000 万行上运行大约 45-50 分钟。 “city”表中有大约4000个城市需要检查。
我有另一个形状文件,其中包含给定国家/地区的 19 个县(仅 1 个国家/地区)。 将县添加到点大约需要 1.5 小时。
我有第三个包含 52 个欧盟国家的形状文件。 使用相同的 sql 查询,它运行了近 25 个小时。
每个表都有几何索引,例如:
CREATE INDEX idx_txt_geom ON txt USING GIST(geom);
问:当它只需要检查几个多边形时,为什么这么慢?
解释分析:
Update (cost=0.00..324.85 rows=1 width=286) (actual time=353.932..353.932 rows=0 loops=1)
Buffers: shared hit=73090 read=1
-> Nested Loop (cost=0.00..324.85 rows=1 width=286) (actual time=0.544..341.936 rows=471 loops=1)
Join Filter: _st_contains(s.geom, prob.geom)
Buffers: shared hit=69985
-> Seq Scan on world s (cost=0.00..83.44 rows=244 width=48) (actual time=0.009..0.319 rows=244 loops=1)
Buffers: shared hit=81
-> Index Scan using idx_prob_geom on prob (cost=0.00..0.73 rows=1 width=270) (actual time=0.003..0.024 rows=9 loops=244)
Index Cond: (s.geom && prob.geom)
Buffers: shared hit=533
Total runtime: 354.640 ms
最佳答案
ST_CONTAINS 无法使用索引。试试这个:
UPDATE tbl
SET city=s.city_name
FROM shp AS s
WHERE
(geom && s.city_geom) and ST_CONTAINS(s.city_geom,geom);
&&
检查边界框并使用索引。
关于postgresql - 使用 ST_Contains() 缓慢更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950939/