Design and best practice · Designing databases

Indexing a FOREIGN KEY

The referenced key needs to be unique; the columns in the child table may need an index of their own. Check your engine and the existing indexes before adding it.

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).

SQLite · before the index
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 it and check 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:

Indexes around a FOREIGN KEY
SideColumnWhy it matters
Parent tablecustomers.idIt must uniquely identify the value that can be referenced.
Child tableorders.customer_idIt 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.
Mental model: the FOREIGN KEY answers “is this reference valid?”; the index answers “how do I efficiently find the rows that hold this reference?”. The two jobs are related, but not identical.

What each engine does

Index on the child columns of a FOREIGN KEY
EngineIs it created automatically?What to check
PostgreSQLNoThe 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 / InnoDBYes, if a suitable one is missingInnoDB requires indexes on foreign and referenced keys; avoid adding another redundant index with the same prefix later on.
SQL ServerNoMicrosoft notes that it is usually useful because FK columns appear in JOINs and in checks when the parent changes.
SQLiteNot required on the child tableSQLite 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.

Composite example
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

  1. Identify the child table. Find the columns that hold the FOREIGN KEY.
  2. Inspect the existing indexes. Do not duplicate an index that already covers the same useful prefix.
  3. Look at the real queries. Check JOINs, filters and operations on the parent table.
  4. Look at the plan. Usa EXPLAIN or the equivalent tool.
  5. Measure writes too. An index speeds up some kinds of access, but it has storage and maintenance costs.