# Query And Index Tuning

> Tune queries with evidence: read EXPLAIN (ANALYZE, BUFFERS) first, choose the right index type and column order (B-tree/partial/composite/covering/GIN), rewrite to be sargable, kill N+1 at the SQL level, and keep statistics fresh.

- Skill: `mcorbett51090/query-and-index-tuning` (Agent Skill)
- Install (CLI): `npx get-skill add mcorbett51090/query-and-index-tuning`
- Raw SKILL.md: https://api.skillmd.com/api/skills/mcorbett51090/query-and-index-tuning/raw
- Safety review: pending (external: skill-scanner PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: mcorbett51090 (https://skillmd.com/u/mcorbett51090)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/mcorbett51090/query-and-index-tuning

---


# Query & Index Tuning

## Read the plan first
`EXPLAIN (ANALYZE, BUFFERS)` — find seq scans, bad row estimates, expensive sorts/joins. Tune what it shows.

## Right index
| Predicate | Index |
|---|---|
| equality / range | **B-tree** |
| filtered subset | **partial** |
| multi-column (order by selectivity) | **composite** |
| avoid heap fetch | **covering** (INCLUDE) |
| jsonb / full-text | **GIN** |

## Or fix the query
Make it **sargable** (no function on the indexed column), avoid `SELECT *`, batch don't loop, kill N+1 with a join/`IN`.

## Stats
Stale stats cause bad plans — `ANALYZE`/autovacuum tuning fixes more than another index.

