Koriym.SqlQuality

Japanese
MySQL query analysis for coding agents and CI. It measures, the agent decides.
A coding agent can propose a rewrite or an index, but it cannot tell by reading the SQL whether the
rewrite returns the same rows, whether MySQL will use the index, or whether a problem is only hidden
by the optimizer today. sql-quality answers those questions against a real database and prints JSON:
- analyze / explain:
EXPLAIN FORMAT=JSON, EXPLAIN ANALYZE, SHOW WARNINGS, the schema and the
optimizer trace go through 18 detectors. Every issue has a stable id, severity, evidence and, where
one applies, a suggested index or rewrite.
- compare: runs the original and the rewritten SELECT and checks that they return the same rows.
- try-index: adds a candidate index invisible, measures the statement with it in this session
only, and drops it.
- --baseline: tells new issues from known ones, so CI fails only on what a change introduced.
Each statement is also explained with the optimizer features turned off: optimizer_impact.hidden_issues
lists the problems the optimizer currently works around, which can come back when statistics or data
volume change.
Requirements
- PHP 8.1+ with PDO MySQL
- MySQL 8.0.18+ (
EXPLAIN ANALYZE; try-index uses invisible indexes)
Installation
composer require --dev koriym/sql-quality
Commands
The connection is read from SQL_QUALITY_DSN, SQL_QUALITY_USER and SQL_QUALITY_PASSWORD, or from
--dsn, --user and --password (default mysql:host=127.0.0.1;dbname=test, root, empty password).
Prefer the environment variables: a password on the command line ends up in shell history and agent transcripts.
stdout carries the JSON report and nothing else; errors go to stderr.
analyze
vendor/bin/sql-quality analyze --sql-dir=sql/ --params=sql/params.json --fail-on=critical
Analyzes every *.sql file in the directory. --params maps a file name to its parameters
(a .json file, or a PHP file returning the same array); a file without an entry runs without
parameters, and one whose :name placeholders are not covered is listed in skipped as
UnboundParameter: name, ....
{
"1_full_table_scan.sql": {"min_views": 1000},
"15_listed_parameters.sql": {"names": ["Alice", "Bob"]}
}
--baseline=FILE takes an earlier analyze report and adds baseline.new, resolved, persisting
and unverified (files the earlier report had and this run did not analyze). With a baseline,
--fail-on counts only the new issues.
vendor/bin/sql-quality analyze --sql-dir=sql/ --params=sql/params.json > baseline.json # on the main branch
vendor/bin/sql-quality analyze --sql-dir=sql/ --params=sql/params.json --baseline=baseline.json --fail-on=warning
explain
vendor/bin/sql-quality explain --sql-file=sql/1_full_table_scan.sql --params='{"min_views":1000}'
vendor/bin/sql-quality explain --sql="SELECT * FROM posts WHERE user_id = :id" --params='{"id":1}'
echo "SELECT * FROM posts" | vendor/bin/sql-quality explain --sql-file=-
One statement, as a file, inline or from stdin. The output is a summary: the issues and the optimizer
impact. --detail=full adds the EXPLAIN JSON, EXPLAIN ANALYZE, SHOW WARNINGS, the schema and the
optimizer trace.
{
"file": "1_full_table_scan.sql",
"sql": "SELECT * FROM posts WHERE view_count > 1000",
"executed": true,
"skipped_reason": null,
"cost": 503.45,
"execution_time_ms": 4.87,
"issues": [
{
"id": "FullTableScan@posts",
"type": "FullTableScan",
"message": "Full table scan detected.",
"documentation": "https://koriym.github.io/Koriym.SqlQuality/issues/FullTableScan",
"severity": "Critical",
"confidence": 0.95,
"evidence": {"table_name": "posts", "rows_examined_per_scan": 4952, "filtered": "33.33", "attached_condition": "(`test`.`posts`.`view_count` > 1000)"},
"suggestion": {"kind": "index", "description": "Add a composite index covering view_count", "ddl": "CREATE INDEX idx_posts_view_count ON posts (view_count)"}
}
],
"optimizer_impact": {"cost_without_optimizer": 503.45, "cost_reduction_percent": 0, "hidden_issues": []}
}