# B-Tree

The most widely used indexing structure.

The log-structured indexes we saw earlier break the database down into variable-size segments, typically several megabytes or more in size, and always write a segment sequentially. By contrast, <mark style="background-color:orange;">B-trees break the database down into fixed-size blocks or pages, traditionally 4 KB in size</mark> (sometimes bigger), and read or write one page at a time. This design corresponds more closely to the underlying hardware, as disks are also arranged in fixed-size blocks.

Each page can be identified using an address or location, which allows one page to refer to another—similar to a pointer, but on disk instead of in memory. We can use these page references to construct a tree of pages, as illustrated in Figure 3-6.&#x20;

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FFh0rZydIgrqlDAY4wwXv%2FScreenshot%202024-02-11%20at%2011.54.28.png?alt=media&#x26;token=22a76847-b5cf-4215-b572-6217341cae99" alt=""><figcaption></figcaption></figure>

Branching factor - number of references to child pages in one page of the B-tree. On 3.6 it is 6. Usually it is several hundred.

If you want to <mark style="background-color:orange;">update the value for an existing key</mark> in a B-tree, you search for the leaf page containing that key, change the value in that page, and write the page back to disk (any references to that page remain valid). If you want to add a new key, you need to find the page whose range encompasses the new key and add it to that page. If there isn’t enough free space in the page to accommodate the new key, it is split into two half-full pages, and the parent page is updated to account for the new subdivision of key ranges—see Figure 3-7.

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FByMsE5tezofYIFElHL95%2FScreenshot%202024-02-11%20at%2012.01.45.png?alt=media&#x26;token=b5b24058-0bd8-47eb-843b-bc0295d1060d" alt=""><figcaption></figcaption></figure>

This algorithm ensures that the tree <mark style="background-color:orange;">remains balanced: a B-tree with n keys always has a depth of O(log n)</mark>. Most databases can fit into a B-tree that is three or four levels deep, so you don’t need to follow many page references to find the page you are looking for. (A four-level tree of 4 KB pages with a branching factor of 500 can store up to 256 TB.)

## Making B-trees reliable

In order to make the database resilient to crashes, it is common for B-tree implementations to include an <mark style="background-color:orange;">additional data structure on disk: a write-ahead log (WAL, also known as a redo log)</mark>. This is <mark style="background-color:orange;">an append-only file to which every B-tree modification must be written before it can be applied to the pages of the tree itself</mark>. When the database comes back up after a crash, this log is used to restore the B-tree back to a consistent state.

An additional complication of updating pages in place is that careful concurrency control is required if <mark style="background-color:orange;">multiple threads are going to access the B-tree</mark> at the same time —otherwise a thread may see the tree in an inconsistent state. This is typically done by <mark style="background-color:orange;">protecting the tree’s data structures with latches (lightweight locks)</mark>. Log-structured approaches are simpler in this regard, because they do all the merging in the background without interfering with incoming queries and atomically swap old segments for new segments from time to time.

## Comparing B-Trees and LSM-Trees

* LSM-trees are typically faster for writes, whereas B-trees are thought to be faster for reads

