2013-07-26 91 views
0

這有點與此相關的問題,但不完全一樣:Performing a MATCH on all nodes on a pathNeo4j的充分利用節點的節點的路徑

像前面的問題,我有一個類似的設置:

(a)-->(b)-->(c)-->(d) 
^ ^ ^ ^
|r |r |r |r 
(aa) (bb) (cc) (dd) 

例創建(見控制檯http://console.neo4j.org/r/vx99q):

CREATE 
    (a {name:'a'}), 
    (b {name:'b'}), 
    (c {name:'c'}), 
    (d {name:'d'}), 
    (aa {name:'aa'}), 
    (bb {name:'bb'}), 
    (cc {name:'cc'}), 
    (dd {name:'dd'}), 
    a-[:rel1]->b, 
    b-[:rel1]->c, 
    c-[:rel1]->d, 
    a-[:r]->aa, 
    b-[:r]->bb, 
    c-[:r]->cc, 
    d-[:r]->dd; 

比方說,我有a第7和第d爲4。我會得到在p像這樣:

START a=node(7), d=node(4) 
MATCH path = a-[:rel1*]->d 
RETURN path 

我想要的是aa,bb,cc和dd。

我很難過。

回答

1

好吧,我花了一些時間來圍繞這個問題,但我明白了。

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) 
+1

你應該能夠刪除'N =從離開ñ只有在START子句和'從N'節點(*)'第二個MATCH和WHERE。這是否有速度差異? – yhw42

+0

我的方式是344ms,你的方式是174ms。超小型數據集,所以我認爲它很重要。 – LameCoder