MySQL CREATE INDEX
An index is a separate, sorted data structure (a B-tree in InnoDB) that lets MySQL find rows by a column's value without reading the whole table, just as a book's index lets you jump to a page instead of reading every page. Indexes make searches, joins and sorts fast; the price is extra disk space and slightly slower writes, because every INSERT, UPDATE and DELETE must update the indexes too.
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE UNIQUE INDEX index_name ON table_name (column);
DROP INDEX index_name ON table_name;Indexes you already have
MySQL creates an index automatically for every PRIMARY KEY, every UNIQUE constraint and every FOREIGN KEY column (if no suitable index exists). information_schema.STATISTICS lists them:
SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'orders'
ORDER BY INDEX_NAME;+-------------+-------------+------------+
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
+-------------+-------------+------------+
| customer_id | customer_id | 1 |
| employee_id | employee_id | 1 |
| PRIMARY | order_id | 0 |
+-------------+-------------+------------+
The primary key has the index PRIMARY; the two foreign key columns got indexes named after the column. NON_UNIQUE = 0 marks a unique index. In the client, SHOW INDEX FROM orders; shows the same information with more columns.
Creating an index
Searching orders by date is common, so order_date deserves an index:
CREATE INDEX idx_orders_date ON orders (order_date);
CREATE INDEX idx_customers_name ON customers (last_name, first_name);
SELECT TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX, COLUMN_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'shop' AND INDEX_NAME LIKE 'idx_%'
ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX;+------------+--------------------+--------------+-------------+
| TABLE_NAME | INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME |
+------------+--------------------+--------------+-------------+
| customers | idx_customers_name | 1 | last_name |
| customers | idx_customers_name | 2 | first_name |
| orders | idx_orders_date | 1 | order_date |
+------------+--------------------+--------------+-------------+
Queries don't change at all when you add an index: the optimizer decides on its own whether using it is worthwhile. ALTER TABLE orders ADD INDEX idx_orders_date (order_date); is an equivalent way to write it.
Is my index used? EXPLAIN
Put EXPLAIN in front of a SELECT to see the plan the optimizer chose. The interesting columns are type (how rows are found), key (which index) and rows (an estimate of rows examined):
EXPLAIN SELECT * FROM customers WHERE country = 'UK';-- without an index on country
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | customers | ALL | NULL | NULL | NULL | NULL | 15 | Using where |
-- after CREATE INDEX idx_country ON customers (country);
| 1 | SIMPLE | customers | ref | idx_country | idx_country | 162 | const | 3 | ... |type = ALL is a full table scan; ref, eq_ref, range and const mean an index is used. (The exact columns vary between versions and servers, and on a 15-row table the optimizer may prefer a scan anyway, so the plan isn't run here.) MySQL 8 also has EXPLAIN ANALYZE, which actually runs the query and reports real timings.
Composite indexes and the leftmost-prefix rule
An index on (last_name, first_name) is sorted by last name, then by first name within each last name, like a phone book. It can serve:
WHERE last_name = 'Silva'WHERE last_name = 'Silva' AND first_name = 'Ana'ORDER BY last_name, first_name
but not WHERE first_name = 'Ana' alone, because first names are scattered all over the index. Put the column you filter on most (and with equality) first.
Unique indexes
A unique index both speeds up lookups and enforces uniqueness. It is the same thing as a UNIQUE constraint:
CREATE UNIQUE INDEX uq_supplier_name ON suppliers (name);
INSERT IGNORE INTO suppliers (name, country) VALUES ('Highland Roasters', 'UK'), ('Bean Brothers', 'Kenya');
SELECT supplier_id, name, country FROM suppliers WHERE supplier_id > 5;+-------------+--------------------+-------------+
| supplier_id | name | country |
+-------------+--------------------+-------------+
| 6 | CleanHome Supplies | Netherlands |
| 7 | Bean Brothers | Kenya |
+-------------+--------------------+-------------+
The duplicate name was rejected (and skipped by IGNORE).
Dropping an index
CREATE INDEX idx_products_price ON products (unit_price);
DROP INDEX idx_products_price ON products;
SELECT COUNT(*) AS price_indexes
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'shop' AND INDEX_NAME = 'idx_products_price';+---------------+
| price_indexes |
+---------------+
| 0 |
+---------------+
When to add an index
| Good candidates | Usually not worth it |
|---|---|
columns in frequent WHERE, JOIN ... ON and ORDER BY |
tiny tables |
| foreign key columns (MySQL adds these for you) | columns with very few distinct values (a yes/no flag) when used alone |
| columns that must be unique | columns that are rarely searched but often written |
Indexes also can't help when the column is wrapped in a function: WHERE YEAR(order_date) = 2025 scans every row, while WHERE order_date >= '2025-01-01' AND order_date < '2026-01-01' can use the index. The same goes for LIKE '%text' with a leading wildcard.
Test yourself
What is the main cost of adding an index?
- Queries return different results
- Extra storage and slower INSERT/UPDATE/DELETE
- The table becomes read-only
- SELECT statements need a hint to use it
An index exists on (last_name, first_name). Which condition can NOT use it efficiently?
- last_name = 'Kim'
- last_name = 'Kim' AND first_name = 'Sara'
- first_name = 'Sara'
- ORDER BY last_name, first_name
Which statement shows how MySQL plans to execute a query?
- DESCRIBE INDEX
- SHOW PLAN
- EXPLAIN
- ANALYZE TABLE