Sql Plan Auditのロゴ

Sql Plan Audit

コミュニティ
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.

概要

公開者mizchi
リポジトリskills
スキル名sql-plan-audit
スター
333
フォーク
4
同梱ファイル
1
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 1個の同梱ファイル

    モデルが作業中に読み取れるスクリプト、テンプレート、参考資料です。ファイルは読み取り専用で、実行されることはありません。

  • オープンソース

    mizchiがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキルSql Plan AuditをTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/mizchi/skills/tree/main/sql-plan-audit
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

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
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでSql Plan Auditを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあると、モデルが自動的にSql Plan Auditを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Sql Plan Auditは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

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.

同梱ファイル

スキルが読み込まれている間、モデルは必要に応じてこれらを読み取ります。読み取り可能なファイルとして提供され、実行されることはありません。

よくある質問

AIスキル「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.

TypingMindでSql Plan Auditを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Sql Plan Auditはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにSql Plan Auditをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/mizchi/skills/tree/main/sql-plan-auditを貼り付けてください。TypingMindがSKILL.mdを読み込み、関連ファイルをまとめて、チャットごとに有効にできるスキルとしてインストールします。

Sql Plan Auditを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストが合致したときにこのスキルを読み込めます。

Sql Plan Auditは何個のAIモデルで使えますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもSql Plan Auditを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Sql Plan Audit」は無料ですか?

mizchiがGitHubで公開しています。ライセンス条件はリポジトリでご確認ください。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇