Composite and Covering Indexes

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.sql SQLite
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.

In this guide
  1. How column order shapes a composite index
  2. Covering an entire query
  3. Design and trade-offs
  4. Equality, range, and ordering example
  5. Check the full query shape
  6. Further reading

How column order shapes a composite index

For example, (customer_id, created_at) can support a customer's orders in date order:

query.sqlSQLite
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.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. An index on (status, customer_id) exists. Which query can use it for a seek?

  2. What does a covering index avoid?

  3. Why should equality columns come before range columns in a composite index?

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.

DATABASE · vizlearn.in/database/composite_and_covering_indexes.html

About the author

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.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.