Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-07-2021 02:34 AM
Context:
Query
Now we have the following simplified query:
PROFILE MATCH
(u:User {Id: '<USER_ID>'})-[:MEMBER_OF]->(g:Group),
(t:Tenant)-[:CHILD_OF*0..5]->(root:Tenant)<-[:BELONGS]-(g)
RETURN t.Id, g.Id
So we want to retrieve all the groups for the given user. However, groups can be inherited and it is why we traverse a hierarchy of tenants.
Issue
The query returns correct results but it is slow.I profiled the query and I believe that the problem is here:
The engine firstly finds all possible combinations of users and groups what gives around 2 mln records and from all these records it selects only 5K for a given user.
From my perspective, it is not optimal. It would be much faster to start from a given user and find groups for it.
Do you have any suggestions? So far I tried to play with hints, different structure of query but all the time the query plan is the same.
Solved! Go to Solution.
01-07-2021 11:40 PM
I found out that described problem can be fixed by rewriting the query in the following way:
MATCH (u:Usert {Id: '<USER_ID>'})-[:MEMBER_OF]->(g1:Group)
WITH COLLECT(g1.Id) AS groups
MATCH(t:Tenant)-[:CHILD_OF*0..5]->(root:Tenant)<-[:BELONGS]-(g2)
WHERE g2.Id IN groups
RETURN t.Id, g2.Id
The main changes are:
In this case, the engine does not try to generate all possible combinations of groups and users.
01-07-2021 11:10 AM
Check this out. You can force an early match that filters out the most irrelevant results, then follow up with matches that are broader.
01-07-2021 11:41 AM
Also look here:
01-07-2021 01:26 PM
My first opinion - (good news)
Just looking at the two steps of the profile - i see that there is a huge pagecache hits and 0 misses, which means the page cache is used to cache the data and avoiding accessing disk which will typically make the query run faster.
If can you post the full query plan, the count of the nodes, your infrastructure settings, and neo4j version (enterprise / community) that would be helpful for me and others in the community to provide more insights.
01-07-2021 11:40 PM
I found out that described problem can be fixed by rewriting the query in the following way:
MATCH (u:Usert {Id: '<USER_ID>'})-[:MEMBER_OF]->(g1:Group)
WITH COLLECT(g1.Id) AS groups
MATCH(t:Tenant)-[:CHILD_OF*0..5]->(root:Tenant)<-[:BELONGS]-(g2)
WHERE g2.Id IN groups
RETURN t.Id, g2.Id
The main changes are:
In this case, the engine does not try to generate all possible combinations of groups and users.
All the sessions of the conference are now available online