Sql Quality Fix

by koriym

skill

Automatically fix SQL performance issues with step-by-step measurement. Rewrites problematic SQL patterns (functions on columns, implicit conversions), creates indexes, measures their impact, and rolls back ineffective indexes. Reports improvements at each step with cost reduction percentages.

Skill Details

Repository Files

1 file in this skill directory


name: sql-quality-fix description: Automatically fix SQL performance issues with step-by-step measurement. Rewrites problematic SQL patterns (functions on columns, implicit conversions), creates indexes, measures their impact, and rolls back ineffective indexes. Reports improvements at each step with cost reduction percentages.

SQL Quality Fix

Automatically fix SQL performance issues with step-by-step measurement.

Arguments

  • $ARGUMENTS: SQL directory and params file
    • Example: "tests/sql tests/params/sql_params.php"
    • With flag: "tests/sql tests/params/sql_params.php --no-index"

Options

  • --no-index: Skip index creation, only suggest DDL

Steps

Step 0: Initial Analysis

php bin/sql-quality analyze \
  --sql-dir="$(echo $ARGUMENTS | cut -d' ' -f1)" \
  --params="$(echo $ARGUMENTS | cut -d' ' -f2)" \
  --format=json

Record as baseline.

Step 1: Fix SQL Files

Apply SQL fixes:

Issue Fix
FullTableScan Add WHERE with indexed columns
FunctionInvalidatesIndex Rewrite: YEAR(col)=2024col >= '2024-01-01'
IneffectiveLikePattern Use prefix match if possible
IneffectiveJoin Reorder JOINs, use explicit syntax

Re-analyze and record SQL fix impact.

Step 2: Create Indexes (one by one)

For each suggested index:

  1. Create index

    CREATE INDEX idx_name ON table(columns);
    
  2. Re-analyze

  3. Evaluate impact

    • Cost improved ≥ 5% → Keep index
    • Cost not improved → Rollback
      DROP INDEX idx_name ON table;
      
      Record as "ineffective, rolled back"

Step 3: Generate Report

Save to build/sql-quality/fix-result.json:

{
  "executed_at": "2024-01-15T10:30:00",
  "steps": [
    {
      "step": "initial",
      "total_cost": 650.00
    },
    {
      "step": "sql_fix",
      "total_cost": 450.00,
      "improvement": "-30.8%",
      "changes": [
        {"file": "1_full_table_scan.sql", "change": "Added WHERE user_id = :user_id"}
      ]
    },
    {
      "step": "index",
      "total_cost": 57.30,
      "improvement": "-87.3%",
      "indexes_created": [
        {"ddl": "CREATE INDEX idx_posts_user_id ON posts(user_id)", "impact": "-60%"}
      ],
      "indexes_rolled_back": [
        {"ddl": "CREATE INDEX idx_posts_title ON posts(title)", "reason": "no improvement"}
      ]
    }
  ],
  "final": {
    "total_cost": 57.30,
    "total_improvement": "-91.2%"
  },
  "manual_review_needed": []
}

Generate markdown:

php bin/sql-quality report --input=build/sql-quality/fix-result.json

Output Summary

SQL Quality Fix: Complete

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Step-by-Step Improvement
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
| Step      | Cost   | Change |
|-----------|--------|--------|
| Initial   | 650.00 | -      |
| SQL Fix   | 450.00 | -30.8% |
| Index     | 57.30  | -87.3% |
| **Final** | **57.30** | **-91.2%** |

SQL Changes:
  ✓ 1_full_table_scan.sql: Added WHERE clause

Indexes Created:
  ✓ idx_posts_user_id (-60% cost)

Indexes Rolled Back (ineffective):
  ✗ idx_posts_title (no improvement)

Manual Review:
  (none)

Report: build/sql-quality/fix-report.md

Related Skills

Attack Tree Construction

Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.

skill

Grafana Dashboards

Create and manage production Grafana dashboards for real-time visualization of system and application metrics. Use when building monitoring dashboards, visualizing metrics, or creating operational observability interfaces.

skill

Matplotlib

Foundational plotting library. Create line plots, scatter, bar, histograms, heatmaps, 3D, subplots, export PNG/PDF/SVG, for scientific visualization and publication figures.

skill

Scientific Visualization

Create publication figures with matplotlib/seaborn/plotly. Multi-panel layouts, error bars, significance markers, colorblind-safe, export PDF/EPS/TIFF, for journal-ready scientific plots.

skill

Seaborn

Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.

skill

Shap

Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Query Writing

For writing and executing SQL queries - from simple single-table queries to complex multi-table JOINs and aggregations

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Scientific Visualization

Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.

skill

Skill Information

Category:Skill
Last Updated:1/14/2026