Showing results for 
Search instead for 
Did you mean: 

How do I improve the performance of counting number of relationships on a node


Using Cypher one could count number of relationships in the following manner

MATCH (n:Actor {name:'Sylvester Stallone'})-[]-() RETURN count(*);

Which will report the number of incoming/outgoing relationships for the Actor named Sylvester Stallone.

Using bin/neo4j-shell and running a profile on the query will produce the following output

The Cypher can be rewritten to

MATCH (n:Actor {name:'Sylvester Stallone'})
RETURN size((n)-[]-())

And to which the profile is:

From the profile above you will see a reference to GetDegree(n,None,BOTH) which occurs as a result of the usage of size( (n)-[]-());
Because of this we do not actually perform a traversal of relationships from (n) but rather consult the degree value stored with the given node.

In the above GetDegree() expression, the 2nd parameter refers to the relationship name, and the 3rd parameter refers to the direction of the relationship.

For example the Cypher of

MATCH (n:Actor {name:'Sylvester Stallone'})
RETURN size( (n)-[:ACTED_IN]-())

would result in the following profile:

and thus a GetDegree(n,Some(ACTED_IN),BOTH)

Nodes 2022
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.