Sql Plan Audit logo

Sql Plan Audit

Community
mizchi
sql-plan-audit

Run EXPLAIN QUERY PLAN against every query in a sqlc-style catalog and diff the plans against a baseline. Detects new full-table SCANs and TEMP B-TREE sort scans introduced by PRs. SQLite/D1-only today; engine extension noted below.

Overview

Publishermizchi
Repositoryskills
Skill namesql-plan-audit
Stars
333
Forks
4
Bundled files
1
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 1 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by mizchi on GitHub. Read the source before you install it.

Installation

Install the Sql Plan Audit AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/mizchi/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/sql-plan-audit .claude/skills/sql-plan-audit
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Sql Plan Audit in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Sql Plan Audit on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Sql Plan Audit is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

SQL Plan Audit

Use this when a project has a sqlc-style query catalog (or any file with -- name: X :type markers) and wants to keep query plans visible in code review.

Why

sqlc enforces SQL syntax at codegen, but it doesn't watch the execution plan. A column rename, a removed index, or a small WHERE-clause addition can flip a query from SEARCH USING INDEX to SCAN TABLE silently. On Cloudflare D1 / SQLite without EXPLAIN ANALYZE, the planner output is the only static signal. This skill freezes that output as a reviewable artifact.

When to invoke

  • Schema or query catalog changed in a PR.
  • A new index was added and you want to confirm queries pick it up.
  • Quarterly audit of an existing query catalog.

Workflow

  1. Identify the schema file and the query catalog file. Typical layout:
    <project>/db/schema.sql
    <project>/db/queries.sql   (or db/sqlite/query.sql for sqlc projects)
  2. Run the runner:
    bash
    node scripts/explain-runner.mjs \
      --schema your-project/db/schema.sql \
      --queries your-project/db/queries.sql \
      --out your-project/.linters/query-plans.txt
  3. Commit the output. The text format is line-stable across runs; diffs surface plan changes.
  4. To enforce in CI, regenerate to JSON and diff against a committed baseline:
    bash
    node scripts/explain-runner.mjs \
      --schema your-project/db/schema.sql \
      --queries your-project/db/queries.sql \
      --baseline your-project/.linters/query-plans.json \
      --format json --fail-on regress
  5. When intentional regressions land (e.g. an index was retired on purpose), regenerate the baseline in the same PR.

What success and failure look like

The runner is deliberately silent on success. Knowing where output lands matters when wiring it into CI:

InvocationstdoutstderrExit
--out <path> (regen)nothingonly Node's node:sqlite experimental warning0
no --out (regen)full text/JSON reportonly the experimental warning0
--baseline <path> --fail-on regress (CI check), cleanfull text/JSON report (or nothing if --out is set)only the experimental warning0
--baseline <path> --fail-on regress (CI check), regressionfull text/JSON reportper-query regression detail (before vs after plans)1
any path, internal error (e.g. schema fails to load)nothing or partialNode stack trace1

Read this as: stderr is where pass/fail diagnostics live. In CI, capture stderr explicitly (2>&1, 2>artifact.log, or separate streams) — piping only stdout to a parser will lose every regression message. If a regen run "prints nothing", check $? and ls <out path> to confirm; that is the documented happy-path.

How the runner handles placeholders

sqlc.arg('x') and sqlc.slice('x') are rewritten to NULL before EXPLAIN runs. The plan does not depend on bind values, only on the SQL shape, so this is safe. ? positional placeholders are left as-is — SQLite accepts them inside EXPLAIN.

Severity markers

  • ! SCAN — full-table scan. Usually a missing index or a query intentionally touching every row.
  • ? TEMP B-TREE — USE TEMP B-TREE FOR ORDER BY / GROUP BY / DISTINCT. Sort happens in memory because no index covers the ordering.
  • SEARCH — index hit. Normal.

A plan with one SCAN on a small table (e.g. users with <1k rows) is often fine; the marker is a flag, not a verdict.

CI integration

Add a step that runs the JSON variant and fails on regression. A typical justfile:

just
sql-plan-audit:
    node scripts/explain-runner.mjs \
      --schema db/schema.sql \
      --queries db/queries.sql \
      --out .linters/query-plans.txt
    node scripts/explain-runner.mjs \
      --schema db/schema.sql \
      --queries db/queries.sql \
      --format json --out .linters/query-plans.json

sql-plan-audit-check:
    node scripts/explain-runner.mjs \
      --schema db/schema.sql \
      --queries db/queries.sql \
      --baseline .linters/query-plans.json \
      --format json --fail-on regress

Pre-push (pkfire / lefthook / pre-commit) is the right boundary — running this on every commit is slow and noisy.

Limitations

  • SQLite EXPLAIN does not report estimated row counts, so the runner cannot rank "bad SCAN of 10M rows" vs "fine SCAN of 50 rows". Combine with manual review.
  • FTS5 virtual tables show as SCAN VIRTUAL TABLE. Treated as info, not flagged.
  • Functions like datetime('now') are evaluated at plan time. Side effects (writes) are not run because the in-memory DB has the same schema but no rows.
  • Subqueries and CTEs may produce extra rows in the plan; the diff treats them stably.

Engine extensibility

The runner is SQLite-specific. To support Postgres / MySQL: swap the node:sqlite driver and the EXPLAIN syntax; the parser, baseline diff, and severity classifier are engine-agnostic. Not implemented here — drop a sibling script when needed.

Requirements

  • Node 22 or newer (uses the built-in node:sqlite module).
  • A sqlc-style query catalog (-- name: X :type markers). Other named-query formats can be supported by adjusting parseQueryCatalog in the runner.

Files

  • scripts/explain-runner.mjs — CLI entrypoint, no dependencies.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Sql Plan Audit AI skill do?

Run EXPLAIN QUERY PLAN against every query in a sqlc-style catalog and diff the plans against a baseline. Detects new full-table SCANs and TEMP B-TREE sort scans introduced by PRs. SQLite/D1-only today; engine extension noted below.

Why use Sql Plan Audit on TypingMind?

Because you install it once and use it with any model. Sql Plan Audit is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Sql Plan Audit in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/mizchi/skills/tree/main/sql-plan-audit. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Sql Plan Audit?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Sql Plan Audit?

As many as you like. As long as a model supports skills, you can use Sql Plan Audit with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Sql Plan Audit AI skill free?

It is published on GitHub by mizchi. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇