← Lessons

quiz vs the machine

Silver1050

Databases

B-Tree Indexes

How balanced search trees make lookups and range scans fast.

4 min read · intro · beat Silver to climb

What a B-Tree Index Is

Most relational databases default to the B-tree (really a B+ tree) for indexes. It is a balanced tree that keeps keys sorted, so the engine can find any row in a few page reads instead of scanning the whole table.

Why It Is Fast

  • The tree stays balanced, so every leaf sits at the same depth.
  • Each node holds many keys, making the tree shallow and wide.
  • A handful of node reads locates any key even in a huge table.

What It Is Good At

  • Equality lookups like finding one user by id.
  • Range queries because keys are stored in order.
  • Sorting and prefix matches that follow the key order.

A query that filters on the leftmost columns of a composite index can use it; one that filters only on a later column usually cannot.

Key idea

A B-tree keeps keys sorted and balanced, giving fast equality lookups, range scans, and ordered reads in just a few page accesses.

Check yourself

Answer to earn rating on the learn ladder.

1. Why are B-tree indexes good for range queries?

2. A composite index on (a, b) can best serve a query filtering on: