Is it better to make a single MERGE request to create multiple nodes / edges in Neo4J Cypher 2.0 or split it into transactions?

I have a long Cypher request (new version of Neo4J 2.0) that creates several nodes and connections using the MERGE command.

Question: do you think that I should divide it into different parts and transfer it as a transaction (for reliability), or should I keep a long single (for speed)?

Here's the request:

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, for example, 50 tags (nodes) and even more edges)

+3
source share
1 answer

As long as your request is not a hundred lines, and the data you created does not exceed 50 thousand elements, I would stick to one request.

.

foreach

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

Params:

{userName:"User", tags: ["tag1",...,"tagN"]}
+7

All Articles