Case study: branch execution inquiry screen (13 tables, Top-N)
The "today's executions for our branch" screen used by brokerage branch staff. It joins 13 tables to show the latest 20 rows, but a transformed date column, an optional search condition (NVL), tie handling in the sort key, and a structure that attaches dimension tables first combine to read all 2.05 million orders and 50,000 executions. Untangling them one at a time cuts Buffers from 32,130 to 5,406 and CPU from 0.47 s to 0.02 s.
SQL
Situation
This is the "branch execution inquiry" screen on a brokerage branch terminal. Staff choose a business date and a branch (by default their own) and, if needed, enter an item code. The screen shows the latest 20 executions and attaches to each row the customer name and grade, the item, market, and sector names, the order channel, estimated commission and transaction tax, and the valuation P&L based on that day's closing price. The "Next" button is rarely pressed.
During trading hours, hundreds of branch staff open this screen frequently. The DBA found this statement as "0.5 seconds per run, the SQL using the most CPU". The result is correct. After tuning, the result (rows, order, values) must stay the same.
Tables
| Table | Columns used by this screen | Rows |
|---|---|---|
STK_EXEC executions | EXEC_NO, ORD_NO, TRD_DT, EXEC_TM, EXEC_QTY, EXEC_PRC | 2,316,020 (about 56,000 per business day) |
STK_ORD orders | ORD_NO, ACCT_NO, ITEM_CD, SIDE_CD, CHANNEL_CD | 2,050,000 |
ACCT accounts | ACCT_NO, CUST_ID, BRANCH_CD, ACCT_TYPE | 200,000 (about 4,000 per branch) |
BRANCH branches | BRANCH_NM | 50 |
CUSTOMERS, GRADE_CD | Customer name, grade name | 100,000 / 3 |
STK_ITEM, STK_MARKET, STK_SECTOR | Item, market, and sector names | 2,000 / 2 / 20 |
CHANNEL | Channel name | 5 |
FEE_POLICY | Commission rate per channel × account type × effective period | 30 |
TAX_RATE | Sell transaction tax rate per market × effective period | 2 |
DAILY_PRICE | Closing price per item × business date | 82,000 |
| Index | Columns |
|---|---|
STK_EXEC_X2 | TRD_DT, EXEC_TM |
STK_EXEC_X1 | ORD_NO |
STK_ORD_PK / STK_ORD_X1 / STK_ORD_X2 | ORD_NO / ACCT_NO, TRD_DT / ITEM_CD, TRD_DT |
ACCT_PK / ACCT_X2 | ACCT_NO / BRANCH_CD, ACCT_STATUS |
| Others | Primary key of each table |
The schema and data distribution are in the common lab environment. Branch 017 has 913 executions on 2025-09-26.
Query
BEFORE · SQL in production
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY e.exec_tm DESC, e.exec_no DESC) AS rn,
e.exec_no, TO_CHAR(e.exec_tm, 'HH24:MI:SS') AS exec_time,
b.branch_nm, a.acct_no, cu.cust_nm, g.grade_nm,
DECODE(o.side_cd, 'B', '매수', '매도') AS side_nm,
i.item_cd, i.item_nm, m.market_nm,
(SELECT s.sector_nm FROM stk_sector s WHERE s.sector_cd = i.sector_cd) AS sector_nm,
(SELECT ch.channel_nm FROM channel ch WHERE ch.channel_cd = o.channel_cd) AS channel_nm,
e.exec_qty, e.exec_prc, e.exec_qty * e.exec_prc AS exec_amt,
ROUND(e.exec_qty * e.exec_prc *
(SELECT f.fee_rate FROM fee_policy f
WHERE f.channel_cd = o.channel_cd AND f.acct_type = a.acct_type
AND e.trd_dt BETWEEN f.from_dt AND f.to_dt)) AS fee_amt,
CASE WHEN o.side_cd = 'S' THEN
ROUND(e.exec_qty * e.exec_prc *
(SELECT t.tax_rate FROM tax_rate t
WHERE t.market_cd = i.market_cd AND e.trd_dt BETWEEN t.from_dt AND t.to_dt))
ELSE 0 END AS tax_amt,
p.close_prc,
(p.close_prc - e.exec_prc) * e.exec_qty * DECODE(o.side_cd, 'B', 1, -1) AS eval_pl
FROM stk_exec e
JOIN stk_ord o ON o.ord_no = e.ord_no
JOIN acct a ON a.acct_no = o.acct_no
JOIN branch b ON b.branch_cd = a.branch_cd
JOIN customers cu ON cu.cust_id = a.cust_id
JOIN grade_cd g ON g.grade = cu.grade
JOIN stk_item i ON i.item_cd = o.item_cd
JOIN stk_market m ON m.market_cd = i.market_cd
LEFT JOIN daily_price p ON p.item_cd = o.item_cd AND p.trd_dt = e.trd_dt
WHERE TO_CHAR(e.trd_dt, 'YYYYMMDD') = :trd_dt
AND a.branch_cd = :branch_cd
AND o.item_cd = NVL(:item_cd, o.item_cd)
)
WHERE rn BETWEEN 1 AND 20
ORDER BY rn
The default binds are 2025-09-26, branch 017, and no item (NULL). In the execution plan, find the three operations with the largest A-Rows and the tables read to the end to produce the 20 result rows. Also note the CPU in the Trace tab.
Approach
This statement has more than one problem. Fixing one leaves another in the way, so there is no effect. Break the statement down in the following order.
1. Split the statement into the "core set" and "decoration"
Write down which of the 13 tables decide which rows enter the 20 results. They are the tables with conditions (TRD_DT, BRANCH_CD, ITEM_CD) and sort keys (EXEC_TM, EXEC_NO). For the rest (names, grade, commission rate, tax rate, closing price), determine whether attaching them after the 20 rows are decided gives the same result. Also confirm that the inner joins neither remove nor add rows (whether they are primary key joins, whether no rows drop out).
2. See whether the core set's conditions have a shape that can use an index
The date condition transforms the column side (Scenario 1). The item condition o.item_cd = NVL(:item_cd, o.item_cd) is split by the optimizer into two branches, "value given" and "no value" (CONCATENATION, Scenario 9). When branches appear, also consider where hints apply.
3. See whether the sort lets the Top-N stop
In ORDER BY e.exec_tm DESC, e.exec_no DESC, the first column matches the order of STK_EXEC_X2(TRD_DT, EXEC_TM), but the second column EXEC_NO is not in the index. EXEC_TM is in seconds, so several executions share the same second. Consider whether reading in index order and stopping at 20 rows can change the result because of ties, and if so, whether there is a way to stop without changing the result.
4. Compare driving sets
There are two ways to build the core set.
| Path | Order | What decides the amount read |
|---|---|---|
| From executions | Today's executions newest first → orders → accounts (branch check) | Executions passed until 20 rows are filled = 20 ÷ (branch share) |
| From the branch | Branch accounts → today's orders → executions → sort | Number of branch accounts (probed even without orders) |
Which is cheaper depends on the branch's share and its number of accounts. Compare them in numbers.
5. Confirm the result is the same
Rows, order, and values must all be the same. Change the binds (enter an item, another branch) and compare the two statements again.
Explanation
Final version: core 20 rows first, decoration only on those 20
AFTER · Core Top-N first + decoration later
SELECT x.rn, x.exec_no, TO_CHAR(x.exec_tm, 'HH24:MI:SS') AS exec_time,
b.branch_nm, x.acct_no, cu.cust_nm, g.grade_nm,
DECODE(x.side_cd, 'B', '매수', '매도') AS side_nm,
i.item_cd, i.item_nm, m.market_nm,
(SELECT s.sector_nm FROM stk_sector s WHERE s.sector_cd = i.sector_cd) AS sector_nm,
(SELECT ch.channel_nm FROM channel ch WHERE ch.channel_cd = x.channel_cd) AS channel_nm,
x.exec_qty, x.exec_prc, x.exec_qty * x.exec_prc AS exec_amt,
ROUND(x.exec_qty * x.exec_prc *
(SELECT f.fee_rate FROM fee_policy f
WHERE f.channel_cd = x.channel_cd AND f.acct_type = x.acct_type
AND x.trd_dt BETWEEN f.from_dt AND f.to_dt)) AS fee_amt,
CASE WHEN x.side_cd = 'S' THEN
ROUND(x.exec_qty * x.exec_prc *
(SELECT t.tax_rate FROM tax_rate t
WHERE t.market_cd = i.market_cd AND x.trd_dt BETWEEN t.from_dt AND t.to_dt))
ELSE 0 END AS tax_amt,
p.close_prc,
(p.close_prc - x.exec_prc) * x.exec_qty * DECODE(x.side_cd, 'B', 1, -1) AS eval_pl
FROM (
SELECT ROWNUM AS rn, y.*
FROM (
SELECT * FROM (
SELECT /*+ NO_OR_EXPAND LEADING(e o a) USE_NL(o a) INDEX_DESC(e stk_exec_x2) */
e.exec_no, e.exec_tm, e.trd_dt, e.exec_qty, e.exec_prc,
o.side_cd, o.item_cd, o.channel_cd, a.acct_no, a.acct_type, a.branch_cd, a.cust_id
FROM stk_exec e
JOIN stk_ord o ON o.ord_no = e.ord_no
JOIN acct a ON a.acct_no = o.acct_no
WHERE :item_cd IS NULL
AND e.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD')
AND a.branch_cd = :branch_cd
ORDER BY e.exec_tm DESC
FETCH FIRST 20 ROWS WITH TIES
)
UNION ALL
SELECT /*+ NO_OR_EXPAND LEADING(o e a) INDEX(o stk_ord_x2) USE_NL(e a) */
e.exec_no, e.exec_tm, e.trd_dt, e.exec_qty, e.exec_prc,
o.side_cd, o.item_cd, o.channel_cd, a.acct_no, a.acct_type, a.branch_cd, a.cust_id
FROM stk_ord o
JOIN stk_exec e ON e.ord_no = o.ord_no
JOIN acct a ON a.acct_no = o.acct_no
WHERE :item_cd IS NOT NULL
AND o.item_cd = :item_cd
AND o.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD')
AND a.branch_cd = :branch_cd
ORDER BY exec_tm DESC, exec_no DESC
) y
WHERE ROWNUM <= 20
) x
JOIN branch b ON b.branch_cd = x.branch_cd
JOIN customers cu ON cu.cust_id = x.cust_id
JOIN grade_cd g ON g.grade = cu.grade
JOIN stk_item i ON i.item_cd = x.item_cd
JOIN stk_market m ON m.market_cd = i.market_cd
LEFT JOIN daily_price p ON p.item_cd = x.item_cd AND p.trd_dt = x.trd_dt
ORDER BY x.rn
What Before did (Buffers 32,130, CPU 0.47 s)
- Because of
TO_CHAR(e.trd_dt, …), it cannot useSTK_EXEC_X2and reads all executions (TABLE ACCESS FULL STK_EXEC, 13,173 blocks, 56,670 rows remaining). - It builds a hash table of the 4,015 branch accounts and hash-joins it with all 2.05 million orders (16,987 blocks). It then hash-joins executions, customers, closing prices, items, grades, and markets, sorts the 913 rows with
WINDOW SORT PUSHED RANK, and keeps 20. - Because of the
NVLcondition,CONCATENATIONsits at the top of the execution plan, and the branch for a given item (STK_ORD_X2) did not run (Starts0).
What After did (Buffers 5,406, CPU 0.02 s)
- The core branch reads
STK_EXEC_X2withINDEX RANGE SCAN DESCENDINGfrom the newest execution, and for each execution finds the order (STK_ORD_PK) and account (ACCT_PK) with NL joins to check the branch. It passes 1,713 executions until it finds 22 executions of branch017, then stops (WINDOW NOSORT STOPKEY). 5,267 blocks. - The 6 decoration tables and 4 scalar subqueries run only on the final 20 rows. Scalar subqueries cache identical inputs, so sector runs only 9 times and channel 4 times.
- Only the no-item branch runs, and the item branch ends immediately at
FILTER(:item_cd IS NOT NULL).
Problems solved one at a time
| # | Problem | Fix | Why the result is the same |
|---|---|---|---|
| 1 | Transformed date column | e.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD') | TRD_DT holds only dates without time |
| 2 | The NVL optional condition creates branches and the hints do not take effect | Split the branches directly with :item_cd IS NULL / IS NOT NULL, hints per branch, NO_OR_EXPAND | The two conditions cannot both be true |
| 3 | Tie handling in the sort (EXEC_NO) blocks the Top-N stop | FETCH FIRST 20 ROWS WITH TIES on EXEC_TM alone, then re-sort that result (22 rows) by EXEC_TM, EXEC_NO to get 20 | The true top 20 are all "rank 20 or better by EXEC_TM", so they are inside the set including ties |
| 4 | Decoration tables joined first | Join after the 20 rows are decided | All are primary key joins and no rows drop out (closing price is an outer join) |
| 5 | Driving set | Executions newest first → orders → accounts | Unrelated to the result; only the cost differs |
Why this is optimal
- The remaining cost is the order and account probes for "the 1,713 executions passed to find 22 executions of branch
017" (about 3,400 blocks). The branch share is 1.6% (913 ÷ 56,670), so filling 22 rows requires passing about 80 times as many. The executions table has no branch, so the branch cannot be filtered by looking at executions alone. - Driving from the branch requires 4,015 probes, including accounts without orders, so it costs more (variation below, 12,340).
- The larger the branch's share, the more reading from executions wins. Branch
001, where corporate accounts concentrate, is 32,130 Before and 395 After (run with bindbranch_cdchanged to001).
Variation
TRY · Tie handling in index order too (cannot stop)
SELECT * FROM (
SELECT /*+ NO_OR_EXPAND LEADING(e o a) USE_NL(o) USE_NL(a) INDEX_DESC(e stk_exec_x2) */
e.exec_no, e.exec_tm, o.item_cd, a.acct_no
FROM stk_exec e JOIN stk_ord o ON o.ord_no = e.ord_no JOIN acct a ON a.acct_no = o.acct_no
WHERE e.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD') AND a.branch_cd = :branch_cd AND o.item_cd = NVL(:item_cd, o.item_cd)
ORDER BY e.exec_tm DESC, e.exec_no DESC
) WHERE ROWNUM <= 20
- The sort key
EXEC_NOis not in the index, soSORT ORDER BY STOPKEYmust receive all 56,670 executions of the day. Each execution finds its order and account with NL, so Buffers are 173,626, five times worse than Before. This is what happens when only the shape "index backward + NL" is matched without checking the stop condition.
TRY · Drive from branch accounts
SELECT * FROM (
SELECT /*+ NO_OR_EXPAND LEADING(a o e) USE_NL(o) USE_NL(e) INDEX(a acct_x2) INDEX(o stk_ord_x1) INDEX(e stk_exec_x1) */
e.exec_no, e.exec_tm, o.item_cd, a.acct_no
FROM stk_exec e JOIN stk_ord o ON o.ord_no = e.ord_no JOIN acct a ON a.acct_no = o.acct_no
WHERE e.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD') AND o.trd_dt = TO_DATE(:trd_dt, 'YYYYMMDD') AND a.branch_cd = :branch_cd AND o.item_cd = NVL(:item_cd, o.item_cd)
ORDER BY e.exec_tm DESC, e.exec_no DESC
) WHERE ROWNUM <= 20
- For each of the 4,015 branch accounts it probes
STK_ORD_X1and finds 810 orders of the day (8,051 blocks), because accounts without orders must also be probed. It collects and sorts all 913 executions, and Buffers are 12,340. There is no sort-key tie problem, but a fixed cost proportional to the number of branch accounts remains. - Note that the
o.trd_dtcondition is also used. With only the execution date,STK_ORD_X1reads all 41 days of the account's orders (Scenario 41 covers the same problem).
Changing the binds
- Item
000001(large cap): Before gets worse at Buffers 81,614, and After's item branch reads only that item's orders of the day throughSTK_ORD_X2, at 3,856. The 11 result rows are the same. - Branch
001(corporate accounts): Before 32,130, After 395.
Key points
- Split a long SQL into the core set that decides the result rows and the decoration that only attaches values. Cut the Top-N in the core set first, and attach the decoration after cutting (Scenario 30).
- The core set's problems (column transformation, optional conditions, sort ties) overlap, so fixing only one can have no effect or make things worse. After each fix, check the execution plan's stop point (
STOPKEY) and A-Rows. - When the tie-breaking sort column is not in the index, cut first including ties with
WITH TIES, then sort again within that set. Write down the reasoning that the result is the same. - The choice of driving set depends on the data distribution (branch share, number of accounts). Compare Before/After with a few representative binds.