Query Processing and the Optimiser
Context: FIT2094_MOC Β· how Oracle turns SQL into an execution plan Β· explains why nested vs correlated subqueries differ in cost Β· conceptual only β not assessed
Quick Revision
- π― Objective: trace SQL through parse β analyse β optimise β execute β understand relative query cost.
- β‘ Key Constraint: the Explain-Plan COST is a relative unit β meaningful only when comparing two versions of the same query.
π Core
- 1. SQL Parser β checks syntax (grammar) and semantics (do the tables/columns exist?).
- 2. Query analysis β decomposes into relational-algebra ops (joins, selects, projects).
- 3. Optimiser (CBO) β the Cost-Based Optimiser estimates each strategyβs cost, picks the cheapest, caches the plan.
- 4. Execution engine β runs the plan, pulling data blocks into cache, returns the result set.
- Cost depends on statistics β CBO estimates rely on gathered stats (
user_tab_col_statistics,user_ind_statistics).
β οΈ Common Mistakes
- π‘ COST has no absolute meaning β a plan cost of 9 vs 39 only says the second is ~4Γ costlier for that query; it is not seconds or rows.
- π‘ Correctness β efficiency β a correlated subquery gives the same answer as a nested one but re-scans per row; you are not penalised for it in this unit.
π§ Active Recall
Why does the correlated version of the "min price per type" query cost ~4Γ the nested version?
Answer
- Short answer: The correlated subquery references the outer row, so
RENTAL/DRONEis scanned once per outer row; the nested version runs once as a hash-based semi-join (scanning the table ~twice total).- Why: Re-execution vs single pass β repeated inner scans (cost 39) vs one hash semi-join (cost 9).