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
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
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| write amplification | 6x | 3x | 7x |
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| leaf fill | 69% | 63% | 78% |
After 3,000 inserts the tree is 3 levels tall with 135 leaf pages and 8 inner pages. Pages per level from the root: 1, 7, 135. Leaves are 69 percent full. Each insert costs 0.16 page writes on average plus 144 bytes of log.
What to try
Each one sets up a starting point. Then make the change it describes.
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.
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.
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.
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.
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