Sub topic 16
Join Performance: Indexes and Execution Plans
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax, and that you understand primary keys and foreign keys. This chapter explains how a database finds matching rows for an inner join and how to check whether that work is efficient.
What an index does
An index is a separate data structure that helps the database find rows without scanning every row in a table. It is similar to an index in a book: the database can locate matching values, then read the corresponding table rows.
For the sample schema, the primary key indexes customers.id and orders.id. The foreign key column orders.customer_id may also need an index:
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);
SELECT c.name, o.id AS order_id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
WHERE c.id = 1;The database can use the index on orders.customer_id to find orders for customer 1. Without that index, it might inspect every order while processing the join.
A foreign key constraint does not always create an index automatically. Check your database system and create the index when the workload needs it.
| Column or condition | Why it may need an index |
|---|---|
| `orders.customer_id` | Finds child rows for a customer |
| `customers.id` | Usually already indexed by the primary key |
| `orders.order_date` | Helps date filtering or ordering |
| `(customer_id, order_date)` | Helps queries filtering by both columns in that order |
Indexes improve reads, but they consume storage and make inserts, updates, and deletes more expensive. Do not index every column. Index columns used often in joins, filters, or ordering, then verify the choice with an execution plan.
Reading an execution plan
An execution plan is the database's chosen procedure for running a query. Use EXPLAIN to inspect the plan:
EXPLAIN
SELECT c.name, o.id AS order_id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
WHERE c.id = 1;Database systems format plans differently, but look for these details:
- The access method: an index scan usually reads selected index entries, while a sequential scan reads the whole table.
- The join algorithm: nested loop, hash join, or merge join.
- Estimated rows: how many rows the optimizer expects each step to produce.
- Actual rows and time, when using an analysis option such as
EXPLAIN ANALYZE. - A large difference between estimated and actual rows, which can indicate stale statistics or an unusual data distribution.
A nested loop can be efficient when the first input produces only a few rows and the second table has a useful index. A hash join can be better when the database must match many rows and no suitable index exists. A merge join can work well when both inputs are already sorted by the join key.
Conditions that prevent useful index access
An index may be less useful when a query applies a function or conversion to the indexed column:
-- Often harder to optimize with an index on order_date
SELECT *
FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2026;A range condition may allow better access:
SELECT *
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2027-01-01';Keep data types compatible across join columns. Avoid casting one side of a join unless necessary. After changing indexes or query conditions, inspect the new plan rather than assuming it improved.
Exercise
Which index is the most directly useful for joining customers.id to orders.customer_id, and what should you check in the execution plan?
Show answer
Create an index on orders(customer_id) if one is not already present. Check whether the plan uses that index, how many rows it estimates and reads, and which join algorithm it chooses.