3.5 Secondary Indexes

If there is only a customer number index, how do we find "the customer named Hong Gildong"? If rows are also frequently looked up by name, we create one more index sorted by name. An additional index created separately from the table's default storage order (the primary key) is called a secondary index. The leaf of the name index records "Hong Gildong → where that row is."

Figure. How a secondary index leads to the record

flowchart TB
  Q["WHERE name = '홍길동'"] --> S["Name secondary index<br/>Root → Branch → Leaf"]
  S --> SL["Leaf entry<br/>홍길동 → row location"]
  SL -->|"PostgreSQL, Oracle<br/>(heap)"| A["Row address<br/>Page number + slot number"]
  A --> H["Read the row from<br/>the heap data page"]
  SL -->|"InnoDB<br/>(clustered)"| K["Primary key value"]
  K --> C["Clustered index<br/>Root → Branch → Leaf"]
  C --> CR["Read the full row<br/>from the leaf"]

Read from the top. Because the query condition is the name, the database descends the name secondary index from root to leaf and finds the '홍길동' entry. Up to here it is the same in every database. The path splits in two depending on what the "row location" recorded in the leaf entry is.

The left path is PostgreSQL and Oracle, which use heaps. The leaf holds a row address made of a page number and slot number, so reading the data page at that address once finishes the job. The right path is InnoDB, which is clustered. The leaf holds not a row address but the primary key value. So with the primary key value obtained, the database must descend the clustered index again from root to leaf to read the full row. Either way, there is a step that reads a table-side page once more after reading the secondary index, and this step is the Key Lookup or RID Lookup.

A secondary index is also a B+Tree, like the primary key index. The difference is that the sort key is a user-defined column rather than the primary key column. And the leaf pages store not the whole record but only the record's location. So when finding a record through a secondary index, the database always looks up the location or primary key value in the index first and then looks up the data page. This operation is called a Key Lookup or RID Lookup.

How a record's location is represented differs by database. PostgreSQL and Oracle, which use heaps, record an address made of a page number and slot number directly. After following the index, you go to that address and you are done. InnoDB, which is clustered, instead records the primary key value rather than the record's address. This is because records frequently move between pages, so the primary key value itself is recorded. After following the secondary index by name to learn the primary key, the database descends the primary key index again to find the record.

In InnoDB, the primary key (the clustered key) is copied into every secondary index, so if the primary key is large, every secondary index grows with it. So it is best to choose a small, simple value for the primary key. Also, as seen earlier, random keys cause splits, so sequentially increasing values are often more advantageous.