【问题标题】:Why should I use the exists() function for pattern existence?为什么我应该使用 exists() 函数来实现模式存在?
【发布时间】:2017-08-24 23:14:25
【问题描述】:

这两个假设的 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

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

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

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

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

    【讨论】:

    • 我注意到这些查询的PROFILE 的输出存在一些差异(请参阅我的编辑)。带有exists() 的查询具有更紧凑的执行计划。
    猜你喜欢
    • 2011-09-09
    • 2011-09-28
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多