Why Indexes Matter
Without an index, MySQL reads the whole table to find rows (a full table scan). An index is a sorted copy of one or more columns that lets MySQL jump to the right rows, like the index of a book. These examples use a table of 20,000 users generated by a recursive CTE, so the effect is visible.
Read the EXPLAIN columns: type (ALL means full scan, ref and range mean an index is used), key (the index chosen), rows (estimated rows to read).
Before the Index
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ALL | NULL | 20216 | Using where |
After the Index
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_email | 1 | NULL |
rows fell from about 20,000 to 1. The query uses idx_big_email and reads one row.
Clustered vs Secondary Indexes
In InnoDB the primary key is the table: rows are stored in primary-key order (a clustered index). Every other index is a secondary index that stores the indexed columns plus the primary key. So a long primary key makes every secondary index bigger. Prefer a short INT or BIGINT key, and avoid random UUID text keys.
Composite Indexes and the Leftmost Prefix Rule
A composite index on (country, created) is sorted by country first, then by date inside each country. It helps queries that use the leftmost columns:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_country_created | 55 | Using index |
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_country_created | 4000 | Using index |
But a query that skips the first column cannot use it well:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | range | idx_big_country_created | 2021 | Using where; Using index for skip scan |
Look at Extra: MySQL 8.0.13+ can use an index skip scan here (it probes each country value in turn), but that is slower than a direct lookup and only works in some cases. Put the column you filter on most often first, with equality columns before range columns.
Covering Indexes
If an index contains every column the query needs, MySQL answers from the index alone. EXPLAIN shows Using index in Extra.
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | range | idx_big_country_created | 2295 | Using where; Using index |
Functions on Columns Break Indexes
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | index | idx_big_created | 19247 | Using where; Using index |
Rewrite it as a range on the raw column so the index can be used:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | range | idx_big_created | 1705 | Using where; Using index |
MySQL 8.0.13+ also supports functional indexes, for example CREATE INDEX idx_month ON big_users ((MONTH(created)));.
Prefix Indexes
For long text columns you can index only the first characters:
It saves space but cannot be used as a covering index and cannot do full ORDER BY on the column. TEXT and BLOB columns can only be indexed with a prefix.
Invisible Indexes (8.0+)
An invisible index is kept up to date but ignored by the optimizer. It is a safe way to test whether you can drop an index: hide it, watch performance, then drop (or make it visible again instantly).
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ALL | NULL | 19767 | Using where |
Descending Indexes (8.0+)
Before 8.0, DESC in an index definition was accepted but ignored. In 8.0 it is real, which helps mixed-order sorting:
Unique Indexes and Dropping
Result:
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
|---|---|---|
| PRIMARY | id | 0 |
| uq_users_email | 0 |
A unique index allows many NULLs in MySQL. Remove an index with DROP INDEX uq_users_email ON users;.
The Cost of Indexes
Every index must be updated on INSERT, UPDATE and DELETE, and takes disk and memory. Index what you filter, join and sort on, and drop indexes nobody uses. The sys.schema_unused_indexes view lists candidates.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Index on every column | Slow writes, big disk use | Index real query patterns |
| Composite index in the wrong order | Index not used | Equality columns first, then range |
| Wrapping the column in a function | Full scan | Compare the raw column |
| Comparing text column with a number | Index ignored, wrong matches | Use the right type |
| Using a random UUID string as primary key | Fragmented, large secondary indexes | Use BIGINT or ordered UUIDs |
| Dropping an index without testing | Surprise slowdowns | Make it invisible first |
Practice
- Create an index so that searching by
countryandcreateduses it. - Check with EXPLAIN that
LIKE 'user77%'uses an index butLIKE '%77'does not. - Make an index invisible and compare plans.
- Find out which columns a covering index would need for a report query.
Conclusion
InnoDB stores rows in primary-key order and adds secondary indexes on top. Composite indexes follow the leftmost-prefix rule, covering indexes avoid table lookups, and 8.0 adds invisible and descending indexes.

