简体   繁体   English

如何解释这个 Cypher 查询的执行路径?

[英]How to explain the execution path of this Cypher query?

Imagine the following graph:想象下图:

在此处输入图片说明

And this query:这个查询:

MATCH(p:Person {id:1})
MATCH (p)-[:KNOWS]-(s)
CREATE (p)-[:LIVE_IN]->(:Place {name: 'Some Place'})

Now, why five LIVE_IN , Place are created even though s is not involved in the CREATE statement?现在,为什么即使CREATE语句中不涉及s ,也会创建五个LIVE_IN , Place is there any place in the docs that explain this behavior?文档中有什么地方可以解释这种行为吗?

Note: this is not about MERGE vs CREATE , although MERGE can solve it.注意:这与MERGECREATE无关,尽管MERGE可以解决它。

EDIT: In response to @Tomaz answer: I have deliberately placed MATCH (p)-[:KNOWS]-(s) in the query and I know how it will behave.编辑:为了回应@Tomaz 的回答:我故意将MATCH (p)-[:KNOWS]-(s)放在查询中,我知道它将如何表现。 I am asking for found explanations.我要求找到解释。 For example, is CREATE will execute for each path or row in the matched patterns regardless of the node involved in the CREATE ?例如,无论CREATE涉及的节点如何, CREATE是否都会针对匹配模式中的每个路径或行执行? what if you have complex matched patterns such as, disconnected graph, Trees...etc?如果您有复杂的匹配模式,例如断开的图、树...等怎么办?

Also note that the direction of relationship KNOWS ( - vs -> ) will effect the number of returned rows (9 vs 1), but CREATE will execute five times regardless of the direction.另请注意,关系KNOWS ( - vs -> ) 的方向将影响返回的行数 (9 vs 1),但无论方向如何, CREATE都会执行五次。

Update:更新:

I have added 3 other node Office and issued the following query:我添加了 3 个其他节点Office并发出以下查询:

MATCH(p:Person {id:1})
MATCH (p)-[:KNOWS]-(s)
MATCH (o:Office)
CREATE (p)-[:LOVE]->(:Place {name: 'Any Place'})

And as result: 15 LOVE Place have been created, so it seems to me that cypher performs Cartesian Product between all nodes:结果:已经创建了 15 个LOVE Place ,所以在我看来,cypher 在所有节点之间执行笛卡尔积

p refer to 1 nodes, s refer to 5 nodes, o refer to 3 nodes => 1 * 5 * 3 = 15 p指 1 个节点, s指 5 个节点, o指 3 个节点 => 1 * 5 * 3 = 15

But I can not confirm this form neo4j docs unfortunately.但不幸的是,我无法确认这种形式的 Neo4j 文档。

This is because the Person with id 1 has five neighbors.这是因为 id 为 1 的 Person 有五个邻居。

In your query you start with:在您的查询中,您从以下内容开始:

MATCH(p:Person {id:1})

This produces a single row, where it finds the node you are looking for.这会生成一行,在其中找到您要查找的节点。 The next step is:下一步是:

MATCH (p)-[:KNOWS]-(s)

This statement found 5 neighbors, so your cardinality or number of rows increases to five.此语句找到 5 个邻居,因此您的基数或行数增加到 5。 And then you run a create statement for each row, which in turn creates five Places.然后你为每一行运行一个 create 语句,它依次创建五个 Places。 You could for example lower the cardinality back to 1 before doing the CREATE and you'll create only a single place:例如,您可以在执行CREATE之前将基数降低回 1,并且您将只创建一个地方:

MATCH(p:Person {id:1})
MATCH (p)-[:KNOWS]-(s)
// aggregation reduces cardinality to 1
WITH p, collect(s) as neighbors
CREATE (p)-[:LIVE_IN]->(:Place {name: 'Some Place'})

When doing cypher query, always have in mind the cardinality you are operating.在进行密码查询时,请始终牢记您正在操作的基数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM