Driving set and join order

An NL join repeats once per row of the side read first. PostgreSQL has no LEADING hint, so LATERAL and OFFSET 0 build the two orders by hand for comparison.

SQL

Situation

A list of VIP customers' orders since June. The query is written as "for each order, check whether its customer is a VIP", and that shape is fixed as the execution order (this reproduces a case where the optimizer is blocked from changing the order). The result is a little over 2,400 rows, yet hundreds of thousands of blocks are read.

Tables

TableColumnsDescription
customerscust_id, cust_nm, grade100,000 customers, 1,000 VIPs
orderscust_id, ord_dt, amt2 million orders, about 236,000 since June
IndexColumns
customers_pkcust_id
customers_x1grade
orders_x1cust_id, ord_dt

Order that reads orders first

BEFORE · Orders first (check customer per order)

SELECT c.cust_nm, o.ord_dt, o.amt
FROM orders o
CROSS JOIN LATERAL (
  SELECT c.cust_nm
  FROM customers c
  WHERE c.cust_id = o.cust_id
    AND c.grade = 'VIP'
  OFFSET 0
) c
WHERE o.ord_dt >= DATE '2025-06-01'

A LATERAL subquery runs once per outer row, and OFFSET 0 is an idiom that stops PostgreSQL from flattening the subquery and reordering. Run it and check how many rows the outer Seq Scan orders emits and how many times the inner side runs.

Approach

1. Count the rows of the side read first (driving)

The cost of an NL join is roughly "outer rows × inner lookup cost". Reading orders first gives 236,000 outer rows; reading customers first gives 1,000 VIPs.

2. Start from the side with the narrower condition

grade = 'VIP' narrows 100,000 customers to 1,000, and ord_dt >= '2025-06-01' narrows 2 million orders to 236,000. Starting from a small set close to the result means less repetition.

3. Check whether an index exists for the inner lookup

Reading customers first makes the inner side "that customer's orders since June". orders_x1(cust_id, ord_dt) finds exactly this range.

Explanation

Customers first (1,000 VIPs → order lookup)

AFTER · Customers first (order lookup per VIP)

SELECT c.cust_nm, o.ord_dt, o.amt
FROM customers c
CROSS JOIN LATERAL (
  SELECT o.ord_dt, o.amt
  FROM orders o
  WHERE o.cust_id = c.cust_id
    AND o.ord_dt >= DATE '2025-06-01'
  OFFSET 0
) o
WHERE c.grade = 'VIP'

Why it got faster

  • Before reads 236,000 orders since June with Seq Scan and checks the customer per order. PostgreSQL 14+ attaches Memoize to the inner side so the same customer is not looked up again, but there are still about 88,000 customers_pk lookups. Buffers about 283,000.
  • After finds 1,000 VIPs with customers_x1 and, per customer, finds only orders since June with orders_x1 (Index Scan Starts 1,000). Buffers about 6,300.
  • The A-Rows of a repeated node is "average rows × executions", so it has rounding error (e.g. the inner A-Rows in Before shows 0). The actual result is 2,431 rows for both queries.

Why this is optimal

  • Only each of the 1,000 VIPs' orders since June is found through the index, so the amount read is proportional to the result rows.
  • The remaining cost is the table access that reads order rows. Each customer's orders are scattered across the table, so each row reads a different block.

Variation: leaving it to the planner

TRY · Plain join (planner chooses the order)

SELECT c.cust_nm, o.ord_dt, o.amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= DATE '2025-06-01'
  • orders comes first in the FROM clause, but the planner picks on its own the same plan as After, reading VIP customers first (Buffers about 6,300). In a plain join, the FROM clause order does not set the execution order. The difference shows when the order is fixed, as in Before, by LATERAL … OFFSET 0, a function, or a view.

Key points

  • An NL join is fast with a small set on the outer (driving) side and a join key index on the inner side.
  • In the execution plan, read the outer node's A-Rows together with the inner node's Starts.
  • DB difference: Oracle sets the order with the LEADING(c o) hint (Oracle scenario 5). PostgreSQL has no hints, so you leave it to the planner or set the order with join_collapse_limit (join order of three or more) or LATERAL … OFFSET 0 (fixing outer and inner).