Optimizing Large Data Deletions in Neo4j
- Compartir esta discusión
- Copiar enlace
- Compartir en X (Twitter)
Resumen
This Neo4j community discussion addresses common challenges when performing large-scale data deletions in graph databases. The thread explores why standard DETACH DELETE queries can hit memory limits or cause high CPU usage due to complex relationship loops. It provides a practical look at troubleshooting performance bottlenecks and finding efficient strategies for removing customer data hierarchies in Neo4j Aura instances.
Hi! I'm currently developing functionality to delete all customer data from my database once a customer leaves, but even with a very small example (about 1300 nodes and 4500 edges, when a potential large customer could have a thousands times that or more) the database query seems to struggle.
My instance is in aura and configured to the minimum CPU/Memory configuration as the project is still in development
The database is organized in a way that all customer data is reachable by paths from a node of label Installation, identified by an id, so i attempted 2 approaches to delete the data.
MATCH to_delete=(:Installation {id: "104069967"})-[*]->() DETACH DELETE to_delete
That one hits a memory limit in the database (calling it by http indicates the following:
The allocation of an extra 2.0 MiB would use more than the limit 278.0 MiB. Currently using 278.0 MiB. dbms.memory.transaction.total.max threshold reached
If i prepend the query with EXPLAIN it shows that it expects 220,744,638,748,247 results, i assume from relations forming loops, although at least in the web console changing DETACH DELETE for RETURN makes the query work (tho not deleting the nodes ofc)
My second approach seeing this was this:
MATCH (i:Installation {id: "104069967"})
MATCH (x)
WHERE EXISTS {
(i)-[*]->(x)
}
CALL(x) {
DETACH DELETE x
} IN TRANSACTIONS
WITH i
DETACH DELETE i
Which doesn't error out from memory but drives the CPU consumption in the database to 100% and takes more than an hour (i haven't actually seen it finish).
Is there a better way of achieving this? I couldn't find anything in the documentation that suggest this approach is wrong but i can't imagine such a tiny amount of nodes hitting the database limits like this is normal behavior.
I am sorry I am just seeing this. Has this been resolved?
No xd, mostly been working in other parts of the code so far
Thanks - should I still pose the question
wdym?
I was not sure if I should ask some of our experts internally about this if you still needed assisatnce.
oh i see i see, yes i could still use the help thanks :D
MATCH to_delete=(:Installation {id: "104069967"})-[*]->() DETACH DELETE to_delete
Why do you match each outgoing relationships from the node you want to DETACH DELETE? (:Installation {id: "104069967"})-[*]->() matches ALL paths of any length (if you graph is a single connected component, this matches your WHOLE database). Then for each of those paths, you go and delete the start node (which is the same node).
MATCH to_delete=(:Installation {id: "104069967"}) DETACH DELETE to_delete
should do the trick. Check with explain.
You could try to not call detach delete for every path in the customer's subgraph. Because with the current query, you're going to hit things multiple time. Assume you have a graph (n1)-[]->(n2)-...->(n100) in there. You'll match all (sub)-paths in there starting with n1. I.e., (n1)-[]->(n2), (n1)-[]->(n2)-[]->(n3), ..., (n1)-[]-...->(n100). Where the last one would suffice.
Try comparing MATCH p=(:Installation {id: "104069967"})-[*]->() RETURN count(p) with MATCH (:Installation {id: "104069967"})-[*]->(t) WITH DISTINCT t RETURN count(t)
Me and Rouven sat next to each other and worked out this query which runs a whole lot quicker in a test db:
MATCH (i:Installation {id: "104069967"})
MATCH (i)-[*]-(x)
WITH DISTINCT x, i
CALL (x) {
DETACH DELETE x
} IN TRANSACTIONS
WITH DISTINCT i
DETACH DELETE i
This matches the origin node i, finds all nodes x connected to it, then calls detach delete on all those nodes, then detach deletes the origin node.
The use of distinct twice here may be overkill, but on the movies example this executes very swiftly!
Thanks guys! I'll try it out :)
Hi I'm curious, how did you get along with the issue?
right i forgot to report here sorry xd It worked great thx :D
Love to hear it 👏