I need to select all entries from a table, that are within a distance from a point.
Each entry is a point geometry (I set a gist index on geom
).
Current runtime is anywhere between 3-15 seconds, depends on how many entries are in a given area (I think), but I'm trying to improve that to under 3sec.
I've seen a few posts on different ways to speed this up, including using ST_DWithin
but it does not seem to improve performance, neither does casting to geography
.
Any ideas how to proceed?
Here's a sample query:
SELECT *
FROM T1
WHERE entry_date >= current_date - interval '1' month
AND ST_DistanceSphere(geom, ST_MakePoint(-74.00072343, 40.7209235)) <= 1000;
ST_DWithin
but it's taking longer (althoughgeog
is indexed).ST_DWithin(geom::geography, ST_SetSRID(ST_MakePoint(-74.00072343, 40.7209235), 4326)::geography, 1000)
. Could it be because I'm "making point" for each iteration? Should this point be created once, and accessed after that? – guyts Jan 22 '20 at 17:05entry_date >= current_date - interval '1' month
is not using an index, due to the minus interval part. You can fix this by using an expression index. Please, post the EXPLAIN output from this query, which will determine what the query parser plan looks like. – John Powell Jan 23 '20 at 15:00