koriym/sql-quality
Composer 安装命令:
composer require koriym/sql-quality
包简介
README 文档
README
A powerful MySQL query analyzer that helps detect potential performance issues in SQL files and provides AI-powered optimization recommendations.
Features
- Detects common performance issues (full table scans, inefficient JOINs, etc.)
- Provides AI-powered optimization recommendations
- Supports multiple output languages
- Generates detailed analysis reports in Markdown format
Requirements
- PHP 8.1+
- MySQL 5.7+ or MariaDB 10.2+
- PDO MySQL extension
Installation
composer require koriym/sql-quality
Usage
<?php namespace Koriym\SqlQuality; use PDO; use function dirname; require dirname(__DIR__) . '/vendor/autoload.php'; $pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); $sqlParams = require 'path/to/sql_params.php'; //return [ // '1_full_table_scan.sql' => ['min_views' => 1000], // '2_filesort.sql' => ['status' => 'published', 'limit' => 10] //]; $analyzer = new SqlFileAnalyzer( $pdo, new ExplainAnalyzer(), 'path/to/sql_dir', new AIQueryAdvisor('以上の分析を日本語で記述してください。') ); // Output to build/sql-quality $analyzer->analyzeSqlDirectory($sqlParams, __DIR__ . '/build/sql-quality');
CLI Usage
sql-quality analyze --sql-dir=sql/ --params=params.php --format=json
sql-quality analyze --sql-dir=sql/ --params=params.php --format=markdown --output=build/sql-quality
sql-quality analyze --sql-dir=sql/ --params=params.php --dsn="mysql:host=localhost;dbname=mydb" --user=root --password=secret
sql-quality analyze --sql-dir=sql/ --params=params.php --lang=ja
Options
| Option | Description | Default |
|---|---|---|
--sql-dir=DIR |
Directory containing SQL files (required) | |
--params=FILE |
PHP file returning SQL parameters array (required) | |
--dsn=DSN |
Database DSN | mysql:host=127.0.0.1;dbname=test |
--user=USER |
Database user | root |
--password=PASS |
Database password | (empty) |
--format=FORMAT |
Output format: json or markdown |
json |
--output=DIR |
Output directory for markdown reports | |
--lang=LANG |
Language for messages: en or ja |
en |
Claude Code Skills
For Claude Code users, automated SQL optimization skills are available:
Installation
From Marketplace:
# Add marketplace /plugin marketplace add koriym/Koriym.SqlQuality # Install plugin (includes all 3 skills) /plugin install sql-quality@sql-quality
For Project Developers:
When you trust this project folder, Claude Code will automatically prompt you to add the marketplace and enable the plugins (configured in .claude/settings.json).
Usage
# Analyze SQL files (CI-friendly) /sql-quality-check tests/sql tests/params/sql_params.php # Auto-fix issues with step-by-step measurement /sql-quality-fix tests/sql tests/params/sql_params.php # Generate parameter bindings from SQL files /sql-params-generate tests/sql
Features
These AI-powered skills:
- Detect performance issues (FullTableScan, IneffectiveJoin, etc.)
- Rewrite problematic SQL patterns (functions on columns, implicit conversions)
- Create indexes and measure their impact in real-time
- Roll back ineffective indexes automatically
- Generate detailed improvement reports with cost reductions
See skills/*/SKILL.md for detailed documentation.
Analysis Reports
Example:
The analyzer generates two types of analysis reports in the specified output directory (e.g., build/sql-quality).
1. Query Analysis List
Shows the overall analysis of each SQL query:
| Column | Description |
|---|---|
| SQL File | Name of the SQL file |
| Cost | Estimated query cost |
| Level | Performance level based on statistical analysis (μ = mean, σ = standard deviation) |
| Issues | Detected performance issues |
| Report | Link to detailed analysis |
Example:
| SQL File | Cost | Exec Time (ms) | Level | Issues | Report |
|---|---|---|---|---|---|
| 1_full_table_scan.sql | 497.95 | 5.92 | Medium (μ ± σ) | FullTableScan | Details |
2. Queries with Optimizer Impact
The MySQL Query Optimizer is a crucial component that automatically optimizes query execution plans. Even when SQL and index design are not optimal, the optimizer attempts to improve performance at runtime.
| Column | Description |
|---|---|
| SQL File | Name of the SQL file |
| Base Access | Access method, row count, and scan percentage with optimizer disabled |
| Optimized Access | Access method, row count, and scan percentage with optimizer enabled |
| Cost Impact | Cost reduction percentage by optimizer (negative values indicate improvement) |
| Base Issues | Issues detected when optimizer is disabled |
| Plan Changes | Detailed execution plan changes (filtering ratio, cost changes, etc.) |
Example Interpretation
Let's look at this example:
| SQL File | Base Access | Optimized Access | Cost Impact | Base Issues | Plan Changes |
|---|---|---|---|---|---|
| 11_nested_loop.sql | ALL, 4897 rows, 100.0% | ALL, 1000 rows, 10.0% → ref, using idx_posts_user_id, 4 rows, 100.0% | -44.9% | FullTableScan | - |
In this example, without the optimizer, the query performs a full table scan processing 4,897 rows. With the optimizer enabled, it uses an index to access only 4 rows. The cost reduction of -44.9% indicates a significant improvement through optimizer intervention.
Understanding Optimizer Impact
While the optimizer improves performance, relying on it may mask potential underlying issues. Additionally, there are risks of unstable performance as data volume grows or statistics change. This feature aims to detect such issues early and guide appropriate solutions by comparing execution plans and performance with and without the optimizer.
Project Statistics
The summary report also includes overall project statistics:
- Total SQL queries analyzed
- Average query cost
- Standard deviation of costs
Multilingual Support
SQL query analysis results support multilingual output in both ExplainAnalyzer and AIQueryAdvisor.
Language Customization in ExplainAnalyzer
While English is the default language, you can customize error messages in ExplainAnalyzer constructor for other languages:
// Japanese error messages $analyzer = new ExplainAnalyzer([ 'FullTableScan' => 'フルテーブルスキャンが検出されました。', 'IneffectiveJoin' => '非効率的な結合が検出されました。', 'FunctionInvalidatesIndex' => '関数の使用によりインデックスが無効化されています。', // ... other messages ]); // Combined with AI Advisor for complete Japanese output $analyzer = new SqlFileAnalyzer( $pdo, $analyzer, $sqlDirectory, new AIQueryAdvisor('以上の分析を日本語で記述してください。') );
This allows you to generate the entire analysis report in your preferred language. Both error messages and AI analysis results will be output in the specified language.
koriym/sql-quality 适用场景与选型建议
koriym/sql-quality 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.56k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 koriym/sql-quality 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 koriym/sql-quality 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 30.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-20