SysDesignAtlas

Database Indexing

How B-tree indexes actually work, composite index ordering, covering indexes, and why the wrong index is often worse than none at all.

HLDMedium13 min readindexdatabasequeryb-tree
Confidence

#The core idea

Without an index, finding the rows that match a condition means reading every single row and checking each one — a full table scan. With an index, the database consults a sorted structure on the side and jumps straight to the matching rows. It's the difference between reading a book cover to cover and using its index.

In cost terms: a scan is O(n) — work grows in step with the number of rows — while an indexed lookup is O(log n), which barely grows at all.

At 100 million rows that's the difference between reading 100 million entries and reading about 27. This is why "add an index" is frequently a 1000× improvement for a few minutes of work, and why it's the first thing to try before any architectural change.

The cost, always worth stating: indexes make reads faster and writes slower. Every insert, update, or delete must maintain every index on the table. They also consume disk and — more importantly — RAM, since an index that doesn't fit in memory loses much of its advantage.

#B+ trees: the default

Nearly every relational index is a B+ tree. The properties that matter:

                    [ 50 | 100 ]              ← internal nodes:
                   /      |     \                keys only, for routing
          [10|30]     [60|80]    [120|150]
          /  |  \      /  |  \     /  |  \
       leaves: sorted key → row-pointer, linked left-to-right
       [..]→[..]→[..]→[..]→[..]→[..]→[..]→[..]
  • All values live in leaves; internal nodes only route. This keeps internal nodes small so more of them fit in memory.
  • Leaves are linked, which makes range scans fast: find the start, then walk sideways. This is why B+ trees serve WHERE created_at BETWEEN ... and ORDER BY so well.
  • High fan-out — each node points at hundreds of children rather than two, so the tree is wide and shallow. With ~100–500 keys per node, four levels addresses billions of rows. That's ~4 disk reads worst case, and the top levels are almost always cached.
  • Self-balancing — depth stays uniform as data changes, so performance is predictable.

Consequence to remember: because B-trees are sorted, they support equality, ranges, prefix matching, and ordering. Hash indexes support equality only — faster for exact lookups, useless for anything else, which is why they're rarely the default.

#Clustered vs secondary

Clustered index — the table is the index; rows are physically stored in the index's order. There can be only one, because rows can only be physically sorted one way. In InnoDB (MySQL's default storage engine) this is the primary key. Postgres works differently: rows go into a separate unordered area called the heap, and every index — including the primary key — just points into it, so there's no true clustered index.

Secondary index — a separate structure that maps the indexed column to a pointer telling you where the actual row lives. Following that pointer is a second lookup, sometimes called a bookmark lookup.

The performance consequence in InnoDB-style engines:

   SELECT * FROM users WHERE email = 'x@y.com'

   1. Search secondary index on email     → finds primary key 4711
   2. Search clustered index for PK 4711  → finds the actual row
      ("bookmark lookup" / double lookup)

Two tree traversals, not one. This makes covering indexes (below) valuable, and it explains why a random UUID primary key hurts in InnoDB: rows are physically stored in PK order, so each random key has to be squeezed into the middle of the table. When the target page (the fixed-size block on disk) is already full, the database splits it into two half-empty pages — a page split. Over time the table ends up full of half-empty pages scattered across the disk (fragmentation), wasting space and slowing scans. Sequential-ish keys (auto-increment, Snowflake IDs, UUIDv7) keep inserts appending to the end. That detail is a great one to raise when a design uses UUIDs.

#Dense vs sparse

A second way to classify indexes, by how many rows get an entry.

Dense index — one entry for every row (or every distinct key). Any lookup goes straight to its row. Every secondary index is dense: the rows aren't stored in that column's order, so there's no way to skip any.

Sparse index — one entry per block of rows, not per row. It only works when the rows are stored sorted by the indexed key, because then a block's first key tells you the range it covers.

   Data stored sorted by key, in blocks:
     block 1: 3, 5, 8, 12      block 2: 15, 19, 22, 30      block 3: 34, 41, 47, 52

   DENSE index (every key)          SPARSE index (first key of each block)
     3 → b1   15 → b2   34 → b3       3 → block 1
     5 → b1   19 → b2   41 → b3      15 → block 2
     8 → b1   22 → b2   47 → b3      34 → block 3
    12 → b1   30 → b2   52 → b3

   Find 22 with the sparse index: the largest entry ≤ 22 is 15,
   so read block 2 and scan it.
DenseSparse
SizeOne entry per row — largeOne entry per block — tiny, often fits in memory
LookupDirectFind the block, then scan within it
Needs sorted data?NoYes — sorted by the index key
Where you see itSecondary indexesThe clustered order of a table; the index blocks of a B+ tree; SSTables in LSM-tree engines; Kafka's segment index

Most real structures mix the two: a B+ tree's upper levels are sparse (one entry per child page), and its bottom level is dense. An LSM engine keeps a small sparse index per file in memory and reads one block from disk per lookup — that's how it holds billions of keys with a modest amount of RAM.

The point to make in an interview: "Sparse indexes trade a short scan for a much smaller index, and they only work on sorted data. That's why storage engines keep data sorted — the sparse index fits in memory."

#Composite indexes and the leftmost rule

An index on (a, b, c) sorts by a, then b within equal a, then c. This has a sharp consequence:

   INDEX (country, city, age)

   ✓ WHERE country='IN'                          uses index
   ✓ WHERE country='IN' AND city='Pune'          uses index
   ✓ WHERE country='IN' AND city='Pune' AND age=30   fully
   ✓ WHERE country='IN' AND age=30               uses country only,
                                                 then filters age
   ✗ WHERE city='Pune'                           CANNOT use it
   ✗ WHERE age=30                                CANNOT use it

You can only use a prefix of the index. Think of a phone book sorted by (last name, first name): finding everyone named "Priya" requires reading the whole book.

Ordering rule of thumb (a predicate is one condition in the WHERE clause):

  1. Equality predicates first.
  2. Then the range predicate.
  3. Then columns needed only for sorting or output.

A range predicate stops the index from being used for anything after it, so WHERE a = 1 AND b > 5 AND c = 3 uses (a, b) of an (a, b, c) index and filters c by scanning. The reason: within a = 1, entries are sorted by b, and c is only sorted within each single value of b. Across the whole range b > 5 — say b = 6, 7, 8 — the c values restart from the lowest for each b, so there's no single sorted run of c = 3 to jump to.

#Covering indexes

If an index contains every column the query needs, the database answers entirely from the index and never touches the table — an index-only scan. This eliminates the bookmark lookup and is often a 2–10× win.

   SELECT user_id, created_at FROM posts
   WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;

   INDEX (user_id, created_at)   → covering. Never reads the table.

   SELECT user_id, created_at, body FROM posts ...
   INDEX (user_id, created_at) INCLUDE (body)  → still covering

Postgres's INCLUDE (and SQL Server's equivalent) stores extra columns in the leaves without making them part of the sort key. You get coverage, but the upper levels of the tree — the ones every lookup walks through — stay small, because they only hold the sort-key columns.

#When indexes don't help

Volunteering these is a good signal because it shows you understand the planner, not just the syntax:

  • Low selectivity — meaning the condition doesn't narrow things down much. An index on a true/false column where half the rows match is worse than a scan: the database would do one scattered read per matching row, and scattered reads are far slower than reading straight through. Planners know this and will correctly ignore your index.
  • Functions on the column. WHERE LOWER(email) = 'x' can't use an index on email. Fix with an expression index on LOWER(email).
  • Leading wildcards. LIKE '%foo' can't use a B-tree (nothing to seek to). Needs a trigram index (which indexes every three-letter chunk of the text, so it can match pieces from the middle of a string) or a search engine.
  • Type mismatches that force implicit casts. If phone is a text column and you write WHERE phone = 5551234 (a number), the database may convert every row's phone to a number to compare — which is a function on the column, so the index is skipped.
  • OR across different columns — often can't use one index; sometimes the database resolves it by searching two indexes separately and merging the matches (a bitmap OR), sometimes it's better rewritten as a UNION of two queries.
  • Small tables. A scan of 500 rows already in memory beats index overhead.

#The write cost, quantified

Each index roughly adds:

  • Insert: one B-tree insert (a few page reads/writes, possible split).
  • Update: only if the indexed column changed — but changing an indexed column means delete + insert in that index.
  • Delete: removal from every index.

A table with 8 indexes can be 5–10× slower to write than the same table with one. On a write-heavy table this can be the actual bottleneck, and "we should drop the indexes nobody's queries use" is a genuinely common fix.

#Other index types worth naming

  • Hash — O(1) equality, no ranges, no ordering.
  • Bitmap — one bitmap (a row of bits, one per table row) per distinct value; excellent for low-cardinality columns (few distinct values, like region or status) combined with AND/OR in analytical workloads, since combining conditions is just ANDing bitmaps. Poor for high-write OLTP — the transactional workload of many small reads and writes — because each write must update the shared bitmaps.
  • GIN / inverted — maps each token/element to the rows containing it. Powers full-text search, JSONB containment, and array membership.
  • GiST / R-tree / geospatial — multi-dimensional; the basis of "points within this bounding box."
  • Partial index — indexes only rows matching a predicate: WHERE status = 'pending'. If 99% of orders are complete and you only ever query pending ones, the index is 1% of the size and stays in RAM. Underused and very effective.
  • LSM-tree — not a B-tree at all; see Storage Engines for why write-heavy systems use it instead.

#Reading a query plan

The practical skill behind all of this. EXPLAIN ANALYZE and look for:

  • Seq Scan on a large table with a selective predicate → missing or unusable index.
  • Index Scan vs Index Only Scan → the latter means covering; the former means bookmark lookups.
  • Rows estimated vs actual wildly different → stale statistics. The planner chooses a plan using stored summaries of the data (row counts, how values are distributed). If those summaries are out of date — say they were gathered when the table had 1,000 rows and it now has 10 million — the planner is choosing badly. Often the real bug, and fixed by refreshing them (ANALYZE).
  • Sort steps that an index ordering could have avoided.
  • Nested Loop with a large outer relation → usually a missing index on the join key. A nested loop takes each row from the first ("outer") table and looks up matches in the second. With a million outer rows and no index on the join column, that is a million scans of the second table.

Check the plan before adding anything: a large discrepancy between estimated and actual rows usually means stale statistics, and no index fixes that.

#Check yourself

Why is a B+ tree only about four levels deep for billions of rows?Show answer
Each node points at hundreds of children, so the tree is wide and shallow — four levels of 100–500 keys address billions of rows, and the top levels stay cached.
What do B-tree indexes support that hash indexes don't?Show answer
Ranges, prefixes and ordering, because the keys are sorted and leaves are linked. Hash indexes only answer exact equality.
What is the leftmost-prefix rule for a composite index on (a, b, c)?Show answer
Only a prefix of the index can be used: a, then a and b, then all three. A query on b or c alone can't use it, and a range on b stops c from being used.
Why does one index on (user_id, created_at) beat two single-column indexes for a feed query?Show answer
WHERE user_id = ? ORDER BY created_at DESC LIMIT 20 reads the rows already in order from one index, with no sort step.
What is a covering index, and why is it faster?Show answer
One that contains every column the query needs, so the database answers from the index alone and skips the lookup into the table — often a 2–10× win.
Dense versus sparse index: what's the trade, and what does a sparse index require?Show answer
Dense has one entry per row and goes straight to it; sparse has one entry per block — tiny, often in memory — then scans the block. Sparse only works on data sorted by the key.
Name three reasons a query might ignore an index you created.Show answer
Low selectivity (half the rows match), a function on the column like LOWER(email), a leading wildcard (LIKE '%foo'), an implicit type cast, or a table small enough that scanning is cheaper.
A table has eight indexes and writes are slow. What's going on?Show answer
Every insert, delete and indexed-column update must maintain every index, which can make writes 5–10× slower. Drop indexes no query uses — databases expose usage statistics to find them.

#What to carry forward

  • An index turns an O(n) scan into an O(log n) lookup — and taxes every write and some RAM.
  • B+ trees are sorted and shallow, so they serve equality, ranges and ordering.
  • Order composite indexes equality first, then range, then sort or output columns; only a prefix is usable.
  • Covering indexes skip the table; partial indexes keep hot subsets small; sparse indexes rely on sorted data.
  • Read the query plan before adding anything — stale statistics are often the real bug.
00:00
/ 13m