Hello,
I am trying to optimize a query I have been working on but do not understand why cypher/neo4j profiler hits the database as much as it does.
The query below tries to find all mutual contacts for a given user $user_id
.
1st pass
profile MATCH (u1:User {user_id: $user_id})-[:CONTACT]->(u2:User)
where exists ((u2)-[:CONTACT]->(u1))
return u1,u2
2nd pass (better but still not great)
profile MATCH (u1:User {user_id: $user_id})-[:CONTACT]->(u2:User)
with u1,u2
match ((u2)-[:CONTACT]->(u1))
return u1,u2
My understanding is that using WITH
I am signaling to the 2nd MATCH
clause the existence of the start and end nodes. However, the profiler seems to tell me that this incurs the most amount of db hits: https://ibb.co/JHR9CzB . I'm confused about best practices for things like this and how to optimize my query. Thank you!