[英]Slow CREATE times using Neo4jClient
我是Neo4j和Neo4jClient的新手。
我正在尝试获得一些简单操作的基准。 我似乎得到了非常慢的插入(创建)。 我知道这不是进行批量插入的最快方法,但是它更能反映我的应用在现实生活中的行为。
目标 :添加一个Person对象并将其链接到“超级用户/人员”
设置 :
结果 :大约需要7秒才能添加100个用户和关系。 这是秒表的一些计时数据:
增加了200人。 经过的时间= 00:00:35.5526979
增加了300人。 经过的时间= 00:00:43.7953471
增加了400人。 经过的时间= 00:00:51.0138293
增加了400人。 经过的时间= 00:00:51.0139585
增加了500人。 经过的时间= 00:00:56.0563539
增加了700人。 经过的时间= 00:01:03.5407927
增加了800人。 经过的时间= 00:01:08.0186756
增加了900人。 经过的时间= 00:01:13.0089620
增加了900人。 经过的时间= 00:01:13.0089809
代码 :这看起来非常简单。 我只有一个循环,可以创建一个新人,然后创建关系:
//Create SuperNode
int supermyId = 4000;// Guid.NewGuid().GetHashCode();
Person superPerson1 = new Person { name = "SUPER Name5 99900", born = 1903, myId = supermyId };
int regularId = 0;
Person regularPerson = null;
graphClient.Cypher
.Create("(person:Person {newUser})")
.WithParam("newUser", superPerson1)
.ExecuteWithoutResults();
sw.Start();
Parallel.For( 0, 5000,
index => {
regularId ++;
regularPerson = new Person() { name = "regular5 " + regularId, born = 1000, myId = regularId };
graphClient.Cypher
.Create("(person:Person {regularPerson})")
.WithParam("regularPerson", regularPerson)
.ExecuteWithoutResults();
graphClient.Cypher
.Match("(superUser:Person)", "(regularUser:Person)")
.Where((Person superUser) => superUser.myId == supermyId)
.AndWhere((Person regularUser) => regularUser.myId == regularId)
.Create("regularUser-[:SUPER5]->superUser")
.ExecuteWithoutResults();
if (regularId % 100 == 0)
{
Console.WriteLine("Added " + regularId + " Persons. Elapsed time = " + sw.Elapsed);
}
});
问题 :
因此,这regularPerson
您创建节点的方式,最好的方法是首先创建一个regularPerson
节点:
var regularPersons = new List<Person>();
for(int i = 0; i < 5000; i++)
{
//Generate users here
}
然后使用Unwind
关键字将其传递给Neo4j
。 这使您可以传递整个集合。
现在,您要做的另一件事是为每个添加项运行两个查询-一个添加人员,两个添加到superUser
,我们可以将其压缩为1个查询,这将运行得更快。
因此,(并且我没有在计算机上键入此命令,因此可以原谅错误),在一个查询中像您一样创建您的superUser
,然后:
graphClient.Cypher
.Match("(su:Person)")
.Where((Person su) => su.myId == supermyId)
.Unwind(regularPersons, "rp")
.Create("(p:Person {rp})-[:SUPER5]->(su)")
.ExecuteWithoutResults();
就像我说的那样,我目前无法测试,但是请稍作尝试,然后尝试一下它的工作原理,通常情况下,您应该看到此生成速度比您实际生成的快了5000倍。
这是我的工作代码,在1.3秒内添加了5000个节点:
void Main()
{
var graphClient = new GraphClient(new Uri("http://localhost.:7474/db/data/"));
graphClient.Connect();
var supermyId = 4000;
Person superPerson1 = new Person { name = "SUPER Name5 99900", born = 1903, myId = supermyId };
graphClient.Cypher
.Create("(person:Person {newUser})")
.WithParam("newUser", superPerson1)
.ExecuteWithoutResults();
var regularPersons = new List<Person>();
for (int i = 0; i < 5000; i++)
{
regularPersons.Add(new Person { name = $"Person_{i}", born = 1900+(i%100), myId = i + 4000 });
}
var now = DateTime.Now;
graphClient.Cypher
.Match("(su:Person)")
.Where((Person su) => su.myId == supermyId)
.Unwind(regularPersons, "rp")
.Create("(p:Person {born: rp.born, myId: rp.myId, name:rp.name})-[:SUPER5]->(su)")
.ExecuteWithoutResults();
Console.WriteLine($"Took {(DateTime.Now - now)} to add");
}
public class Person
{
public int myId { get; set;}
public int born { get;set; }
public string name { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.