简体   繁体   中英

How to batch Neo4j Cypher queries

So i have over 130M nodes of one type and 500K nodes of another type, i am trying to create relationships between them as follows:

MATCH (p:person)
MATCH (f:food) WHERE f.name=p.likes
CREATE (p)-[l:likes]->(f)

The problem is there are 130M relationships created and i would like to do it in a similar fashion to PERIODIC COMMIT when using LOAD CSV

Is there such a functionality for my type of query?

Yes, there is. You'll need the APOC Procedures library installed ( download here ). You'll be using the apoc.periodic.commit() function in the Job Management section. From the documentation:

CALL apoc.periodic.commit(statement, params) - repeats a batch update statement until it returns 0, this procedure is blocking

You'll be using this in combination with the LIMIT clause, passing the limit value as the params.

However, for best results, you'll want to make sure your join data (f.name, I think) has an index or a unique constraint to massively cut down on the time.

Here's how you might use it (assuming from your example that a person only likes one food, and that we should only apply this to :persons that don't already have the relationship set):

CALL apoc.periodic.commit("
MATCH (p:person)
WHERE p.likes IS NOT NULL
AND NOT (p)-[:likes]->(:food)
WITH p LIMIT {limit}
MATCH (f:food) WHERE p.likes = f.name
CREATE (p)-[:likes]->(f)
RETURN count(*)
", {limit: 10000})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM