Is it better to make one MERGE request for multiple nodes / edges creation in Neo4J Cypher 2.0 or to split it into transactions?

StackOverflow https://stackoverflow.com/questions/21675747

  •  09-10-2022
  •  | 
  •  

Question

I have a long Cypher query (the new Neo4J 2.0 version), which creates multiple nodes and connections using the MERGE command.

The question is: do you think I'm better off splitting it into different parts and submitting it as a transaction (for robustness) or should I keep the long single one (for speed)?

Here's the query:

MATCH (u:User {name: "User"}) MERGE (tag1:Hashtag {name:"tag1"}) MERGE (tag2:Hashtag    
{name:"tag2"}) MERGE (tag3:Hashtag {name:"tag3"}) MERGE (tag4:Hashtag {name:"tag4"}) 
MERGE tag1-[:BY]->u MERGE tag2-[:BY]->u MERGE tag3-[:BY]->u MERGE tag4-[:BY]->u;

(I purposefully made the request shorter, imagine that there are like 50 tags (nodes) and even more edges for example)

Était-ce utile?

La solution

As long as your query statement is not hundreds of lines and your data created doesn't exceed 50k elements, I'd stick with one query.

But you should use parameters instead.

I would also rewrite your query with foreach and parameters

MATCH (u:User {name: {userName}) 
FOREACH (tagName in {tags} | 
    MERGE (tag:Hashtag {name:tagName}) 
    MERGE (tag)-[:BY]->(u)
)

params:

{userName:"User", tags: ["tag1",...,"tagN"]}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top