We've got a table of approximately 300,000 points and would like to find the nearest neighbor (within 10km) to each point in the same table. The most efficient query we've found so far is below, but it's slow enough that it's basically unworkable ... hasn't run to completion after multiple hours. Geometry is EPSG 4326, PostGIS 2.1.8.
SELECT DISTINCT ON(p1.id) p1.id AS p1_id,
p2.id AS p2_id, ST_Distance_Sphere(p1.geom, p2.geom)
FROM points p1, points p2
WHERE p1.id <> p2.id AND ST_DWithin(p1.geom, p2.geom, 10000)
ORDER BY p1.id, ST_Distance_Sphere(p1.geom, p2.geom);
We've also tried a couple of other options including using the PostGIS KNN operator and looping through points individually using a cursor function.
Is there a more efficient query we can use, or maybe impoved nearest neighbor support in newer versions of PostGIS?
No comments:
Post a Comment