Sql Schema Audit logo

Sql Schema Audit

Community
mizchi
sql-schema-audit

Index coverage and N+1 review aids for SQLite/D1 schemas with a sqlc catalog. Surfaces unused indexes (with FK CASCADE awareness so cascade-load-bearing indexes are not flagged), queries that scan tables without index help, and `for`-loops calling generated SQL fns.

Overview

Publishermizchi
Repositoryskills
Skill namesql-schema-audit
Stars
333
Forks
4
Bundled files
2
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.

  • 2 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 Schema 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-schema-audit .claude/skills/sql-schema-audit
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Sql Schema 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 Schema 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 Schema 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 Schema Audit

Use this for two recurring DBA review tasks:

  1. Where is index work missing? Walk every query plan and attribute each SCAN <table> to either a known intentional case (no covering index possible) or a gap (table has no indexes at all).
  2. Where might code N+1? Find for-loops that call sqlc-generated functions — review aid, not a hard fail, because batch inserts are legitimate.

index-coverage.mjs

bash
# `scripts/` is relative to THIS skill's directory; from elsewhere use the
# absolute path. `--out` is OPTIONAL — omit it and the report prints to stdout.
# The printed report IS the deliverable; the file is just a persisted copy.
node scripts/index-coverage.mjs \
  --schema your-project/db/schema.sql \
  --queries your-project/db/queries.sql \
  [--out your-project/.linters/index-coverage.txt]

Output sections:

  • Per-query SCAN attribution: each query that produces a SCAN step is listed with the SCAN's rationale. FIX marker means the table has no indexes at all (immediate work). No marker means the table has indexes but the planner chose to scan anyway — usually fine (small table, unusual WHERE shape).
  • Drop candidates: indexes that no catalog query referenced via SEARCH USING INDEX and don't cover any FK CASCADE source column.
  • FK-cascade-load-bearing indexes: unused-by-SELECT indexes whose leading columns match a foreign key's from list. SQLite does NOT auto-create indexes on FK columns; without an explicit index, ON DELETE CASCADE walks the child table with a full scan. These look "unused" but are load-bearing for delete latency.

The FK-awareness step matters: in a typical schema with many owner_user_id foreign keys, most "unused" indexes are actually serving cascade deletes. Dropping them silently regresses delete performance.

When dropping a candidate

Cross-check before dropping:

  1. The audit only inspects sqlc-managed queries. Inline SQL (FTS5 MATCH, dynamic LIKE, vector lookups) bypasses the audit. Grep the codebase for the index name or its column combination before dropping.
  2. The planner may pick the index dynamically for shapes the static EXPLAIN doesn't replicate (e.g. when a different bind value distribution changes the chosen index). A drop candidate is "no SELECT picks it in the analyzed catalog" — that's necessary, not sufficient.
  3. An index that's logically "redundant to" a superset index can usually be dropped — the planner will fall through to the longer index. Verify with sql-plan-audit after the drop to make sure no query regressed to SCAN.

n-plus-one.mjs

bash
node scripts/n-plus-one.mjs your-project/src

Regex-based scan for for-loops that call a sqlc-generated function within 12 lines. Tunable:

  • --callee-prefix @db.,db. to match other binding styles (Rust state.db., Go q., etc.).
  • --window 20 to widen the look-ahead.

The script returns 0 always — N+1 candidates need human review. Use it as a review aid, not a CI gate.

Most candidates in well-typed codebases are legitimate batch inserts (create_skill_file inside for file in files, add_skill_tag inside for tag in tags). One genuine SELECT-in-loop is the kind of thing this report exists to catch.

When to invoke

  • After a schema change (added or removed index).
  • Before a release: a one-time "what's unused?" pass.
  • When a feature lands a for-loop touching the DB — eyeball the n-plus-one report.

Not in scope

  • Schema drift between code and production: needs reading the live DB schema and diffing against schema.sql. Out of scope here.
  • CHECK / NOT NULL audit: schemalint or similar. Probably worth a follow-up skill.
  • EXPLAIN ANALYZE / actual row counts: SQLite doesn't expose them statically.

Engine extensibility

The PRAGMA-based introspection (PRAGMA index_list, PRAGMA index_info, PRAGMA foreign_key_list) is SQLite-specific. For Postgres: query pg_indexes + pg_stat_user_indexes (which gives actual usage counts — much more accurate than the EXPLAIN-based heuristic) + information_schema.referential_constraints. For MySQL: information_schema.STATISTICS + information_schema.KEY_COLUMN_USAGE.

The N+1 detector is engine-agnostic, but the --callee-prefix heuristic depends on codegen conventions (sqlc / sqlx / typeorm / prisma).

Requirements

  • Node 22 or newer (uses the built-in node:sqlite module).
  • A sqlc-style query catalog or compatible format.

Files

  • scripts/index-coverage.mjs — per-query SCAN attribution + drop candidates + FK-cascade-load-bearing list.
  • scripts/n-plus-one.mjsfor-loop sqlc-call detector.

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 Schema Audit AI skill do?

Index coverage and N+1 review aids for SQLite/D1 schemas with a sqlc catalog. Surfaces unused indexes (with FK CASCADE awareness so cascade-load-bearing indexes are not flagged), queries that scan tables without index help, and `for`-loops calling generated SQL fns.

Why use Sql Schema Audit on TypingMind?

Because you install it once and use it with any model. Sql Schema 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 Schema Audit in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/mizchi/skills/tree/main/sql-schema-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 Schema 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 Schema Audit?

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

Is the Sql Schema 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 👇