Indexes and Indexing
Context
This is why you need to learn MySQL indexes and indexing:
MySQL leverages hardware, optimizations, and indexes to achieve performance when accessing data. Hardware is an obvious leverage because MySQL runs on hardware: the faster the hardware, the better the performance. Less obvious and perhaps more surprising is that hardware provides the least leverage. I explain why in a moment. Optimizations refer to the numerous techniques, algorithms, and data structures that enable MySQL to utilize hardware efficiently. Optimizations bring the power of hardware into focus. And focus is the difference between a light bulb and a laser. Consequently, optimizations provide more leverage than hardware. If databases were small, hardware and optimizations would be sufficient. But increasing data size deleverages the benefits of hardware and optimizations. Without indexes, performance is severely limited.
To illustrate these points, think of MySQL as a fulcrum that leverages hardware, optimizations, and indexes to figuratively lift data, as shown below.

Without indexes (on the right side), MySQL achieves limited performance with relatively small data. But add indexes to the balance, and MySQL achieves high performance with large data:

Indexes provide the most and the best leverage. They are required for any nontrivial amount of data. MySQL performance requires proper indexes and indexing, both of which this chapter teaches in detail.
I have a story about the power of good indexes. Several years ago, I designed and implemented an application that stores a lot of data. Originally, I estimated the largest table not to exceed a million rows. But there was a bug in the data archiving code that allowed the table to reach one billion rows. For years, nobody noticed because response time was always great. Why? Good indexes.
Copyright 2021 Daniel Nichter
No reproduction of this excerpt without permission
Key Points
- Indexes provide the most and the best leverage for MySQL performance
- Do not scale up hardware to improve performance—that’s the last solution
- Tuning MySQL is not necessary to improve performance with a reasonable configuration
- An InnoDB table is a B-tree index organized by the primary key
- MySQL accesses a table by index lookup, index scan, or full table scan—index lookup is the best
- To use an index, a query must use a leftmost prefix of the index—the leftmost prefix requirement
- MySQL uses an index to find rows matching
WHERE
, group rows forGROUP BY
, sort rows forORDER BY
, avoid reading rows (covering index), and to join tables EXPLAIN
prints a query execution plan (or EXPLAIN plan) that details how MySQL executes a query- Indexing requires thinking like MySQL to understand the query execution plan
- Good indexes can lose effectiveness for a variety of reasons
- MySQL uses three algorithms to join tables: nested-loop join (NLJ), block nested-loop (BNL), and hash join
Pitfalls
- Not having a left-most prefix
- Indexing “all the columns”
- Duplicate indexes
- Too many indexes
- Very low cardinality
- Focusing on advanced optimizations before mastering the basics
- Not realizing that indexes cannot provide infinite leverage (benefit)
Hack MySQL Articles
Additional Resources
Resource | Type | About |
---|---|---|
Optimization and Indexes | MySQL manual | Foundational knowledge. Must read. |
Understanding the Query Execution Plan | MySQL manual | Foundational knowledge. Must read. |
Introduction to indexes @ PlanetScale | Vidoes | Fantastic series of videos. Must watch. |
B-trees and database indexes by Benjamin Dicken @ PlanetScale | Article | Fantastic article about B-tree indexes. Must read. |
Database Internals by Alex Petrov | Book | Fantastic book that provides deep, technical knowledge—including how B-tree indexes are implemented. Not written for MySQL but directly applicable to MySQL. |
Copyright 2025 Daniel Nichter