Most efficient way to Clone a Branch from a top level node

(Johncrockeratwork) #1


We have a requirement to clone a tree from a given root node into exactly the same tree structure from a newly created Root node. The cloned tree only has to traverse down to level "n" and only traverse certain relationships.

Does anyone have any suggestions for the most efficient way of doing this? Our initial experiments have taken somewhere in the region of "minutes" and have put a medium-large load on the server.

(Tim Hanssen) #2


Can you share your query are you are running it now? And tell me a bit more about the scope, how many nodes on avg ect.

(Andrew Bowman) #3

I answered a similar question on Stack Overflow recently, are you the same one who asked that question?

If not, then my answer may be helpful for you.

If so, then it would help to know what's going on in the query, and I'd request you PROFILE your query and add the query plan (after expanding all elements of the plan) to your question.

(Michael Hunger) #4

There is also a apoc procedure for that:


(Andrew Bowman) #5

I think this gets messy, as the cloned relationships attach to all outside nodes as well as to the original nodes, leaving an exercise for the caller on how to delete all the relationships between the clones and the originals.

I've added an APOC feature request for a new procedure apoc.refactor.cloneSubgraph() that is aimed at this particular use case: cloning nodes and the relationships between them, only creating relationships between the clones and not to any of the originals, and allowing a means to define a replacement or standin for nodes when it comes to relationship creation. For this example, it could be used to clone a branch and attach the branch to a different root node instead of the old root (or instead of cloning the old root).

(Andrew Bowman) #6

As an update, I have a PR open with a new APOC procedure for doing exactly this: