使用ST_Contains()进行缓慢更新


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个小时。

每个表都有索引,如:

CREATE INDEX idx_txt_geom ON txt USING GIST(geom);

Q:当它只需要检查几个多边形时为什么这么慢?

解释分析:

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不能使用index。试试这个:

UPDATE tbl
SET city=s.city_name
FROM shp AS s
WHERE
(geom && s.city_geom) and ST_CONTAINS(s.city_geom,geom);

&&检查边界框并使用索引

最新更新