Skip to content

Choose join order from a cost model instead of the written order - #4

Merged
SpookyJumpyBeans merged 1 commit into
mainfrom
feature/join-reordering
Sep 8, 2026
Merged

SpookyJumpyBeans merged 1 commit into
mainfrom
feature/join-reordering

Conversation

@SpookyJumpyBeans

@SpookyJumpyBeans SpookyJumpyBeans commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

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:

  • analyze walks 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.
  • Selectivity and join cardinality use the textbook formulas, including |A| * |B| / max(ndv) for equijoins.
  • choose_join_order runs 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.

Reorder operator. Reordering moves columns around inside the row, so SELECT * would otherwise start reporting a different column order. Project cannot fix this because it flattens qualifiers and would break o.region further up, so Reorder permutes back to the declared order with the schema intact.

reorder=True threaded through plan / 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:

facts rows out cost-based as written speedup
1,000 20 0.004s 0.006s 1.30x
20,000 400 0.073s 0.108s 1.48x
100,000 2,000 0.324s 0.711s 2.19x

The plan it picks:

HashJoin f.dim_id = d.id
  HashJoin t.id = f.tiny_id
    Filter t.id = 1
      Scan t
    Scan f
  Scan d

The filtered row leads, the join to f collapses the fact table immediately, and d joins 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=True and reorder=False and 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant