Skip to content

Lesson 2 of 9 · 26m

B-Tree Indexes in Practice

B-trees are still the default index because they are predictable. Learn their shape, their costs, and why most indexing mistakes are really selectivity mistakes.

Most application developers treat indexes as fairy dust: sprinkle CREATE INDEX on a slow query and hope. B-trees are more boring and more useful than that. They are balanced trees of sorted keys that trade write amplification and storage space for fast lookup, range scans, and ordering.

What a B-tree actually is

A B-tree node is usually one database page. Keys inside it are sorted, and child pointers partition the key space. The tree stays shallow because each internal node can hold hundreds or thousands of separators.

                 [40 | 80]
                /    |            [10|25]   [55|70]   [90|120]
         / | \     / | \     /  |  \
      leaves with row pointers or clustered row data

Searching for key 72 means comparing at the root, descending one branch, and then scanning a leaf. That is normally 3-5 page reads, even for a billion rows. Height grows logarithmically, which is why indexes scale so well.

Ordered indexes are good at ordered work

A B-tree can do equality lookup, prefix search, range scan, and sometimes return rows already sorted:

CREATE INDEX orders_customer_created_idx
    ON orders (customer_id, created_at DESC);

SELECT *
FROM orders
WHERE customer_id = 9182
  AND created_at >= now() - interval '30 days'
ORDER BY created_at DESC
LIMIT 20;

The column order matters. (customer_id, created_at) supports customer_id = ? and then ordered scanning within that customer. Reversing the columns is not equivalent.

Indexes are write tax

Every insert must maintain every index. Every update that changes an indexed column is effectively delete plus insert in the index. On a write-heavy table, five redundant indexes can cost more than the queries they save.

-- PostgreSQL: indexes that are large but barely used
SELECT schemaname, relname, indexrelname,
       pg_size_pretty(pg_relation_size(indexrelid)) AS size,
       idx_scan
FROM pg_stat_user_indexes
ORDER BY pg_relation_size(indexrelid) DESC;

idx_scan = 0 does not prove an index is useless — it may enforce a constraint or serve rare but critical jobs — but it is a strong prompt to investigate.

Corruption and bloat are operational facts

Because index pages are constantly split, merged, vacuumed, and reused, they can bloat. High-volume update/delete workloads leave dead space behind. PostgreSQL has REINDEX; MySQL/InnoDB often uses table rebuilds or online DDL. Random UUID primary keys are a classic amplifier because inserts land all over the tree instead of mostly at the right edge.

Field notes

  • A composite index is not a pile of single-column indexes with extra steps; column order is part of the design.
  • LIKE '%abc' cannot use a normal B-tree prefix ordering. Trigram or full-text indexes exist for a reason.
  • Low selectivity alone is not disqualifying; a flag plus another selective column can be excellent.
  • Covering indexes reduce heap lookups but make rows wider and maintenance heavier. Measure the trade-off.
  • In InnoDB, the primary key is clustered: secondary indexes point back to it, so enormous primary keys are copied into every secondary index.

Was this lesson helpful?

Sign in to rate