Lucidnonsense
Lucidnonsense

Reputation: 1243

Neo4j: Slow match under unwind

I'm trying to read in a row of data (not from csv but passed as parameters) by unwinding and then merging. It seems that the match part of the query under the unwind is taking a really long time (whereas a simple create is more or less instant). I'm a bit confused because the the match should be fairly quick to run since it can index on the label first.

Here's a minimal version of my query (way more data will be input than just an id in real life):

WITH *, timestamp() as time0
WITH * UNWIND range(0, 9000) as unwound_data0
MERGE (node0:Node {id: unwound_data0}) ON CREATE SET node0.dbcreated = time0
WITH time0, collect(node0) as nodes0
RETURN time0

If I simplify it to

UNWIND range(0, 9000) as unwound_data0
MATCH (node0: Node)
RETURN node0

It takes just as long. But if I change match to create, then its very fast.

Any ideas on how to speed this up?

Upvotes: 1

Views: 465

Answers (1)

Justin Boylan-Toomey
Justin Boylan-Toomey

Reputation: 31

Thought I would provide a slightly more detailed answer for anyone landing here. To speed this up you may need to create an index for the id property you are attempting to match on.

You can create an index in Cypher with the below command:

CREATE INDEX index_name
FOR (a:LabelName)
ON (a.propertyName)

Set index_name, LabelName and propertyName to the values relevant to you.

If the property you are merging/matching on is unique then you can instead create a constraint in cypher with the below command:

CREATE CONSTRAINT constraint_name
ON (a:LabelName)
ASSERT a.id_property IS UNIQUE

Creating a constraint on the property of interest creates an index that will be used during lookup operations. It also ensures that a property is unique, throwing an error if you try to create a node with the same property value.

Upvotes: 1

Related Questions