# Less efficiency while performing create relation with neo4j from python

**URL:** https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487
**Category:** Operations
**Tags:** relationship
**Created:** [October 26, 2021, 5:38pm UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487 "2021-10-26T17:38:25Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 26, 2021, 5:38pm UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/1 "2021-10-26T17:38:25Z")

</div>

Hi , I am trying to make relations among nodes using python (neo4j lib). The main job that is creating the nodes for the data is done but making relationships among them is really taking time than I expected (it is almost 12hrs for exact 213867 relations).  
The main concern is that no process is using my CPU more than 20% , especially Zulu platform ,which does the work i.e creating the relations . Is there a way to speedup this process or to increase my cpu usage?

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 26, 2021, 10:23pm UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/2 "2021-10-26T22:23:04Z")

</div>

Hi @grpnpraveen !

Can you share your model and the queries you are using in order to do the import?

I do assume you already created some indexes in order to speed up the _MATCH_ process.

Bennu

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 3:35am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/3 "2021-10-27T03:35:14Z")

</div>

@Bennu how can I share the model with you here ? by the way its 213867 relations, it just completed.  
yes, I put some ids to match.  
thanks in advance

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 7:16am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/4 "2021-10-27T07:16:01Z")

</div>

Hi @grpnpraveen!

You can always do it yourself on [Arrow](https://arrows.app/#/local/id=qxkYrm1vFACirDLs_QZi) website.

Maybe you can share the queries with a EXPLAIN so we can check if the indexes are working properly on your query.

Bennu

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 7:42am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/5 "2021-10-27T07:42:29Z")

</div>

@Bennu thanks for the reply.  
I don't think that the nodes are that complex. First, I try here to explain about what I am facing with.  
I read a csv first where it has ids of cast for each movie in a row ie "0991993|11688537|1190847" , which are ids of the cast that need to be connected.  
This is the code.  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/b/f/bf891b751ddc9ce18388c12a43ef8a0b2e1d98d2.png)

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/e/4e69d520f08b511bbcb9de96ae78a9cda357ceb6.png)

pls do ask if any 🤗

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 7:59am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/6 "2021-10-27T07:59:17Z")

</div>

Hi @grpnpraveen !

Can you share the result pipeline of this query?

```auto
EXPLAIN MATCH(p:actor),(m:movie)
WHERE p.id = 0991993 AND m.id = setMovieIdHere
return *

```

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 8:17am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/7 "2021-10-27T08:17:27Z")

</div>

Sure @Bennu !  
I used this , here instead of 0958345 , I put 958345

```auto
WHERE p.id = 958345 AND m.id = "tt0037711"
return *

```

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/c/cc158947d8c50ea8844ac66aa4e7cea257f5a520.png)

I have created relation of actor for 5000 movies only! It took 12 hrs +. I still need to add for 85000 movies.

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 8:25am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/8 "2021-10-27T08:25:15Z")

</div>

Hi @grpnpraveen !

Just as I tho. You have no Indexes! If you feel confident enough on us, try this:

```auto
CREATE INDEX ID_ACTOR for (n:actor) on (n.id);
CREATE INDEX ID_MOVIE for (n:movie) on (n.id);

```

Then change your query to:

```auto
MATCH(p:actor)
WHERE p.id = $actor_id
with p
MATCH (m:movie) 
WHERE m.id = $movie_id
WITH p,m
CREATE (p)-[r:acted-in]->(m)
RETURN type(r)

```

Bennu

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 8:28am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/9 "2021-10-27T08:28:58Z")

</div>

@Bennu right now I am creating relations for directors and writers which may take some time.  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/a/ca27aea357c4e9181323fc7510abfcba700cdd0a.png)  
I am confident enough to add those . I will, after completion of these directors and writers.  
If you don't mind Can you explain what is this INDEX is?

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 8:31am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/10 "2021-10-27T08:31:13Z")

</div>

Hi!

You should add indexes on your _directors_ and _writers_ as well.

Index in general:

[Database index - Wikipedia](https://en.wikipedia.org/wiki/Database_index#:~:text=Indexes%20are%20used%20to%20quickly,efficient%20access%20of%20ordered%20records).

Index in particular:

> **[Indexes for search performance - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/indexes-for-search-performance/)**
>
> This section explains how to manage indexes used for search performance.

Bennu

PS: With this you easily go from 12h to 12 minutues or much less.

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 8:33am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/11 "2021-10-27T08:33:23Z")

</div>

Can I add this index even now ? Cz I have already added all the nodes .

Also,

```auto
CREATE INDEX ID_ACTOR for (n:actor) on (n.id);
CREATE INDEX ID_MOVIE for (n:movie) on (n.id);

```

what is ID\_ACTOR,ID\_MOVIE here?

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 8:34am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/12 "2021-10-27T08:34:53Z")

</div>

Sure. But I strongly suggest you to retry the whole process with the indexes Online in order to verify everything has being said.

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 8:38am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/13 "2021-10-27T08:38:43Z")

</div>

okay ! thanks a lot .

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 8:40am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/14 "2021-10-27T08:40:16Z")

</div>

> [@grpnpraveen](#):
>
> what is ID\_ACTOR,ID\_MOVIE here?

The name of the index.

Spend some time reading the articles shared. It may help a lot.

---

<div class="post-metadata">

### Author: ![grpnpraveen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/grpnpraveen/32/20621_2.png) [@grpnpraveen](https://community.neo4j.com/u/grpnpraveen)
#### Post date: [October 27, 2021, 10:09am UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/15 "2021-10-27T10:09:28Z")

</div>

@Bennu OMG!! THANKS for millennium!  
It just completed in 10 min for 5000 movies!😻

---

<div class="post-metadata">

### Author: ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)
#### Post date: [October 27, 2021, 12:22pm UTC](https://community.neo4j.com/t/less-efficiency-while-performing-create-relation-with-neo4j-from-python/46487/16 "2021-10-27T12:22:39Z")

</div>

Told ya 😉

It's all about indexing
