3.2 B+Tree

The simplest way to find a value in a sorted list is binary search. It checks the middle value, decides whether the target lies in the front or back half, and halves the search range each time. Using this method on disk as it is, however, creates a problem. Finding one row among 1 million takes about twenty comparisons, and if each comparison requires examining a different page, that many pages must be read from disk or the buffer pool.

Figure 3-2. The process of binary search

block-beta
  columns 16
  ix0["0"] ix1["1"] ix2["2"] ix3["3"] ix4["4"] ix5["5"] ix6["6"] ix7["7"] ix8["8"] ix9["9"] ix10["10"] ix11["11"] ix12["12"] ix13["13"] ix14["14"] ix15["15"]
  s0_0["1020"] s0_1["1580"] s0_2["2210"] s0_3["3100"] s0_4["3920"] s0_5["4821"] s0_6["5050"] s0_7["5300"] s0_8["5870"] s0_9["6340"] s0_10["6915"] s0_11["7480"] s0_12["8020"] s0_13["8655"] s0_14["9130"] s0_15["9710"]
  st0["step 1: range [0..15], mid = 7 -> 5300 > 4821 -> keep [0..6]"]:16
  s1_0["1020"] s1_1["1580"] s1_2["2210"] s1_3["3100"] s1_4["3920"] s1_5["4821"] s1_6["5050"] s1_7["5300"] s1_8["5870"] s1_9["6340"] s1_10["6915"] s1_11["7480"] s1_12["8020"] s1_13["8655"] s1_14["9130"] s1_15["9710"]
  st1["step 2: range [0..6], mid = 3 -> 3100 < 4821 -> keep [4..6]"]:16
  s2_0["1020"] s2_1["1580"] s2_2["2210"] s2_3["3100"] s2_4["3920"] s2_5["4821"] s2_6["5050"] s2_7["5300"] s2_8["5870"] s2_9["6340"] s2_10["6915"] s2_11["7480"] s2_12["8020"] s2_13["8655"] s2_14["9130"] s2_15["9710"]
  st2["step 3: range [4..6], mid = 5 -> 4821 = 4821 -> found"]:16
  class ix0,ix1,ix2,ix3,ix4,ix5,ix6,ix7,ix8,ix9,ix10,ix11,ix12,ix13,ix14,ix15 muted
  class st0,st1,st2 plain
  class s0_0,s0_1,s0_2,s0_3,s0_4,s0_5,s0_6,s0_8,s0_9,s0_10,s0_11,s0_12,s0_13,s0_14,s0_15,s1_0,s1_1,s1_2,s1_4,s1_5,s1_6,s2_4,s2_6 slot
  class s0_7,s1_3 hot
  class s1_7,s1_8,s1_9,s1_10,s1_11,s1_12,s1_13,s1_14,s1_15,s2_0,s2_1,s2_2,s2_3,s2_7,s2_8,s2_9,s2_10,s2_11,s2_12,s2_13,s2_14,s2_15 free
  class s2_5 data

This is the process of finding 4821 among 16 sorted values (the values are examples for illustration). In the first step, the search compares against 5300 at position 7, the middle of the full range 0 to 15. Since 4821 is smaller, the right half from position 7 onward is eliminated from the search range, leaving only 0 to 6. In the second step, it compares against 3100 at position 3, the middle of 0 to 6. Since 4821 is larger, the left side is discarded, leaving only 4 to 6. In the third step, it finds 4821 at position 5, the middle of 4 to 6.

Each time, comparing against the middle value of the remaining range eliminates half, so among 16 values it takes 3 steps to find the target. The number of comparisons is proportional to the logarithm (base 2) of the number of rows, so for 1 million rows it is about twenty. This is fast in memory, but if each comparison requires reading one page, it becomes twenty page reads.

Because disk reads are expensive, the B+Tree organizes pages so as to minimize the number of search steps. One page divides the key range of the data below it not into two branches but into many. A page records multiple key ranges together with the numbers of the lower pages responsible for them, such as "keys A to C go to page 1, keys D to F go to page 2." With many branches, the number of tree levels drops dramatically.

Figure 3-3. B+Tree structure and search path

flowchart TB
  R["Root page<br/>Key range → lower page number"]
  B1["Branch page 1<br/>Key range → leaf page number"]
  B2["Branch page 2<br/>Key range → leaf page number"]
  L1["Leaf 1<br/>Sorted keys + row locations"]
  L2["Leaf 2<br/>Sorted keys + row locations"]
  L3["Leaf 3<br/>Sorted keys + row locations"]
  L4["Leaf 4<br/>Sorted keys + row locations"]
  R -->|"① Compare key ranges"| B1
  R --> B2
  B1 --> L1
  B1 -->|"② Compare key ranges"| L2
  B2 --> L3
  B2 --> L4
  L1 <-->|"Link to adjacent leaf"| L2
  L2 <-->|"③ Move sideways for a range scan"| L3
  L3 <--> L4

The root page at the top and the branch pages in the middle store only value ranges and links to lower pages. The leaf pages contain the actual sorted key values together with the locations of their rows, or the rows themselves. Whether a location or the row itself is stored depends on how the table is stored, which Section 3.4 covers.

To find a single row, follow the numbers. ① At the root page, pick the range the target key belongs to and descend to the corresponding branch page. ② At the branch page, compare ranges again and descend to a leaf page. When the key is found in the leaf page, you get the row's location (or the row itself). ③ Leaves are linked sideways to each other, so a range scan starts at the leaf containing the start key and proceeds to the adjacent leaves in order.

  • Root: The top page of the tree. It holds boundary values that divide the whole key space into several ranges, along with the lower page numbers.
  • Branch: A middle-level page. It holds key ranges that further subdivide the range it is responsible for, along with links to lower pages.
  • Leaf: The bottom level, which records the actual sorted key values and the locations where those values are found.

Where, then, is this index stored? An index is made of pages too. Each root, branch, and leaf is one 8KB page, and inside it are stored key values and the page numbers of the level below. Suppose a single root or branch page represents 400 downward paths. Following just three steps, once at the root, once at a branch, and once at a leaf, locates the desired record among 400 × 400 × 400 = about 64 million records. One more level reaches tens of billions of rows. Even in a table of hundreds of millions of rows, finding one row requires reading only three or four pages. This is how the B+Tree optimizes search.

The buffer pool improves performance once more on top of this. The root and branch pages are on the path every lookup passes through, so they stay in the buffer pool almost all the time. Thanks to cache hits, the number of page reads from disk drops accordingly.

The "+" in the name has meaning too. In a B+Tree, the leaf pages are linked sideways to one another. So when searching by range, such as "customer numbers 1000 through 1100," you descend to find 1000 and then scan sequentially along the leaves. There is no need to climb back up to the parent branch and come down again. Because the sorted order is preserved, ORDER BY can often be processed without a separate sort. A query that sorts by primary key often incurs no extra sorting cost.