【问题标题】:Neo4j relation write performance with Neo4j.Driver.V1Neo4j 关系写入性能与 Neo4j.Driver.V1
【发布时间】:2017-07-15 01:02:57
【问题描述】:

我正在评估将 Neo4j 用作交互式应用程序中的数据存储。使用下面的代码,我大约需要 40 毫秒来添加一个关系,这对于我们的需求来说太慢了,因为我们的模型可以有数万个关系。 这是典型的表现吗?有什么改进代码的技巧吗?我测试了 12 种关系类型,总共 6652 个关系。

using (var session = driver.Session())
{
    foreach (var relationType in relationTypes)
    {
        var nodeArray = relationType.Value.Select(n => new Dictionary<string, string> {{"from", n.Item1}, {"to", n.Item2}}).ToArray();
        var dictionary = new Dictionary<string, object> {{"nodes", nodeArray}};
        var relationCommand =
            string.Format(
                "UNWIND $nodes as node WITH node.from as from, node.to as to "
              + "MATCH (f {{nodeId:from}}), (t {{nodeId:to}}) "
              + "CREATE (f)-[:" + relationType.Key + "]->(t) ");
        session.Run(relationCommand, dictionary);
    }
}

【问题讨论】:

  • 您不使用任何标签,因此没有唯一的约束/索引。您的查询根本没有优化
  • 它肯定是未优化的。 :) 好的,所以需要标签来提高 MATCH 的效率?
  • 标签 + 您匹配的属性的索引/唯一约束(此处为 nodeId),因此查找为 O(1) + 1 :标签 + 属性
  • 很好的建议,将每个关系的时间缩短到 1.5 毫秒左右。随意写出来作为答案,我会接受的。
  • 详细添加了答案

标签: c# neo4j cypher database-performance


【解决方案1】:

不将标签与要匹配的索引属性结合使用会使查询的性能极差。

最好的:

为您的节点添加标签。

CREATE (n:Label {id: 1}) 例如在创建节点时,或者如果您想为已创建的节点添加通用标签,您可以这样做

MATCH (n) SET n:Label

然后为你的 nodeId 属性创建一个唯一的约束:

CREATE CONSTRAINT ON (n:Label) ASSERT n.nodeId IS UNIQUE

然后在您的查询中使用它:

var relationCommand =
            string.Format(
                "UNWIND $nodes as node WITH node.from as from, node.to as to "
              + "MATCH (f:Label {{nodeId:from}}), (t:Label {{nodeId:to}}) "
              + "CREATE (f)-[:" + relationType.Key + "]->(t) ");
        session.Run(relationCommand, dictionary);

享受差异!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多