Repository navigation
Choose join order from a cost model instead of the written order - #4
Merged
Merged
Conversation
Joins ran in the order they appeared in the query. That is fine until it isn't: a query that joins a fact table to a wide dimension before applying a filter that leaves one row carries every fact row through the wide join for nothing. Adds tinyquery/cost.py. `analyze` walks a table once for row count and per-column distinct values, which is what ANALYZE collects, except this one can look at everything because the table is already in memory. Selectivity and join cardinality use the textbook formulas, and `choose_join_order` runs the Selinger subset DP over left-deep plans, costing each join as build side plus probe side. Reordering moves columns around inside the row, so SELECT * would have started reporting a different column order. Project cannot fix that because it flattens qualifiers and would break o.region further up, so this adds a Reorder operator that permutes back to the declared order and keeps the schema intact. The planner takes reorder=True, threaded through plan/compile_sql/execute/ explain. Turning it off restores the previous behaviour exactly, which is how the tests compare the two plans on identical data and how the benchmark measures the difference. On a fact table joined to a 200 row dimension and a 50 row table filtered to one, the reordered plan runs 1.30x faster at 1,000 facts and 2.19x at 100,000. The gap widens because the cost of a bad order scales with the intermediate result, the same shape as hash join versus nested loop and unlike pushdown, which stays flat. 27 new tests, 76 total.
SpookyJumpyBeans
force-pushed
the
feature/join-reordering
branch
from
September 8, 2026 02:51
f67a6d3 to
6c6a36b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Joins ran in the order they appeared in the query. That is fine until it isn't: joining a fact table to a wide dimension before applying a filter that leaves one row carries every fact row through the wide join for nothing.
What was added
tinyquery/cost.py:analyzewalks a table once for row count and per-column distinct values. That is what ANALYZE collects, except this one looks at everything rather than sampling, which is only honest because the tables are already in memory.|A| * |B| / max(ndv)for equijoins.choose_join_orderruns the Selinger subset DP over left-deep plans, costing each join as build side plus probe side, and skipping any step that would be a cross product.Reorderoperator. Reordering moves columns around inside the row, soSELECT *would otherwise start reporting a different column order.Projectcannot fix this because it flattens qualifiers and would breako.regionfurther up, soReorderpermutes back to the declared order with the schema intact.reorder=Truethreaded throughplan/compile_sql/execute/explain. Off restores the old behaviour exactly, which is how the tests compare plans on identical data.Results
Fact table joined to a 200 row dimension and a 50 row table filtered to one row:
The plan it picks:
The filtered row leads, the join to
fcollapses the fact table immediately, anddjoins against something small. The gap widens with size, because the cost of a bad order scales with the intermediate result. Same shape as hash join versus nested loop, unlike pushdown which stays flat at 1.7x.Tests
27 new, 76 total. Every correctness test runs the same query with
reorder=Trueandreorder=Falseand compares, including a fuzz test over random three-table shapes, so a reordering bug shows up as a disagreement rather than as a plausible wrong answer. Separate unit tests cover the cost model itself.Limits
Left-deep only, gives up above 10 tables, and declines when a join key is not a simple qualified column. Estimates assume independence, so correlated predicates mislead it the way they mislead every optimiser.