Cut the Top-N first, then attach the aggregate

Attach each customer's total order count and cumulative amount to the 20 most recent paid orders. Joining a per-customer aggregate view first aggregates 2 million orders and joins 140,000 rows before picking 20. Cutting the 20 rows first and aggregating only those 20 customers drops Buffers from 27,900 to 4,461.

SQL

Situation

The payment check screen shows the 20 most recent paid orders, together with each customer's order count and cumulative amount so far. The per-customer aggregate was attached as-is from an inline view also used on other screens.

Tables

TableColumnsDescription
ORDERSSTATUS, ORD_DT, CUST_ID, AMTAbout 140,000 paid (PAID) orders. Customers 1–100 have 2,000 or more orders
IndexColumns
ORDERS_X1CUST_ID, ORD_DT
ORDERS_X2STATUS, ORD_DT, ORD_ID

Query

BEFORE · Join the aggregate view, then Top-N

SELECT * FROM (
  SELECT o.ord_id, o.ord_dt, o.amt, o.cust_id, s.ord_cnt, s.tot_amt
  FROM orders o
  JOIN (SELECT cust_id, COUNT(*) AS ord_cnt, SUM(amt) AS tot_amt
        FROM orders
        GROUP BY cust_id) s ON s.cust_id = o.cust_id
  WHERE o.status = 'PAID'
  ORDER BY o.ord_dt DESC
) WHERE ROWNUM <= 20

Compare the A-Rows of HASH GROUP BY and HASH JOIN with the 20 result rows.

Approach

1. See which table holds the sort key

ORDER BY o.ord_dt DESC is a column of the outer orders table, and reading ORDERS_X2 backward already gives that order (Scenario 2). The aggregate view is not used in the sort key. Check whether the plan still requires the aggregation to finish before the 20 rows are picked.

2. Determine whether the join changes the row count

To cut the Top-N before the join, the join must neither add nor remove result rows. The aggregate view has one row per cust_id (no additions), and a customer with orders always exists in the aggregate view (no removals). Confirm that the query shape guarantees both.

3. Choose how to attach the aggregate to the 20 cut rows

If the 20 rows are cut first, at most 20 customers need aggregating. Check in the execution plan whether the join condition goes into the aggregate view so that only those 20 are aggregated (join predicate pushdown, Scenario 18).

Explanation

Join the aggregate view to an inline view that cuts 20 rows first

AFTER · Top-N first, aggregate only 20 customers

SELECT t.ord_id, t.ord_dt, t.amt, t.cust_id, s.ord_cnt, s.tot_amt
FROM (SELECT * FROM (
        SELECT ord_id, ord_dt, amt, cust_id
        FROM orders
        WHERE status = 'PAID'
        ORDER BY ord_dt DESC
      ) WHERE ROWNUM <= 20) t
JOIN (SELECT cust_id, COUNT(*) AS ord_cnt, SUM(amt) AS tot_amt
      FROM orders
      GROUP BY cust_id) s ON s.cust_id = t.cust_id
ORDER BY t.ord_dt DESC

Why it got faster

  • Before reads all 2 million orders to aggregate 100,000 customers (HASH GROUP BY, 13,950 blocks), then reads all 140,179 paid orders again, hash-joins, and sorts (13,950 blocks). Buffers 27,900.
  • After reads 20 rows backward from ORDERS_X2 and stops (6 blocks). The aggregate view gets VIEW PUSHED PREDICATE, and for one customer at a time it collects only that customer's orders through ORDERS_X1 and aggregates them (20 executions). Buffers 4,461.
  • The result is the same. The join condition equals the aggregate view's GROUP BY key, so rows do not multiply, and the customer of each outer order always exists in the aggregate view, so rows are not lost.

Why this is optimal

  • The remaining 4,455 blocks are the cost of reading the 4,403 orders of the 20 customers from the table. Two of the customers of the latest 20 rows are heavy customers (2,000 or more orders), so their orders make up most of it. SUM(amt) must read the table because AMT is not in the index.
  • Without the cumulative amount, the aggregation finishes with the index alone. The variation below checks this.

Variation: attach only the order count

TRY · Order count only (aggregate from the index alone)

SELECT t.ord_id, t.ord_dt, t.amt, t.cust_id, s.ord_cnt
FROM (SELECT * FROM (
        SELECT ord_id, ord_dt, amt, cust_id
        FROM orders
        WHERE status = 'PAID'
        ORDER BY ord_dt DESC
      ) WHERE ROWNUM <= 20) t
JOIN (SELECT cust_id, COUNT(*) AS ord_cnt
      FROM orders
      GROUP BY cust_id) s ON s.cust_id = t.cust_id
ORDER BY t.ord_dt DESC
  • COUNT(*) only needs to count the CUST_ID range of ORDERS_X1, so it does not read the table. Buffers 64.

Key points

  • In a Top-N query, attach joins and aggregates unrelated to the sort key after cutting the 20 rows. Attaching them first loses partial range processing and computes over the whole aggregation target.
  • Before cutting, confirm that the join does not change the row count. If it does (e.g. an inner join that drops customers missing from the aggregate view), fewer than 20 rows can come out after cutting.
  • Check with VIEW PUSHED PREDICATE whether the join condition goes into the aggregate view. If it does not, write a scalar subquery or LATERAL so that each customer is aggregated separately.