Sovos
Sovos

Reputation: 3390

neo4j cypher, performance on specifying labels when id(n) is in conditions

has anyone tested/knows if - when querying a Neo4j database with Cypher - specifying the

MATCH node:labels

makes the selection faster even if

WHERE id(node) = x

is in place?

Upvotes: 1

Views: 130

Answers (1)

jjaderberg
jjaderberg

Reputation: 9952

MATCH (n)
WHERE ID(n) = {x}
RETURN n

should be negligibly faster than

MATCH (n:MyLabel)
WHERE ID(n) = {x}
RETURN n

Both queries first get the node by internal id but while the first query returns the second filters the result on hasLabel(n:MyLabel).

It's a good example of where its possible to overuse labels. Similarly, if for the pattern (a:Person {name:"Étienne Gilson"})-[:FRIEND]->(b:Person)-[:FRIEND]->(c:Person) I know that only :Person nodes have -[:FRIEND]- relationships, there is no point in filtering b and c on that label. If a remote node should be retrieved from index then the label should be included to indicate that, i.e. -[:FRIEND]->(b:Person {name:"Jacques Maritain"}), but when no (indexed) property is included in that part of the pattern, the nodes will be reached by traversal and if only people have friends an additional filter on hasLabel(b:Person) would be pointless.

I understand this blog post to mean that filtering on a label is as cheap as a bitwise &, so performance difference should be very small.

Upvotes: 2

Related Questions