# Operation to generate a unique node + relationship from existing node information extremely slow

**URL:** <https://community.neo4j.com/t/operation-to-generate-a-unique-node-relationship-from-existing-node-information-extremely-slow/61196>\
**Category:** Cypher\
**Tags:** performance\
**Created:** [March 2, 2023, 8:56am UTC](https://community.neo4j.com/t/operation-to-generate-a-unique-node-relationship-from-existing-node-information-extremely-slow/61196 "2023-03-02T08:56:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbourjeily](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mbourjeily/32/27822_2.png) [@mbourjeily](https://community.neo4j.com/u/mbourjeily)\
**Post date:** [March 2, 2023, 8:56am UTC](https://community.neo4j.com/t/operation-to-generate-a-unique-node-relationship-from-existing-node-information-extremely-slow/61196/1 "2023-03-02T08:56:32Z")

</div>

Hi . I have a DB with around 4 million records. Max and Initial heap 5g, page cache 7g.  
I am trying to generate a parent/child relationship for nodes based on some of the information (fatherName, lastName, other specific ) criteria.  
If I run the following query , I added a condition on the lastName=' ABC' to limit results so it operate only on 700 records . It's very fast but it create duplicate nodes.

```auto
match(p:Person) where not (p)-[:CHILD_OF {parentRelationship:'FATHER_OF'}]-(:Person) and p.fatherName is not null and p.lastName='ABC 
merge (father:Person {firstName:p.fatherName,lastName:p.lastName,registryKadaa:p.registryKadaa,registryMouhafaza:p.registryMouhafaza,registryTown:p.registryTown,registryNumber:p.registryNumber}) on CREATE set father.constituency=p.constituency,father.name=p.fatherName+' '+p.lastName
 merge (p)-[:CHILD_OF {parentRelationship:'FATHER_OF'}]->(father)

```

Based on this article https:/[https://neo4j.com/developer/kb/understanding-how-merge-works/](https://neo4j.com/developer/kb/understanding-how-merge-works/)/neo4j.com/developer/kb/understanding-how-merge-works/  
since i am creating a new relationship and new node , I should the father node so it gets created, then merge the relationship so I am tried the following query but after several hour it's not finishing.

`CALL apoc.periodic.iterate("match(p:Person) where not (p)-[:CHILD_OF {parentRelationship:'FATHER_OF'}]-(:Person) and p.fatherName is not null and p.lastName='ABC' return p", "merge (father:Person {firstName:p.fatherName,lastName:p.lastName,registryKadaa:p.registryKadaa,registryMouhafaza:p.registryMouhafaza,registryTown:p.registryTown,registryNumber:p.registryNumber}) on CREATE set father.constituency=p.constituency,father.name=p.fatherName+' '+p.lastName merge (p)-[:CHILD_OF {parentRelationship:'FATHER_OF'}]->(father)",{batchSize:5000});`
