Clustered vs Secondary Indexes in MySQL InnoDB
Clustered vs Secondary Indexes in MySQL InnoDB
In MySQL's InnoDB storage engine, the physical organization of data on disk is dictated by the Clustered Index. Understanding the architecture of clustered indexes versus Secondary (Non-Clustered) Indexes is fundamental to writing high-performance queries and designing schemas.
1. The Clustered Index: Table AND Index in One
In InnoDB, the table IS the clustered index. There is no separate "heap" file for table rows.
- Leaf Nodes Store Complete Rows: The leaf pages of the clustered index B+Tree contain the actual, full table row data (all columns).
- Physical Ordering: Rows are physically sorted and stored on disk in Primary Key order.
- One per Table: Because physical data on disk can only be sorted in one order, a table can have exactly one clustered index.
How InnoDB Chooses the Clustered Index:
- 1The explicit
PRIMARY KEYdefined on the table. - 2If no
PRIMARY KEYexists, InnoDB chooses the firstUNIQUEindex where all columns areNOT NULL. - 3If neither exists, InnoDB creates an internal, hidden 6-byte row ID column (
GEN_CLUST_INDEX) that increments monotonically.
2. Secondary (Non-Clustered) Indexes
Any index you create other than the primary key is a Secondary Index (e.g., CREATE INDEX idx_email ON users(email);).
Key Architectural Difference:
- Leaf nodes of a secondary index do NOT store row data or disk pointers.
- Instead, the leaf nodes store the indexed column value + the Primary Key value!
The Cost of the "Double Lookup" (Bookmark Lookup)
When a query uses a secondary index to retrieve columns not present in that index:
- 1First Search: MySQL searches the secondary index B+Tree to find the matching Primary Key.
- 2Second Search (Double Lookup): MySQL takes that Primary Key and traverses the clustered index B+Tree to fetch the full row data.
This secondary traversal introduces extra I/O overhead. (In Topic 8, we will learn how Covering Indexes eliminate this double lookup completely!).
Primary Key Design Guidelines for InnoDB
Because secondary indexes store copies of the primary key in every leaf entry:
- 1Keep Primary Keys Small: A 4-byte
INT UNSIGNEDor 8-byteBIGINTkeeps all secondary indexes compact. Using longVARCHAR(255)or UUID strings (CHAR(36)) bloats every secondary index on disk and fills the buffer pool. - 2Use Monotonically Increasing Keys: Auto-incrementing integers insert cleanly onto the right-most page of the B+Tree. Random keys (like UUID v4) cause B+Tree page splits, fragmentation, and heavy disk I/O.
Multiple Choice Questions
1. What is stored in the leaf nodes of an InnoDB Clustered Index?
A. Just row pointers to an unorganized heap file B. The complete, actual table row data containing all columns C. A hash value of the primary key D. The table DDL definition Answer: B Explanation: In InnoDB, the clustered index is the table itself; leaf pages store full physical row records sorted by primary key.
2. What do the leaf nodes of an InnoDB Secondary Index store?
A. The complete row data B. The indexed column value plus the Primary Key value C. A memory pointer to the query cache D. An SHA-256 checksum Answer: B Explanation: InnoDB secondary index leaves store the indexed column values paired with the corresponding primary key value.
3. What is a "Double Lookup" (or Bookmark Lookup) in MySQL InnoDB?
A. Executing the same query twice to verify cache results B. Searching a secondary index to find the primary key, then searching the clustered index to retrieve remaining row columns C. Checking two databases on different servers D. A deadlock between two transactions Answer: B Explanation: A double lookup occurs when MySQL uses a secondary index to find a row's primary key, then must traverse the clustered index to read the full row.
4. Why does using a long string or random UUID as a Primary Key hurt performance in InnoDB?
A. Strings cannot be sorted B. Every secondary index stores the primary key, causing index bloat, and random inserts cause frequent B+Tree page splits C. MySQL prohibits strings as primary keys D. Strings disable the redo log Answer: B Explanation: Large PKs bloat all secondary index pages, while non-sequential random values force expensive page splits and index fragmentation.
5. How many Clustered Indexes can a single InnoDB table contain?
A. Unlimited B. As many as there are unique columns C. Exactly one D. Up to 16 Answer: C Explanation: Because physical table data can only be sorted on disk in one physical order, a table can only possess a single clustered index.
Composite Multi-Column Indexes & Leftmost Prefix Rule
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| B-Tree vs Hash Index Data Structures | Composite Multi-Column Indexes & Leftmost Prefix Rule |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.