The search space decides how many plans you can think of; the rules decide what shapes are reachable; and the cost model decides who wins. Whether an optimizer is usable depends heavily on whether its cost model “makes sense”.
This post takes COLUMBIA’s cost model apart, plus the traps we hit when calibrating it on TPC-H.
1. What COLUMBIA’s cost model looks like
Minimal: a cost model is a set of constants plus a local-cost formula per physical operator.
| Component | Implementation |
|---|---|
| Parameters | CM: 12 doubles, from the CMS/Cm file |
| Local cost | Each physical operator implements FindLocalCost(LocalLogProp, InputLogProps) |
| Total cost | FinalCost = local cost + Σ input costs |
| Representation | A single double; -1 means infinity (infeasible) |
Its design philosophy is “compare, don’t predict” — don’t ask it “how many milliseconds will this query take”; ask it “which of plan A and plan B is cheaper”.
2. Where local-cost formulas come from
For a scan, local cost usually has three parts:
touch_copy = per-page throughput cost × pages
fetching = random I/O cost × seeks
cpu = per-row compare/project cost × rows
The coefficients (cost per page, per seek, per CPU row) are where the 12 constants come from. They should be derived from hardware calibration: sequential bandwidth, random-read latency, cycles per row.
The key point: absolute values don’t matter; monotonicity does. As long as “more rows is more expensive” and “random I/O costs more than sequential” hold, the optimizer can rank plans correctly.
3. Calibration on TPC-H
We let DuckDB generate the SF=1 catalog and a cost model:
lineitem6,001,215 rows,orders1,500,000, …- From those row counts and widths, scan / join / aggregate costs are derived.
Verification isn’t “compare milliseconds” but compare plan shapes: for the same query, does our plan pick the same join order and operator types as DuckDB? Agreement means the relative cost relations are right.
A real example: q1’s final plan costs 6441.93, where item nodes (projection, comparison) cost 0 — they produce no I/O, only CPU work. The number itself is meaningless; its size relative to alternatives (e.g., a different aggregation strategy) is what matters.
4. Three real traps
① Negative cost
COST asserts Number == -1 || Number >= 0. We hit negative costs twice:
TouchCopyCost’s min/max exclusion made cost negative for a single table;OUTER_JOIN/THETA_JOINlacked properties soCard == -1, and the formula went negative.
Lesson: every input to a cost formula needs a lower bound; -1 may only mean “infeasible”, never participate in arithmetic.
② Pruning lower bounds must be admissible
Search prunes with a LowerBound: if a partial plan already exceeds a known upper bound, drop the path. That requires the bound to never overestimate — overestimating prunes the optimum.
③ Few cost readers make debugging easier
COLUMBIA reads cost only in physop.cpp (TouchCopyCost/FetchingCost + FindLocalCost), so “why this plan?” has only two or three suspects — far easier than cost logic scattered everywhere.
5. Honest boundaries
This cost model is uncalibrated: constants are empirical, not measured hardware parameters. It guarantees roughly correct plan ordering, not physically meaningful costs.
To make it production-grade you need: statistics (histograms, NDV), better cardinality estimation, and reverse-calibration from real execution data.
Related
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.