Before continuing: This entry is part of the indexes guide. If you are new to the topic, start with INDEX.
Example of an index on a FOREIGN KEY
The teaching database contains orders.customer_id as a foreign key to customers.id. In the copy used for this test there is no explicit index on orders(customer_id).
EXPLAIN QUERY PLAN
SELECT id, date, total
FROM orders
WHERE customer_id = 1
ORDER BY id;Now we add an index that starts with the foreign key column and run exactly the same query again.
CREATE INDEX idx_orders_customer_fk
ON orders (customer_id);
EXPLAIN QUERY PLAN
SELECT id, date, total
FROM orders
WHERE customer_id = 1
ORDER BY id;The query returns the same three orders for customer 1: 101, 103 and 111. The plan shows that SQLite can use the index to find the rows by customer_id. It does not prove that a table of 18 orders gets meaningfully faster; for real performance figures you need representative data and measurements.
Differences and trade-offs
First, tell the two sides of the relationship apart
Suppose orders.customer_id references customers.id. They are two different needs:
| Side | Column | Why it matters |
|---|---|---|
| Parent table | customers.id | It must uniquely identify the value that can be referenced. |
| Child table | orders.customer_id | It lets you find a customer's child rows quickly and is often used in JOINs and referential checks. |
Mixing up the two sides leads to an oversimplified rule such as “every FOREIGN KEY is already indexed”. That does not hold across engines. The useful question is: is there already a usable index on the columns that hold the reference?
Why the child index can help even though the query is correct without it
A foreign key protects integrity, but the engine also needs to find related rows. That access comes up in several situations:
- Frequent JOINs: for example, joining customers to their orders on
customer_id. - Filtering on the relationship: fetching all the orders of a particular customer.
- Changes to the parent row: when a referenced key is deleted or updated, the engine may need to find the child rows that still point to it.
What each engine does
| Engine | Is it created automatically? | What to check |
|---|---|---|
| PostgreSQL | No | The referenced key is backed by a unique index; the index on the child table is a separate decision based on queries and maintenance. |
| MySQL 8.4 / InnoDB | Yes, if a suitable one is missing | InnoDB requires indexes on foreign and referenced keys; avoid adding another redundant index with the same prefix later on. |
| SQL Server | No | Microsoft notes that it is usually useful because FK columns appear in JOINs and in checks when the parent changes. |
| SQLite | Not required on the child table | SQLite recommends it on most real systems to avoid linear scans when looking up child rows. |
So a portable migration should not assume that the same CREATE INDEX is needed on every engine. Inspect the indexes that already exist first, then create only the one that is missing.
If the FOREIGN KEY has several columns
With a composite foreign key, the columns work as a unit. If a relationship uses (country_id, customer_number), a natural candidate index on the child table starts with those columns in an order compatible with the relationship and the queries you want to speed up.
CREATE INDEX idx_orders_customer_composite
ON international_orders (country_id, customer_number);Do not automatically add a single-column index if there is already a composite index that starts with the relationship columns and the engine can use it for that pattern. A redundant index also takes up space and adds work to INSERT, UPDATE and DELETE.
A practical sequence for deciding
- Identify the child table. Find the columns that hold the FOREIGN KEY.
- Inspect the existing indexes. Do not duplicate an index that already covers the same useful prefix.
- Look at the real queries. Check JOINs, filters and operations on the parent table.
- Look at the plan. Usa EXPLAIN or the equivalent tool.
- Measure writes too. An index speeds up some kinds of access, but it has storage and maintenance costs.