我正在尝试找到获得半径属性计数的最快方法。我见过其他一些类似的问题。有没有人知道为什么第三个查询给出了不同的结果?它非常快。
慢
SELECT count(*)
FROM property
WHERE ST_Distance_Sphere(geom_pt, ST_MakePoint(-104.989879,39.736355))<=2000;
计数= 2665
非常慢
SELECT count(*)
FROM property
WHERE ST_Distance(geom_pt, ST_SetSRID(ST_MakePoint(-104.989879, 39.736355),4326)::geography)<=2000;
计数= 2665
非常快
SELECT count(*)
FROM property
WHERE ST_Within(geom_pt,ST_Transform(ST_Buffer(ST_Transform(ST_SetSRID(ST_MakePoint(-104.989879, 39.736355), 4326), 3857), 2000), 4326));
计数= 1794
答案 0 :(得分:2)
不,您希望ST_DWithin
使用GIST索引。
SELECT count(*)
FROM property
WHERE ST_DWithin(
geom_pt, -- make sure this is also geography
ST_MakePoint(-104.989879, 39.736355)::geography,
2000 -- note distance in meters.
);
您也不必在地理位置上将srid设置为4326。这是默认设置。
如果您没有索引,
CREATE INDEX ON property USING gist(geom_pt);
VACUUM property;
您也可以在CLUSTER
上geom_pt
。