Optimization

Composite Indexes: Multi-Column Power

Senior Data Analyst
March 15, 2026
5 min read

The Scenario

My query filters on `customer_id` AND `order_date`. A single-column index only helps with one.

The Composite Index

CREATE INDEX idx_orders_cust_date ON orders (customer_id, order_date);

Column Order Matters!

The index on `(customer_id, order_date)` helps queries that filter:

  • By `customer_id` alone: ✓
  • By `customer_id` AND `order_date`: ✓
  • By `order_date` alone: ✗ (The index MUST be used left-to-right).
  • Pro Tip

    Think of it like a phone book sorted by Last Name, then First Name. You can look up "Smith," but you can't easily look up everyone named "John."

    *Day 75: Avoiding Index Killers.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.