/sql-quality:sql-quality-fixfrom sql-quality
Fixes MySQL performance problems in SQL files by rewriting queries and testing indexes. Verifies rewrites return identical rows and measures index effectiveness as invisible indexes before committing.
Ships in sql-quality. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Fix the problems sql-quality analyze reports, one at a time, and keep only fixes the tool has verified.
$ARGUMENTS: the SQL directory, optionally the params file, and options.
sql/ sql/params.phpsql/ sql/params.php --keep-indexes--keep-indexes: keep the indexes try-index finds effective, in the database. Without it no index
is left behind; the effective ones are reported as DDL for a migration.
compare says equivalent: true with both_empty: false.try-index says effective: true.CREATE INDEX, DROP INDEX or any other DDL yourself; try-index adds the trial index
INVISIBLE, so no other session's plan changes, and drops it again.try-index builds a real index: it reads the whole table.SQ=vendor/bin/sql-quality; [ -x "$SQ" ] || SQ=bin/sql-quality
mkdir -p build/sql-quality
$SQ analyze --sql-dir=<dir> --params=<params> > build/sql-quality/baseline.json
Exit 2: stop and report stderr. Files in skipped with UnboundParameter need parameters first
(/sql-params-generate).
For each issue whose suggestion.kind is rewrite, or whose type is fixed by rewriting
(FunctionInvalidatesIndex, ImplicitTypeConversion, OrderByRand, DeepOffset, UnnecessaryDistinct,
IneffectiveUnion, DependentSubquery), write the rewritten statement and check it:
$SQ compare --before=<dir>/<file> --after-sql="<rewritten SQL>" --params=<params>
equivalent: true, both_empty: false, and same_order: true when the statement has an
ORDER BY: replace the file's statement. Report cost_change_percent and issues.resolved.both_empty: true: the parameters return no rows, so the check proves nothing. Pick parameters that
return rows (--before-params / --after-params take a JSON object) and compare again.same_order: false on a statement with an ORDER BY: the rows come back in another order. Do not
apply it; list it for manual review. Without an ORDER BY the order is not part of the result.A rewrite that changes column names, or a statement that depends on NOW() / RAND(), cannot pass;
list it for manual review instead.
For each issue with suggestion.kind index, try its suggestion.ddl against the file:
$SQ try-index --sql-file=<dir>/<file> --params=<params> --index="<suggestion.ddl>" [--keep]
Pass --keep only with --keep-indexes. Exit 0 is effective (the plan uses the index and the cost
fell at least 5%), exit 1 is not. Exit 2 with Lock wait timeout: a transaction holds the table; retry
later rather than waiting it out. InvisibleIndexesPresent: the table already has an invisible index;
report it and skip the trial. stderr names the DROP INDEX that removes the trial index; if a run is
interrupted, run that statement before anything else. Record index.ddl, cost_change_percent and issues.resolved
either way. Two suggestions for the same table: try the composite one first.
$SQ analyze --sql-dir=<dir> --params=<params> --baseline=build/sql-quality/baseline.json > build/sql-quality/after.json
baseline.new must be empty; if a rewrite introduced an issue, revert it. baseline.unverified lists
files that no longer analyze; revert the change that caused it.
Write build/sql-quality/fix-report.md:
SQL Quality Fix
Total cost 441706 → 120330 (-72.8%) · issues 47 → 21 · new 0
Rewrites (verified equivalent)
3_function_on_indexed_column.sql DATE(created_at) = :d → range resolved [email protected]_at
Indexes (effective, as invisible indexes)
CREATE INDEX `idx_posts_view_count` ON `posts` (`view_count`) -95.8% kept: no
Not applied
30_order_by_rand.sql OrderByRand@posts rewrite changes the result (random order); review by hand
CREATE INDEX ... ON comments (content) not used by the plan
Take every number from the tool's output; do not estimate.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub koriym/koriym.sqlquality --plugin sql-quality3 skills in this plugin
/sql-params-generateWrite the parameter file sql-quality needs for SQL files with :name placeholders, using values that exist in the database. Use when sql-quality skips files with UnboundParameter, or when asked to generate SQL parameters./sql-quality-checkCheck SQL files for MySQL performance problems (full table scans, ineffective joins, filesorts, functions on indexed columns, implicit conversions and more) and report them, CI style, without changing anything. Use when asked to check, lint or review the SQL of a project, or to gate a change on new SQL problems.Other plugins' skills for the same job