Composite (Multi-Column) Index and the Leftmost Prefix Rule
Last updated: 2026-11-04
Composite (Multi-Column) Index and the Leftmost Prefix Rule
A composite index combines several columns into one key in the order given, so it can serve queries that constrain a prefix of those columns — the leftmost prefix rule. An index on (a, b) serves WHERE a = ?, WHERE a = ? AND b = ?, and WHERE a = ? ORDER BY b, but it cannot efficiently serve a query that constrains only b, because a cannot be skipped. Column order must match the query pattern: equality columns first, range columns after.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> ( <col1> [ASC | DESC], <col2> [ASC | DESC], ... )
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| col1, col2, ... | The index key columns, in order. The order is the priority: earlier columns can be searched on their own, later ones only take effect when the earlier ones are constrained too. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 30000
CREATE INDEX
ANALYZE
-- First EXPLAIN: the leading column tenant_id is constrained, so the composite index is used
Aggregate (cost=211.45..211.46 rows=1 width=8)
-> Bitmap Heap Scan on idx_demo_multi (cost=6.61..210.70 rows=300 width=0)
Recheck Cond: (tenant_id = 7)
Heap Blocks: exact=191
Buffers: shared hit=191 read=3
-> Bitmap Index Scan on idx_multi_tenant_created (cost=0.00..6.54 rows=300 width=0)
Index Cond: (tenant_id = 7)
Index Searches: 1
Buffers: shared hit=3
Planning Time: 0.160 ms
Execution Time: 0.535 ms
-- Second EXPLAIN: constraining only created_at (the second column) cannot use the leftmost prefix, so it falls back to a seq scan
Aggregate (cost=610.73..610.74 rows=1 width=8)
-> Seq Scan on idx_demo_multi (cost=0.00..567.00 rows=17493 width=0)
Filter: (created_at > '2026-06-01 00:00:00+00'::timestamp with time zone)
Rows Removed by Filter: 12534
Buffers: shared hit=192
Planning Time: 0.159 ms
Execution Time: 6.802 ms
-- Query result
count
-------
300
(1 row)
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- The leftmost prefix rule: an index on (a, b, c) serves queries on a, a+b and a+b+c, but cannot efficiently serve queries that constrain only b or c.
- Column order decides effectiveness: put equality columns first and range columns after, because a B-tree can no longer narrow the scan with later columns once a range predicate appears.
- In this example (tenant_id, created_at DESC) and (created_at DESC, tenant_id) serve entirely different query sets: the first filters by tenant then sorts by time, the second filters by time.
- The DESC keyword itself barely affects efficiency; what matters is that ORDER BY created_at DESC can reuse the ordering stored in the index and skip an extra sort.
- Keep composite indexes narrow: every extra column grows the index and the insert cost, and two or three columns usually cover the main query patterns.
- If a query repeatedly misses the leading column, consider a separate index that starts with that column.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat exactly does the leftmost prefix rule mean?
AThe keys of a composite index are a concatenation in a fixed order, so a lookup must start from the first column and continue consecutively. For (a, b), WHERE a = 1 AND b = 2 and WHERE a = 1 both use the index, but WHERE b = 2 does not — skipping a makes the key impossible to locate. It is like flipping a dictionary: you must start with the first letter.
QDoes the column order of a composite index really matter that much?
AVery much so. An index on (a, b) only serves prefixes starting with a, while (b, a) only serves prefixes starting with b. If the workload has both tenant-based and time-based access patterns, build two indexes — or both orderings, paying the extra write and space cost.