3.11 Chapter 3 Summary

  1. An index is a separate set of pages holding sorted key column values and record locations. Without an index, a search on a non-primary-key column requires a full scan that reads every page of the table; with an index, the number of pages to reference drops sharply.
  2. A B+Tree is a wide, shallow tree, so only a few descents are needed. Because one page branches into hundreds of paths, even in a table of hundreds of millions of rows, reading just a few root, branch, and leaf pages finds the desired record. Leaves are linked sideways, which also favors range scans and sorting.
  3. How the table is stored changes the role of the index. A heap table (PostgreSQL, Oracle) stores rows in arbitrary order, and the index points to the row's address. In a clustered index (InnoDB, SQL Server), the primary key order and the physical storage order are the same. In the clustered approach, secondary indexes hold the primary key value, so a primary key that is small and sequentially increasing is advantageous.
  4. The lookup pattern decides which index to create. Use a multi-column index (leftmost prefix rule) when rows are often searched by several columns, a covering index to let the index alone answer the query, and a filtered index when only a subset of rows is searched often (Sections 3.6 to 3.8).
IndexConvenient whenCaution
Secondary indexSearching often by a non-primary-key columnReads a table page once more after the index
Multi-column indexSeveral columns are used together as conditionsHard to use without a condition on the leading column
Covering indexFew columns are needed and queried oftenThe index grows with the columns it carries
Filtered indexOnly a subset of rows is searched oftenThe query condition must match the filter predicate
  1. Indexes carry a cost. Every index must be updated whenever a record is added, modified, or deleted. They also take additional storage and buffer pool space. Use them actively on read-heavy tables. On write-heavy tables, be careful about creating indexes.

Figure. Trade-off between index read improvement and write cost

flowchart LR
  ADD["Add an index"]
  subgraph READ ["Read benefits"]
    direction TB
    R1["Fewer pages read<br/>in condition searches"]
    R2["Range scans and ORDER BY<br/>handled in leaf order"]
    R3["If covering,<br/>table page reads are skipped"]
  end
  subgraph WRITE ["Write cost"]
    direction TB
    W1["Every index updated on each<br/>INSERT, UPDATE, DELETE"]
    W2["More page splits and log records"]
    W3["Takes storage and<br/>buffer pool space"]
  end
  ADD --> R1
  ADD --> R2
  ADD --> R3
  ADD --> W1
  ADD --> W2
  ADD --> W3
  READ --> D1["Read-heavy tables<br/>Use actively"]
  WRITE --> D2["Write-heavy tables<br/>Create carefully"]

From "Add an index" in the middle, arrows go out to two groups. The upper group, read benefits, is the effects seen in this chapter. Condition searches read fewer pages (Sections 3.1 and 3.2); because leaves are sorted and linked sideways, range scans and ORDER BY can be processed in leaf order (Section 3.2); and if all needed columns are in the index, table pages are not read (Section 3.7).

The lower group, write cost, is the burden the same index creates. Every index must be updated whenever a row is added, modified, or deleted (Section 3.9), page splits and their log records occur when index pages fill up (Section 3.3), and the index itself takes storage and buffer pool space (Section 3.9). As the two conclusions at the far right show, use indexes actively on read-oriented tables where the read benefit outweighs the write cost, and create them carefully on write-heavy tables.