Sub topic 14
Indexes and the Cost of Speed
This chapter assumes you understand tables, primary keys, foreign keys, and basic SELECT queries. An index is an extra data structure that helps a database find rows faster. It works like the index at the back of a book: the database can locate a value without reading every row.
How an index helps a query
Suppose orders contains millions of rows. This query asks for one customer's orders:
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = 42;Without an index on customer_id, the database may scan every row and test its value. This is called a table scan. With an index, the database can find the entries for 42 and then read the matching rows.
The database's query planner chooses a strategy. It considers the available indexes, the estimated number of matching rows, and the cost of reading data. An index does not guarantee that the planner will use it.
Creating and using an index
This SQLite example creates a composite index. A composite index contains more than one column.
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_date TEXT NOT NULL,
total_amount REAL NOT NULL
);
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES
(100, 42, '2026-01-15', 59.90),
(101, 42, '2026-02-03', 24.50),
(102, 17, '2026-01-20', 80.00);
CREATE INDEX idx_orders_customer_date
ON orders (customer_id, order_date);
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = 42
AND order_date >= '2026-01-01'
ORDER BY order_date;The index is ordered first by customer_id, then by order_date within each customer. It can efficiently support this query because the query filters by the first column and then uses the second column.
Column order matters:
| Index | Helps most with |
|---|---|
| `(customer_id, order_date)` | `customer_id = 42`, with optional date filtering |
| `(order_date, customer_id)` | date filtering, with optional customer filtering |
A composite index usually helps with the first column or with a left-to-right prefix of its columns. An index on (customer_id, order_date) is therefore not automatically a good replacement for an index on order_date.
The cost of speed
Indexes improve reads, but they consume resources.
- An index uses disk space and memory.
INSERT,UPDATE, andDELETEoperations must maintain every affected index.- An index on a column with only two possible values, such as
is_active, may not reduce the work enough to justify its cost. - Too many indexes make schema design harder and can slow writes.
- An index can become less useful when its data distribution changes.
Primary keys commonly have indexes created automatically. Foreign key columns are also frequent index candidates because joins and parent-child lookups often search them. Some database systems do not automatically index foreign keys, so check the system's behavior.
A common error is indexing every column “just in case.” Start with slow, important queries. Inspect their plans, add a focused index, and measure again. Also consider write-heavy workloads, where faster reads may not justify slower writes.
Exercise
Why might an index on (customer_id, order_date) help a query filtering by customer_id but not fully replace an index on order_date?
Show answer
The composite index is ordered by customer_id first. The database can efficiently find a customer range and then search dates inside it, but date-only searches may need to scan much of the index.