Problem
Scanning an entire table to find rows is far too slow once data outgrows memory. Queries need both exact lookups and ordered range scans with predictable latency. Each disk access is expensive, so the structure must minimize page reads.
Also known as: b-plus-tree, btree-index
Provide ordered, logarithmic-time lookups and range scans over disk-resident data using a shallow, wide tree of fixed-size pages.
Scanning an entire table to find rows is far too slow once data outgrows memory. Queries need both exact lookups and ordered range scans with predictable latency. Each disk access is expensive, so the structure must minimize page reads.
The B-tree, in practice the B+ tree, is the default index structure of relational databases. Its high fanout keeps the tree only a few levels deep, so each lookup needs just a handful of page reads, and the first levels usually stay cached.
A composite index on (customer_id, created_at) serves equality on the first column and ranges on the second.
CREATE INDEX idx_orders_cust_time ON orders (customer_id, created_at);
root: [ 400 | 900 ]
internal: [100|250] [500|700] [950|990]
leaves: k1 k2 ... -> k3 k4 ... -> (linked for range scans)
Leaf pages are chained, so a range scan finds the start key once and then walks sideways.
Because pages are updated in place, a write first traverses to the correct leaf, modifies it in the buffer cache and logs the change in the write-ahead log. When a leaf is full it splits into two and the parent gains a separator key; in rare cases the split cascades to the root, which then grows the tree by one level. Sequential keys append to the rightmost leaf and keep pages dense, whereas random keys land everywhere and leave pages half full. That is why key choice has a visible effect on index size and insert speed.
Low - indexes consume extra disk and buffer cache
Low - periodic reindex and statistics refresh
Medium - composite key order and covering indexes need care
Page splits under random inserts cause write amplification and fragmentation
Unused or redundant indexes slow writes with no read benefit
Stale statistics lead the planner to ignore a good index