postgresql - 使用 ST_Contains() 缓慢更新

标签 postgresql postgis postgresql-performance

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/

相关文章:

SQL 仅列出唯一/不同的值

sql - 查找所有具有特定艺术家姓名轨道的播放列表?

java - SPRING MVC + Hibernate,无法获取所有记录

openstreetmap - 高速公路导出的经纬度,例如 US 101 说排列在 N-S 方向

sql - 将 varchar 转换为 bool 值

linux - 使用 pgadmin 4 在 linux 的 postgresql 中导入 shapefile

postgresql - 带有长列表的 Sqlx WHERE IN 查询

arrays - 查询嵌套在 JSON 数组深处的数据的最有效方法?

postgresql - Postgres INSERT ON CONFLICT DO UPDATE 与 INSERT 或 UPDATE

postgresql - 是否可以在 postgresql 的另一个模式中使用一个模式的枚举类型