Neo4j 2.0 Merge with unique constraints performanc

2019-02-28 10:43发布

问题:

Here's the situation: I have a node that has a property ContactId which is set as unique and indexed. The node label is :Contact (node:Contact {ContactId:1})

I have another node similar to that pattern for Address: (node2:Address {AddressId:1})

I now try to add a new node that (among other properties, includes ContactId (for referencing)) (node3:ContactAddress {AddressId:1,ContactId:1})

When I run a merge command for each, the time for adding a node that contains a property that is set as unique in another node type seems to make the process much slower.

The ContactAddress node only contains relational properties between the Contact and Address nodes. Contact and Address nodes contain up to 10 properties each. Is this a bug, where Neo4j is check the property key -> value -> then node label?

Code and screenshot below:

string strForEach = string.Format("(n in {{{0}}} |  
MERGE (c:{1} {{{2} : n.{2}}}) SET c = n)", propKey, label, PK_Field);

var query = client
            .Cypher
            .ForEach(strForEach)
            .WithParam(propKey, entities.ToList());

回答1:

Constraint checks are more expensive than just inserts. They also take a global lock on the constraint to prevent multiple insertion.

I saw you don't use parameters, but string substitiution, I really recommend to change that and go with parameters.

Also setting the whole node c to n triggers constraint check again.

Your probably want to use the ON CREATE SET clause of MERGE

(n in {nodes} |  
MERGE (c:Label {key : n.key}}) ON CREATE SET c.foo = n.foo, c.bar = n.bar )