Recommend Indexes
Purpose
Produce a deliberate index set for a workload: every index justified by queries it serves, overlaps consolidated, dead indexes removed, and the write-side cost stated. The result is fewer, better indexes rather than one per complaint.
When to use
- A new table, schema or feature needs its initial indexes from known access patterns.
- A table has many indexes, slow writes or large storage, and reads still scan.
- The engine's missing-index suggestions or an advisor report must be evaluated before applying.
When not to use
- One specific query is slow and its plan is available. Use
query-optimization.
- The table structure itself (keys, normalization, partitioning) is being designed. Use
database-schema-design.
- Deploying the chosen index changes safely on a large live table. Use
schema-migration-plan.
Inputs
Required:
- The database engine and the table definitions with existing indexes.
- The workload: top queries by total cost or frequency (text or described predicates, joins, sorts) and the DML pattern (insert/update/delete rates).
Optional:
- Index usage statistics (seeks, scans, lookups, updates), table sizes and growth, missing-index suggestions, latency targets, maintenance windows, replication setup.
If the workload is unknown, ask for the top queries; do not design indexes from the schema alone except as [ASSUMPTION]-marked starting points.
Process
- Profile the workload: list queries with frequency, cost and latency target; mark the table as read-heavy, write-heavy or mixed. Label inferred patterns
[ASSUMPTION].
- Extract per query the access pattern: equality predicates, range predicates, join columns,
ORDER BY/GROUP BY, selected columns and selectivity of each predicate.
- Group queries that share leading equality columns; design one index per group rather than per query.
- Order key columns: equality columns first (most selective or most shared first), then the range or sort column; add columns needed only for output as included/non-key columns when the engine supports it, to avoid lookups on hot paths.
- Consider specialized forms where justified: filtered/partial indexes for hot subsets (e.g. open orders), unique indexes that also enforce business keys, composite indexes that support foreign key checks and cascades, and engine-specific types (full-text, spatial, inverted, columnar) for analytics or search patterns.
- Review existing indexes: flag unused (no reads since a representative period, including month-end jobs), duplicate, and left-prefix redundant indexes; propose merges.
- Estimate the write-side cost of the final set: extra writes per insert/update, lock and log volume, page splits for non-sequential keys, storage size, and maintenance/rebuild time.
- Evaluate engine suggestions critically: merge them into the grouped design instead of applying them one by one; reject those serving rare queries on write-heavy tables.
- Define the rollout: create before drop, online creation where supported, disable or make invisible before dropping when possible, and monitoring of target queries and write latency.
- Fill the output template. If the goal continues, suggest
schema-migration-plan to deploy the changes, query-optimization for queries still slow afterwards or capacity-planning if storage growth is significant.
Output format
# Index Recommendation: <table/schema>
Engine: <...> | Workload profile: <read-heavy/write-heavy/mixed> | Period of stats: <...>
## Workload Summary
| Query / pattern | Frequency | Predicates (eq / range) | Sort / group | Current access |
|---|---|---|---|---|
## Proposed Index Set
| Action (create/alter/drop/keep) | Index | Key columns | Included | Filter | Serves queries | Write/storage cost |
|---|---|---|---|---|---|---|
## Removed or Merged Indexes
- <index> – <reason: unused since / duplicate of / prefix of>
## Rollout and Monitoring
1. <step> – online? – verification
Monitor: <target queries, write latency, index usage after N days>
## Assumptions and Open Questions
- [ASSUMPTION] ...
Quality checklist
Common pitfalls
- Applying missing-index suggestions one by one; they overlap heavily and ignore write cost.
- Dropping an "unused" index whose usage statistics were reset by a restart or that serves a quarterly job.
- Indexing low-selectivity columns alone (status, flags) instead of combining them with a selective column or using a filtered index.
Example
Input: "orders: 14 indexes, 3k inserts/s, 40 updates/s on status; reports filter by customer_id + created_at range, ops screen shows open orders by warehouse."
Excerpt of output:
- Create
(customer_id, created_at) INCLUDE (status, total) serving 6 report queries; replaces ix_customer and ix_customer_date (left-prefix duplicates).
- Create filtered
(warehouse_id, created_at) WHERE status = 'OPEN' serving the ops screen; small because open orders are ~2% [ASSUMPTION: confirm ratio].
- Drop 4 indexes with zero reads over 35 days including month end; net result 9 indexes, fewer writes per insert.
1---2name: index-recommendation3description: "Recommends indexes for a table or database from its real workload: groups queries by access pattern, designs key column order, included columns, filtered/partial indexes, consolidates overlapping and removes unused indexes, and weighs read gains against write amplification, storage and maintenance cost. Use when designing indexes for a new schema or feature, reviewing an over-indexed or under-indexed table, or acting on missing-index suggestions from the engine."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/index-recommendation7 version: "1.0.0"8 language: en9 category: 08-data10 role: dba11 area: database12 title: "Recommend indexes"13 related: "query-optimization, database-health-check, schema-migration-plan, database-schema-design, capacity-planning"14 prompt: "Our orders table has 14 indexes, inserts are getting slow and some reports are still scanning. Here are the top 20 queries and index usage stats; recommend an index set."15---1617# Recommend Indexes1819## Purpose20Produce a deliberate index set for a workload: every index justified by queries it serves, overlaps consolidated, dead indexes removed, and the write-side cost stated. The result is fewer, better indexes rather than one per complaint.2122## When to use23- A new table, schema or feature needs its initial indexes from known access patterns.24- A table has many indexes, slow writes or large storage, and reads still scan.25- The engine's missing-index suggestions or an advisor report must be evaluated before applying.2627## When not to use28- One specific query is slow and its plan is available. Use `query-optimization`.29- The table structure itself (keys, normalization, partitioning) is being designed. Use `database-schema-design`.30- Deploying the chosen index changes safely on a large live table. Use `schema-migration-plan`.3132## Inputs33Required:34- The database engine and the table definitions with existing indexes.35- The workload: top queries by total cost or frequency (text or described predicates, joins, sorts) and the DML pattern (insert/update/delete rates).3637Optional:38- Index usage statistics (seeks, scans, lookups, updates), table sizes and growth, missing-index suggestions, latency targets, maintenance windows, replication setup.3940If the workload is unknown, ask for the top queries; do not design indexes from the schema alone except as `[ASSUMPTION]`-marked starting points.4142## Process431. Profile the workload: list queries with frequency, cost and latency target; mark the table as read-heavy, write-heavy or mixed. Label inferred patterns `[ASSUMPTION]`.442. Extract per query the access pattern: equality predicates, range predicates, join columns, `ORDER BY`/`GROUP BY`, selected columns and selectivity of each predicate.453. Group queries that share leading equality columns; design one index per group rather than per query.464. Order key columns: equality columns first (most selective or most shared first), then the range or sort column; add columns needed only for output as included/non-key columns when the engine supports it, to avoid lookups on hot paths.475. Consider specialized forms where justified: filtered/partial indexes for hot subsets (e.g. open orders), unique indexes that also enforce business keys, composite indexes that support foreign key checks and cascades, and engine-specific types (full-text, spatial, inverted, columnar) for analytics or search patterns.486. Review existing indexes: flag unused (no reads since a representative period, including month-end jobs), duplicate, and left-prefix redundant indexes; propose merges.497. Estimate the write-side cost of the final set: extra writes per insert/update, lock and log volume, page splits for non-sequential keys, storage size, and maintenance/rebuild time.508. Evaluate engine suggestions critically: merge them into the grouped design instead of applying them one by one; reject those serving rare queries on write-heavy tables.519. Define the rollout: create before drop, online creation where supported, disable or make invisible before dropping when possible, and monitoring of target queries and write latency.5210. Fill the output template. If the goal continues, suggest `schema-migration-plan` to deploy the changes, `query-optimization` for queries still slow afterwards or `capacity-planning` if storage growth is significant.5354## Output format55```markdown56# Index Recommendation: <table/schema>57Engine: <...> | Workload profile: <read-heavy/write-heavy/mixed> | Period of stats: <...>5859## Workload Summary60| Query / pattern | Frequency | Predicates (eq / range) | Sort / group | Current access |61|---|---|---|---|---|6263## Proposed Index Set64| Action (create/alter/drop/keep) | Index | Key columns | Included | Filter | Serves queries | Write/storage cost |65|---|---|---|---|---|---|---|6667## Removed or Merged Indexes68- <index> – <reason: unused since / duplicate of / prefix of>6970## Rollout and Monitoring711. <step> – online? – verification72Monitor: <target queries, write latency, index usage after N days>7374## Assumptions and Open Questions75- [ASSUMPTION] ...76```7778## Quality checklist79- [ ] Every proposed index lists the queries it serves; no index serves nothing.80- [ ] Column order follows equality, then range/sort, and matches the grouped patterns.81- [ ] Overlapping, duplicate and unused indexes are addressed, with usage period checked against periodic jobs.82- [ ] Write amplification and storage cost are stated for the final set.83- [ ] Rollout creates before it drops and names monitoring after the change.84- [ ] Assumed workload patterns are labeled; no usage numbers are invented.85- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.8687## Common pitfalls88- Applying missing-index suggestions one by one; they overlap heavily and ignore write cost.89- Dropping an "unused" index whose usage statistics were reset by a restart or that serves a quarterly job.90- Indexing low-selectivity columns alone (status, flags) instead of combining them with a selective column or using a filtered index.9192## Example93Input: "orders: 14 indexes, 3k inserts/s, 40 updates/s on status; reports filter by customer_id + created_at range, ops screen shows open orders by warehouse."9495Excerpt of output:96- Create `(customer_id, created_at) INCLUDE (status, total)` serving 6 report queries; replaces `ix_customer` and `ix_customer_date` (left-prefix duplicates).97- Create filtered `(warehouse_id, created_at) WHERE status = 'OPEN'` serving the ops screen; small because open orders are ~2% `[ASSUMPTION: confirm ratio]`.98- Drop 4 indexes with zero reads over 35 days including month end; net result 9 indexes, fewer writes per insert.