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

旧城冷巷雨未停 提交于 2019-12-10 19:09:58

问题


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)


回答1:


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"]}


来源:https://stackoverflow.com/questions/21675747/is-it-better-to-make-one-merge-request-for-multiple-nodes-edges-creation-in-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!