Repository navigation
Assume containment in join and group count estimates - #2400
Open
chenjinbao1989 wants to merge 1 commit into
Open
chenjinbao1989 wants to merge 1 commit into
chenjinbao1989 wants to merge 1 commit into
Conversation
chenjinbao1989
force-pushed
the
pr-join-containment
branch
2 times, most recently
from
November 22, 2026 21:57
d0592f1 to
2844a5a
Compare
In TPC-DS, dimension tables like date_dim cover far more values than the fact tables reference: date_dim has 73049 days, the sales tables 1823, inventory 261. Queries restrict the dimension to a range the facts cover, but the planner assumes the dimension's remaining keys are a random sample of all of them, and underestimates joins to the facts, sometimes by orders of magnitude. Change the estimates in five places: - In eqjoinsel(), clamp a join column's number of distinct values to the rows of its base relation left after restriction, as if they all occurred on the other side. A fact joined to a dimension restricted to a year is then estimated at the fact rows for that year, not 1/200 of them. - The same, when a side of the join is itself a join: a column's distinct values are clamped to the rows of that join input, and, if the column's relation is joined there on a unique key to another relation, to the distinct values of the other relation's join column. In q72, inventory joined to date_dim on its key leaves 261 distinct weeks, not 10430, for the join to the other date_dim on d_week_seq. This makes a join clause's selectivity depend on the join inputs, so while set_joinrel_size_estimates() runs, join clause selectivities aren't cached; costing paths still uses the cache. - In estimate_num_groups(), when the restrictions of a relation refer only to its grouping columns, they keep or remove whole groups, so scale the relation's number of groups by the restriction's selectivity, instead of the formula for a random sample of rows, which barely reduces it. - In estimate_num_groups(), replace grouping columns of a relation joined on a unique key by the referencing column of the other relation. If all grouping columns then belong to one relation, and extended ndistinct statistics on it cover them, use those statistics, scaled by the fraction of the relation's rows in the input. In q87, (c_last_name, c_first_name, d_date) over store_sales becomes (ss_customer_sk, ss_sold_date_sk), which is correlated: a ticket has about 10 lines. Without covering statistics the mapping isn't used. - When deciding whether a SubPlan can be hashed, size the hash table by the estimated number of distinct values of the subquery's output instead of its rows. At TPC-DS scale factor 10, with extended statistics on the customer and date keys of store_sales, catalog_sales and web_sales, these changes on top of the other planner changes take the 99 queries from 226 s to 213 s; ORCA takes 207 s. q72 goes from 6.6 s to 6.8 s (12.1 s with only the first three changes), q87 from 2.2 s to 2.3 s (3.5 s), q74 from 7.7 s to 2.8 s, q64 from 14.3 s to 10.0 s, q39 from 5.0 s to 3.0 s. The results are unchanged.
chenjinbao1989
force-pushed
the
pr-join-containment
branch
from
November 23, 2026 02:42
2844a5a to
46a5335
Compare
Member
|
Hi, just a quick tip. If the last test run was successful without failure, we can re-run the tests after approval to save the runner resource. FYI. |
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.
In TPC-DS, dimension tables like date_dim cover far more values than the fact tables reference: date_dim has 73049 days, the sales tables 1823, inventory 261. Queries restrict the dimension to a range the facts cover, but the planner assumes the dimension's remaining keys are a random sample of all of them, and underestimates joins to the facts, sometimes by orders of magnitude. Change the estimates in five places:
In eqjoinsel(), clamp a join column's number of distinct values to the rows of its base relation left after restriction, as if they all occurred on the other side. A fact joined to a dimension restricted to a year is then estimated at the fact rows for that year, not 1/200 of them.
The same, when a side of the join is itself a join: a column's distinct values are clamped to the rows of that join input, and, if the column's relation is joined there on a unique key to another relation, to the distinct values of the other relation's join column. In q72, inventory joined to date_dim on its key leaves 261 distinct weeks, not 10430, for the join to the other date_dim on d_week_seq. This makes a join clause's selectivity depend on the join inputs, so while set_joinrel_size_estimates() runs, join clause selectivities aren't cached; costing paths still uses the cache.
In estimate_num_groups(), when the restrictions of a relation refer only to its grouping columns, they keep or remove whole groups, so scale the relation's number of groups by the restriction's selectivity, instead of the formula for a random sample of rows, which barely reduces it.
In estimate_num_groups(), replace grouping columns of a relation joined on a unique key by the referencing column of the other relation. If all grouping columns then belong to one relation, and extended ndistinct statistics on it cover them, use those statistics, scaled by the fraction of the relation's rows in the input. In q87, (c_last_name, c_first_name, d_date) over store_sales becomes (ss_customer_sk, ss_sold_date_sk), which is correlated: a ticket has about 10 lines. Without covering statistics the mapping isn't used.
When deciding whether a SubPlan can be hashed, size the hash table by the estimated number of distinct values of the subquery's output instead of its rows.
At TPC-DS scale factor 10, with extended statistics on the customer and date keys of store_sales, catalog_sales and web_sales, these changes on top of the other planner changes take the 99 queries from 226 s to 213 s; ORCA takes 207 s. q72 goes from 6.6 s to 6.8 s (12.1 s with only the first three changes), q87 from 2.2 s to 2.3 s (3.5 s), q74 from 7.7 s to 2.8 s, q64 from 14.3 s to 10.0 s, q39 from 5.0 s to 3.0 s. The results are unchanged.