2016-02-26 38 views
1

我试图用C#在Neo4j上创建1,000,000(平凡)顶点。 (Community Edition 2.3.2)。性能非常差 - 完成创建节点需要900多秒。我做的事情效率低下吗?为什么需要这么长时间?Neo4j C#顶点创建性能

 var client = new GraphClient(new Uri("http://localhost:7474/db/data"), 
      "neo4j", "password"); 
     client.Connect(); 

     DateTime t = DateTime.Now; 
     Debug.WriteLine(t); 

     for (int j = 0; j < 100; j++) { 
      using (var scope = new TransactionScope(
       TransactionScopeOption.Required, 
       new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted} 
       )) 
      { 
       for (int i = 0; i < 10000; i++) { 
        var index = new Category { label = i.ToString() }; 
          client.Cypher 
           .Create("(class:testItem {index})") 
           .WithParam("index", index) 
           .ExecuteWithoutResults(); 
       } 
       scope.Complete(); 
      } 
      Debug.WriteLine(j); 
     } 

     Debug.WriteLine(DateTime.Now.Subtract(t).Seconds); 

public class Category 
{ 
    public String label { get; set; } 
} 

回答

1

在迈克尔的回答大厦 - 你会看代码将是:

var client = new GraphClient(new Uri("http://localhost:7474/db/data")); 
client.Connect(); 

var data = new List<Category>(); 
for (int i = 0; i < 20000; i++) 
    data.Add(new Category { Label = i.ToString()}); 

DateTime t = DateTime.Now; 
Debug.WriteLine(t); 

for (int j = 0; j < 50; j++) 
{ 
    client.Cypher 
     .Unwind(data, "item") 
     .Create("(:testItem { Label : item.Label })") 
     .ExecuteWithoutResults(); 

    Debug.Write($"{j},"); 
} 
Debug.WriteLine(); 
Debug.WriteLine($"Took {DateTime.Now.Subtract(t).Seconds} seconds"); 

这造成10万个节点〜21秒,当我试图用10000个节点重复100次,花费了24秒,所以随着批量大小的变化可能会有不同的表现,我一次性尝试了100万次 - 花了大约26秒。无论哪种方式 - 远远超过900多秒!

0

据克里斯斯卡登那些创建,请求不成批成几个HTTP请求,但他们每个人发送一个单独的请求(不知道他们共享所有一个Tx)。

对它们进行批处理是比较有效的,例如,把10-50k值成一个列表,并把它作为“数据”参数设置为暗号声明是这样的:

UNWIND {data} as value CREATE (:TestItem {index: value}); 

,或者如果你有多个属性创建为PARAM

UNWIND {data} as row CREATE (:TestItem {index: row.index, foo: row.foo, bar: row.bar}); 

词典列表对于合并操作,你可以这样做:

UNWIND {data} as row MERGE (t:TestItem {index: row.index}) ON CREATE SET t.foo=row.foo, t.bar = row.bar; 

甚至

UNWIND {data} as row MERGE (t:TestItem {index: row.index}) ON CREATE SET t += row; 

UNWIND {data} as row MERGE (t:TestItem {index: row.index}) ON CREATE SET t += row.props;