liveBook · Manning

Composite index

As explained in Just Use Postgres!

A composite index is a database index defined over multiple columns, ordering entries first by one column and then by subsequent columns.

A composite index, also called a multicolumn index, covers several table columns. PostgreSQL sorts entries by the first column, then by each subsequent column within the preceding values, allowing one index to support related filtering and ordering queries. section 4.6 section 4.10

One index for a regional leaderboard

For player statistics, create an index ordered by region, score, and win count: CREATE INDEX idx_region_score_win_count ON game.player_stats (region, score DESC, win_count DESC); This ordering supports a query that finds players in one region by score and win count, and lets DISTINCT ON (region) return the highest-scoring player per region without an additional sort. EXPLAIN shows a Bitmap Index Scan for the filtered query and an Index Scan for the leaderboard query. section 4.6.2

Use composite indexes deliberately:

  • Put the leading column first when queries constrain or order by it; an index on (region, score) is not suited to a query using only score. section 4.6
  • Match the indexed order to the query order. Changing a leaderboard from region, score DESC, win_count DESC to region, win_count DESC, score DESC can force a sequential scan and sort. section 4.6.3
  • Reuse one composite index across related queries instead of adding many single-column indexes, while remembering that every index requires maintenance. section 4.6
  • Apply the same pattern to time-series data when queries commonly combine a timestamp with an identifier, such as (recorded_at, watch_id). section 9.7.1
Listing 4.8 Creating a composite index
CREATE INDEX idx_region_score_win_count 
ON game.player_stats (region, score DESC, win_count DESC);

Inspect the complete CREATE INDEX statement for the leaderboard example, including column order and descending sort directions. section 4.6.2

How Just Use Postgres! covers it

How deep it goes
Explains column ordering, leading-column behavior, execution plans, and caveats, but defers broader index administration.
Examples
Demonstrates regional player rankings, filtered score queries, and smartwatch measurements filtered by timestamp and device.
What it assumes
Assumes SQL query and WHERE/ORDER BY familiarity while establishing index categories, B-tree defaults, and execution-plan inspection.
What you can do after
Diagnose/debug with it by creating indexes, reading EXPLAIN plans, and checking whether ordering and filters use the intended index.
How it connects
Connects composite indexes with B-tree indexes, query execution plans, DISTINCT ON, and time-series data.

Also explained in

Compare how 3 books explain composite index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.