MySQL Indexes Complete Guide
Short answer: a MySQL index is a structure that lets the server find rows without scanning the whole table. InnoDB uses B-tree indexes by default. Add indexes to columns used in WHERE, JOIN, ORDER BY and GROUP BY, and check with EXPLAIN that MySQL actually uses them.
Why indexes matter
Without an index, MySQL reads every row to answer WHERE email = 'a@x.com' (a full table scan). On a million-row table that is slow. With an index on email, MySQL looks up the value in a sorted tree in a few steps. For the concept in general, see database indexes explained.
Creating indexes
-- when creating a table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
city VARCHAR(100),
created_at DATETIME,
UNIQUE KEY uq_users_email (email),
KEY idx_users_city (city)
);
-- on an existing table
CREATE INDEX idx_users_created ON users (created_at);
ALTER TABLE users ADD INDEX idx_users_city_created (city, created_at);
-- remove
DROP INDEX idx_users_created ON users;
-- list
SHOW INDEX FROM users;
Types of indexes
| Type | Purpose |
|---|---|
| PRIMARY KEY | Unique, not null; in InnoDB it is the clustered index that stores the rows |
| UNIQUE | Enforces unique values and speeds lookups |
| Regular (KEY / INDEX) | Speeds up searches and sorts |
| Composite | Covers several columns together |
| FULLTEXT | Natural-language search in text columns |
| Spatial | Geographic data |
Composite indexes and the leftmost prefix rule
ALTER TABLE orders ADD INDEX idx_cust_status_date (customer_id, status, created_at);
MySQL can use this index for queries that filter on:
customer_idcustomer_idandstatuscustomer_id,statusandcreated_at
It cannot use it efficiently for queries on status alone or created_at alone, because the index is sorted by the first column first. Put columns used with equality first, and range columns last. A condition like created_at > ... stops the index being used for columns after it.
Check with EXPLAIN
EXPLAIN SELECT * FROM users WHERE email = 'a@x.com';
| Column | What to look for |
|---|---|
type | const, ref, range are good; ALL means a full scan |
key | The index actually used (NULL means none) |
rows | Estimated rows examined; lower is better |
Extra | Using index is a covering index; Using filesort or Using temporary may need attention |
Covering indexes
If an index contains all columns a query needs, MySQL answers from the index alone:
ALTER TABLE users ADD INDEX idx_city_email (city, email);
SELECT city, email FROM users WHERE city = 'Pune'; -- Extra: Using index
When indexes are not used
- Functions on the column:
WHERE YEAR(created_at) = 2026. Use a range:created_at >= '2026-01-01' AND created_at < '2027-01-01'. - Leading wildcards:
LIKE '%son'cannot use the index;LIKE 'son%'can. - Type mismatches: comparing a string column to a number forces conversion.
- Low selectivity: an index on a column with few distinct values (like a yes/no flag) helps little.
- OR across different columns can prevent index use; consider
UNION.
The cost of indexes
Every insert, update and delete must also update the indexes, and indexes use disk and memory. Do not index everything. Remove duplicates: with an index on (a, b), a separate index on (a) is redundant.
Practical tips
- Index foreign key columns used in joins: MySQL joins.
- Keep indexed columns small; for long strings consider a prefix index like
(title(50)). - Use the slow query log to find queries that need indexes: how to optimize MySQL queries.
- Run
ANALYZE TABLEto refresh statistics after big data changes.
New to the database? Read what MySQL is.
FAQs
Does the primary key need a separate index?
No. It is automatically indexed.
How many indexes are too many?
When writes slow noticeably or indexes overlap. Keep only those that serve real queries.
Should I index boolean columns?
Usually not alone. They may help as part of a composite index.
Does MySQL create indexes for foreign keys?
InnoDB requires an index on a foreign key column and creates one if none exists.
