Prompt Engineering SQL Indexes

Missing Index Analysis — Which Indexes, at What Cost

Map every predicate, join, and sort to the index that serves it — or doesn't. Composite order rules, covering decisions, and the write tax nobody mentions.

Overview

Missing-index analysis goes wrong in both directions: indexes that were never needed, and indexes designed with the wrong column order that never get used. This prompt uses the index strategy goal: map every predicate, join column, and sort to the index (or absence) that serves it; design composites by the rules — equality columns first, then ranges, then order-by, because column order decides usability; consider covering with its storage and maintenance price; and count the cost honestly — every index taxes every write, and an index serving one slow report may not be worth slowing every insert. With no index list provided, the contract refuses to assume: checking what exists is step one.

How to use this resource

  1. Check what exists first

    No assumed indexes: the current state is evidence to gather, and near-miss indexes may just need extending.

  2. Map clauses to indexes

    Every predicate, join, and sort accounted for — no index without a clause it serves.

  3. Pay the write tax knowingly

    Each candidate states which inserts and updates it slows — and whether the workload affords it.

Why This Works

  • Clause mapping replaces index folklore with checkable claims
  • Composite order rules prevent the index that exists but never gets used
  • Write-tax honesty separates index strategy from index hoarding

Best for

  • Endpoints slow under load with unverified index suspicions
  • Tables that accumulated overlapping indexes over years
  • Write-heavy workloads where index costs are real

Not for

  • Reading an existing plan's index choices — that's the Execution Plan Analysis goal
  • Schema redesign — the contract's non-goals rule it out of scope

Use cases

  • Working out which indexes a slow lookup workload is missing
  • Ordering composite index columns correctly
  • Deciding whether a covering index is worth its maintenance

FAQ

How does the prompt decide the column order for a composite index on region, status, and last_order_at?

It applies one ordering rule: equality columns first, then ranges, then order-by. For the customers lookup, region and status are equality predicates, so they lead; last_order_at drives the ORDER BY DESC, so it trails. The prompt justifies that sequence because column order decides whether the index gets used at all, not merely how large it is.

When is adding an index not worth it even though it speeds up the query?

When its write tax outweighs the read gain. The prompt's cost rule flags that every index slows every insert, so an index serving one slow report may not justify taxing every write. Each candidate must state which inserts and updates it slows and weigh that against write amplification, storage, and maintenance before it recommends anything.

Can the prompt recommend indexes if I don't paste my existing index list or execution plan?

It can, but it refuses to assume any index or plan exists. With no index list, step one becomes checking the current ones, since a near-miss index that could be extended beats a new overlapping one; with no plan, it hands you the command to capture your engine's actual-execution-plan facility. Plan-dependent conclusions stay flagged pending that evidence.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

Projects that use this resource

Workflows that use this resource

Guides for this resource

Tip: Save time by exploring related resources and tools that integrate with this resource.