Search code examples
neo4jcypher

How to optimize multiple matches before deleting all matching nodes?


I have graph where location is root nodes under which I have cloud resources. These locations connect with others through a pair of gateways connected by bidirectional HAS_REMOTECONNECTION edges.

How can I optimize following query where I am trying to delete everything under a location including location itself?

MATCH (loc:location{resource_id:'some_id', tenant_id:'some_tenant_id'})
    OPTIONAL MATCH (loc)-[:LOCATION_HAS_DE]->(deplenvs:(cluster|node|vpc))
    OPTIONAL MATCH (deplenvs)-[:DE_HAS_GW]->(gateways)
    OPTIONAL MATCH (deplenvs)-[:DE_HAS_PART]->(parts)-[*]->(extras)
RETURN loc, deplenvs, gateways, parts, extras

Solution

  • The start point of optimising would be to ensure you have a compound index set up on your location node's resource_id and tenant_id properties. That will give you fast access to your location nodes.

    From there you are just pointer hopping, which is about as optimised as you can get.