With parallel execution, you need a unique constraint on :PERSON(name), as unique constraints have schema locks that prevent duplicate creation in this situation.
If names aren't unique, then either disable parallel import or clean your data so there are no duplicates.
You could also lock on a singular node before the MERGE to guarantee mutual exclusion, but that has the same affect as disabling parallel execution, so toggling parallel to false is the better option.
EDIT
So the problem you're running into here is that your MERGE isn't backed by a unique constraint. In order to get this, both the property and the label for the constraint must be present in the pattern, it won't work if you only have them in the ON CREATE or ON MATCH sections.
So if your constraint is on :PERSON(pid)
, then your pattern should be:
MERGE (l:PERSON {pid:payload.id})
and then you can set the name in the ON CREATE section.
If names are supposed to be unique, then you can get the same effect by creating a unique constraint on :PERSON(name)
While you can use EXPLAIN on the query to get the query plan, since this is a query string, you will need to do some cut and pasting to see the plan for the query in question, as well as make a small addition at the top so the query can compile. You can try this:
EXPLAIN
WITH $payload as payload
MERGE (l:PERSON {name :payload.name})
ON CREATE SET
l.pid = payload.id,
l.createdDate= timestamp(),
l.lastModifiedDate= timestamp()
ON MATCH SET
l.lastModifiedDate= timestamp()
You want to see a NodeUniqueIndexSeek(Locking)
operator in the plan in order for the MERGE to work correctly and use correct locking to prevent duplicates.
If you see NodeIndexSeek
then only an index is being used, it won't prevent duplicates as there's nothing to lock on to guarantee mutual exclusion. You need that unique constraint.
And if NodeByLabelScan
is there instead, that's even worse, as you don't have a unique constraint or an index backing your MERGE. If execution time was a problem for you earlier, it's probably because it was doing a label scan, which won't perform well at all for loading.