3
votes

I have a Neo4j graph which is made up by a total of 100.000 users and 2.000.000 relationships (friendships between the users). A user has about 20 friendships.

Now I'm trying to see how much time it takes to find the friends of a specific user (depth 1), the friends of friends (depth 2) and the friends of friends of friends (depth 3).

This is the cypher queries I ran (for user with id 86660):

For depth 1

MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF]->(u2:User)
RETURN u2.name

For depth 2

MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF]->(u2:User)-[:FRIEND_OF]->(u3:User)
RETURN u3.name

For depth 3

MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF]->(u2:User)-[:FRIEND_OF]->(u3:User)-[:FRIEND_OF]->(u4:User)
RETURN u4.name

depth 1 (it returns me 17 results) and depth 2 (it returns 320 results) queries took few milliseconds while depth 3 is endless.

How can I get the result for depth3 in a reasonable time?

UPDATE

With PROFILE I get this:

PROFILE 
    MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF]->(u2:User)-[:FRIEND_OF]->(u3:User)-[:FRIEND_OF]->(u4:User)
    RETURN u4.name

enter image description here

1
Can you run profile on your query and post extended viewTomaž Bratanič
I shut down my pc and start it again after few minutes (it was warm, cpu was working really hard). There were processes working on background which made my computer performance poor. I re-ran the query again and it took 13 seconds. By the way I'm going to update my question including the extended view you asked for. I think I can do better than 13 seconds, even if I'm using an old pc.splunk
can you try MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF*3..3]->(u2:User) RETURN u2.name just for fun i have no clue if it will helpTomaž Bratanič
Thank you! yes it helped (it took less than 1 second). Where can I find an explanation about the difference between ()-[:FRIEND_OF*3..3]->() and ()-[:FRIEND_OF]->()-[:FRIEND_OF]->()-[:FRIEND_OF]->() ?splunk

1 Answers

1
votes

Apparently this helps

MATCH (u1:User{idUtente:"86660"})-[:FRIEND_OF*3..3]->(u2:User) RETURN u2.name

I think the difference between my query and yours is you map all the entities along the way, while [:FRIEND_OF*3..3] limits my query to check only for entitites that are 3 hops away. You can find more info in documentation