Try
↑ ↓ move↵ openesc close

B-tree page writes lab

Insert keys into a B-tree and count the pages it splits, the pages it must write and the bytes written for each byte of record, with the write-ahead log on and off.

inserts 3,000 of 30,000height 3page 4,096 bytes

Pages in each level of the tree

root1level 27leaves135

Every lookup reads one page per level, from the root down to a leaf.

leaf pages
135
page splits
140
leaf fill
69%
reads per lookup
2
page writes per insert
0.16
log bytes per insert
144
write amplification
6.4x
Bytes written per byte of record
Bytes written per byte of record. Text version available below the chart.0x3x5x8x10x501,5503,000insertsx
How full the leaf pages are
How full the leaf pages are. Text version available below the chart.0%25%50%75%100%501,5503,000inserts%

What to try

Each one sets up a starting point. Then make the change it describes.

  1. Random keys against ascending keys

    Compare the leaf fill and the page writes per insert with random keys, then switch the key order to ascending.

    Look for. Random keys leave leaves about two thirds full and touch a different page almost every time. Ascending keys fill each page before the next one starts, and each insert dirties the same few pages.

  2. Turn the log off

    Set the write-ahead log to off and read the write amplification.

    Look for. Without a log, each insert has to put its dirty page on disk before it is safe. Write amplification jumps, because a whole page is written for one record.

  3. Checkpoint rarely

    Keep the log on and raise the checkpoint spacing to 5000 inserts.

    Look for. A page that changes many times between checkpoints is written once. Page writes per insert fall. The price, which this model does not show, is a longer replay of the log after a crash.

  4. Small pages make a taller tree

    Set keys per page to 8 and read the height and the reads per lookup, then try 64.

    Look for. Small pages mean fewer keys per node, so the tree needs more levels and a cold lookup reads more pages. Big pages are shallow but each page write is bigger.

  5. Cache the top of the tree

    Set levels held in memory to 0, then to 2, and watch reads per lookup.

    Look for. Every level you keep in memory removes one disk read from each lookup. The root and its children are tiny next to the leaves, so keeping them costs little.

New to the idea? Read the lesson first. Every number here comes from a seeded simulator, so the same seed gives the same run.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms