Skip to content

⚡ [performance improvement] Batch entity and relation queries in GraphStore#120

Open
wjohns989 wants to merge 1 commit into
mainfrom
perf/batch-graph-queries-9450529864398618501
Open

⚡ [performance improvement] Batch entity and relation queries in GraphStore#120
wjohns989 wants to merge 1 commit into
mainfrom
perf/batch-graph-queries-9450529864398618501

Conversation

@wjohns989
Copy link
Copy Markdown
Owner

💡 What: Added a new method add_entities_and_relations_batch to GraphStore and updated the call sites in muninn/core/memory.py to use it instead of looping through entities and relations.
🎯 Why: To prevent N+1 query inefficiencies during graph node and relation insertion, drastically speeding up the query execution time.
📊 Measured Improvement: Non-batched execution for 100 entities and 100 relations took ~2.14 seconds, whereas the batched execution takes ~0.06 seconds. This corresponds to an approximate 35x performance improvement.


PR created automatically by Jules for task 9450529864398618501 started by @wjohns989

Replaced individual graph store queries inside loops with a new batch query using UNWIND to avoid N+1 queries.

Co-authored-by: wjohns989 <56205870+wjohns989@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a batch insertion method, add_entities_and_relations_batch, in the graph store to optimize the addition of entities and relations during memory creation and updates. The review feedback suggests improving the robustness of this method by using MERGE instead of CREATE for mentions and relations to ensure idempotency and prevent duplicate edges. Additionally, it is recommended to catch specific exceptions rather than a generic Exception to align with production-grade standards and avoid masking potential errors.

Comment on lines +327 to +329
"MATCH (m:Memory {id: m_info.mid}), (e:Entity {id: m_info.eid}) "
"CREATE (m)-[:MENTIONS {role: m_info.role}]->(e)",
{"mentions": mentions_batch}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using CREATE for relationships in a batch operation can lead to duplicate edges if the same entity is extracted multiple times or if the operation is retried. Switching to MERGE ensures idempotency and maintains graph integrity.

Suggested change
"MATCH (m:Memory {id: m_info.mid}), (e:Entity {id: m_info.eid}) "
"CREATE (m)-[:MENTIONS {role: m_info.role}]->(e)",
{"mentions": mentions_batch}
"UNWIND $mentions AS m_info "
"MATCH (m:Memory {id: m_info.mid}), (e:Entity {id: m_info.eid}) "
"MERGE (m)-[:MENTIONS {role: m_info.role}]->(e)",

Comment on lines +336 to +338
"CREATE (a)-[:RELATES_TO {predicate: r_info.pred, confidence: r_info.conf, "
"source_memory: r_info.src, created_at: r_info.now}]->(b)",
{"relations": relations_batch}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the mentions batch, using CREATE for RELATES_TO can result in duplicate edges. Using MERGE is a more robust approach for production-grade knowledge graphs.

Suggested change
"CREATE (a)-[:RELATES_TO {predicate: r_info.pred, confidence: r_info.conf, "
"source_memory: r_info.src, created_at: r_info.now}]->(b)",
{"relations": relations_batch}
"MATCH (a:Entity {id: r_info.s_id}), (b:Entity {id: r_info.o_id}) "
"MERGE (a)-[:RELATES_TO {predicate: r_info.pred, confidence: r_info.conf, "
"source_memory: r_info.src, created_at: r_info.now}]->(b)",

{"relations": relations_batch}
)
return True
except Exception as e:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching a generic Exception is discouraged in production-grade code as it can mask unexpected errors. It is better to catch specific exceptions related to the database driver or handle known failure modes explicitly.

References
  1. Production-Grade Only: NEVER use placeholders, stubs, or 'samples' for core logic. All code must be production-ready, typed, and robust. (link)
  2. When handling exceptions, catch specific exceptions rather than a generic Exception to avoid masking errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant