sql-optimizerlisted
Install: claude install-skill SilviaAre95/wayworks
# SQL Optimizer
Optimize the SQL for: **$ARGUMENTS**
## Steps
1. **Parse the query** — Read the SQL and understand:
- What data is being requested
- Join structure and cardinality
- Filter conditions and their selectivity
- Aggregations and groupings
- Subqueries and CTEs
2. **Identify performance issues**:
| Issue | Symptom | Fix |
|-------|---------|-----|
| Full table scan | No WHERE clause index match | Add index or rewrite filter |
| SELECT * | Fetching unused columns | Select only needed columns |
| Correlated subquery | Subquery runs per row | Rewrite as JOIN or CTE |
| Implicit type cast | Filter on wrong type | Cast explicitly or fix schema |
| Missing JOIN index | Slow JOIN on unindexed column | Add index on join column |
| Unnecessary DISTINCT | Masking a join issue | Fix the join instead |
| ORDER BY + LIMIT without index | Sort on full result set | Add composite index |
| N+1 in application | Multiple queries in loop | Use JOIN or IN clause |
3. **Rewrite the query** — Produce an optimized version with:
- Clear CTEs instead of nested subqueries
- Proper index utilization
- Minimal data fetched
- Comments explaining non-obvious choices
4. **Suggest schema changes** if needed:
- Indexes that would help this query
- Materialized views for expensive aggregations
- Partitioning for large table scans
5. **Estimate impact** — Qualitative assessment:
- "This eliminates a full table scan on a ~1M