Neo4j.Driver.V1 的 Neo4j 关系写入性能
Neo4j relation write performance with Neo4j.Driver.V1
我正在评估 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);
}
}
不将标签与要匹配的索引 属性 结合使用会使查询性能极差。
最好的:
为您的节点添加标签。
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);
享受不同!
我正在评估 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);
}
}
不将标签与要匹配的索引 属性 结合使用会使查询性能极差。
最好的:
为您的节点添加标签。
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);
享受不同!