简体   繁体   English

Neo4j更高效的Cypher查询,用于查找具有关系的连接节点

[英]Neo4j more efficient Cypher query for finding connected nodes with a relationship

I have a patch where I know a starting and end node with an unknown number of nodes between the 2. I wish to collect a collection of nodes and each nodes exiting relationship in the chain. 我有一个补丁,其中我知道一个起始节点和结束节点,其中2个节点之间的节点数量未知。我希望收集节点的集合以及链中每个退出节点的关系。

PROFILE MATCH p = (:Question{id:'1234'})-[:Answer*0..3]->(t)-
[:Answer]->(:Question{id:'5678'})
WHERE t:Set OR t:Read
OPTIONAL MATCH (x)-[v:Answer]->(y)
WHERE x.id <> '1234' and x IN nodes(p) AND v IN rels(p)
return x,v

This query is pretty inefficient as the OPTIONAL MATCH (x)-[v:Answer]->(y) requires a full nodes scan. 该查询的效率非常低,因为OPTIONAL MATCH (x)-[v:Answer]->(y)需要完整的节点扫描。

I know that t and as a result x will be of types Set or Read which would reduce the scan quite a bit but don't think there is a way to query this. 我知道t ,结果x的类型将是SetRead ,这会大大减少扫描量,但不认为有查询此方法的方法。

Is there any way to optimise this? 有什么办法可以优化这个?

You can simply unwind the path you have already got: 您可以简单地展开已经拥有的路径:

MATCH p = (:Question{id:'1234'})-[:Answer*0..3]->(t)-[:Answer]->(:Question{id:'5678'})
WHERE t:Set OR t:Read
WITH nodes(p) AS nds, 
     rels(p) AS rls
UNWIND range(1, length(nds)-2) AS i
RETURN nds[i] AS x, 
       rls[i] AS v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM