Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j query takes too long

Tags:

neo4j

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

like image 409
splunk Avatar asked Oct 23 '25 18:10

splunk


1 Answers

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

like image 187
Tomaž Bratanič Avatar answered Oct 26 '25 12:10

Tomaž Bratanič



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!