Skip to content

perf: skip redundant index creation in Neo4jDriver when indices already exist#1288

Open
carlos-alm wants to merge 1 commit intogetzep:mainfrom
carlos-alm:perf/skip-redundant-index-creation
Open

perf: skip redundant index creation in Neo4jDriver when indices already exist#1288
carlos-alm wants to merge 1 commit intogetzep:mainfrom
carlos-alm:perf/skip-redundant-index-creation

Conversation

@carlos-alm
Copy link

@carlos-alm carlos-alm commented Mar 2, 2026

Summary

  • Add a fast path to Neo4jDriver.build_indices_and_constraints(): query SHOW INDEXES YIELD name (1 roundtrip) and skip creation if all 31 expected indices already exist
  • Add an instance-level _indices_verified flag so repeated calls within the same process are free
  • Extract get_neo4j_expected_index_names() from existing CREATE queries (regex over single source of truth — no separate manifest)
  • Graceful fallback: if SHOW INDEXES fails, falls through to the original 31-query behavior
Scenario Before After
All indices exist 31 queries (~1-2s) 1 query (~50ms)
Some missing 31 queries 32 queries (1 check + 31 creates)
Second call, same instance 31 queries 0 queries (flag)
delete_existing=True unchanged unchanged
SHOW INDEXES fails N/A falls through to 31 queries

Files changed

File Change
graphiti_core/graph_queries.py Add get_neo4j_expected_index_names() + regex
graphiti_core/driver/neo4j_driver.py Fast-path logic + _indices_verified flag
tests/driver/test_neo4j_driver.py New test file (8 tests)

Test plan

  • get_neo4j_expected_index_names() returns exactly 31 index names
  • Fast path: all indices exist → 1 query only
  • Partial missing → falls through to creation
  • delete_existing=True → bypasses fast path
  • SHOW INDEXES failure → graceful fallback to individual queries
  • _indices_verified flag → second call is free (0 queries)
  • delete_existing=True → resets verified flag
  • Ruff format/lint clean, Pyright 0 errors

@danielchalef
Copy link
Member

danielchalef commented Mar 2, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@carlos-alm
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

danielchalef added a commit that referenced this pull request Mar 2, 2026
…dy exist

Add a fast path to build_indices_and_constraints(): query SHOW INDEXES
(1 roundtrip) and skip creation if all expected indices already exist.
Add an instance-level _indices_verified flag so repeated calls within
the same process are free.

- All indices exist: 31 queries -> 1 query
- Some missing: 31 queries -> 32 queries (1 check + 31 creates)
- Second call, same instance: 31 queries -> 0 queries (flag)
- SHOW INDEXES fails: graceful fallback to 31 individual queries
- delete_existing=True: unchanged behavior
@carlos-alm carlos-alm force-pushed the perf/skip-redundant-index-creation branch from 77a1a58 to 0ec8195 Compare March 2, 2026 11:33
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.

2 participants