【发布时间】: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