UPDATE tbl
SET city=s.city_name
FROM shp AS s
WHERE
ST_CONTAINS(s.city_geom,geom);
使用上面的代码,我可以将确切的城市添加到 GPS 点。它在 5000 万行上运行大约 45-50 分钟。“城市”表中大约有 4000 个城市需要检查。
我有另一个形状文件,其中包含给定国家/地区的 19 个县(只有 1 个国家/地区)。将县添加到点大约需要 1.5 小时。
我有 52 个欧盟国家的第三个形状文件。它使用相同的 sql 查询运行了将近 25 个小时。
每个表都有 geom 索引,例如:
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