在具有相同属性的节点之间创建关系(Creating relationships between nodes with same properties)

编程入门 行业动态 更新时间:2024-10-28 11:34:28
在具有相同属性的节点之间创建关系(Creating relationships between nodes with same properties)

我需要在所有节点之间创建具有相同属性值的关系。

例如,我可以使用以下查询:

match (p1:Person), (p2:Person) where p1 <> p2 and p1.someproperty = p2.someproperty merge(p1)-[r:Relationship]-(p2) return p1,r, p2

但如果我有大约200k的节点,这个脚本运行的时间很长。

有没有其他更快的方法来创建这种关系?

谢谢

I need to create relationships between all the nodes, which have the same property values.

For example I can use the following query:

match (p1:Person), (p2:Person) where p1 <> p2 and p1.someproperty = p2.someproperty merge(p1)-[r:Relationship]-(p2) return p1,r, p2

But if I have about 200k of nodes, this script running quite long.

Are there any other faster ways to create such relationships?

Thanks

最满意答案

您编写的查询首先在所有人员节点配对之间创建一个笛卡尔积,然后对每个配对进行过滤以找到实际相关的那些,然后创建关系。 这是非常昂贵的,n ^ 2操作。

相反,您可能只需要遍历所有Person节点,并找到具有该属性的相应人员节点,并创建关系。

此外,如果您对相关属性有索引或唯一约束,您应该会看到性能大大提高,否则它将是每个比较对该标签中所有节点的节点扫描,这是慢查询的另一个因素。

此外,我鼓励你尽可能不返回节点和关系,假设它在数千或数十万个结果附近。 这可能是另一个因素。

match (p1:Person) with p1 match (p2:Person) where p2.someproperty = p1.someproperty and p1 <> p2 merge(p1)-[r:Relationship]-(p2)

您应该能够解析此查询和旧查询,并查看它们将如何运行。

The query you wrote first creates a cartesian product between all pairings of person nodes, then does filtering on each pairing to find the ones that are actually related, then creates the relationship. That is very expensive, an n^2 operation.

Instead, you may want to go through all Person nodes just once, and find the corresponding person node with the property, and create the relationship.

Also, you should see greatly increased performance if you have either an index or unique constraint on the property in question, otherwise it will be a node scan over all nodes in that label with each comparison, another contributing factor to the slow query.

Also, I encourage you not to return the nodes and relationship if possible, assuming that it's in the neighborhood of thousands or hundreds of thousands of results. That's probably another factor.

match (p1:Person) with p1 match (p2:Person) where p2.someproperty = p1.someproperty and p1 <> p2 merge(p1)-[r:Relationship]-(p2)

You should be able to EXPLAIN both this query and your old one and see how they're both going to run.

更多推荐

本文发布于:2023-08-04 16:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   属性   关系   Creating   nodes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!