Lesson 3 of 9 · 25m
Indexing and Query Plans That Survive Growth
Clustered keys, composite indexes, EXPLAIN, covering indexes, and the schema habits that keep InnoDB fast at production scale.
Most database performance work is not exotic tuning. It is realizing that a query which scanned 4,000 rows in development now scans 40 million because the table grew and the index did not match the access pattern. InnoDB rewards precise indexing and punishes optimistic ones.
The primary key is the table layout
InnoDB clusters rows by primary key. That means the primary key is not just a constraint; it determines physical locality. Small, stable, monotonically increasing keys are usually kind to insert-heavy workloads. Random UUIDs as primary keys scatter inserts across the B-tree, inflate secondary indexes, and fragment pages; if you must expose UUIDs, use them as unique external identifiers while retaining a compact internal numeric primary key.
CREATE TABLE events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
account_id INT UNSIGNED NOT NULL,
event_type VARCHAR(40) NOT NULL,
created_at DATETIME(3) NOT NULL,
payload JSON,
PRIMARY KEY (id),
KEY idx_account_time (account_id, created_at),
KEY idx_type_time (event_type, created_at)
) ENGINE=InnoDB;Composite indexes have an order
An index (account_id, created_at) can serve WHERE account_id = ?, and it can serve WHERE account_id = ? AND created_at >= ?. It cannot efficiently serve WHERE created_at >= ? alone because the leading column is missing. Index design starts with the queries you actually run, not a ritual of indexing every column.
EXPLAIN
SELECT id, created_at
FROM events
WHERE account_id = 17
AND created_at >= '2026-11-01'
ORDER BY created_at DESC
LIMIT 50;Look at key, rows, and Extra. Using index means a covering index supplied all needed columns. Using filesort is not automatically fatal, but it becomes interesting when the sorted row count is large or the query is hot.
Covering indexes reduce the second lookup
Because secondary indexes end with the primary key, an index can sometimes answer a query without reading clustered rows. This is powerful for hot reads, but every extra index costs write amplification, memory, and backup size.
CREATE INDEX idx_account_created_cover
ON events (account_id, created_at, id, event_type);Do not add a covering index to every query-shaped complaint. Measure query frequency and write cost first. A rarely-used report does not deserve to tax every insert forever.
Histograms, statistics, and stale plans
The optimizer uses index statistics and, in newer versions, histograms. They can go stale after large data loads or uneven growth.
ANALYZE TABLE events;
SHOW INDEX FROM events;Field notes
EXPLAIN ANALYZEin MySQL 8 shows actual execution behavior; use it on a safe clone or production-like data distribution.SELECT *defeats many covering-index strategies and quietly moves more bytes than necessary.- Drop duplicate indexes carefully: an index that looks redundant may exist for a foreign key or a different sort order.
Was this lesson helpful?