Why are inserts with random keys, such as random UUIDs, slower into a B-tree than inserts with increasing keys?
Sketch your own answer first, then compare it with the one below.
A strong answer
Increasing keys always land in the rightmost leaf. That page is hot in memory, it fills completely, and then the tree adds the next page. Writes are close to sequential.
Random keys land on a different leaf each time. Once the tree is bigger than memory, most inserts need a page read to find the leaf, and each one dirties a different page. Splits of half-full pages leave leaves only about two thirds full on average, so the tree is also larger.
The usual fix is a key that is roughly time ordered, or a separate surrogate key for the clustered index. The trade-off is hot-spotting the right edge under heavy concurrent writes.
Sources
- Goetz Graefe (2011). Modern B-Tree Techniques.