Estimated reading time: 1 min read

An execution plan shows how MySQL retrieves and sorts rows. Use it to choose an index for the work the query actually needs to do.

Reading the execution plan

EXPLAIN describes the plan MySQL chose. EXPLAIN ANALYZE runs the statement and adds observations such as actual rows, loops, and timings. Compare the rows processed with the rows returned to see whether scans, filters, or sorts account for the cost. Account for the work of running the query before using it in production.

Ordering columns in a composite index

A B-tree index on (tenant_id, created_at) groups entries by tenant and orders them by time within each tenant. A query that filters by one tenant and requests its newest rows can use that order. The same index offers a different access path for a query that filters only by time because it starts with tenant_id.

Storage and write cost

An index consumes storage, and writes that affect its entries require maintenance. Start with the index that matches the measured query pattern, compare the plan after the change, and check write latency before adding overlapping indexes.

All fundamentals