[英]PostgreSQL clever use of ST_Within
maybe my question can seem stupid, but I'd like to ask if somebody can tell me how can I improve this query: 也许我的问题看似愚蠢,但我想问一下是否有人可以告诉我如何改进这个查询:
UPDATE twitter
SET poi=poi.type_name FROM poi
WHERE ST_Within (ST_SetSRID(ST_MakePoint(x_coords,y_coords),4326),
ST_SetSRID(ST_Buffer(poi.wkb_geometry, 0.0005),4326));
I have 2 tables with points: " twitter
" and " poi
", I need to create a buffer of 50m around " poi
" and find all points of " twitter
" which are within this buffer and then copy " type_name
" attribute of " poi
" to " twitter
" point. 我有两个带点的表:“
twitter
”和“ poi
”,我需要在“ poi
”周围创建一个50米的缓冲区,找到这个缓冲区内的所有“ twitter
”点,然后复制“ poi
”的“ type_name
”属性“到” twitter
“点。 The problem is that the query lasts too long and seems it'll never finish, from time to time I try to check if some attributues were copied, but " twitter.poi
" column remains always empty. 问题是查询持续时间过长,似乎永远不会完成,我不时会尝试检查是否复制了某些属性,但“
twitter.poi
”列始终为空。
I tried to limit the query by adding in WHERE " username
" to check if the query is right and it works. 我试图通过添加WHERE“
username
”来限制查询,以检查查询是否正确并且它是否有效。
UPDATE twitter
SET poi=poi.type_name FROM poi
WHERE uname='some_username' AND ST_Within (ST_SetSRID(ST_MakePoint(x_coords,y_coords),4326),
ST_SetSRID(ST_Buffer(poi.wkb_geometry, 0.0005),4326));
The only one thing that I can't understand is how to improve the query for entire " twitter
" table. 我无法理解的唯一一件事是如何改进整个“
twitter
”表的查询。
You didn't provide a table definition, but you seem to have two columns for the coordinates in table twitter
: x_coords
and y_coords
. 您没有提供表定义,但您似乎在表
twitter
有两列坐标: x_coords
和y_coords
。 Either replace those with a geometry
column and create a simple GiST index or create a functional GiST index like: 替换具有
geometry
列的那些并创建简单的GiST索引或创建功能性GiST索引,如:
CREATE INDEX idx_twitter_point_4326
ON twitter USING gist (ST_SetSRID(ST_MakePoint(x_coords,y_coords), 4326));
And another one on poi
: 另一个关于
poi
:
CREATE INDEX idx_poi_wkb_geometry_4326
ON poi USING gist (ST_SetSRID(p.wkb_geometry, 4326));
This should be used to speed up ST_Within()
: 这应该用于加速
ST_Within()
:
This function call will automatically include a bounding box comparison that will make use of any indexes that are available on the geometries.
此函数调用将自动包含一个边界框比较,该比较将使用几何上可用的任何索引。
The function ST_distance() might serve you better: 函数ST_distance()可能会更好地为您服务:
UPDATE twitter t
SET poi = p.type_name
FROM poi p
WHERE ST_Distance(ST_SetSRID(ST_MakePoint(t.x_coords, t.y_coords), 4326)
,ST_SetSRID(p.wkb_geometry, 4326)) < 0.0005
AND t.poi IS DISTINCT FROM p.type_name;
This is assuming there can at most one poi
in the vicinity of a twitter
entry. 这假设在
twitter
条目附近最多只能有一个 poi
。 Else you should use a subquery that picks the closest poi
for each twitter
. 否则,你应该使用一个子查询,为每个
twitter
选择最接近的 poi
。
The additional WHERE
clause t.poi IS DISTINCT FROM p.type_name
avoid empty updates. 附加的
WHERE
子句t.poi IS DISTINCT FROM p.type_name
避免空更新。
Not using PostGis myself, so untested. 我自己没有使用PostGis,所以未经测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.