← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · ✎ updated 7d
← Back to glossary

Composite Index Design

Database PHP 5.0+ Advanced
debt(d5/e5/b7/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

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.

e5 Effort Remediation debt — work required to fix once spotted

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.

b7 Burden Structural debt — long-term weight of choosing wrong

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.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

Also Known As

multi-column index compound index leftmost prefix rule

TL;DR

A composite index covers multiple columns — column order determines which queries benefit, following the leftmost prefix rule.

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

✗ More columns in an index is always better — a wide composite index has higher write overhead and may not be used if the leftmost column is not in the WHERE clause.

Why It Matters

An index on (status, created_at) cannot serve a query filtering only on created_at — the column order is an architectural decision with large performance consequences.

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

✗ Vulnerable
-- 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)
✓ Fixed
-- 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);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 165
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 2 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S
No pings yet today
Ahrefs 1
PetalBot 16 Perplexity 11 Google 11 Ahrefs 11 SEMrush 11 Scrapy 11 Amazonbot 6 Bing 6 Unknown AI 3 Twitter/X 2 Brave Search 2 Applebot 2 ChatGPT 1 Meta AI 1 Sogou 1
crawler 92 crawler_json 2 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Database database A database is an organized collection of data stored electronically, designed so programs can efficiently retrieve, add, update, and delete information.

Nearly every application needs to remember information between sessions. Databases provide the reliable, fast, and organized storage that makes persistent data possible at any scale.

💡 Always use prepared statements with placeholders—never concatenate user input directly into database queries.

Ask Codex about Database →
⚡ DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Order composite index columns: equality conditions first, range condition last, then ORDER BY columns — WHERE status='active' AND created_at > X uses index (status, created_at) not (created_at, status)
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
EXPLAIN showing multiple single-column indexes used separately; range condition column before equality column in composite index
Auto-detectable: ✓ Yes mysql-slow-query-log pt-query-digest laravel-debugbar
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File


✓ schema.org compliant