cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Why isn't it faster for nodes that have been created?

lingvisa
Graph Fellow

I have been used:

MERGE (Node{id:nodeID}) ON MATCH SET ...

to populate an empty graph, where nodeID has been created as one index. I have two questions:

  1. I read the difference between MERGE ... ON MATCH SET and MERGE ... ON CREATE SET. It seems to me to build graph from scratch, I should use ON CREATE, but NO MATCH. But I don't understand why MERGE ... ON MATCH SET ... also successfully created the graph.

  2. After the graph is built and I run the same code to rebuild it just for testing, and it takes the same amount of time to finish. Shouldn't it be much faster since all the nodes have already been created in the first pass?

1 ACCEPTED SOLUTION

Hi @lingvisa

1
This is my test code.

MERGE (n:Sample {id: 1})
  ON CREATE SET n.create_datetime = datetime()
  ON MATCH SET n.update_datetime = datetime();

The first time, 'ON CREATE SET' was executed because there were no nodes.

{
  "id":1,
  "create_datetime":"2020-11-06T20:07:28.768000000Z"
}  

The second time, 'ON MATCH SET' was executed.
You can see the 'update_datetime' property.

{
  "id":1,
  "update_datetime":"2020-11-06T20:09:44.215000000Z",
  "create_datetime":"2020-11-06T20:07:28.768000000Z"
}  

2
MERGE is slower than CREATE.
You can see this when you create a lot of data.
But otherwise, I don't think there is much difference.

if the id:nodeID property you're searching for doesn't have an index, it will take longer to find it the first time.

View solution in original post

3 REPLIES 3

Hi @lingvisa

1
This is my test code.

MERGE (n:Sample {id: 1})
  ON CREATE SET n.create_datetime = datetime()
  ON MATCH SET n.update_datetime = datetime();

The first time, 'ON CREATE SET' was executed because there were no nodes.

{
  "id":1,
  "create_datetime":"2020-11-06T20:07:28.768000000Z"
}  

The second time, 'ON MATCH SET' was executed.
You can see the 'update_datetime' property.

{
  "id":1,
  "update_datetime":"2020-11-06T20:09:44.215000000Z",
  "create_datetime":"2020-11-06T20:07:28.768000000Z"
}  

2
MERGE is slower than CREATE.
You can see this when you create a lot of data.
But otherwise, I don't think there is much difference.

if the id:nodeID property you're searching for doesn't have an index, it will take longer to find it the first time.

@koji That's a good test case to illustrate the difference of the two. Still if I only execute:

MERGE (n:Sample {id: 1})
  ON MATCH SET n.update_datetime = datetime();

The documentation says that "Merging nodes and setting properties on found nodes.". When the node '1' is not in the graph, it can't find it, so it should do nothing. That was my mistaken thought. Now I know that 'MERGE' itself also creates new nodes if not found. If not found, it will ignore the SET part, which is different from MERGE ON CREATE SET.

Hi @lingvisa

I think your understanding is correct.
The following code has the same meaning as the following.

MERGE (No Node):

CREATE (s:Sample {id: 1})
SET s.create_datetime = datetime();

MERGE (Node exists):

MATCH (n:Sample {id: 1})
SET n.update_datetime = datetime();