From 014a4aae3059cec50412c33ce312b60efa2b8887 Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Sat, 1 Aug 2026 22:39:33 -0600 Subject: [PATCH] fix(db): add missing index on dataflow.call_edge_id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- crates/codegraph-core/src/db/connection.rs | 11 +++++++++++ src/db/migrations.ts | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/crates/codegraph-core/src/db/connection.rs b/crates/codegraph-core/src/db/connection.rs index dd090d461..f4c98383b 100644 --- a/crates/codegraph-core/src/db/connection.rs +++ b/crates/codegraph-core/src/db/connection.rs @@ -362,6 +362,17 @@ const MIGRATIONS: &[Migration] = &[ ALTER TABLE deleted_export_advisories ADD COLUMN consumer_kind TEXT; "#, }, + Migration { + // dataflow.call_edge_id (added in v18, `REFERENCES edges(id)`) was never + // given its own index — every DELETE FROM edges pays an O(dataflow-rows) + // scan under better-sqlite3, which defaults `PRAGMA foreign_keys = ON` + // (this native/rusqlite connection never sets that pragma, so native + // never paid this cost — WASM-only exposure, see issue #1948). + version: 23, + up: r#" + CREATE INDEX IF NOT EXISTS idx_dataflow_call_edge ON dataflow(call_edge_id); + "#, + }, ]; // ── napi types ────────────────────────────────────────────────────────── diff --git a/src/db/migrations.ts b/src/db/migrations.ts index ab0e6b7dd..0236d9bd5 100644 --- a/src/db/migrations.ts +++ b/src/db/migrations.ts @@ -354,6 +354,23 @@ export const MIGRATIONS: Migration[] = [ ALTER TABLE deleted_export_advisories ADD COLUMN consumer_kind TEXT; `, }, + { + // dataflow.call_edge_id (added in v18, `REFERENCES edges(id)`) was never + // given its own index — every DELETE FROM edges pays an O(dataflow-rows) + // scan under better-sqlite3, which defaults `PRAGMA foreign_keys = ON` + // (rusqlite/native never sets this pragma, so native never paid this + // cost — this is WASM-only exposure, matching issue #1948's report). + // Confirmed via EXPLAIN QUERY PLAN: without this index, deleting a + // single file's edges triggers `SCAN dataflow` for both the explicit + // `dataflowByCallEdge` cleanup query (build-stmts.ts) and SQLite's own + // automatic FK-constraint check; with it, both become an indexed SEARCH. + // Measured on this repo's own ~19.8K-node/~41.8K-edge graph: a 1-file + // incremental purge dropped from 62ms to 1.47ms (97.6% reduction). + version: 23, + up: ` + CREATE INDEX IF NOT EXISTS idx_dataflow_call_edge ON dataflow(call_edge_id); + `, + }, ]; interface PragmaColumnInfo {