# CSV import issue

**URL:** <https://community.neo4j.com/t/csv-import-issue/62899>\
**Category:** Import / Export\
**Created:** [June 14, 2023, 2:38pm UTC](https://community.neo4j.com/t/csv-import-issue/62899 "2023-06-14T14:38:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 14, 2023, 2:38pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/1 "2023-06-14T14:38:34Z")

</div>

Hello,  
I am not able to import the data using CSV files.  
It is taking too much time to execute the query.  
There are 1 million nodes. I want to create relationship between them. I am executing the query on Neo4j browser but it is not completing within the time period.  
What can I do to execute it quickly?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 14, 2023, 4:52pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/2 "2023-06-14T16:52:07Z")

</div>

Can you include your query and sample of the data files?

Are you using a batch approach, such as apoc.periodic.commit/iterateor cypher call in transactions?

Do you have indexed on the properties used in your match/merge statements.

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 14, 2023, 5:15pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/3 "2023-06-14T17:15:22Z")

</div>

This is the query:  
LOAD CSV WITH HEADERS FROM "file:///file.csv" as row MATCH (from:A {Name: row.AlD}), (to:B {Name: row.BID})  
Create (from)-[:CONNECT {UniquelD: row.UniquelD})-\>(to)

Label A and label B is having 3 properties.  
I am not using apoc.

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [June 15, 2023, 10:20am UTC](https://community.neo4j.com/t/csv-import-issue/62899/4 "2023-06-15T10:20:07Z")

</div>

@anshulchaintha7

What version of Neo4j?  
Do you have indexes defined for :A(Name) and :B(Name)?  
Have you tried using periodic commit on the load csv similar to what is described at [LOAD CSV - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/load-csv/#load-csv-importing-large-amounts-of-data) ( note this is Neo4j v5 syntax and so if you are using v4.x or v3.x this is not applicable)

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 15, 2023, 11:19am UTC](https://community.neo4j.com/t/csv-import-issue/62899/5 "2023-06-15T11:19:12Z")

</div>

I have defined no indexes yet.  
A.csv has 3 properties B.csv has 3properties.

Community edition 4.33

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [June 15, 2023, 1:07pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/6 "2023-06-15T13:07:39Z")

</div>

@anshulchaintha7

Neo4j and like most all RDBMS will benefit from indexes especially for queries which are searching of which the `MATCH` statements you have defined are such.

Please create indexes for labels :A and :B and on property Name. [Indexes for search performance - Cypher Manual](https://neo4j.com/docs/cypher-manual/4.3/indexes-for-search-performance/)

also see [LOAD CSV - Cypher Manual](https://neo4j.com/docs/cypher-manual/4.3/clauses/load-csv/#load-csv-setting-the-rate-of-periodic-commits) so as to have `LOAD CSV` commit at every N rows

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 15, 2023, 6:26pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/7 "2023-06-15T18:26:49Z")

</div>

@dana_canzano  
Please suggest me the quicker query.  
I have already mentioned the LOAD CSV query in the thread.  
Take this query as reference and please suggest me.

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [June 15, 2023, 9:50pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/8 "2023-06-15T21:50:35Z")

</div>

@anshulchaintha7  
Per my last update indexes are described at [Indexes for search performance - Cypher Manual](https://neo4j.com/docs/cypher-manual/4.3/indexes-for-search-performance)

and specifically you should create an index as

CREATE INDEX AName FOR (n:A) ON (n.Name);  
CREATE INDEX BName FOR (n:B) ON (n.Name);

this is similar to what is described in the doc at [Indexes for search performance - Cypher Manual](https://neo4j.com/docs/cypher-manual/4.3/indexes-for-search-performance/?#administration-indexes-create-a-single-property-index-for-nodes)

And as to why an index is suggested and again this is true for Neo4j as well as any RDBMS. Without an index we will scan all nodes with a label named :A and see which node has a property Name = row.AID. If you have 100k :A nodes then for each row in the CSV we will examine 100k nodes. If your csv has 100 rows of data then its 100 x 100k searches. Now if we have in index for each row in the CSV we use the index to find the node which has Name=row.AID which will be much faster.

As to changing `LOAD CSV` this too is described in the doc and per my prior update and as such [LOAD CSV - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/load-csv/?#load-csv-setting-the-rate-of-call-in-transactions) and so try

```auto
LOAD CSV WITH HEADERS FROM "file:///file.csv" as row 
call {
            with row
            MATCH (from:A {Name: row.AlD}), (to:B {Name: row.BID})
            Create (from)-[:CONNECT {UniquelD: row.UniquelD})->(to)
        } IN TRANSACTIONS OF 100 ROWS
;

```

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 17, 2023, 12:06pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/9 "2023-06-17T12:06:02Z")

</div>

It's taking too much time.  
Is there a way I can execute the query in background and wait for its completion

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 17, 2023, 1:13pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/10 "2023-06-17T13:13:48Z")

</div>

@dana_canzano has provided two important changes to improve your performance, indexing and batching. Your query is very basic. Those two changes should really help. Did you try them without success?

You can use the cypher-shell CLI tool to execute the query.

Is this a new database that you loading with this csv file? If so, you can try the neo4j admin import

> **[Neo4j-admin import - Operations Manual](https://neo4j.com/docs/operations-manual/current/tutorial/neo4j-admin-import/)**
>
> This tutorial provides detailed examples to illustrate the capabilities of importing data from CSV files with the command \`neo4j-admin database import\`.

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 17, 2023, 3:03pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/11 "2023-06-17T15:03:14Z")

</div>

I am getting an error mentioned below:  
Invalid input 'IN': expected  
"CALL"  
"CREATE"  
"DELETE"  
"DETACH"  
"FOREACH"  
"LOAD"  
"MATCH"  
"MERGE"  
"OPTIONAL"  
"REMOVE"  
"RETURN"  
"SET"  
"UNION"  
"UNWIND"  
"USE"  
"WITH"  
 (line 7, column 12 (offset: 260))  
" } IN TRANSACTIONS OF 100 ROWS"

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 17, 2023, 3:47pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/12 "2023-06-17T15:47:45Z")

</div>

Can you paste the entire query?

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 17, 2023, 4:00pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/13 "2023-06-17T16:00:24Z")

</div>

LOAD CSV WITH HEADERS FROM "file:///SManageV.csv" as row  
call {  
with row  
MATCH (from:S {Name: row.SlD}), (to:V {Name: row.VID})  
Create (from)-[:Managed\_By {UniquelD: row.UniquelD})-\>(to)  
} IN TRANSACTIONS OF 100 ROWS  
;

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 17, 2023, 4:11pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/14 "2023-06-17T16:11:11Z")

</div>

You terminated your relationship with a ')' instead of a ']'.

```auto
LOAD CSV WITH HEADERS FROM "file:///SManageV.csv" as row
call {
with row
MATCH (from:S {Name: row.SlD}), (to:V {Name: row.VID})
Create (from)-[:Managed_By {UniquelD: row.UniquelD}]->(to)
} IN TRANSACTIONS OF 100 ROWS

```

---

<div class="post-metadata">

**Author:** ![dmcanzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dmcanzano/32/28927_2.png) [@dmcanzano](https://community.neo4j.com/u/dmcanzano)\
**Post date:** [June 18, 2023, 12:03am UTC](https://community.neo4j.com/t/csv-import-issue/62899/15 "2023-06-18T00:03:27Z")

</div>

@anshulchaintha7

also my response earlier suggested to create an index on :A and :B labeled nodes. however your last update includes a query which uses nodes with label :S and :V

do you have indexes on these labels as well ??

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 18, 2023, 1:57am UTC](https://community.neo4j.com/t/csv-import-issue/62899/16 "2023-06-18T01:57:46Z")

</div>

Yes @dmcanzano @glilienfield  
I have created indexes for both the labels and used the query  
:auto LOAD CSV WITH HEADERS FROM "file:///ServerManageVM.csv" as row

CALL {

WITH row

MATCH (From: Server {Name: row.ServerID}), (to: VM {Name: row.VMID}) Create (from)-[:MANAGED BY {Unique ID: row. Unique ID}]-\>(to) } IN TRANSACTIONS OF 100 ROWS

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 18, 2023, 2:36am UTC](https://community.neo4j.com/t/csv-import-issue/62899/17 "2023-06-18T02:36:29Z")

</div>

Are you still having issues?

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 18, 2023, 2:49am UTC](https://community.neo4j.com/t/csv-import-issue/62899/18 "2023-06-18T02:49:24Z")

</div>

Yes  
Same performance  
And let me try to use Neo4j admin tool  
I want to run query in background without internet connection  
Will it work?

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [June 18, 2023, 1:07pm UTC](https://community.neo4j.com/t/csv-import-issue/62899/19 "2023-06-18T13:07:45Z")

</div>

@anshulchaintha7

> > Same performance

details? does it take 10s, 120s , 5 minutes?  
how many rows in the csv?

> [@anshulchaintha7](#):
>
> erverID}), (to: VM {Name: row.VMID}) Create (from)-

your query of

```auto
:auto LOAD CSV WITH HEADERS FROM "file:///ServerManageVM.csv" as row

CALL {

WITH row

MATCH (From: Server {Name: row.ServerID}), (to: VM {Name: row.VMID}) Create (from)-[:MANAGED BY {Unique ID: row. Unique ID}]->(to) } IN TRANSACTIONS OF 100 ROWS

```

will not run as-is as `MANAGED BY` as well as `Unique ID` contain spaces and property names can not contain spaces.

Also this is once again changing. Initially it was :A and :B labels, then :S and :V labels and now :Server and :VM?

Can you run and return the results of

```auto
:auto profile LOAD CSV WITH HEADERS FROM 'http://www.neo4j.com/ServerManageVM.csv' as row

CALL {

WITH row

MATCH (From: Server {Name: row.ServerID}), (to: VM {Name: row.VMID}) Create (from)-[:MANAGED_BY {Unique_ID: row. Unique_ID}]->(to) } IN TRANSACTIONS OF 100 ROWS

```

which will provide a query plan as to how the query is processed. Are the indexes being utilized?

---

<div class="post-metadata">

**Author:** ![anshulchaintha7](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anshulchaintha7/32/27878_2.png) [@anshulchaintha7](https://community.neo4j.com/u/anshulchaintha7)\
**Post date:** [June 19, 2023, 2:42am UTC](https://community.neo4j.com/t/csv-import-issue/62899/20 "2023-06-19T02:42:34Z")

</div>

30 minutes  
16666 rows  
Getting same error: Invalid input 'IN': expected  
yes  
FYI I am using community edition 4.3.3

[Next page](https://community.neo4j.com/t/csv-import-issue/62899.md?page=2)
