After investigation, we have noticed that Neo4j is retrieving too many relationships that we do not need. Regarding this, we modified the query like this :
MATCH (user:N{userId:"1234"})
OPTIONAL MATCH (user)<-[:owner]-(i:N) WHERE NOT (i)-[:permission]->()
MATCH (i)-[r]->()
RETURN COLLECT(DISTINCT i) AS nodes,COLLECT(DISTINCT r) AS relations
UNION
MATCH (user:N{userId:"1234"})
OPTIONAL MATCH (user)<-[:hasRole]-(:N:Role)-[:extendRole *0..]->(r:N:Role)
OPTIONAL MATCH (r)<-[:permission]-(p:N:Perm) WHERE p.perm = "READ" WITH p
MATCH (p)<-[:group]-(:N:Group)<-[:groupExtend *0..]-(:N:Group)<-[:nodes]-(i:N)
MATCH (i)-[r]->()
RETURN COLLECT(DISTINCT i) AS nodes,COLLECT(DISTINCT r) AS relations
The key modification is the COLLECT(DISTINCT r), which seems to return the same number of relationships as before, but faster than COLLECT((i)-->()) we used to do before.
Here is the request PROFILE :
It seems like there is one branch less for the collect.
If someone in the Neo4j staff could explain this.
Thanks.