Database Composite Indexes
What you will learn
- Create an index on more than one column
- Apply the leftmost-prefix rule
- Choose the column order
- Recognise a covering index
1. One index, several columns
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). The indexes you create live in your browser's copy; Reset data removes them. For EXPLAIN QUERY PLAN the cell shows only the plan text.
A composite index (also called a multi-column index) is built on two or more columns, sorted by the first, then by the second within equal firsts, and so on, like a phone book sorted by last name, then first name.
In the Shop, index the city a parcel is shipped to and its status together:
In the Bank, index the account and the kind of a ledger row together:
In the School, index the section and the due date of an assignment together:
CREATE INDEX idx_orders_ab ON orders (ship_city, status);
(done)
CREATE INDEX idx_transactions_ab ON transactions (account_id, kind);
(done)
CREATE INDEX idx_assignments_ab ON assignments (section_id, due_date);
(done)
2. The leftmost-prefix rule
Because the index is sorted by its first column first, it can help any query that fixes the first column, with or without the second. It cannot help a query on the second column alone, just as a phone book sorted by last name is no use for finding everyone with the first name "Rina". (In the cells below the first column is the one in CREATE INDEX ... (first, second) above.)
EXPLAIN QUERY PLAN SELECT * FROM orders WHERE ship_city = 'Leeds' AND status = 'paid';
EXPLAIN QUERY PLAN SELECT * FROM orders WHERE ship_city = 'Leeds';
EXPLAIN QUERY PLAN SELECT * FROM orders WHERE status = 'paid';
EXPLAIN QUERY PLAN SELECT * FROM orders WHERE ship_city = 'Leeds' ORDER BY status;
plan
------------------------------------------------------------------
SEARCH orders USING INDEX idx_orders_ab (ship_city=? AND status=?)
(1 row)
plan
-----------------------------------------------------
SEARCH orders USING INDEX idx_orders_ab (ship_city=?)
(1 row)
plan
-----------
SCAN orders
(1 row)
plan
-----------------------------------------------------
SEARCH orders USING INDEX idx_orders_ab (ship_city=?)
(1 row)
EXPLAIN QUERY PLAN SELECT * FROM transactions WHERE account_id = 17 AND kind = 'deposit';
EXPLAIN QUERY PLAN SELECT * FROM transactions WHERE account_id = 17;
EXPLAIN QUERY PLAN SELECT * FROM transactions WHERE kind = 'deposit';
EXPLAIN QUERY PLAN SELECT * FROM transactions WHERE account_id = 17 ORDER BY kind;
plan
-----------------------------------------------------------------------------
SEARCH transactions USING INDEX idx_transactions_ab (account_id=? AND kind=?)
(1 row)
plan
------------------------------------------------------------------
SEARCH transactions USING INDEX idx_transactions_ab (account_id=?)
(1 row)
plan
-----------------
SCAN transactions
(1 row)
plan
------------------------------------------------------------------
SEARCH transactions USING INDEX idx_transactions_ab (account_id=?)
(1 row)
EXPLAIN QUERY PLAN SELECT * FROM assignments WHERE section_id = 1 AND due_date = '2025-02-01';
EXPLAIN QUERY PLAN SELECT * FROM assignments WHERE section_id = 1;
EXPLAIN QUERY PLAN SELECT * FROM assignments WHERE due_date = '2025-02-01';
EXPLAIN QUERY PLAN SELECT * FROM assignments WHERE section_id = 1 ORDER BY due_date;
plan
-------------------------------------------------------------------------------
SEARCH assignments USING INDEX idx_assignments_ab (section_id=? AND due_date=?)
(1 row)
plan
----------------------------------------------------------------
SEARCH assignments USING INDEX idx_assignments_ab (section_id=?)
(1 row)
plan
----------------
SCAN assignments
(1 row)
plan
----------------------------------------------------------------
SEARCH assignments USING INDEX idx_assignments_ab (section_id=?)
(1 row)
In order:
- Both columns: used for both (
first=? AND second=?). - First column only: used for
first=?. - Second column only: not used (
SCAN). - First column fixed and
ORDER BYthe second: used, and noUSE TEMP B-TREEline appears, because within one value of the first column the index already holds the rows sorted by the second.
So the order of columns matters. A reasonable starting rule: put the columns you test with = first, then the one you sort or range by, and make sure the leading column is one your queries always include.
One composite index replaces the need for a separate index on the first column, since it serves every query that fixes the first column. That saves space and write time.
3. Covering indexes
If every column a query needs is inside the index, the database can answer from the index alone without touching the table. This is a covering index, and the plan says so:
EXPLAIN QUERY PLAN SELECT ship_city, status FROM orders WHERE ship_city = 'Leeds';
EXPLAIN QUERY PLAN SELECT ship_city, status, customer_id FROM orders WHERE ship_city = 'Leeds';
plan
--------------------------------------------------------------
SEARCH orders USING COVERING INDEX idx_orders_ab (ship_city=?)
(1 row)
plan
-----------------------------------------------------
SEARCH orders USING INDEX idx_orders_ab (ship_city=?)
(1 row)
EXPLAIN QUERY PLAN SELECT account_id, kind FROM transactions WHERE account_id = 17;
EXPLAIN QUERY PLAN SELECT account_id, kind, txn_date FROM transactions WHERE account_id = 17;
plan
---------------------------------------------------------------------------
SEARCH transactions USING COVERING INDEX idx_transactions_ab (account_id=?)
(1 row)
plan
------------------------------------------------------------------
SEARCH transactions USING INDEX idx_transactions_ab (account_id=?)
(1 row)
EXPLAIN QUERY PLAN SELECT section_id, due_date FROM assignments WHERE section_id = 1;
EXPLAIN QUERY PLAN SELECT section_id, due_date, title FROM assignments WHERE section_id = 1;
plan
-------------------------------------------------------------------------
SEARCH assignments USING COVERING INDEX idx_assignments_ab (section_id=?)
(1 row)
plan
----------------------------------------------------------------
SEARCH assignments USING INDEX idx_assignments_ab (section_id=?)
(1 row)
The first query needs only the two indexed columns: COVERING INDEX. The second also needs one more column, which is not in the index, so each match requires a trip to the table. Adding a third column to the index makes an aggregate fully covered:
DROP INDEX idx_orders_ab;
CREATE INDEX idx_orders_abc ON orders (ship_city, status, order_date);
EXPLAIN QUERY PLAN SELECT MAX(order_date) FROM orders WHERE ship_city = 'Leeds' AND status = 'paid';
SELECT MAX(order_date) AS result FROM orders WHERE ship_city = 'Leeds' AND status = 'paid';
plan
----------------------------------------------------------------------------
SEARCH orders USING COVERING INDEX idx_orders_abc (ship_city=? AND status=?)
(1 row)
result
----------
2026-09-27
(1 row)
DROP INDEX idx_transactions_ab;
CREATE INDEX idx_transactions_abc ON transactions (account_id, kind, amount_cents);
EXPLAIN QUERY PLAN SELECT SUM(amount_cents) FROM transactions WHERE account_id = 17 AND kind = 'deposit';
SELECT SUM(amount_cents) AS result FROM transactions WHERE account_id = 17 AND kind = 'deposit';
plan
---------------------------------------------------------------------------------------
SEARCH transactions USING COVERING INDEX idx_transactions_abc (account_id=? AND kind=?)
(1 row)
result
------
288900
(1 row)
DROP INDEX idx_assignments_ab;
CREATE INDEX idx_assignments_abc ON assignments (section_id, due_date, max_points);
EXPLAIN QUERY PLAN SELECT SUM(max_points) FROM assignments WHERE section_id = 1 AND due_date = '2025-02-01';
SELECT SUM(max_points) AS result FROM assignments WHERE section_id = 1 AND due_date = '2025-02-01';
plan
-----------------------------------------------------------------------------------------
SEARCH assignments USING COVERING INDEX idx_assignments_abc (section_id=? AND due_date=?)
(1 row)
result
------
NULL
(1 row)
The trade-off: a wider index is larger and slower to maintain, so add covering columns only for queries that are important and frequent.
Worked example: indexing an "orders for a city this month" screen
The screen runs WHERE city = ? AND placed >= ? AND placed < ? ORDER BY placed. Equality on city first, then the range and sort column placed:
CREATE INDEX idx_orders_city_placed ON orders (city, placed);
Within one city the index holds orders sorted by date, so the database can jump to the city, then to the start of the month, and read on in order: no scan, no sort. Reversing the columns, (placed, city), would not suit this screen, because the date range would cut across all cities. Run on the lesson's table, the plan was SEARCH orders USING INDEX idx_orders_city_placed (city=? AND placed>? AND placed<?) with no USE TEMP B-TREE line, so there was no separate sort. With an index on (placed, city) instead, the plan was SEARCH orders USING INDEX idx_pc (placed>? AND placed<?): only the date range narrowed the search, and city was checked row by row.
Common mistakes
- Expecting a
(city, status)index to helpWHERE status = .... - Creating both
(city)and(city, status); the second already servescityqueries. - Putting a range column before the equality columns, which wastes the later columns.
- Making huge covering indexes for rare queries.
- A composite index is sorted by its first column, then the second, and so on.
- It helps queries that use its leftmost columns, not ones that skip the first.
- Column order matters: equality columns first, then range or sort columns.
- A covering index holds every column the query needs, so the table is not touched.
- Wider indexes cost more space and slower writes.
Practice
1. An index is on (a, b, c). Which of these can use it: WHERE a = 1, WHERE b = 2, WHERE a = 1 AND b = 2?
Show answer
WHERE a = 1 and WHERE a = 1 AND b = 2. Not WHERE b = 2, which skips the first column.
2. Why was ORDER BY status free in the lesson's fourth plan?
Show answer
With city fixed, the index already lists the rows sorted by status.
3. What does COVERING INDEX in a plan mean?
Show answer
The query was answered from the index alone, without reading the table.
4. You often run WHERE city = ? and sometimes WHERE city = ? AND status = ?. How many indexes do you need?
Show answer
One: (city, status) serves both.
5. Why not put every column in every index?
Show answer
Indexes would be big and slow every insert, update and delete.
6. For WHERE country = ? AND age > ?, which column order would you try first?
Show answer
(country, age): equality column first, range column second. Confirm with the plan.
?>