Blog
IntermediateReferenceSQL Server Performance Internals

Index design for real workloads

How the optimiser uses a B-tree index - seeks versus scans, composite key order, covering and included columns - and the write cost every index charges, so indexes are designed for the workload rather than collected.

Applies to
SQL Server2016-2025 (13.x-17.x)
matureUpdated Reviewed
intermediateAssumes working familiarity; skips the fundamentals.

What you will be able to do

  • Explain seek, scan, and key lookup in terms of what pages are actually read.
  • Design a composite key so the leftmost columns serve the equality predicates first.
  • Decide between a wider key and an INCLUDE list, and say what each choice costs.
  • State what an index charges on INSERT, UPDATE, and DELETE, and justify deleting an index that does not earn it.

Concepts covered

An index is a sorted copy of some of your columns, stored as a B+ tree, that exists to answer “where are the rows that match” without reading the whole table. That definition is the whole theory; every design rule is a consequence of it. The B-tree is ordered on its key columns, so a query can binary- search for a value in the key’s leftmost columns - and only those.

The analogy is a printed telephone directory versus a library card catalogue. A directory sorted by surname then first name lets you jump straight to “Smith, John” - the seek - but finding everyone called John means reading every page, because the first name is not the primary sort. The card catalogue is the nonclustered index: the card says the book exists and where it sits, and fetching the book itself is the second trip - the key lookup. A covering index is the card that also prints the abstract, so nobody walks to the shelf at all.

Seek, scan, lookup

Three access patterns cover most of what a plan does with an index:

  • Seek - the engine descends the tree using the leftmost key columns and lands on the matching page. Cheap, scales with tree depth not table size.
  • Scan - the engine reads every leaf page. Sometimes the honest choice (most of the table is needed anyway); sometimes the symptom of an index that cannot serve the predicate.
  • Key lookup - the nonclustered index found the row’s pointer but the query needs columns the index does not hold, so the engine makes a second trip to the clustered index or heap for the rest of the row. One lookup is invisible; a million of them is a second query hiding inside the first.

What an index does to one query

  1. Predicate

    A filter on one or more columns

    Only the leftmost key columns can be used to seek - a predicate on the third column alone cannot use the tree

  2. Seek or scan

    Engine walks the tree if the key leads with your predicate columns

    A scan is not automatically bad - it is bad when it reads far more than the query needed

  3. Cover or lookup

    If the index holds every column the query needs, the lookup never happens

    INCLUDE columns ride along on the leaf pages without affecting key order

  4. Result

    Rows returned

    The write cost is already paid - every modification touched the index too

The index either answers the whole query, or it answers where to look and sends you back for the rest.

The key-order rule that matters

A composite index (A, B, C) can seek on A, on A+B, or on A+B+C. It cannot seek on B alone or C alone - the tree is sorted on A first. Design order:

  1. Equality columns first, in the order they are most discriminating (the column that filters the most rows leads).
  2. Range and sort columns last - a range on the leftmost edge stops the seek from reaching the columns after it.
  3. INCLUDE for the rest - columns needed only in the output, not for seeking or sorting, go in the leaf pages via INCLUDE, where they cost nothing in the key and never force a lookup.
-- Serves: WHERE tenant_id = @t AND status = 'open' ORDER BY created_at
CREATE NONCLUSTERED INDEX ix_orders_tenant_status
ON dbo.orders (tenant_id, status, created_at)
INCLUDE (total_amount);

That single index seeks on the equality pair, returns rows already ordered for the ORDER BY, and never does a key lookup for total_amount. It is one index doing the work of three.

Every index is a write tax

The read benefit is obvious; the write cost is what people forget:

Operation What an index adds
INSERT A new key entry in every index, possibly a page split if the key lands in a full page
UPDATE of a key column A delete plus an insert in that index - it is a move, not an update
UPDATE of a non-key column Still touches the index if the column is in an INCLUDE list
DELETE The entry must be removed from every index, and ghost cleanup has its own cost

Five narrow indexes that each serve a different read are defensible. Five indexes that all start with the same leading column and differ only in what they INCLUDE are usually one index designed carefully. The audit sys.dm_db_index_usage_stats shows reads and writes per index; an index with heavy writes and almost no seeks is paying rent it never earns - and dropping it is the only fix that costs nothing at query time.

What does not help

  • An index on a low-cardinality column alone - a status column with three values filters too little to justify a seek; it earns a place only as part of a composite.
  • A function-wrapped predicate - WHERE YEAR(created_at) = 2025 cannot seek the index on created_at. Rewrite as a range (>= '2025-01-01' AND < '2026-01-01') so the key can be used.
  • More indexes to fix a bad plan - when the estimate is wrong, the index is innocent. Go to the statistics lesson; adding an index there is treating the symptom.

Guided practice

A beginner exercise, worked in a scratch database. The scenario is the lesson’s own: an orders table queried by WHERE tenant_id = @t AND status = 'open' ORDER BY created_at, also returning total_amount. Work each step and check yourself before moving on.

  1. Build the table. Create dbo.orders with tenant_id int, status varchar(20), created_at datetime, total_amount decimal(10,2) and a key column, then insert a few thousand rows spread across several tenants and statuses. Check: SELECT COUNT(*) FROM dbo.orders returns your rows, spread across more than one tenant. An empty or tiny table makes every plan trivially cheap and proves nothing - the point is to watch the access pattern change.
  2. Run the query with no supporting index. Run the predicate query with the actual execution plan visible and note which operator reads the table. Check: a scan - no sorted path to your predicate exists, so every leaf page is read. If you see a seek you did not create, check what the table’s existing key covers; the lesson’s claim is that only leftmost key columns can be used to seek.
  3. Create the lesson’s index. CREATE NONCLUSTERED INDEX ix_orders_tenant_status ON dbo.orders (tenant_id, status, created_at) INCLUDE (total_amount); Check: the key is equality columns first (tenant_id, status), the sort column last (created_at), and total_amount in INCLUDE rather than the key. If you put total_amount in the key, revisit the key-order rule - output-only columns ride on the leaf pages and add nothing useful to the key.
  4. Re-run the same query and compare plans. Same text, same parameters, actual plan on. Check: a seek on the equality pair, rows already ordered for the ORDER BY so no sort operator, and no key lookup - total_amount comes straight off the leaf pages. If a lookup or sort remains, re-check the index definition against the query text; this one index is meant to do the work of three.
  5. Audit the write tax. Run a few INSERT and UPDATE statements against the table, then query sys.dm_db_index_usage_stats for reads and writes per index. Check: every modification added writes to the index - that is the tax. An index with heavy writes and almost no seeks is paying rent it never earns, and dropping it is the only fix that costs nothing at query time; if your reads are zero here, that is the expected result on a table only you have written to.

Check your understanding

Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.

  1. What is an index, in one sentence? A sorted copy of some of the table’s columns, stored as a B+ tree, that answers “where are the rows that match” without reading the whole table. Every design rule follows from that ordering - if the definition was fuzzy, reread the opening.
  2. What is a key lookup, and when does it become expensive? The nonclustered index found the row’s pointer but not every column the query needs, so the engine makes a second trip to the clustered index or heap for the rest of the row. One is invisible; a million is a second query hiding inside the first - an INCLUDE column removes the trip entirely.
  3. Why can an index on (A, B, C) seek on A and A+B but not on B alone? The tree is sorted on A first - within equal A values it is sorted on B, so a predicate that skips A has no ordered path to descend. Only the leftmost key columns can be used to seek; if this surprised you, revisit the key-order rule.
  4. Why does WHERE YEAR(created_at) = 2025 defeat the index on created_at, and what is the rewrite? The function wraps the column, hiding the key’s order behind a computation - the engine can no longer binary-search the tree. Rewriting as a range (>= '2025-01-01' AND < '2026-01-01') exposes the key again. Function-wrapped predicates are in the “what does not help” list.
  5. sys.dm_db_index_usage_stats shows an index with heavy writes and almost no seeks. What is the diagnosis and the fix? The index is paying write tax - an entry on every insert, a move on key updates, a removal on delete - without earning any read benefit, so dropping it is free at query time. The caveat: confirm the reads really are absent across a representative window, because the audit shows usage, not intent. If you wanted to keep it “just in case”, revisit the write-tax table.

Where SQL Server sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Deployed with

Commonly run alongside

  • Windows Failover Clustering

Alternatives

Solves a similar problem differently