为什么我应该使用exists()函数来存在模式?

时间:2017-08-24 23:14:25

标签: neo4j cypher

这两个假设的Cypher查询产生相同的结果:

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

1 个答案:

答案 0 :(得分:3)

在WHERE子句中使用时,它们在语义上应该相等,但是在WHERE子句之外需要exists()。

一个例子是当你想要一个布尔来表示模式是否存在时。

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