cancel
Showing results for
Did you mean:

## Counting outgoing edges from vertices with unique property and index

Node

Hi Neo4j community,

I appreciate your great support as always. I have a dataset with Customer vertices and Order vertices and these are connected in the following way

(Customer) --[:PURCHASED]--> (Order)

and no edges going from Order to Customer.

Now I would like to find out what the minimum, maximum and average amount of Orders per Customer are and perform the following query.

``````MATCH (c:Customer) WITH
SIZE([(c)--(o:Order) | o]) AS amount
RETURN min(amount), max(amount), avg(amount)``````

This works perfectly fine. However, I would like to know if I can speed things up as the Customer vertices have the property 'customerID' which is unique and I have enforced a uniqueness constraint with respective index on 'customerID' for Customer vertices.

I have tried several approaches, but cannot quite get there and I am not even sure if this is possible.

Thank you very much for any help.

Best,

Philipp

3 REPLIES 3
Ninja

MATCH (c:Customer)
WITH size((c)-[:PURCHASED]->()) AS outdegree
RETURN min(outdegree) AS min, max(outdegree) AS max, avg(outdegree) AS avg

DO NOT specify a label in the size function while using a pattern for this use case, otherwise Neo4j won't be able to use the internal outgoing relationship counter in each customer node.

By the way, a vertice is a part of a geometric structure as being just a point in the space, a node is much more complex so that's why we call them node.

Ninja

I do not think so, since you are not searching on anything specific. Your solution creates a list and uses list comprehension to calculate the count. You could try an alternative approach, like the following:

``````match(n:Customer)
optional match(n)--(m:Order)
with n, count(m) as count
return min(count) as min, max(count) as max, avg(count) as avg``````

Hi,