Composite Index¶
1. What is it? (2-3 lines)¶
A Composite Index (also called a Multi-Column Index) is an index built on two or more columns of a database table ((colA, colB, colC)). Data inside the index leaf pages is sorted lexicographically according to the specified left-to-right column order.
2. Why does it exist?¶
Queries frequently filter or sort by multiple conditions (WHERE tenant_id = 5 AND status = 'ACTIVE' ORDER BY created_at DESC). Creating single-column indexes on tenant_id, status, and created_at forces the query optimizer to pick one index or attempt expensive Index Intersections. A Composite Index evaluates all conditions in a single \(O(\log N)\) B+ Tree seek.
3. Where is it used?¶
- Multi-tenant enterprise SaaS applications (
tenant_id, user_id). - E-commerce order lookups (
user_id, status, created_at). - Time-series cursor pagination (
created_at, id).
4. How does it work? (simple flow)¶
- Lexicographical B+ Tree Sorting: Index keys are stored as composite tuples
(colA, colB, colC). Rows are sorted bycolAfirst; ifcolAvalues are identical, sorted bycolB; ifcolBidentical, sorted bycolC. - Leftmost Prefix Rule: The B+ Tree can ONLY be searched if the query filters include the leftmost column (
colA). - Valid:
WHERE colA = 1,WHERE colA = 1 AND colB = 2,WHERE colA = 1 AND colB = 2 AND colC = 3. - Invalid (Skipped Index):
WHERE colB = 2,WHERE colC = 3(Bypass index, triggers Full Table Scan). - Range Truncation: A range filter (
<,>,LIKE 'abc%') on an earlier column (colB) stops index usage for subsequent columns (colC).
5. Advantages¶
- Combines multiple column predicates into a single B+ Tree traversal.
- Eliminates secondary sorting operations (
ORDER BYfilesort) if index column order matchesORDER BY. - Acts as a Covering Index if query selects only a subset of composite index columns.
6. Limitations¶
- Leftmost Prefix Constraint: Queries that omit the leading column cannot utilize the index.
- Range Truncation Boundary: Once a range condition (
>) is applied tocolB,colCcannot be filtered via index seek. - Larger index size on disk and RAM compared to single-column indexes.
7. Common interview questions (10)¶
- What is the Leftmost Prefix Rule in Composite Indexes?
- Given index
(A, B, C), will queryWHERE B = 2 AND C = 3use the index? Why or why not? - How does column order in
(A, B)affect index efficiency based on column selectivity/cardinality? - What happens to composite index usage in query
WHERE A = 1 AND B > 10 AND C = 5? - How does Index Skip Scan (MySQL 8.0+) bypass the Leftmost Prefix Rule under low cardinality?
- How should you order columns in a Composite Index to satisfy both
WHEREandORDER BYclauses? - Why is
(tenant_id, created_at)better than(created_at, tenant_id)for multi-tenant range queries? - What is the maximum number of columns allowed in a MySQL or PostgreSQL composite index?
- How does Index Condition Pushdown (ICP) optimize composite index range queries?
- How do composite indexes support Cursor-Based Pagination on non-unique columns?
8. Common misconceptions¶
- Misconception: Order of columns in
WHEREclause must match index column order. Fact: Query optimizer automatically reorders equality predicates inWHEREclauses to match index column order! - Misconception: Creating separate indexes on
AandBis identical to composite index(A, B). Fact: Two single-column indexes require index intersection/merge, which is far slower than a single composite index seek.
9. Related Concepts¶
10. 30-second cheat sheet¶
- Rule 1: Leftmost Prefix Rule — Query MUST filter by leading column
Ato use index(A, B, C). - Rule 2: Column Ordering — Put high-cardinality equality columns first (
tenant_id), then range columns (created_at). - Rule 3: Range Truncation —
WHERE A = 1 AND B > 5 AND C = 10\(\implies\) Index filtersA&B, but scansC. - Benefit: Replaces filesort and index merges with a single \(O(\log N)\) B+ Tree seek.