cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Running Multi-statement Cypher Query Using Neo4j Python Drivers(Neo4j-driver, Py2neo)

OmShanti
Node Clone

Hi -

I am trying to find a way to run a multi-statement Cypher Query. These statements are separated from each other using semicolon(;), and i would like to execute them as single Cypher statement(in one go) in my application using Python Bolt drivers.

I have explored both Neo4j Standard Driver as well as Py2neo and understood that we can execute these statements one by one but i want to execute them in one go since executing them one by one will lead to performance issue fo the application. I explored extensively explored for the multi-statement Cypher aspect but couldn't find any concrete documentation material for this use-case using any of these 2 python drivers.

Here is the sample Cypher query containing 3 LOAD CSV statements that refer to Northwind database(Nodes: Customer, Order, and Relationship: Customers)

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///customer.csv' AS row FIELDTERMINATOR ','
MERGE (n:Customer {id: toInt(row.id)})
SET n.id = toInt(row.id),n.Address = toString(row.Address),n.Company = toString(row.Company),n.Job = toString(row.Job);

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///order.csv' AS row FIELDTERMINATOR ','
MERGE (n:Order {id: toInt(row.id)})
SET n.id = toInt(row.id),n.shipcity = toString(row.shipcity),n.orderdate = toString(row.orderdate),n.shipaddress = toString(row.shipaddress);

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///relationship_customers.csv' AS line FIELDTERMINATOR ','
MATCH (a:Order {id: toInt(line[0])})
MATCH (b:Customer {id: toInt(line[1])})
MERGE (a)-[r:CUSTOMERS]->(b);

Can you please share any pointers in this regard.

1 REPLY 1

Hi Om Shanti,

Multi statement is something for the Neo4j Browser or cypher shell.
They both fire the statements sequentially in separate calls to the database (session.run(query)).
Especially in the case of loadcsv you will run those statement sequentially. Sometimes to get the best performance I split one loadcsv statement up in multilple loadcsv statements.

What really helps performance is 'batching', which is not applicable for the loadcsv case.
In this case you can give a list of for instance Persons as a cypher parameter to one cypher statement instead of firing a cypher statement fir every Person.

Can you give me some more information about your use case?

regards

Kees