Composite Index Design
debt(d5/e5/b7/t7)
Closest to 'specialist tool catches' (d5). The term's detection_hints list mysql-slow-query-log, pt-query-digest, and laravel-debugbar as tools — these are specialist profiling/query analysis tools, not default linters. You must actively run EXPLAIN or analyze slow query logs to detect misuse; it won't surface in normal development until queries become slow.
Closest to 'touches multiple files / significant refactor' (e5). While quick_fix suggests the conceptual fix is simple (reorder columns), actually fixing composite index issues often requires: creating new indexes, dropping old ones, updating multiple queries that depend on index assumptions, and potentially rewriting ORM query builders. Migration and index rebuild time on large tables adds operational effort.
Closest to 'strong gravitational pull' (b7). Composite index design is a load-bearing architectural decision — the column order affects all queries touching those columns. The term applies across all PHP contexts (web, cli, queue-worker) per applies_to. Once established, the index structure shapes how you write WHERE clauses, how you order JOINs, and constrains future query patterns. Changing it requires migrations and careful coordination.
Closest to 'serious trap' (t7). The misconception explicitly states developers believe 'more columns is always better' and miss the critical leftmost prefix rule. This contradicts intuition from simpler indexing where you just add an index on a column. The common_mistakes confirm: putting range before equality seems logical but breaks usability. A competent developer from other domains would guess wrong about column ordering.
Also Known As
TL;DR
Explanation
A composite index on (a, b, c) supports queries filtering on a, (a, b), or (a, b, c) — but not on b alone or c alone. The leftmost prefix rule means the most selective or most frequently filtered column should come first, unless a range predicate forces a different order. Including columns needed by the SELECT clause creates a covering index, eliminating table access entirely. Wrong column order is one of the most common database performance mistakes.
Diagram
flowchart TD
IDX[Index on<br/>status, created_at, id]
subgraph Uses index fully
Q1[WHERE status='pending'<br/>AND created_at > '2026-01-01']
end
subgraph Uses index partially
Q2[WHERE status='pending'<br/>only leftmost column]
end
subgraph Cannot use index
Q3[WHERE created_at > '2026-01-01'<br/>skipped leftmost column]
end
IDX --> Q1
IDX -.->|partial| Q2
IDX -.->|full scan| Q3
style Q1 fill:#238636,color:#fff
style Q2 fill:#d29922,color:#fff
style Q3 fill:#f85149,color:#fff
Common Misconception
Why It Matters
Common Mistakes
- Putting the range predicate column before the equality predicate column — the index stops being usable for equality filtering after the range.
- Not checking EXPLAIN to verify the index is actually being used after creation.
- Duplicating single-column indexes when a composite index already covers them — wasted write overhead.
- Not including ORDER BY columns in the index — sort still hits the table without them.
Code Examples
-- Wrong order: range column first blocks equality filtering
CREATE INDEX idx_wrong ON orders (created_at, status);
-- Query: WHERE status = 'pending' AND created_at > '2026-01-01'
-- Index only used for created_at scan — status not leveraged
-- EXPLAIN shows: Index Scan (not Index Only Scan, partial use)
-- Correct: equality column first, then range
CREATE INDEX idx_orders_status_date ON orders (status, created_at DESC);
-- WHERE status = 'pending' AND created_at > '2026-01-01'
-- Full composite index used — efficient
-- Covering index (eliminates table access):
CREATE INDEX idx_orders_covering ON orders (status, created_at DESC, id, total);