Too slowly recount nodes and relationships

Hi community,
I need some help with a query, we are very happy with funtion of database but we have a problem when we count a lot of data and all their relations.
For example, when we have a count from 1.000 nodes the database reply very quickly, but when we need check 130.000 nodes with all of relations, the query is too slowly
Then I need some help for get the count nodes with your relations.
Today we count all as follows:
We search a root person and get all people with relation to person, after that in the response we push all results filtered with some conditionals,
in this case are Internal, External and Vacant.

MATCH (p:Person{alias: 'Batman'})<-[:REPORT_TO*0..]-(per:Person {recount: true})
with {
    internal: size((per)<-[:REPORT_TO*1..]-(:Person {type: 'Internal', recount: true})),
    external: size((per)<-[:REPORT_TO*1..]-(:Person {type: 'External', recount: true})),
    vacant: size((per)<-[:REPORT_TO*1..]-(:Person {type: 'Vacant', recount: true}))
} as json
WHERE json.internal > 0  OR json.external > 0 OR json.vacant > 0


¿Which is the best form for count nodes and relations in all your depth?

Do you have any indexes on the node attributes you're filtering on? You're correct to be using pattern comprehension to aggregate up the size. What I'm thinking is this might be a case where you'd want to refactor the Person.type as a Type node with a relationship between the person. Another modeling idea would be to use multiple labels on the Person Node. Add a secondary label of :Internal, :External, Vacant to the person node.

These are just ideas to test to see if Neo4j doesn't have to read the attributes but can just rely on the meta data or traverse the graph instead if that will yield better performance.

1 Like

I understand, we currently create properties with those types and create indexes to optimize searches.

Being so wide the amount of relationships that exist between the nodes is slow and with an integration in nodejs gives error for response times.

Use additional labels instead of recount: true -> :Recount the same for your additional type properties.

You should also use an upper limit for your path lengths

Is your per person the leaf? then you can add an where not (per)<-[:REPORTS_TO]-()
filter to return only one path per pattern.

your sub-size computations are also quite expensive,
so I would do them step by step by the most limiting first

so I would use a path expander in APOC for that.

and construct the JSON at the end.