MySQL Indexes: Clustered, Secondary, Composite, Prefix and Invisible

MySQL 70 min min read Updated: Nov 27, 2026 Intermediate
MySQL Indexes: Clustered, Secondary, Composite, Prefix and Invisible
Intermediate Topic 1 of 3

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

sql EXPLAIN SELECT * FROM big_users WHERE email = 'user777@example.com';

MySQL plan:

tabletypekeyrowsExtra
big_usersALLNULL20216Using where

After the Index

sql EXPLAIN SELECT * FROM big_users WHERE email = 'user777@example.com';

MySQL plan:

tabletypekeyrowsExtra
big_usersrefidx_big_email1NULL

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:

sql EXPLAIN SELECT id FROM big_users WHERE country = 'IN' AND created = '2024-03-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersrefidx_big_country_created55Using index
sql EXPLAIN SELECT id FROM big_users WHERE country = 'IN';

MySQL plan:

tabletypekeyrowsExtra
big_usersrefidx_big_country_created4000Using index

But a query that skips the first column cannot use it well:

sql EXPLAIN SELECT id FROM big_users WHERE created = '2024-03-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersrangeidx_big_country_created2021Using 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.

sql EXPLAIN SELECT country, created FROM big_users WHERE country = 'US' AND created >= '2024-06-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersrangeidx_big_country_created2295Using where; Using index

Functions on Columns Break Indexes

sql EXPLAIN SELECT id FROM big_users WHERE MONTH(created) = 3;

MySQL plan:

tabletypekeyrowsExtra
big_usersindexidx_big_created19247Using where; Using index

Rewrite it as a range on the raw column so the index can be used:

sql EXPLAIN SELECT id FROM big_users WHERE created >= '2024-03-01' AND created < '2024-04-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersrangeidx_big_created1705Using 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:

sql CREATE INDEX idx_email_prefix ON big_users (email(10));

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).

sql EXPLAIN SELECT * FROM big_users WHERE email = 'user777@example.com';

MySQL plan:

tabletypekeyrowsExtra
big_usersALLNULL19767Using 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:

sql CREATE INDEX idx_country_created_desc ON big_users (country ASC, created DESC);

Unique Indexes and Dropping

sql CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(40)); CREATE UNIQUE INDEX uq_users_email ON users (email); SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users' ORDER BY INDEX_NAME;

Result:

INDEX_NAMECOLUMN_NAMENON_UNIQUE
PRIMARYid0
uq_users_emailemail0

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

MistakeWhat happensFix
Index on every columnSlow writes, big disk useIndex real query patterns
Composite index in the wrong orderIndex not usedEquality columns first, then range
Wrapping the column in a functionFull scanCompare the raw column
Comparing text column with a numberIndex ignored, wrong matchesUse the right type
Using a random UUID string as primary keyFragmented, large secondary indexesUse BIGINT or ordered UUIDs
Dropping an index without testingSurprise slowdownsMake it invisible first

Practice

  1. Create an index so that searching by country and created uses it.
  2. Check with EXPLAIN that LIKE 'user77%' uses an index but LIKE '%77' does not.
  3. Make an index invisible and compare plans.
  4. 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.

Quick Summary: clustered, secondary, composite, covering, prefix, invisible, descending indexes.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators