0
votes

I am creating a social media app whose back-end is Neo4j. Here, I want to fetch friends activities on user's homepage. I have put a limit of 10 activities per scroll. This is my current Neo4j Query:

MATCH (m:Person { id: '$id'})-[:FOLLOWS*1..1]->(f:Person)-[:HAS_ACTIVITY]->(la:Activity)-[:NEXT*0..]->(act:Activity)
WHERE act.verb IN ['post' ,'post_media_item' ,'watch_activity'] 
RETURN f, act ORDER BY act.published DESC LIMIT 10

In the above query user are depicted by Person node and User-feed is depicted by Activity node. "m" is current user whose friends "f" are depicted by the relationship "Follows". Activity for each user are interlinked with the relationship "NEXT" and are linked with Person node with relationship "HAS_ACTIVITY". It is intentional to keep maximum hops blank as a Person can have any number of activities.

For this query as Friend List of a person grows, so does the overall query execution time. Currently for a user having 50 friends the time taken is 3-5 seconds. I have to perform further aggregation from the result of this query which takes query execution time to 7-10 seconds.

Please find attached a screenshot of PROFILE and EXPLAIN:

PROFILE PROFILE

How can I optimize my Cypher query so that I get the quickest result irrespective of a number of friends?

Neo4j Version used: 3.0.6

1

1 Answers

0
votes

A simple thing would be to update to Neo4j 3.5.x at least.

If a person has all activities linked to it with HAS_ACTIVITY relationship, why do you need to further traverse the NEXT relationship? Would the following query not suffice?

MATCH (m:Person { id: '58c1370350b91a0005be0136'})-[:FOLLOWS*1..1]->(f:Person)-[:HAS_ACTIVITY]->(la:Activity)
WHERE la.verb IN ['post' ,'post_media_item' ,'watch_activity'] 
RETURN f, la ORDER BY la.published DESC LIMIT 10