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.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX <index_name> ON <table_name> ( <col1> [ASC | DESC], <col2> [ASC | DESC], ... )

⚙️ Parameters

index_nameName of the new index.
table_nameThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏