Repository navigation
fix(db): add missing index on dataflow.call_edge_id (#1948) - #2203
Merged
Merged
Conversation
dataflow.call_edge_id (added in migration v18, `REFERENCES edges(id)`) was never given its own index. Every DELETE FROM edges forces SQLite to scan the entire dataflow table to enforce the FK constraint, and the explicit dataflowByCallEdge cleanup query in build-stmts.ts scans it too — both confirmed via EXPLAIN QUERY PLAN to flip from `SCAN dataflow` to an indexed SEARCH once this index exists. better-sqlite3 defaults `PRAGMA foreign_keys = ON`; the native/rusqlite connection never sets this pragma, so only the WASM engine ever paid this cost — matching #1948's report that WASM-only detectMs regressed while native stayed flat. Measured on this repo's own graph (~19.8K nodes, ~41.8K edges): a 1-file incremental purge dropped from 62ms to 1.47ms, and confirmed end-to-end via the real build CLI that overall detectMs for a 1-file rebuild drops well below the pre-regression 3.15.0 baseline (56ms). The prior /fixer pass's hypothesis (blaming clearDeletedExportAdvisories/ recordDeletedExportAdvisories for uncached db.prepare() calls) measured at only ~0.3ms per call — not the actual cause. This fix targets the verified root cause instead. No README/CLAUDE.md/ROADMAP.md changes needed — neither enumerates individual migration versions (docs check acknowledged). Refs #1948
Contributor
Greptile SummaryAdds a mirrored SQLite migration for the TypeScript and native database engines to index
Confidence Score: 5/5The PR appears safe to merge; the migration is compatible with fresh and existing databases and remains synchronized across both database engines. Both migration paths create Important Files Changed
Reviews (1): Last reviewed commit: "fix(db): add missing index on dataflow.c..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root-causes and fixes the WASM engine's
detectMsregression reported in #1948.dataflow.call_edge_id(added in migration v18,REFERENCES edges(id)) was never given its own index. EveryDELETE FROM edgesforces SQLite to scan the entiredataflowtable to enforce the foreign-key constraint, and the explicitdataflowByCallEdgecleanup query inbuild-stmts.tsscans it too. Confirmed viaEXPLAIN QUERY PLAN: without the index both queries showSCAN dataflow; with it, both become an indexedSEARCH.Why WASM-only:
better-sqlite3defaultsPRAGMA foreign_keys = ON. The native/rusqlite connection (crates/codegraph-core/src/db/connection.rs) never sets this pragma at all, so it stays at SQLite's true default of OFF — native never pays the FK-check-scan cost. This exactly matches #1948's own report that native'sdetectMsstayed flat (2-3ms) while WASM regressed.Investigation note
A previous
/fixerpass's comment on #1948 hypothesized the cause was uncacheddb.prepare()calls insideclearDeletedExportAdvisories/recordDeletedExportAdvisories. I measured that path directly (microbenchmark against both an in-memory DB and a real WAL-mode file-backed DB) and found it costs ~0.02-0.3ms per call — nowhere near the ~57ms reported gap. That hypothesis doesn't hold up quantitatively. Instrumenting the realdetectChangespipeline end-to-end on this repo's own build pointed topurgeStaleReverseDeps→purgeFilesFromGraph→ the FK-check scan as the actual dominant cost (~81ms of ~91ms total on one profiling run).Verification
EXPLAIN QUERY PLANbefore/after:DELETE FROM edges ...and thedataflowByCallEdgedelete both flip fromSCAN dataflowtoSEARCH dataflow USING COVERING INDEX idx_dataflow_call_edgepurgeFilesDataagainst this repo's own built graph (~19.8K nodes, ~41.8K edges): 62.01ms → 1.47ms (97.6% reduction) for a 1-file purgecodegraph build --engine wasm, 1-file touch + incremental rebuild):purgeStaleReverseDepsdropped from 62.02ms → 1.79ms, totalhandle*Buildfrom 63.26ms → 2.93ms — combined withgetChangedFiles(~9ms), overalldetectMsfor a 1-file rebuild now lands well under the pre-regression 3.15.0 baseline of 56mscargo test --release(crates/codegraph-core): 669 passednpm test: 260 files, 4197 passed, 30 skipped, 2 todo, 0 failednpm run lint: cleansrc/db/migrations.tsv23,crates/codegraph-core/src/db/connection.rsv23) and is a plainCREATE INDEX IF NOT EXISTS, safe on existing databasesRefs #1948