I have stored 2.5 million data points (addresses) using the geometry data type in column latlong with SRID 4326. The latlon – column has a GIST index defined. I would select all addresses within a distance given in metres.
I understand that SRID 4326 measures distance in degrees, so it doesn't make sense in my case. However the query
select *
from adresse
where st_distance(latlong::geography, ST_SetSRID(ST_Point(16.520, 47.846), 4326)::geography) < 200
is incredibly slow, I guess it is unable to use the index.
How can I speed up this query?
Best Answer
You want to use ST_DWithin() which takes advantage of indexing
See also http://postgis.net/2013/08/26/tip_ST_DWithin/