# Composite & Partial Indexes

> Index several columns together, learn the leftmost-column rule that decides whether it helps, and shrink an index with a partial one.

Subject: SQL | Lesson 40 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/sql/composite-and-partial-indexes
Previous: [Indexes & B-Trees](https://codewithanimation.in/sql/indexes-and-b-trees.md) | Next: [EXPLAIN & Query Plans](https://codewithanimation.in/sql/explain-and-query-plans.md)

## One index, several columns, or only some rows

A **composite** (multicolumn) index covers more than one column: `CREATE INDEX ON orders (customer, status)`. Its entries are sorted by `customer` first, and by `status` inside each customer, like a phone book sorted by last name and then first name.

A **partial** index covers only the rows that satisfy a condition: `CREATE INDEX ON orders (customer) WHERE status = 'open'`. It holds fewer entries, so it is smaller and cheaper to keep up to date.

## Match the index to the query

A query that filters on two columns together can often be answered by one composite index much faster than by two separate ones. A table where most rows are "done" and only a few are "open" does not need an index entry for every finished row.

## The leftmost-column rule

The manual's rule: equality tests on the leading columns, plus a range test on the first column that has no equality test, limit the part of the index that is read. Tests on columns further right are checked inside the index, which saves table visits, but they do not shrink the part you scan.

In the lab, the table has 40 orders, 8 of them Asha's, and 6 open. With the index `(customer, status)` the query `customer = 'Asha' AND status = 'open'` reads just 2 entries. The query `customer = 'Asha'` reads Asha's 8 entries, still narrow, because customer is the leading column.

The query `status = 'open'` alone cannot start from the left end of `(customer, status)`, so the whole index is read (40 entries), although only 6 rows are fetched. Flip the index to `(status, customer)` and the picture reverses. PostgreSQL 18 can sometimes skip ahead, but the manual says that is generally only worthwhile when the leading column has very few distinct values.

A range on the next column keeps the scan narrow: with `(customer, day)`, the query `customer = 'Asha' AND day >= 20` reads only 2 entries.

## Common mistake: Too many columns, and partial index limits

The manual says multicolumn indexes should be used sparingly: one column is usually enough, and an index with more than three columns is unlikely to help unless the way the table is used is very predictable. Every extra index is more work on every write.

A partial index is used only when the query's `WHERE` clearly includes its condition. `WHERE customer = 'Asha'` alone does not say `status = 'open'`, so rows outside the index could match, and the planner cannot use it. Parameterized clauses, where the value arrives later, do not match a partial index either.

Do not make one partial index per category (one for each status, one for each country). The manual calls that a bad idea: a single composite index on `(category, data)` does the job better.

A **unique partial index** is a handy trick: `CREATE UNIQUE INDEX ... ON tests (subject, target) WHERE success` allows many failed tests for a pair, but only one successful one.

## Tip: Equality columns first, test with EXPLAIN

Put the columns you test with `=` first, and a column you test with a range after them. Check each new index with `EXPLAIN` against the real queries, and drop indexes that no query uses.

## Try it: Pick a query and an index

Start with (customer, status). Then change the query, then change the index, and watch how many index entries are read and how many table rows are visited.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/composite-and-partial-indexes

## Check yourself

### Question 1

There is an index on `(customer, status)`. Which query can search it from the left end?

- A. WHERE customer = 'Asha'
- B. WHERE status = 'open'
- C. Neither, a composite index only works for both columns together

Correct answer: A

A composite index is sorted by the first column, then the second. A test on the first column can use it.

- Why not B: status is the second column. Without the leading column, the index has no left end to start from.
- Why not C: The manual says it can be used with any subset of its columns, and is most efficient when the leading columns are constrained.

### Question 2

You need `WHERE customer = 'Asha' AND day >= 20` to be fast. Which index narrows the scan most?

- A. (customer, day)
- B. (day, customer)
- C. (customer) alone

Correct answer: A

Equality on the leading column, then a range on the next one, is the case the manual says limits the scanned part of the index.

- Why not B: Here the leading column has only a range test, which narrows less than an equality test on the leading column.
- Why not C: It finds Asha's rows, but the day test then needs a table visit for each of them.

### Question 3

A partial index covers `WHERE status = 'open'`. A query filters only on `customer = 'Asha'`. Can the planner use the partial index?

- A. No, because the query does not imply status = 'open'
- B. Yes, because it contains Asha's rows
- C. Yes, but only on a small table

Correct answer: A

A partial index is only complete for rows that satisfy its predicate, so it is used only when the query's WHERE condition implies it.

- Why not B: It only contains Asha's open rows. Her other rows are missing, so using it would give a wrong answer.
- Why not C: Table size does not matter. The planner needs the query to guarantee the index's condition.

## Remember this

- A composite index is sorted by its first column, then the second, and so on.
- Equality tests on the leading columns, then a range on the next, limit how much of the index is read.
- A partial index holds only rows that match a condition, so it is smaller, and it is used only when the query implies that condition.
- Use few columns (rarely more than three), put equality columns first, and check with EXPLAIN.
