Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Database › Indexes and execution plans

Composite and covering indexes

A composite index can be used only in column order

Why it exists — With only indexes made separately for each column, for a query with several conditions the DB usually narrows the range using just one of them, and must filter by the other conditions by reading the found rows one by one. A composite index ties the columns used together into one index in a fixed order, so that the range is narrowed by several conditions at once, and a query that selects only those columns is answered by reading only the index.

Where it's used today — It is created for list queries in which the same combination of conditions and sorting recurs, such as "one member's paid orders, most recent first". It is usual to put columns compared with equality first and columns used for range conditions or sorting after them. When the combinations of conditions vary so much that one order cannot fit them all, indexes are created only for the frequently used combinations, and you also weigh that more indexes make writes slower.

An index on several columns is sorted by the first column, then by the next column, and so on. So the WHERE conditions must start from the first column to narrow the search range, and once a range condition is reached, the columns after it cannot be used to narrow the range. When every column the query reads is in the index, so that the table does not have to be visited, the index is called a covering index for that query.

Checked against real execution · All 32 combinations run on MySQL 8.4 and compared (EXPLAIN and Handler_read counters)

Concepts in this lab

Composite index and leftmost prefix
A composite index is an index built on two or more columns together. It is sorted by the first column first and then by the second column within that, so it can be used for a search only when the conditions use its columns in order, starting from the left. An index on (a, b, c) is used for a condition on a or on a and b, but not for a condition on b alone (as an exception, in MySQL 8.0.13 and later the optimizer can use this index with Skip Scan when the leading column has few distinct values).
Covering index
Why it exists — Even when a secondary index finds the rows, if the columns the query needs are not in the index, the table has to be read again for every row found, so the query gets slower as the result grows. A covering index makes the columns the query uses part of the index columns, which removes this extra read.
Where it's used today — It is used when designing an index around a query that often reads only a few columns, such as a list or a count. The deferred join, which first picks only the ids through the index on a list with a large OFFSET and then reads just those rows, also relies on it.
An index that contains every column a query needs, so the result can be produced from the index alone without reading the table rows. Skipping the table access makes the read faster. In a MySQL execution plan, it appears as Using index in the Extra column.
Clustered index and secondary index
Why it exists — If rows are piled up separately in the table and the index only points to their locations, then even a lookup by primary key needs one more read of the row after reading the index. A clustered index stores the rows themselves in key order, so lookups by primary key and range lookups finish with only an index search.
Where it's used today — This is why, in InnoDB, the primary key is set to an increasing value such as AUTO_INCREMENT. If you use a random UUID as the primary key, rows are inserted in the middle, so page splits are frequent, and each secondary index grows larger because the long key is attached to it.
A clustered index stores the row data itself at the bottom of the index (the leaf nodes), so the data of the table is stored in the order of its key. In MySQL InnoDB, the primary key is the clustered index. An InnoDB secondary index holds the primary key value in its leaf nodes. After a lookup in the secondary index, that primary key is used for one more lookup in the clustered index to read the row.
Cardinality and selectivity
Cardinality is the number of distinct values in a column. The more distinct values there are (for example, a national ID number or an email address), the fewer rows one condition leaves and the more effective an index is. The fewer kinds of values there are (for example, gender), the more rows match a condition and the less effective an index is. Selectivity is the cardinality divided by the number of rows in the table; the closer it is to 1, the better a single condition narrows down the rows.
Query execution plan (EXPLAIN)
The plan that the database decides on for how to run a query. It shows which indexes are used, in what order the tables are joined, and how many rows the database expects to read. You view it by putting EXPLAIN in front of the query, and it is the starting point for finding the cause when a query is slow.

Try these

Running with the default settings

  1. The orders table has 1,000,000 rows. The index is sorted in the order member_id → status → created_at. Entries with the same first column are grouped together, within that group they are in order of the second column, and within that in order of the third column.
  2. Column #1 (member_id): there is the condition member_id = 7, and it is the first column of the index, so this condition narrows the range to read.
  3. Column #2 (status): there is the condition status = 'PAID', and the columns before it are all fixed, so this condition narrows the range to read.
  4. Column #3 (created_at): there is the condition created_at >= '2026-09-01', and the columns before it are all fixed, so this condition narrows the range to read.
  5. 50 index entries in the narrowed range are read.
  6. SELECT * needs columns that are not in the index too. For each of the remaining 50 entries, the query goes to the table (the clustered index) and reads the whole row.

Result of this run

Code

CREATE INDEX idx_orders ON orders (member_id, status, created_at);

SELECT *
  FROM orders
 WHERE member_id = 7
   AND status = 'PAID'
   AND created_at >= '2026-09-01';

Assumptions of this model

All 32 combinations were run on MySQL 8.4.11 against an orders table with 1,000,000 rows and compared with this lab. In the data, 1,000 members have 1,000 rows each (member_id = 7 is 0.1% of all rows), status has three values and 'PAID' is 50%, and 10% of the rows match the date condition, and the three conditions were made independent of each other. type and Extra are exactly what EXPLAIN showed. The amounts read are the Handler_read_% values shown by SHOW SESSION STATUS after each query ran to the end. Index entries read is Handler_read_next, and for the combinations with Using index condition (index condition pushdown) in Extra it is the value measured by running the query once more with the same plan and this feature turned off. With it turned on, only the entries that remain after filtering at the index stage are counted, and that remaining number is the table rows read. For the combinations with Using index in Extra the table is not read, so the table rows read are 0, and for a full table scan it is Handler_read_rnd_next minus the one last call that detects the end. The optimizer chooses a skip scan by calculating costs, so the result depends on the data distribution and the MySQL version. With this data it chose a skip scan in six combinations that select only columns in the index, and in the two combinations where created_at, which has many values, would have to be skipped, it compared the costs and did not choose it (checked with the optimizer trace). With the order (member_id, created_at, status) and all three conditions, key_len in EXPLAIN is 51, which includes status, but the index entries actually read were the 100 that match the member_id and created_at conditions.

Previous lab: Indexes and full table scans · Next lab: Reading the execution plan

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these