neo4j performance issue when filtering nodes

Viewed 43

What is the most efficient way to match all path that contains node with label a, b or c but which can contains other type of nodes. It works for smaller graph but got performance issue on larger.

For example :

  • a -> b -> c must be matched
  • a -> b -> d -> c must be matched
  • a -> b -> d no match
  • a -> b no match

Any idea ? I did it with with

MATCH (a)
WHERE exists((a)-->(b)) and exists((a)-->(c))
1 Answers

I would go with this:

MATCH (a)-[*]->(b)-[*]->(c)

This allows any number of nodes between a and b and between b and c. Depending on the number of nodes you have, this might not be very performant. In that case, I would work with indices (which are cheap in Neo4j) to speed things up, and perhaps even script your Cypher query using Python and dynamic string replacement.

Related