Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
EXPLAIN QUERY PLAN
SELECT o.id, i.product FROM orders o JOIN order_items i ON i.order_id = o.id WHERE o.id = 104;text
QUERY PLAN
SEARCH o USING INTEGER PRIMARY KEY (rowid=?)
SCAN iThe order itself is found by primary key, but its lines need a SCAN of order_items. On a large table that scan dominates the cost. With an index on the join column, the lines are found by a search:
sql
CREATE INDEX items_order ON order_items(order_id);
EXPLAIN QUERY PLAN
SELECT o.id, i.product FROM orders o JOIN order_items i ON i.order_id = o.id WHERE o.id = 104;text
QUERY PLAN
SEARCH o USING INTEGER PRIMARY KEY (rowid=?)
SEARCH i USING INDEX items_order (order_id=?)On PostgreSQL, the planner would weigh a hash join, which needs no index, against a nested loop with index lookups. For small result sets driven by a filtered outer table, the indexed nested loop usually wins. For joining two whole large tables, a hash join with no index is often best, so an index is not always the answer.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
EXPLAIN QUERY PLAN
SELECT o.id, i.product FROM orders o JOIN order_items i ON i.order_id = o.id WHERE o.id = 104;
CREATE INDEX items_order ON order_items(order_id);
EXPLAIN QUERY PLAN
SELECT o.id, i.product FROM orders o JOIN order_items i ON i.order_id = o.id WHERE o.id = 104;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Index child foreign key columns used in joins
- SQLite scans the child table without an index on the join column
- Indexed nested loops suit selective joins
- Hash joins need no index for large full joins
Common mistakes
- Leaving foreign key columns unindexed
- Assuming every join needs an index
Follow-ups
- What is a hash join?
- How does an unindexed foreign key slow parent deletes?
Tips
- Read the SCAN line and say which index would remove it