Chapter 3. B+Tree and Indexes

Suppose the table has grown much larger. There are 1 million data pages, and you must find a single row that matches a condition. Reading and checking the pages one by one will find it eventually, but in the worst case every one of the 1 million pages must be read once.

To reduce this cost, we build a separate structure that leads to a row's location by key value. The top page records broad ranges such as "if the key is in this range, go to this lower page," and the pages below it record narrower ranges. The bottom pages record exact locations, such as "the row with this key is in slot 5 of page 17." By following just three levels of pages in this way, you arrive at the page that holds the desired row out of 1 million pages. This is the role of a database index. The most representative data structure for building an index is the B+Tree.

This chapter looks at how an index is structured, how that structure is maintained when new rows are inserted, and how adding an index affects reads and writes.