B-Tree 00:00:01 Where can forty-two still be? A bookseller wants record forty-two from a file too large to scan for every customer. The first disk page contains separators and pointers. Comparing a key with those separators rules out whole shelves of possible pages. 42 lies between 25 and 50. Take the second pointer. 42 is larger than 40. Take the last pointer. 42 is in this page. A search for 43 ends in the same leaf. It is not there, so it is nowhere. A missing key is a useful test. A correct search can explain why no other leaf could possibly contain forty-three. 00:00:42 A page runs out of slots To see why the search stays short, use a deliberately tiny tree whose nodes can hold only two keys. Real storage pages usually fan out much further. Insert the keys in increasing order and watch the middle one. The root is an empty leaf. Insert ten into the root. Insert twenty beside it; the two entries fit and stay in order. Inserting thirty would give this node three keys, one above its allowed maximum. 20, the middle key, goes up and becomes the new root. Forty has joined the right leaf. Adding fifty would put thirty, forty and fifty in that leaf, so forty moves up as the new separator. 30 lies between 20 and 40: the middle child. The split changes the route through the index. It does not create one unusually deep branch. Every leaf remains the same distance from the root. 00:01:41 Repair a page after deletion Removing one entry can leave a node below its permitted minimum. The parent separator now matters twice, for routing future readers and for rebalancing the neighboring pages. The middle leaf has two keys, one more than it needs. Deleting ten empties the left leaf. It now contains fewer keys than this tree permits. The separator 20 comes down, and 30 takes its place. Deleting twenty empties a leaf again. This time the neighboring leaf has just its own minimum and cannot lend a key. The two leaves and their separator 30 become one leaf. If a sibling can spare an entry, borrowing avoids a merge. If it cannot, combine the siblings and bring the separating key down from their parent. 00:02:30 One descent for a whole shelf The bookseller now wants everything from thirty-five through seventy-five. In this B-plus-tree example the leaves have links. Once the first leaf is found, restarting at the root for every key wastes useful locality. The search ends in leaf 2. 30 is too small. 40 and 50 are in the range. Leaf 2 points to leaf 3. Nothing above is read. 60 and 70 are in the range. 80 is above 75, so the scan ends. Forty-two needed one search. A range needs one starting search followed by a walk across consecutive leaves, stopping as soon as a key exceeds the upper bound. 00:03:14 What insertion order changes Finally, imagine loading eight fresh entries into an existing index. The keys may arrive in order or scattered throughout the key space. Predict which arrangement revisits the same leaf most often. Every key is larger than the last, so all eight go to the last leaf. With this sequence of scattered keys, all four leaves are modified. This increases the number of dirty pages that eventually need writing. For scattered random insertions into a working set much larger than memory, visiting an uncached leaf becomes more likely. Adjacent insertions can improve locality, while a busy last page can also become a contention point. The right index question is which pages this particular workload will read and change.