Query result timing

I have osm data and trying to find nearest points for my given input of start and end lat & long, in this i am applying filter of tags, and tags are available in relationship CONNECTED_TO, now after getting my nearest points in db i am giving it to another query to get shortest route between them. Now this whole process is taking too much time like in minutes, i am posting both the queries below please suggest me like what to change in queries for reducing my result timing.
shortest route
`WITH point({latitude: ${node.latitude}, longitude: ${node.longitude}}) AS givenPoint

        MATCH (n:Node)
        WITH n, givenPoint, point.distance(givenPoint, point({latitude: n.latitude, longitude: n.longitude})) AS distanceToNode
        WHERE distanceToNode <= 500
        WITH n, distanceToNode


        MATCH (w:Way)
        WHERE n.id IN w.nodes AND (w.tags =~ '.*${filters}.*')


        RETURN w, n, distanceToNode
        ORDER BY distanceToNode ASC
        LIMIT 1

nearest point
`WITH point({latitude: ${node.latitude}, longitude: ${node.longitude}}) AS givenPoint

        MATCH (n:Node)
        WITH n, givenPoint, point.distance(givenPoint, point({latitude: n.latitude, longitude: n.longitude})) AS distanceToNode
        WHERE distanceToNode <= 500
        WITH n, distanceToNode


        MATCH (w:Way)
        WHERE n.id IN w.nodes AND (w.tags =~ '.*${filters}.*')


        RETURN w, n, distanceToNode
        ORDER BY distanceToNode ASC
        LIMIT 1
        `

A couple of things to try:

  1. Store the Point as a property, i.e. point({latitude: n.latitude, longitude: n.longitude})
  2. Create a Point index on your new property
  3. Refactor the query so the distance is part of the match predicate, then maybe it can leverage the point index
  4. Use relationships to relate 'n' to 'w', so you don't have to use the 'n.id IN w.nodes' predicate (you should not use 'keys' to related nodes, but use relationships - it is much faster)

Refactored query:

WITH point({latitude: 30, longitude: 30}) as givenPoint
MATCH (n:Node)
WHERE point.distance(givenPoint, n.point) <= 500
WITH n, point.distance(givenPoint, n.point) as distanceToNode
MATCH (w:Way)
WHERE n.id IN w.nodes AND (w.tags =~ '.*${filters}.*')
RETURN n, w, distanceToNode
ORDER BY distanceToNode ASC
LIMIT 1
1 Like

Thank you for the guidance

1 Like