Learning navigation

Code with Animation

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.

SQL7 min readConcept 40 of 44

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.

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.

SELECT * FROM orders
WHERE customer = 'Asha' AND status = 'open';
 
CREATE INDEX orders_cs ON orders (customer, status);

The index is searched from its left end.

Index entries read2 of 40

Table rows visited2 of 40

Rows returned: 2 rows. The index holds 40 entries.

The leading columns (customer, status) narrow the search to 2 of 40 index entries.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1There is an index on `(customer, status)`. Which query can search it from the left end?
  2. 2You need `WHERE customer = 'Asha' AND day >= 20` to be fast. Which index narrows the scan most?
  3. 3A partial index covers `WHERE status = 'open'`. A query filters only on `customer = 'Asha'`. Can the planner use the partial index?

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.

Done with this concept?

Mark it complete to track your progress. No login needed.