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"