Join predicate pushdown: computing an aggregate view only for the needed customers

A join with a GROUP BY inline view tends to compute the whole view first. Pushing the join condition into the view (JPPD) aggregates only the 1,000 needed customers through the index.

SQL

Situation

A screen shows the cumulative order count and last order date of 1,000 VIP customers. The per-customer aggregation was built as an inline view, like a shared view, and joined with the VIP customers. The view is used by other queries too, so tuning must leave the view as is.

Tables

TableColumnTypeDescription
CUSTOMERSCUST_ID, CUST_NM, GRADE100,000 customers, 1,000 VIPs
ORDERSCUST_ID, ORD_DT2 million orders
IndexColumns
ORDERS_X1CUST_ID, ORD_DT
CUSTOMERS_X1GRADE

Query

View v computes the order count and last order date of every customer. Before prevents the optimizer from doing anything but computing the view separately (NO_MERGE, NO_PUSH_PRED). It reproduces a situation where the view cannot be changed, or where the optimizer cannot apply the transformation.

BEFORE · Aggregate the whole view, then join

SELECT /*+ NO_MERGE(v) NO_PUSH_PRED(v) */ c.cust_id, c.cust_nm, v.cnt, v.last_dt
FROM customers c
JOIN (SELECT cust_id, COUNT(*) AS cnt, MAX(ord_dt) AS last_dt
      FROM orders
      GROUP BY cust_id) v
  ON v.cust_id = c.cust_id
WHERE c.grade = 'VIP'

Run it and see how many rows the HASH GROUP BY inside the view aggregated (A-Rows), and how many of them were actually used.

Approach

1. Compare the rows the view produced with the rows used

If the view aggregated all 100,000 customers but only 1,000 VIPs remain after the join, 99% of the aggregation was thrown away. The condition outside the view (VIP) was not passed into the view.

2. See whether the join condition can move into the view

If the join condition v.cust_id = c.cust_id can become WHERE cust_id = :outer_cust_id inside the view, each VIP customer needs only that customer's orders aggregated. The view then runs "once per customer" instead of "everything at once", in the shape of an NL join.

3. Check for an index usable inside the view

"Once per customer" is cheap only with an index that finds one customer's orders (leading CUST_ID of ORDERS_X1). If the aggregated column (ORD_DT) is also in the index, the table is not read either.

4. Separate the approach that leaves the view alone from the one that changes it

Consider separately whether an optimizer transformation can solve it with the view as is, and whether the query shape must change.

Explanation

Join predicate pushdown (PUSH_PRED)

AFTER · PUSH_PRED

SELECT /*+ LEADING(c v) USE_NL(v) PUSH_PRED(v) */ c.cust_id, c.cust_nm, v.cnt, v.last_dt
FROM customers c
JOIN (SELECT cust_id, COUNT(*) AS cnt, MAX(ord_dt) AS last_dt
      FROM orders
      GROUP BY cust_id) v
  ON v.cust_id = c.cust_id
WHERE c.grade = 'VIP'

Why it got faster

  • Before uses about 14,500 Buffers. The view read all 2 million ORDERS rows to aggregate 100,000 customers, then joined the result with the 1,000 VIPs.
  • After uses about 2,700 Buffers. It reads the 1,000 VIPs first and, for each customer, range-scans ORDERS_X1 by CUST_ID to count and take the maximum date (INDEX RANGE SCAN Starts 1,000). ORD_DT is in the index, so the table is not read.
  • Not a single character of the view definition changed. This is the result of the optimizer's transformation that pushes the join condition into the view (Join Predicate Pushdown, JPPD). The execution plan shape also changed from "aggregate everything → join" to "aggregate per customer through the index".

The same effect through query shape (LATERAL)

AFTER · CROSS APPLY

SELECT c.cust_id, c.cust_nm, v.cnt, v.last_dt
FROM customers c
CROSS APPLY (SELECT COUNT(*) AS cnt, MAX(o.ord_dt) AS last_dt
             FROM orders o
             WHERE o.cust_id = c.cust_id) v
WHERE c.grade = 'VIP'
  • CROSS APPLY (or LATERAL) is syntax for referencing the outer row directly inside the view. It states the pushdown in the query instead of leaving it to the optimizer, and produces the same execution plan (about 2,700 Buffers).
  • Caution: it is an aggregation without GROUP BY cust_id, so a customer with no orders also yields one row with cnt = 0. To match the original query (inner join), filter out customers without orders. The current data has no VIP without orders, so the results are the same.

Why this is optimal

  • It reads only the index range of each of the 1,000 VIPs (an average of 20 rows per customer, about 2 index blocks). It read only the needed orders of the needed customers.
  • Among the high-volume customers (IDs 1-100), the VIP customer (ID 100) has many orders, so that customer's range is long. Still, only the index is read, so there is no table access.
  • If the target were most customers rather than VIPs, the cost of one lookup per customer would exceed one full aggregation. In that case the Before shape (aggregate everything, then hash join) is right.

Variation: removing all hints

TRY · No hints (optimizer's choice)

SELECT c.cust_id, c.cust_nm, v.cnt, v.last_dt
FROM customers c
JOIN (SELECT cust_id, COUNT(*) AS cnt, MAX(ord_dt) AS last_dt
      FROM orders
      GROUP BY cust_id) v
  ON v.cust_id = c.cust_id
WHERE c.grade = 'VIP'
  • In this environment (23ai), the optimizer chooses the same plan as Before on its own (aggregate the whole view, then MERGE JOIN, about 14,500 Buffers). JPPD is a cost-based transformation, so it is not chosen when the optimizer misestimates the number of VIP customers or orders per customer.
  • The plan the optimizer picks is not always optimal. Compare E-Rows and A-Rows in the execution plan to check whether the estimates are right first.

Key points

  • View Merging: merges the view with the outer query and optimizes them as a single query. Blocked with NO_MERGE.
  • Join Predicate Pushdown (JPPD): without merging the view, it pushes the join condition into the view and runs the view for each outer row. Controlled with PUSH_PRED and NO_PUSH_PRED.
  • The fewer rows selected outside, the more JPPD wins; the more rows, the more aggregate-then-hash-join wins.
  • Differences by DB: PUSH_PRED is an Oracle transformation. PostgreSQL does not push join conditions into a GROUP BY subquery, so it must be written directly with LATERAL; SQL Server uses CROSS APPLY.