In this example:
i-want-to-display-in-postgis-files-that-are-less-than-10-000meter-and-their-calc
I am looking for the recording lines (the cards in postgis), which are at a distance of 10 km. Then I want to display this in a view and add pagination to it; whether the result returned is 5 records or 20,000 is not important.
Now, I want to know the 13 (whatever) rows of records (the cards) that are close to A (lon, lat).
I could use the same query but:
If let's admit I put
SELECT ST_Distance (geog, ST_MakePoint (49.9, 6.7) :: geography, true) FROM data WHERE ST_DWithin (geog, ST_MakePoint (49.9,6.7), 10000, true);
I am obliged to specify 10,000 meters to St_DWithin. If I look for the 13 lines of records in a very large city, it is very likely to have a hundred of them within 1km, but in a small town it is unlikely.
In addition, in my database, the registration lines, so the files are grouped by theme. So nothing tells me that if I look for cards with a certain theme, even in a big city the 9th card can be located after 10 km.
For me I have 2 approaches:
The first is the query I'm currently using, which is very long, between 800ms and 1 second:
SELECT *, ST_DistanceSphere( st_point(lon, lat) , st_point(6.5, 48.7)) as result from data +
where
id_poi != '%s
order by 13 limit 13
If I follow the advice from my previous post I have therefore modified the query like this:
SELECT *, ST_Distance(geog , st_point(6.5, 48.7)) as result from data
where
id_poi != '%s'
order by 13 limit 13
but the performances are not there.
This is a bit normal, I have about 410,000 rows to sort and I ask postgis to calculate the distances of ALL the files then to order them then to limit the result to 13.
So my question, how to get the 13 record cards closest to a given lon and lat?
thank you