Column order in a multi-column index decides which queries can use it. Watch one query take the index and an almost identical one refuse it.
Composite and Covering Indexes
Which queries can use INDEX (status, customer_id)
query.sqlSQLite
-- The index is on (status, customer_id), in that order.
-- This query leads with status, so it can use it.
EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE status = 'paid' AND customer_id = 42;
Result
Worth knowing
A composite index is sorted by its first column, then by its second within each value of the first, and so on.
That makes any leading prefix of the columns usable, and nothing else. (a, b) serves a and a, b — never b alone.
A covering index contains every column the query needs, so the table is never visited at all.
Two single-column indexes are not equivalent to one composite index over the same columns.
Overview
An index on (last_name, first_name) is ordered first by last name, then by first name within each last name. It is useful for searches on last_name and often for searches on both columns. It is generally less useful for a query that filters only by first_name.
An index on (last_name, first_name) is ordered first by last name, then by first name within each last name. It is useful for searches on last_name and often for searches on both columns. It is generally less useful for a query that filters only by first_name.
For example, (customer_id, created_at) can support a customer's orders in date order:
query.sqlSQLite
SELECT order_id, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC;
Result
Equality conditions on leading columns commonly narrow the search before later columns are used. A range condition changes how later columns can help. The planner and engine matter: PostgreSQL can sometimes use skip scans, so the “leftmost prefix” is a useful design guide, not a universal absolute.
Covering an entire query
An index is covering for a query when it contains all the columns needed to find and return the requested rows. The engine may then avoid reading the table for some or all results. In SQLite this is reported as a “covering index” in EXPLAIN QUERY PLAN. PostgreSQL's index-only scans have additional visibility requirements, so an index containing every selected column does not guarantee a table-free read.
Design and trade-offs
Choose the column order from real query patterns: equality filters, range filters, joins, and requested ordering. Then inspect representative plans. Do not add every selected column to the index automatically. Larger indexes consume storage, slow inserts and updates, and increase cache pressure. Two separate indexes can help independent filters, but are not equivalent to one composite index when the query needs a particular combined order.
Start from the simpler indexing guide, then compare plans before and after an index change.
Equality, range, and ordering example
For an index on (tenant_id, created_at), a query that filters tenant_id by equality and then asks for a created_at range can often use both parts effectively. A query filtering only on created_at cannot generally navigate the same ordered structure as directly as an index beginning with that column. If the query also sorts by created_at, the index direction and filter shape may let the engine avoid a separate sort. Verify the exact plan and data selectivity; one index cannot optimally serve every combination of filters.
Check the full query shape
The best index depends on all parts of the query, not just its WHERE clause. If a report filters by tenant, sorts newest-first, and selects a few columns, test an index ordered by tenant and timestamp, with included columns only if the database supports them and measurements justify the extra size. An index can satisfy a sort in one direction or its reverse, but mixed directions and NULL ordering may change that. Read the plan for both a common tenant and a tenant with unusually many rows.
An index on (status, customer_id) exists. Which query can use it for a seek?
The index is sorted by status first, so all the 'paid' rows are contiguous. Rows for customer 42 are scattered across every status, so there is no block to find.
What does a covering index avoid?
An ordinary lookup searches the index and then fetches each row from the table by random access, which usually dominates. A covering index removes that second stage.
Why should equality columns come before range columns in a composite index?
An equality narrows to a contiguous block that the next column is sorted within. Once a range opens up, the following columns are no longer in a single ordered run.
Cheat sheet
Composite and Covering Indexes
Build a composite index, reorder the columns, and watch the query plan switch between an index scan and a full table scan on the same query.
Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.