Query optimization (code by Claude, guidance by Georgios Evangelidis)

An optimizer turns a declarative SQL query into an efficient evaluation plan. It parses the query into relational algebra, rewrites it with equivalences (pushing selections and projections down), estimates the size of every intermediate result from catalog statistics, and searches the space of plans, here with System R dynamic programming over left-deep join trees, choosing access paths and join methods by estimated I/O cost.

Catalog

RelationPagesTuples / pageTuples
Column statistics
ColumnTypeBytesNKeysLowHighData

Query

SELECT … FROM … WHERE (conditions, [NOT] IN / [NOT] EXISTS subqueries, joined by AND) … GROUP BY … HAVING … ORDER BY; two blocks may be combined with UNION [ALL], INTERSECT or EXCEPT. Ctrl + Enter runs it.

Indexes

OnIndexTypeClusteredPages
Add on

The cost model, and what it leaves out