Answers:
无需功能的另一种选择
update points set country = t1.country from
(
select points.oid, countries.name as country from
countries INNER JOIN points on st_contains(countries.wkb_geometry,points.wkb_geometry)
) t1
where t1.oid = points.oid
我怀疑(尽管我没有测试过)这会比使用您的示例中的嵌套函数更快。
我的运行输出说明(希望您的外观相似)。如果您有更多的Seq扫描结果,那就需要看一下,也许索引设置不正确。
Update on points (cost=1.18..29.40 rows=121 width=129)"
-> Nested Loop (cost=1.18..29.40 rows=121 width=129)"
Join Filter: _st_contains(countries.geometry, public.points.geometry)"
-> Hash Join (cost=1.18..2.37 rows=28 width=220)"
Hash Cond: (public.points.oid = public.points.oid)"
-> Seq Scan on points (cost=0.00..1.08 rows=28 width=114)"
-> Hash (cost=1.08..1.08 rows=28 width=110)"
-> Seq Scan on points (cost=0.00..1.08 rows=28 width=110)"
-> Index Scan using "countries_Idx" on countries (cost=0.00..0.91 rows=1 width=414)"
Index Cond: (geometry && public.points.geometry)"
好的。。。我做了一些黑客工作,发现SQL FUNCTION帮了我大忙。有人对将其带到桥上有任何想法吗?
CREATE OR REPLACE FUNCTION getcountry (
country_geom geometry
) RETURNS TABLE(country text) AS $$
SELECT b.name as country FROM
geonames d, world_borders b WHERE
$1 && b.wkb_geometry
AND intersects($1, b.wkb_geometry) ;
$$ LANGUAGE SQL;
UPDATE geonames
SET country = val
FROM (SELECT getcountry(point_geom) FROM geonames) AS val