Skip to content

Assume containment in join and group count estimates - #2400

Open
chenjinbao1989 wants to merge 1 commit into
apache:mainfrom
chenjinbao1989:pr-join-containment
Open

chenjinbao1989 wants to merge 1 commit into
apache:mainfrom
chenjinbao1989:pr-join-containment

Conversation

@chenjinbao1989

Copy link
Copy Markdown
Contributor

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
chenjinbao1989 force-pushed the pr-join-containment branch 2 times, most recently from d0592f1 to 2844a5a Compare November 22, 2026 21:57
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.
@tuhaihe

tuhaihe commented Nov 23, 2026

Copy link
Copy Markdown
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.

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.

2 participants