Optimize getting all incoming and outgoing edges for each node and computing the difference in amounts

Hello @tomas_vrba :slight_smile:

This is a solution:

MATCH (n)
CALL {
    WITH n MATCH (n)-[r]->() RETURN count(r) AS occurrences
    UNION
    WITH n MATCH (n)<-[r]-() RETURN count(r) AS occurrences
}
WITH n, collect(occurrences) AS values
SET n.balance = values[0] - values[1]

To speed-up this query, you can do several things if you are not working on all nodes of the database:

  • specify a label or a relation type
  • specify a UNIQUE CONSTRAINT and use the index created in a WHERE clause

Regards,
Cobra