Indexes & Performance — Database

An index speeds up searching (SELECT with WHERE, JOIN, ORDER BY) but slows down INSERT, UPDATE, DELETE. A composite index (customer_id, created_at) is sorted…

When Sunset Coffee first opened, the "My orders this month" page appeared instantly. Now the orders table holds 20,000 orders, and the same page is starting to feel slow. In the SQL lesson Indexes & Performance you already met the cure: a one-column index, and SCAN turns into SEARCH.

An index is a sorted list of a column's values, each with a pointer to its row, so the database can jump straight to the rows it wants without reading the whole table. Here we design indexes: which columns deserve one, and when one index holds two columns, in which order. Picture a phone book sorted by family name, then first name. Finding "Santoso, Budi" is very fast. But finding everyone whose first name is "Budi" still means reading the book from the first page to the last.

The table we use

Sunset Coffee is busy now. The orders table holds 20,000 orders from 200 customers, 1 January to 7 October 2026:

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  status TEXT NOT NULL,       -- 'done' or 'pending'
  created_at TEXT NOT NULL,   -- the order's day, '2026-10-02'
  total INTEGER NOT NULL
);

Siti (customer_id 2) has 100 orders, 2 of them since 1 October. The "My orders this month" page runs:

SELECT * FROM orders
WHERE customer_id = 2 AND created_at >= '2026-10-01';

The main job of an index is to speed up finding data, meaning SELECT with WHERE, JOIN and ORDER BY. Let's compare three index designs with SQLite's EXPLAIN QUERY PLAN:

Index SQLite's plan Entries walked
none yet SCAN orders 20,000
(customer_id) SEARCH orders USING INDEX idx_orders_customer (customer_id=?) 100
(customer_id, created_at) SEARCH orders USING INDEX idx_orders_customer_date (customer_id=? AND created_at>?) 2

The last column is the number of rows matching the part of the index the plan uses. A one-column index already cuts 20,000 down to 100, but 98 of those are Siti's orders from other months, thrown away after being read.

Composite index: one index, two columns

CREATE INDEX idx_orders_customer_date ON orders(customer_id, created_at);

Column order: the leftmost prefix

Guess first: can a query with only WHERE created_at >= '2026-10-01', no customer_id, still use the index (customer_id, created_at)?

The index (customer_id, created_at) can usually only be used starting from its leftmost column. This is the leftmost prefix rule:

Query Uses the index? Plan
WHERE customer_id = 2 yes SEARCH ... (customer_id=?)
WHERE customer_id = 2 AND created_at >= '2026-10-01' yes, both columns SEARCH ... (customer_id=? AND created_at>?)
WHERE created_at >= '2026-10-01' no SCAN orders

Without customer_id, October's orders are scattered over 200 different places in the index, just like the first name "Budi" in the phone book. So SQLite reads the whole table.

Flip the order to (created_at, customer_id) and Siti's query becomes SEARCH ... (created_at>?): SQLite walks 500 entries (every order since 1 October) to find Siti's 2, and the query WHERE customer_id = 2 alone becomes a SCAN. The rule of thumb: columns searched with = go first, range columns (>=, BETWEEN) or ORDER BY go last. The leftmost prefix rule also holds in MySQL and PostgreSQL. The exception is skip scan: after ANALYZE, SQLite uses this index for a query on created_at alone (SEARCH ... (ANY(customer_id) AND created_at>?)), because customer_id has only 200 distinct values. MySQL 8.0.13 and later and PostgreSQL 18 also have skip scan. Do not design indexes around it.

Which columns deserve an index

Two questions decide: where is the column used, and how many distinct values does it have (its cardinality).

The column is... Example Advice
only shown in SELECT, never searched total no index needed
used in WHERE, JOIN or ORDER BY, with many distinct values (high cardinality) customer_id (200 values), email make an index
used in WHERE, but with few values (low cardinality) status (2 values), a boolean column consider a partial index

A plain index on status barely helps: 'done' is on 19,600 rows, so "going through the index" still means reading nearly all of them. What gets searched is usually the rare value, like the 400 'pending' orders. That's what a partial index is for, an index holding only the rows that meet a condition:

CREATE INDEX idx_orders_pending ON orders(created_at) WHERE status = 'pending';

This index holds only 400 entries, and SELECT * FROM orders WHERE status = 'pending' is answered with SCAN orders USING INDEX idx_orders_pending: it reads those 400 entries only, not 20,000 rows. SQLite and PostgreSQL have partial indexes, MySQL doesn't yet.

The price of an index

Every index has to be updated whenever the data changes. Indexes slow down INSERT, UPDATE and DELETE, and take up storage. A table with 4 indexes means one INSERT writes to 5 places. When importing millions of rows at once, teams often drop the indexes first and create them again after the import, because building an index once is far faster than updating it millions of times.

In a Laravel migration:

$table->string('email')->index();                // a one-column index
$table->index(['customer_id', 'created_at']);   // composite, array order = column order

⚠️ Wrong order, wrong index, too many indexes

TL;DR: An index speeds up searching (SELECT with WHERE, JOIN, ORDER BY) but slows down INSERT, UPDATE, DELETE. A composite index (customer_id, created_at) is sorted column by column from the left, so it serves customer_id alone or customer_id + created_at (20,000 entries down to 2), but not created_at alone (SCAN, except through skip scan): that's the leftmost prefix rule. = columns first, range columns last. A column in WHERE/JOIN/ORDER BY with high cardinality: make an index. A column that's only selected: no index needed. Low cardinality: consider a partial index (... WHERE status = 'pending'). In Laravel: ->index() or $table->index([...]).

⚡ An index speeds up searching

Write:

CREATE INDEX idx_orders_customer_date ON orders(customer_id, created_at);

Meaning: An index is a sorted list of a column's values, each with a pointer to its row. Searching (SELECT with WHERE, JOIN, ORDER BY) gets fast: Siti's orders this month go from 20,000 entries down to 2. The price is slower INSERT, UPDATE and DELETE.

⚡ Column order: the leftmost prefix

Write:

SELECT * FROM orders
WHERE created_at >= '2026-10-01';
Guess first: can this query use the index (customer_id, created_at)?

Usually not. An index can only be used starting from its leftmost column. Without customer_id, SQLite reads the whole table (SCAN orders).

Meaning: Columns searched with = go first, range columns (>=, BETWEEN) go last.

⚡ Which columns deserve an index

Meaning: A column in WHERE, JOIN or ORDER BY with many distinct values: make an index. A column that's only displayed: no index needed. A column with few values, like status: consider a partial index.

In the exercise, check "select_only" first, then the "low" cardinality.

What you will learn