2017-08-24 22 views
1

這兩個假設的Cypher查詢產生相同的結果:爲什麼我應該使用exists()函數進行模式存在?

MATCH(s:Start) 
WHERE exists((s)-[:CONNECTED_TO]->(:End)) 
RETURN s 

MATCH(s:Start) 
WHERE (s)-[:CONNECTED_TO]->(:End) 
RETURN s 

唯一不同的是,第二個查詢具有對exists()功能沒有呼叫,但語義這兩個查詢是平等的。對?

那麼,爲什麼和什麼時候應該使用exists()函數傳遞一個模式作爲參數?

編輯:

我注意到的PROFILE輸出一些差異:

PROFILE MATCH(s:Start) 
WHERE exists((s)-[:CONNECTED_TO]->(:End)) 
RETURN s 
+------------------+----------------+------+---------+-----------+-----------------------------------------------+ 
| Operator   | Estimated Rows | Rows | DB Hits | Variables | Other           | 
+------------------+----------------+------+---------+-----------+-----------------------------------------------+ 
| +ProduceResults |    2 | 1 |  0 | s   | s            | 
| |    +----------------+------+---------+-----------+-----------------------------------------------+ 
| +Filter   |    2 | 1 |  5 | s   | NestedExpression(Filter-Expand(All)-Argument) | 
| |    +----------------+------+---------+-----------+-----------------------------------------------+ 
| +NodeByLabelScan |    3 | 3 |  4 | s   | :Start          | 
+------------------+----------------+------+---------+-----------+-----------------------------------------------+ 

Total database accesses: 9 

PROFILE MATCH(s:Start) 
WHERE (s)-[:CONNECTED_TO]->(:End) 
RETURN s 

+------------------+----------------+------+---------+-------------------------+-------------------------+ 
| Operator   | Estimated Rows | Rows | DB Hits | Variables    | Other     | 
+------------------+----------------+------+---------+-------------------------+-------------------------+ 
| +ProduceResults |    2 | 1 |  0 | s      | s      | 
| |    +----------------+------+---------+-------------------------+-------------------------+ 
| +SemiApply  |    2 | 1 |  0 | s      |       | 
| |\    +----------------+------+---------+-------------------------+-------------------------+ 
| | +Filter  |    1 | 0 |  1 | anon[29], anon[47], s | anon[47]:End   | 
| | |    +----------------+------+---------+-------------------------+-------------------------+ 
| | +Expand(All) |    1 | 1 |  4 | anon[29], anon[47] -- s | (s)-[:CONNECTED_TO]->() | 
| | |    +----------------+------+---------+-------------------------+-------------------------+ 
| | +Argument  |    3 | 3 |  0 | s      |       | 
| |    +----------------+------+---------+-------------------------+-------------------------+ 
| +NodeByLabelScan |    3 | 3 |  4 | s      | :Start     | 
+------------------+----------------+------+---------+-------------------------+-------------------------+ 

Total database accesses: 9 

回答

3

當WHERE子句中使用時,它們應該是語義上相等,但存在其中存在的情況下()需要在WHERE子句之外。

一個例子是當你想要一個布爾值來表示模式是否存在。

MATCH (s:Start) 
RETURN exists((s)-[:CONNECTED_TO]->(:End)) as connectedToEnd 
+0

我注意到這些查詢的'PROFILE'輸出有些差異(請參閱我的編輯)。具有'exists()'的查詢具有更緊湊的執行計劃。 –

相關問題