MCPInstall

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.

Installs
60
Last 24h
—
Trending rank
—
Security
Not audited

Install

Works with every agent the skills CLI supports.

npx skills add https://github.com/mizchi/skills --skill sql-schema-audit
  1. 01

    Install

    Run the command in your project. The CLI asks which agents to install for.

  2. 02

    Describe the task

    Ask your agent for something this skill covers. It loads sql-schema-audit automatically when relevant.

  3. 03

    Review the result

    The agent follows the SKILL.md steps, scripts and references bundled with the skill.

SKILL.md

The instructions your agent reads when the skill is triggered.

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

# `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

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.mjs — for-loop sqlc-call detector.

FAQ

What does the sql-schema-audit 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.

How do I install sql-schema-audit?

Run npx skills add https://github.com/mizchi/skills --skill sql-schema-audit in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/sql-schema-audit/ (personal) or .claude/skills/sql-schema-audit/ (project).

Which AI agents can use sql-schema-audit?

Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.

Is sql-schema-audit safe to install?

No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.

Who maintains sql-schema-audit?

It is published by mizchi in mizchi/skills. It has 60 recorded installs.

More from mizchi/skills

View all →

Open the skill to read its SKILL.md instructions.

AI & Agents346 installs

pi-coding-agent

mizchi/skills

Open the skill to read its SKILL.md instructions.

130 installs

playwright-cli

mizchi/skills

Open the skill to read its SKILL.md instructions.

Testing & QA108 installs

playwright-test

mizchi/skills

Open the skill to read its SKILL.md instructions.

Testing & QA91 installs

Open the skill to read its SKILL.md instructions.

88 installs

Open the skill to read its SKILL.md instructions.

Docs & Writing71 installs

Popular data & databases skills

View category →

Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pgcron, pgmq), vector/semantic search (pgvector), and restoring dumps (pgrestore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.

OfficialData & Databases462.2K installs

prisma-client-api

prisma/skills

Prisma Client API reference covering model queries, filters, operators, and client methods. Use when writing database queries, using CRUD operations, filtering data, or configuring Prisma Client. Triggers on "prisma query", "findMany", "create", "update", "delete", "$transaction".

OfficialData & Databases393.5K installs

Deprecated compatibility name for prisma-orm-setup. Use only when a user or existing integration explicitly invokes prisma-database-setup; otherwise select prisma-orm-setup.

OfficialData & Databases392.3K installs

prisma-cli

prisma/skills

Prisma ORM 7 CLI reference for existing Prisma 7 applications or explicitly selected Prisma 7 workflows. Covers prisma init, generate, migrate, db, dev, studio, and mcp. For setup, connection repair, or Prisma 8, use prisma-orm-setup; not the Prisma Platform CLI.

OfficialData & Databases390.8K installs

prisma-postgres

prisma/skills

Deprecated compatibility name for prisma-postgres-setup. Use only when a user or existing integration explicitly invokes prisma-postgres; otherwise select prisma-postgres-setup.

OfficialData & Databases384.4K installs

Required reference for Prisma ORM 7 SQL driver adapter work. Use when implementing or modifying adapters, adding database drivers, or touching SqlDriverAdapter, Transaction, savepoint, result mapping, or DriverAdapterError behavior. Covers current transaction lifecycle, optional savepoint hooks, original database-error preservation, and verification.

OfficialData & Databases378.9K installs