Article DetailsDetailsHelp DetailsSometimes within a query, the same path needs to be traversed multiple times, with the only differences being properties or labels of the nodes at the end of the expansion. While you can obtain correct results by performing the MATCHing multiple times, if the path to be expanded is truly nearly identical, it may be more efficient to perform this only once, and use list filtering to obtain the required separate sets of results from the larger initial set. Take for example a social network where people can follow other people, and they can also follow groups, and the same relationship is used in both cases: (:Person)-[:FOLLOWS]->(:Person) (:Person)-[:FOLLOWS]->(:Group) If we wanted to get a list of persons that are followed and, a separate list of groups that are followed, a straightforward approach might be to perform this in two separate MATCHes, collecting results between. For example: MATCH (me:Person {id:$myId}) MATCH (me)-[:FOLLOWS]->(follow:Person) WITH me, collect(follow) as peopleFollowed MATCH (me)-[:FOLLOWS]->(group:Group) WITH me, peopleFollowed, collect(group) as groupsFollowed RETURN me, peopleFollowed, groupsFollowed You could express the same thing more succinctly with pattern comprehension, for example: MATCH (me:Person {id:$myId}) WITH me, [(me)-[:FOLLOWS]->(follow:Person) | follow] as peopleFollowed, [(me)-[:FOLLOWS]->(group:Group) | group] as groupsFollowed RETURN me, peopleFollowed, groupsFollowed Or in Neo4j 5.x with collect{} subqueries which is useful if more complex predicates or logic is needed, for example: MATCH (me:Person {id:$myId}) WITH me, collect {MATCH (me)-[:FOLLOWS]->(follow:Person) RETURN follow} as peopleFollowed, collect {MATCH (me)-[:FOLLOWS]->(group:Group) RETURN group} as groupsFollowed RETURN me, peopleFollowed, groupsFollowed However each of these approaches, no matter how succinct, still traverses the same path multiple times. Therefore there is an opportunity for optimization. For example: MATCH (me:Person {id:$myId}) WITH me, collect {MATCH (me)-[:FOLLOWS]->(follow:Person|Group) RETURN follow} as followed WITH me, [follow IN followed WHERE follow:Person] as peopleFollowed, [follow IN followed WHERE follow:Group] as groupsFollowed RETURN me, peopleFollowed, groupsFollowed Here, we only expand the path once, and the intersecting criteria in this case can be expressed in the label pattern. For a more complex criteria, we might express that in the WHERE clause, or even omit it and handle it later in the list filtering. The list filtering executes over the single list of results, yielding separate lists depending on the filter criteria, and from this we have saved ourselves from having to traverse the same path multiple times. In this particular case the path expanded is short and not very complex, and we would not be saving much time at all by optimizing, but in cases where the path is very complex or costly, this technique may be helpful, especially in cases where it is not just 2 expansions of the same path, but many. InformationTitleImproving query performance by reusing similar expansion resultsArticle Created Date3/15/2024, 10:54 PMURL Name20700416831891-Improving-query-performance-by-reusing-similar-expansion-resultsLast Modified Date3/18/2024, 4:02 AM