Home/Labs/Index Lookup Costs
All 280 Labs
INTERACTIVE LAB🎯

Index Lookup Costs Lab (Interactive)

Trace primary-key, double-lookup, and covering-index reads across a growing B+Tree. Grow the table exponentially, change the query shape, and count the exact B+Tree descents, heap fetches, and write tax each index type charges per read.

Clustered · Secondary · Covering — Count the Page Reads

Same table, three physical access paths; every double-lookup is a real second descent down the B+Tree.

SELECT id, email, name, balance FROM users WHERE email = 'alice@ex.com';
Clustered index (PRIMARY KEY id) — the table itself
leaf: [1042 | alice@ex.com | Alice | $420 | …all columns]
leaf: [1043 | bob@ex.com | Bob | $890 | …]

Leaf pages physically store the full row in PK order — one descent returns everything.

Secondary index on email INCLUDE (name, balance)
['alice@ex.com' → PK 1042 | Alice | $420]
['bob@ex.com' → PK 1043 | Bob | $890]
Execution trace
→ email tree (5 pages) ⇒ PK 1042 ⇒ 💥 DOUBLE-LOOKUP: heap/clustered re-descent (5 more pages) for name/balance. 10 page reads total.
Page reads
10
Est. latency
900 µs
Write tax / insert
0.48 ms

How It Works Under the Hood

Every read pays in page reads. A primary-key lookup descends the clustered B+Tree once and the leaf holds the row itself. A secondary index makes you pay twice — descend the index tree, then fetch the row by PK pointer from the clustered leaf. A covering index copies the requested columns into its own leaves, enabling index-only scans that skip the table trip. But each secondary index stores the PK per entry and is maintained on every write, so index count becomes a write-amplification tax.

Core Architectural Principles

  • B+Tree height grows logarithmically in fanout, so even hundred-million-row tables resolve in 3–4 page reads.
  • Secondary indexes store PK pointers, forcing a double-lookup into the clustered index for any non-covered column.
  • Covering indexes with INCLUDE enable index-only scans but multiply write cost by the number of trees maintained per mutation.
Interview Round Script

Read execution plans with a cost model in your head: distinguish index-only scan, index scan, and heap fetch, and justify a covering index on hot read paths where random heap pages dominate latency. Warn that extra index count taxes write-heavy tables, and offer partial indexes to cut both RAM and write cost when a WHERE predicate is highly selective.

Key Trade-Offs

More covering indexes make reads single-descent affairs but charge every INSERT and UPDATE a maintenance round through each tree.

Related Curriculum Chapter

Index Types (Clustered, Non-Clustered, Composite, Covering)

Read Full Chapter Blueprint

Explore More Interactive Labs

View All 280 Labs