Review a Proposed Database Index Against the Real Query Patterns Before Adding It

You are a database engineer reviewing a proposal to add an index. Your job is to decide whether the index will help the queries that actually run, and what it will cost.

I will provide: (1) the proposed CREATE INDEX statement, (2) the table definition, (3) the top queries touching this table with their approximate frequency, (4) the EXPLAIN output for the slow query if I have it, and (5) the table's approximate row count and write rate.

Work through these steps in order and show your reasoning for each:

1. Map each provided query to the proposed index. For every query, state whether the planner can use the index, which columns it can use for equality, range, and sort, and where column order prevents use (for example, a range condition on an earlier column stops later columns from narrowing the scan).
2. Check selectivity. Say which columns are likely to be selective and which are not, and what you would need to know to be sure (distinct counts, skew). Do not assume uniform distribution without saying so.
3. Look for an existing index that already covers the need, or that the new one would make redundant (a prefix of another index). Recommend extending an existing index instead of adding one when that works.
4. Estimate the cost: extra write amplification given the write rate, storage, and the effect on bulk loads or backfills. Name which operations get slower.
5. Identify what you cannot determine from the input, and list the exact commands I should run to find out (EXPLAIN ANALYZE, a distinct-count query, an index usage statistics query).
6. Give a verdict: add as proposed, add with a modified column order or partial condition, extend an existing index, or do not add. Include the rollout approach for a live table (building without blocking writes where the database supports it) and the measurement that will confirm the index is used and helped.

Do not claim a performance improvement as certain. Express expected gains in terms of what the plan will change (index scan instead of sequential scan, removed sort) and mark every number you give as an estimate.

When to use it: before merging a migration that adds an index, or when a teammate proposes one in response to a slow-query alert.

How to adapt it: name your database engine and version so syntax and feature advice (partial indexes, covering columns, online builds) is accurate. For read-heavy analytics tables, tell the model to weight write cost lower.

Example input: a CREATE INDEX on orders (status, created_at), a table with 40 million rows, a query that filters on customer_id and sorts by created_at, and one that filters on status = 'pending'.

Good output: notes that the first query cannot use the index efficiently because customer_id is not a leading column, suggests (customer_id, created_at) instead, observes that status has few distinct values so a partial index on pending rows may serve the second query more cheaply, lists the verification commands, and gives a concrete build-and-measure plan.

Sign in to upvote this prompt.