Avoid redundant node label checks by using granular relationship names
When optimizing for performance, using relationship labels that are exclusive to node patterns allows the removal of
label filtering on nodes. Consider a graph model where the relationship likes
is only used to define
a relationship between two person
nodes. We could write the following query to find this pattern:
MATCH (n:person)-[:likes]->(m:person) RETURN n, m
The person
label check on n and m is redundant, as we defined the relationship to only appear when both
are of the type person
. To optimize on performance, we can write the query as follows:
MATCH (n)-[:likes]->(m) RETURN n, m
This pattern can also apply when properties are exclusive to a single node label. Assume that only person
nodes have the property email
, therefore verifying the node label matches person
is
redundant. Writing this query as:
MATCH (n:person) WHERE n.email = 'xxx@gmail.com' RETURN n
Is less efficient than writing this query as:
MATCH (n) WHERE n.email = 'xxx@gmail.com' RETURN n
You should only adopt this pattern when performance is important and you have checks in your modeling process to
ensure these edge labels are not reused for patterns involving other node labels. If you later introduce an
email
property on another node label such as company
, then the results will differ
between these two versions of the query.