language-sql
Installation
SKILL.md
SQL
Query optimization basics
- Start with the access pattern: filters, joins, grouping, sorting, and result size.
- Index columns used for selective
WHERE, join keys, and stableORDER BYclauses. - Prefer narrow projections over
SELECT *; return only the columns callers need. - Avoid accidental row multiplication in joins; check cardinality before adding
DISTINCT. - Watch for N+1 query loops at application boundaries.
EXPLAIN and EXPLAIN ANALYZE
- Use
EXPLAINto inspect the planned access path before changing indexes or query shape. - Use
EXPLAIN ANALYZEwhen you need actual timing and row counts; run it against safe data and statements. - Compare estimated vs actual rows. Large gaps often mean stale statistics, skewed data, or missing predicates.
- Optimize the highest-cost operation first, but confirm the full query got faster.