From 04ed709b3439cb40b77ddb14faff4b63b22ce76f Mon Sep 17 00:00:00 2001 From: yangdx Date: Thu, 6 Nov 2025 21:34:47 +0800 Subject: [PATCH] Optimize entity deletion by batching edge queries to avoid N+1 problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Add batch get_nodes_edges_batch call • Remove individual get_node_edges calls • Improve query performance --- lightrag/lightrag.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lightrag/lightrag.py b/lightrag/lightrag.py index af2067d8..9b0e5fe1 100644 --- a/lightrag/lightrag.py +++ b/lightrag/lightrag.py @@ -3333,15 +3333,16 @@ class LightRAG: # 7. Delete entities that have no remaining sources if entities_to_delete: try: + # Batch get all edges for entities to avoid N+1 query problem + nodes_edges_dict = await self.chunk_entity_relation_graph.get_nodes_edges_batch( + list(entities_to_delete) + ) + # Debug: Check and log all edges before deleting nodes edges_to_delete = set() edges_still_exist = 0 - for entity in entities_to_delete: - edges = ( - await self.chunk_entity_relation_graph.get_node_edges( - entity - ) - ) + + for entity, edges in nodes_edges_dict.items(): if edges: for src, tgt in edges: # Normalize edge representation (sorted for consistency) @@ -3364,6 +3365,7 @@ class LightRAG: f"Edge still exists: {src} <-- {tgt}" ) edges_still_exist += 1 + if edges_still_exist: logger.warning( f"⚠️ {edges_still_exist} entities still has edges before deletion"