优化创建 [英] Optimize Create

查看:47
本文介绍了优化创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2万个苹果。

如何用比这更聪明的方式创建它们?

How do I create them in a smarter way than this?

        foreach (var a in apples)
        {
            graphClient.Cypher
                .Create("(a:Apple {newApple})")
                .WithParam("newApple", a)
                .ExecuteWithoutResults();
        }

寻找一种通用的方式来传递对象而无需指定每个属性。

Looking for a generic way to be able to pass objects without specifying each property.

class Fruit
{
    [JsonProperty(PropertyName = "Color")]
    public bool Color { get; set; }
}

class Apple : Fruit
{
    [JsonProperty(PropertyName = "Variety")]
    public String Variety { get; set; }
}


推荐答案

我想 apples 是词典列表。在这种情况下,在纯Cypher中进行更优化的查询将如下所示:

I suppose apples is a list of dictionaries. In that case, a more optimized query in plain Cypher would look like this:

UNWIND {apples} AS newApple
CREATE (a:Apple)
SET a = newApple

我还没有使用Neo4jClient .NET库,但遵循以下原则应该可以正常工作:

I have not yet used the Neo4jClient .NET lib, but something along these lines should work:

graphClient.Cypher
    .Unwind(apples, "newApple")
    .Create("(a:Apple)")
    .Set(...)
    .ExecuteWithoutResults();

2万个节点可能在单个事务中工作,但是值得进行一些批处理并使用大约。 1万个节点。

20k nodes might work in a single transaction, but it's worth to implement some batching and use batches of approx. 10k nodes.

更新。根据Chris Skardon的建议更新了实现。

Update. Updated the implementation per Chris Skardon's suggestion.

备注。在Cypher查询中,如果您使用的是Neo4j 3.2+,则应切换到新参数语法,它使用 $ param 样式参数,因此查询为稍微易于阅读:

Remark. In the Cypher query, if you are using Neo4j 3.2+, you should switch to the new parameter syntax, that uses $param style parameters, so the query is slightly easier to read:

UNWIND $apples AS newApple
CREATE (a:Apple)
SET a = newApple

这篇关于优化创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆