AI Agent Hub
Back to skills
SQL Query Optimizer icon

SQL Query Optimizer

Development Updated 2026.08.30

Paste the following prompt into your AI chat to install this skill:

Please install @user_c1ff727a/sql-pro-v2 following https://skillhub.cn/install/skillhub.md.

About this skill

Problem

Slow query diagnosis often gets stuck between adding an index, rewriting the query, and tuning the database. Function-wrapped columns such as YEAR(created_at) or LOWER(name), leading-wildcard LIKE '%prefix', correlated subqueries, deep OFFSET pagination, and large UPDATE / DELETE transactions can all make execution plans diverge from expectations.

How It Works

The skill works with SELECT, INSERT, UPDATE, DELETE, and DDL statements, optionally using database type, table schemas, index lists, execution plans, and performance goals. Key steps:
- Gather context: identify missing schema, indexes, EXPLAIN output, and statistics for MySQL, PostgreSQL, SQL Server, Oracle, SQLite, or MariaDB.
- Detect anti-patterns: scan filtering, joins, subqueries, sorting, pagination, aggregation, set operations, locking, and DML issues, labeled by severity.
- Explain execution plans: analyze signals such as seq scan, filesort, Using temporary, Sort Method: external merge, Key Lookup, and TABLE ACCESS FULL to locate the cost driver.
- Produce structured output: return a Problem / Impact / Suggestion table, a rewritten SQL query, and index recommendations such as B-tree, composite, partial, or expression indexes.

Boundaries

It is useful for query diagnosis, rewrite suggestions, and index strategy discussion, but it does not replace validation in a real data environment. Index benefit depends on row count, selectivity, concurrency, buffer pool, and statistics; some rewrites change semantics, performance characteristics, or maintenance cost, so each change should be evaluated before production use.

Use Cases

  • When a slow-query alert fires, use `EXPLAIN` to distinguish full scans, filesorts, and lock waits.
  • Review order SQL to spot correlated subqueries, deep pagination, and wrapped columns, then propose rewrites.
  • Design composite indexes for report endpoints covering filters and sort keys to reduce temp-disk spills.
  • Check batch `UPDATE` and `DELETE` jobs to see if they need chunking to avoid long transactions and lock escalation.

Best For

  • Backend engineers troubleshooting online endpoints who need to map slow SQL to anti-patterns and plan metrics.
  • Data engineers writing report queries who need to optimize filtering, sorting, aggregation, and index advice.
  • DBAs reviewing schema changes who need to assess index benefit, lock impact, and rewrite risk.
  • Engineers maintaining legacy order systems who need to audit correlated subqueries, deep pagination, and bulk DML.