Improve Query performance

Hi all,

I'm working on a query that involves a spatial search and filtering based on relationships between nodes. The goal is to find the nearest points of interest (POIs) within a certain distance and return their names, ordered by their distance and the number of repetitions.

Here’s the query I’m currently using:

MATCH (ca:Calle {cod:7})-[]-(i:Inmueble)
WITH ca, i AS inmuebles 
LIMIT 100
CALL spatial.withinDistance('Data.osm', inmuebles.punto, 5) 
YIELD node, distance
WITH node AS n, distance, ca
MATCH (c:OSMWay:Direccion)-[:GEOM]->(n)
WHERE c.name IS NOT NULL
WITH c, ca, distance 
ORDER BY distance ASC 
LIMIT 10
RETURN c.name, ca.nombre, COUNT(c.name) AS repeticiones 
ORDER BY repeticiones DESC 
LIMIT 1;
  1. How can I improve the performance of this query, especially considering the use of spatial.withinDistance and the multiple WITH clauses?
  2. Are there any indices or strategies that could help with optimizing spatial queries?
  3. Should I consider restructuring the query for better readability and performance, or is there a more efficient way to achieve the same result?

You should have an index on property ‘cod’ for label ‘Calle’, so your initial match is fast.

Your query is not complicated beyond the use of the spatial library method to search for near by nodes.

You will call spatial.withinDistance up to 100 times. Is it possible to reduce that set first because some may be close to each other?

Thank you for answering my question. I've enhanced the query performance by reducing the maximum distance for the database search.