Composite and Covering Indexes, and the Write Cost
Overview
Knowing that indexes are fast is not enough. You also need to know how to design them for real queries, and why adding too many hurts.
This lesson covers indexes on several columns, indexes that answer a query alone, how to check what the database is doing, and the cost every index adds to writes.
Detailed Notes
What is a composite or covering index
- A composite index is one index on several columns, such as
(user_id, created_at). Entries are sorted by the first column, then by the second inside each first value. - A covering index contains every column the query needs, so the database answers from the index alone and never touches the table.
The problem it solves
A query often filters on one column and sorts by another. Two separate single-column indexes cannot do both jobs well. And even with an index, the database still has to fetch each full row from the table, which costs extra reads. Composite and covering indexes remove that extra work.
How it works
Column order matters. A composite index on (user_id, created_at) is like a phone book sorted by last name, then first name. It is great for:
user_id = 42user_id = 42 ORDER BY created_at
It does not help a query on created_at alone, just as a phone book does not help you find everyone named "Anna". This is the leftmost prefix rule: the index helps queries that use its columns starting from the left. Put the column you filter with equality first, then the range or sort column.
Covering. If your query only needs user_id, created_at and total, an index on those three columns lets the database return results straight from the index. This is often the fastest option for hot queries.
Selectivity. An index is useful when it narrows results a lot. An index on a column with only two values (like is_active) rarely helps, because half the table still matches.
Query plans. Ask the database how it will run a query (usually EXPLAIN) to see whether it uses an index or scans everything. Check the plan before and after adding an index.
The write cost. Every index must be updated on each insert, update or delete. Five indexes mean one write turns into six changes. Indexes also use disk and memory. So:
- Index the columns used in frequent filters, joins and sorts.
- Drop indexes nobody uses.
- Be careful on write-heavy tables.
A simple example
A phone book sorted by (last name, first name): looking up "Smith, John" is instant, but finding everyone called "John" means reading the whole book. And every extra directory you keep (street, business) must be fixed whenever someone moves.
System design example
A messaging app loads the latest 50 messages of a chat:
SELECT id, sender, text FROM messages
WHERE chat_id = 7 ORDER BY sent_at DESC LIMIT 50;
A composite index on (chat_id, sent_at) jumps to that chat and reads 50 entries already in order, with no extra sort. If the team then adds six more indexes "just in case", message sending gets slower because each new message updates seven structures. Design indexes from your real queries, and measure.
Summary
- A composite index covers several columns, and the column order matters.
- It helps queries that use its leftmost columns.
- A covering index answers the query without reading the table.
- Check query plans with EXPLAIN instead of guessing.
- Every index speeds reads but slows writes and uses space.
Architecture Diagram
Each write updates the table and every index on it.
- 1The application inserts a row.
- 2The database writes the row to the table.
- 3The same change is applied to each index.
- 4More indexes mean more work per write.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

