I wrote a normal query to snap the next way point from a pair of coordinates in EPSG4326, nothing complex. To optimize the performance a bit I made a boundary condition to minimize the points needed to be checked...
Works smooth so far - but then I wrote a stored procedure to make later a neat intergeneration... (quasi wrapper). And here is where my headache starts:
- Single SQL: 650ms
- Stored Procedure: 45xxxms
- Single SQL is 70x faster than my storage procedure!
Code below...
CREATE OR REPLACE FUNCTION next_way_point(p geometry) RETURNS next_way_point_result AS $$
SELECT gid, source, target, the_geom, ST_distance(the_geom, ST_SetSRID($1,4326)) as dist
FROM ways
WHERE the_geom && ST_Buffer(ST_setSRID($1,4326), 0.1)
ORDER BY dist LIMIT 1 $$
LANGUAGE SQL;
Called like this:
SELECT * FROM next_way_point(ST_MakePoint(7.26, 47.52));
VS
SELECT gid, source, target, the_geom, ST_distance(the_geom, ST_SetSRID(ST_MakePoint(7.26, 47.52),4326)) as dist
FROM ways
WHERE the_geom && ST_Buffer(ST_setSRID(ST_MakePoint(7.26, 47.52),4326),0.1)
ORDER BY dist LIMIT 1;
