← Lessons

quiz vs the machine

Platinum1740

Databases

The Secondary Index Lookups

Why a secondary index read often needs a second hop back to the clustered index.

5 min read · advanced · beat Platinum to climb

Two trees, two hops

A secondary index in InnoDB is its own B plus tree keyed on the indexed columns. Its leaf entries do not hold the full row. Instead each entry stores the primary key value of the matching row.

The bookmark lookup

To return columns not in the index, InnoDB performs a second step:

  • It finds the entry in the secondary index, getting the primary key.
  • It then walks the clustered index with that primary key to fetch the full row.

This second hop is called a bookmark lookup or row lookup. For a query that touches many rows, the extra hops can dominate cost.

Covering indexes

If every column a query needs is present in the secondary index, InnoDB can answer entirely from that index and skip the clustered lookup. This is a covering index, and the optimizer reports it as Using index in the EXPLAIN output.

Practical tuning

  • Add columns to an index so it covers a hot query.
  • Avoid selecting wide columns when only a few are needed.

Key idea

A secondary index stores primary key values, so non covered queries do a second clustered index hop per row, while a covering index answers from the secondary tree alone.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a secondary index leaf entry store as its row pointer?

2. What is a covering index?

3. What does the extra hop to the clustered index get called?