Join ordering
Overview
Join ordering chooses the sequence in which relations are combined. For suitable inner joins, many orders preserve the logical result but produce different intermediate sizes. An optimizer searches candidate orders and access paths; exhaustive search grows rapidly, so practical engines constrain search. Outer joins, lateral dependencies and other semantics can restrict legal reorderings.
First example
SELECT c.id,o.id,l.id FROM opt_customer c JOIN opt_order o ON o.customer_id=c.id JOIN opt_line l ON l.order_id=o.id ORDER BY l.id;Observed SQLite result (rows in JSON):
[ [ 2, 1, 1 ], [ 2, 1, 2 ], [ 3, 2, 3 ], [ 1, 42, 4 ]]How it works
The first query joins four customers, 100 orders and four lines; duplicate lines for order 1 remain distinct. In the second, SQLite CROSS JOIN prevents reordering across its operands and the plan starts at the four-line relation, then uses primary-key lookups. This is an engine-specific diagnostic control; PostgreSQL explicit joins require different planner settings to constrain search. A good order is not always the smallest table first.
Practical example
EXPLAIN QUERY PLAN SELECT c.id,o.id,l.id FROM opt_line l CROSS JOIN opt_order o CROSS JOIN opt_customer c WHERE l.order_id=o.id AND o.customer_id=c.id;Observed SQLite result (rows in JSON):
[ [ 4, 0, 0, "SCAN l" ], [ 6, 0, 0, "SEARCH o USING INTEGER PRIMARY KEY (rowid=?)" ], [ 9, 0, 0, "SEARCH c USING INTEGER PRIMARY KEY (rowid=?)" ]]Practice and mistakes
Practice: rewrite the first query starting with opt_line, then join order and customer, retaining ORDER BY l.id. Expect the same four triples. Hint: preserve both join keys. Compare EQP output; do not force a join order merely because it looks intuitive.
PostgreSQL 17 example — NOT RUN
BEGIN;SET LOCAL statement_timeout='10s';SET LOCAL lock_timeout='2s';CREATE TEMP TABLE opt_customer(id INTEGER PRIMARY KEY, name TEXT NOT NULL);INSERT INTO opt_customer VALUES(1,'Ada'),(2,'Reza'),(3,'Sara'),(4,'Mina');CREATE TEMP TABLE opt_order(id INTEGER PRIMARY KEY,customer_id INTEGER NOT NULL REFERENCES opt_customer(id),amount INTEGER NOT NULL,status TEXT NOT NULL,region TEXT NOT NULL);WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<100)INSERT INTO opt_order SELECT i,1+(i%3),10*i,CASE WHEN i<=10 THEN 'rare' ELSE 'common' END,CASE WHEN i<=10 THEN 'east' ELSE 'west' END FROM n;CREATE INDEX opt_amount ON opt_order(amount);CREATE INDEX opt_status ON opt_order(status);CREATE INDEX opt_region ON opt_order(region);CREATE INDEX opt_customer_idx ON opt_order(customer_id);CREATE TEMP TABLE opt_line(id INTEGER PRIMARY KEY,order_id INTEGER NOT NULL REFERENCES opt_order(id));INSERT INTO opt_line VALUES(1,1),(2,1),(3,2),(4,42);CREATE INDEX opt_line_order ON opt_line(order_id);ANALYZE opt_order;ANALYZE opt_customer;ANALYZE opt_line;
EXPLAIN SELECT c.id,o.id,l.id FROM opt_customer c JOIN opt_order o ON o.customer_id=c.id JOIN opt_line l ON l.order_id=o.id;SET LOCAL join_collapse_limit=1;EXPLAIN SELECT c.id,o.id,l.id FROM opt_line l JOIN opt_order o ON l.order_id=o.id JOIN opt_customer c ON o.customer_id=c.id;ROLLBACK;Expected result, not executed: "Compare join trees; four output rows, no fixed plan shape."