Skip to main content
← All posts

Part 7: Query Execution Engines: Volcano Iterators & Cost-Based Optimization

Md. Harun-Or-RoshidNov 5, 20263 min readArticle
Part 7: Query Execution Engines: Volcano Iterators & Cost-Based Optimization

Module Overview

When an application submits a declarative SQL query, the database engine must translate the text into an optimal sequence of low-level algorithms. In this seventh installment, we analyze the Query Processing Pipeline: Abstract Syntax Trees, logical query plans, the classic Volcano Iterator Model, physical join implementations (Nested Loop, Sort-Merge, Grace Hash Join), and Cost-Based Optimization (CBO) utilizing catalog statistics and dynamic programming.

1. The Query Processing Pipeline

  1. Lexical & Syntactic Analysis: Parses raw SQL text into an Abstract Syntax Tree (AST), ensuring grammar conformance.
  2. Semantic Analysis & Binder: Validates table and column names against the system catalog, binds identifiers, and verifies user access privileges.
  3. Logical Plan Generation: Converts the bound AST into an initial tree of relational algebra operators (Select, Project, Join).
  4. Cost-Based Optimization (CBO): Explores alternative algebraically equivalent physical execution trees, evaluates catalog statistics, and selects the lowest-cost plan.
  5. Code Generation / Execution: Compiles the physical plan into bytecode or invokes the execution engine to stream tuple results.

2. The Volcano Iterator Execution Model

First formulated by Goetz Graefe in 1994, the Volcano Model (also termed the Pipeline or Iterator Model) defines every physical operator in the execution tree through a simple, standardized interface containing three core methods:

  • open(): Initializes operator state, allocates hash tables or sort buffers, and opens child iterators.
  • next(): Returns either the next single matching tuple or an EOF marker.
  • close(): Cleans up scratch memory allocations and closes child operator streams.

Pipelined Processing Advantage

Tuples are pulled demand-driven from the root of the tree down to the leaves one record at a time. The engine can begin streaming results to the client before preceding operations finish reading their entire tables, dramatically reducing memory footprint.

3. Physical Join Algorithms Compared

Join AlgorithmTime ComplexityI/O CostBest Applied When
Nested Loop Join (NLJ)$O(|R| \cdot |S|)$$M + (M \cdot N)$One table is tiny, or the inner table has an index on the join key (Index NLJ: $O(|R| \cdot \log |S|)$).
Sort-Merge Join (SMJ)$O(R \log R + S \log S)$$3(M + N)$Both tables are already sorted on join attributes (e.g., clustered index) or query includes ORDER BY.
Grace Hash Join$O(|R| + |S|)$$3(M + N)$Large unsorted tables with equality join predicates; builds in-memory hash table on smaller relation.

4. Cost-Based Optimization & Statistical Selectivity

The Cost-Based Optimizer (CBO) estimates the total system cost (a weighted sum of disk block I/Os, CPU instruction cycles, and memory pressure) for thousands of feasible execution plans:

Cost = (Disk_Pages_Fetched × W_disk) + (Tuples_Processed × W_cpu) + (Sort_Operations × W_mem)

To accurately predict the cardinality (number of output rows) of intermediate joins and filters, the database engine maintains an internal statistical catalog:

  • Equi-Width & Equi-Depth Histograms: Partition attribute domain values into buckets to estimate selectivity for range queries (e.g., salary BETWEEN 50000 AND 80000).
  • HyperLogLog (HLL): Probabilistic cardinality estimators that calculate the number of distinct values (NDV) in billion-row datasets using only kilobytes of state.
  • Dynamic Programming (Selinger Algorithm): Solves the combinatorial join-ordering search space ($N!$ permutations for $N$-way joins) by finding optimal plans bottom-up for sub-relations.

Next in Curriculum

In Part 8: Transactions: ACID Guarantees, Isolation Levels & MVCC Concurrency, we transition from single-query execution to multi-user concurrency control, locks, and Multi-Version Concurrency Control (MVCC).

Share:
Share on X
Share on LinkedIn
Share on Facebook
Share on Reddit
Share on WhatsApp
Share on Telegram
Copy link

Related reading

Query Execution & Cost-Based Optimization | Draftsbook · Draftsbook