Skip to content
Optimal Lab

Databases · interactive

B-tree indexes

Find one row without an index and the database reads every page of the table. A B-tree keeps the keys sorted in a shallow tree, so it reads one page per level. Find id 44 both ways.

Index on id · one box = one page

  1. 2956
  1. 1120
  2. 3847
  3. 6574
  1. 258
  2. 111417
  3. 202326
  4. 293235
  5. 384144
  6. 475053
  7. 565962
  8. 656871
  9. 747780

The table, as stored (no index): 9 pages

  1. 41874
  2. 23592
  3. 683514
  4. 50775
  5. 296220
  6. 441171
  7. 563280
  8. 174726
  9. 653853

Page reads with the index1

Page reads with a full scan9

Start at the root. 29 ≤ 44 < 56: go middle. 1 page read so far.

Find id

level 1 of 3
  1. 1

    The root holds a few keys that split the whole range of ids. One page read.

    root keys: 29, 56

  2. 2

  3. 3

  4. 4