Skip to content

SQLite Query Plan Diff

In plain English

Captures SQLite EXPLAIN QUERY PLAN baselines from .sql or .json files and fails CI whenever a schema migration silently drops back to full-table scans.

A zero-dependency Python 3.12 CLI and library that snapshots SQLite EXPLAIN QUERY PLAN strategies and catches index regressions before schema changes ship.

1. Why I built it

Built to turn the programmatic index-plan verification technique proved inside University Course DB into a standalone reusable CI linter for any Python/SQLite project.

Problem being solvedFunctional tests still pass after a migration drops an index or breaks a covering index — only query-planner inspection catches the performance regression up front.

2. How it works

Implemented `QuerySpec`, `PlanSnapshot`, `PlanFinding`, `PlanReport`, `classify_access_kind()`, `parse_sql_queries()`, and CLI `main()` inside `src/sqlite_plan_diff/checker.py`.

Schema migrations and modified SELECT column lists can silently force SQLite off a covering index back to full table scans or external TEMP B-TREE sorts while returning identical rows in small test fixtures.

SQLite Query Plan Diff (`v1.0.0`) parses `-- name:` / `-- params:` annotated `.sql` files or structured `.json` query specs, connects to existing disk databases safely via URI `?mode=ro`, inspects query steps (`classify_access_kind`), and detects `COVERING_INDEX -> INDEX_SEARCH`, `INDEX -> FULL_SCAN`, missing baseline queries, and optional `--strict-temp-btree` regressions.

Includes `explain`, `capture`, and `check` subcommands with `--format text` / `--format json` reporting and explicit exit codes (`0` pass, `1` regression, `2` input/SQL error) with zero external runtime dependencies.

3. Output & Source Specimen

Query Plan Regression Check Flow

011. load_query_specs() -> Parses annotated `-- name:` / `-- params:` .sql or .json specs
022. _connect_sqlite(?mode=ro)-> Inspects existing DBs strictly read-only without side-effect creates
033. classify_access_kind() -> COVERING_INDEX | INDEX_SEARCH | FULL_SCAN | OTHER
044. diff_snapshots() -> Flags FULL_SCAN, covering downgrade, MISSING, or TEMP B-TREE (exit 1)
Exact access classification and regression diffing in sqlite_plan_diff/checker.pyRun checks: sqlite-plan-diff check :memory: queries.sql --schema next.sql --baseline base.json

Interactive plan checker (mirrors EXPLAIN QUERY PLAN scan & temp B-tree checks)

Interactive SQLite EXPLAIN QUERY PLAN Regression Simulator
EXPLAIN QUERY PLAN traceCOVERING_INDEX

CREATE INDEX idx_enrolments_mod_mark ON enrolments(module_code, mark);

SELECT student_id, mark FROM enrolments WHERE module_code = ? AND mark >= ?

->SEARCH enrolments USING COVERING INDEX idx_enrolments_mod_mark (module_code=? AND mark>?)
BASELINE INTACTexit 0

Execution strategy matches saved COVERING_INDEX baseline without table fetches or full scans.

COVERING_INDEX -> COVERING_INDEXchecked_queries: 1

4. Design Decisions & Challenges

Key Design Decisions

  • 01.Handled modern SQLite `SCAN table USING COVERING INDEX` lines explicitly so index-only scans are not misclassified as unindexed table scans, while still flagging mixed joins containing any unindexed `SCAN`.
  • 02.Opened existing disk `.db` paths with `?mode=ro` URI mode when `--schema` is omitted so inspecting a database never creates or mutates files on disk.
  • 03.Added `--strict-temp-btree` opt-in enforcement alongside readable `--format text` and machine-parseable `--format json` output.

Edge Cases Handled

  • Handling indented `-- name:` / `-- params:` annotations inside multiline `.sql` files cleanly and preventing missing file paths from silently creating empty SQLite databases.

5. Testing & Limits

Automated Checks

Verified with 9 pytest checks in `tests/test_checker.py` covering index vs full-scan classification, mixed join scans, covering-index downgrades, TEMP B-TREE strict mode, `.sql` parsing, read-only DB guards, and CLI exit codes `0`/`1`/`2`.

Current Limits & Next Steps

  • Targets SQLite `EXPLAIN QUERY PLAN` output rather than PostgreSQL JSON explain trees.
  • Bindings assume positional `?` parameter arrays rather than `:named` parameter objects.

6. Key Takeaways

What building SQLite Query Plan Diff reinforced

Inspecting planner steps automatically inside CI bridges correctness tests and query performance without fragile timing benchmarks.

  • — 9 pytest checks and Ruff lint pass cleanly (`v1.0.0`).
  • — Source hosted at github.com/16lv/sqlite-query-plan-diff.

Recent Activity

Latest repository updates

All changes for 16lv/sqlite-query-plan-diff

feat: release v1.0.0 with annotated .sql queries, explain command, and read-only DB mode

Completed SQLite Query Plan Diff v1.0.0 supporting indented '-- name:' / '-- params:' .sql files, explain subcommand, URI ?mode=ro inspection, and 9 pytest checks.

2026-11-24fd7ab7d

Initial v0.1.0 release of SQLite EXPLAIN QUERY PLAN regression checker

Committed directly to sqlite-query-plan-diff (main) · Captures SQLite EXPLAIN QUERY PLAN baselines from .sql or .json files and fails CI whenever a schema migration silently drops back to full-table scans.

2026-11-230ce7cb7