好吧,我花了一些時間來圍繞這個問題,但我明白了。
START n=node(*), a=node(1), z=node(4)
MATCH path = a-[:rel1*]->z
WITH n, path
MATCH n-[:r]->o
WHERE n IN path
RETURN o
雖然它看起來很慢,但我想可能有更好的方法來做到這一點。
輸出:
o
(5 {name:"aa"})
(6 {name:"bb"})
(7 {name:"cc"})
(8 {name:"dd"})
編輯: yhw42提到,如果修改,像這樣的查詢可以運行得更快:
START a=node(1), z=node(4)
MATCH path = a-[:rel1*]->z
WITH path
MATCH n-[:r]->o
WHERE n IN path
RETURN o
在樣本,它沒有。我的辦法是:
Query Results
+--------------------+
| o |
+--------------------+
| Node[5]{name:"aa"} |
| Node[6]{name:"bb"} |
| Node[7]{name:"cc"} |
| Node[8]{name:"dd"} |
+--------------------+
4 rows
344 ms
Execution Plan
ColumnFilter(symKeys=["n", "path", "o", " UNNAMED88"], returnItemNames=["o"], _rows=4, _db_hits=0)
PatternMatch(g="(n)-[' UNNAMED88']-(o)", _rows=4, _db_hits=0)
Filter(pred="any(-_-INNER-_- in path where n == -_-INNER-_-)", _rows=4, _db_hits=0)
ColumnFilter(symKeys=["path", "n", "a", " UNNAMED53", "z"], returnItemNames=["n", "path"], _rows=9, _db_hits=0)
ExtractPath(name="path", patterns=[" UNNAMED53=a-[:rel1*]->z"], _rows=9, _db_hits=0)
PatternMatch(g="(a)-[' UNNAMED53']-(z)", _rows=9, _db_hits=0)
NodeById(name="Literal(List(4))", identifier="z", _rows=9, _db_hits=9)
NodeById(name="Literal(List(1))", identifier="a", _rows=9, _db_hits=9)
AllNodes(identifier="n", _rows=9, _db_hits=9)
他的做法是:
Query Results
+--------------------+
| o |
+--------------------+
| Node[5]{name:"aa"} |
| Node[6]{name:"bb"} |
| Node[7]{name:"cc"} |
| Node[8]{name:"dd"} |
+--------------------+
4 rows
174 ms
Execution Plan
ColumnFilter(symKeys=["path", "n", "o", " UNNAMED74"], returnItemNames=["o"], _rows=4, _db_hits=0)
PatternMatch(g="(n)-[' UNNAMED74']-(o)", _rows=4, _db_hits=0)
Filter(pred="any(-_-INNER-_- in path where n == -_-INNER-_-)", _rows=4, _db_hits=0)
AllNodes(identifier="n", _rows=9, _db_hits=9)
ColumnFilter(symKeys=["a", "z", " UNNAMED42", "path"], returnItemNames=["path"], _rows=1, _db_hits=0)
ExtractPath(name="path", patterns=[" UNNAMED42=a-[:rel1*]->z"], _rows=1, _db_hits=0)
PatternMatch(g="(a)-[' UNNAMED42']-(z)", _rows=1, _db_hits=0)
NodeById(name="Literal(List(4))", identifier="z", _rows=1, _db_hits=1)
NodeById(name="Literal(List(1))", identifier="a", _rows=1, _db_hits=1)
你應該能夠刪除'N =從離開ñ只有在START子句和'從N'節點(*)'第二個MATCH和WHERE。這是否有速度差異? – yhw42
我的方式是344ms,你的方式是174ms。超小型數據集,所以我認爲它很重要。 – LameCoder