SQL Optimizer - not choosing the better join order #137701
Labels
A-sql-optimizer
SQL logical planning and optimizations.
branch-release-24.2
Used to mark GA and release blockers, technical advisories, and bugs for 24.2
C-bug
Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
T-sql-queries
SQL Queries Team
Describe the problem
The optimizer seems to be missing a join reordering opportunity that would avoid a full scan.
This issue was originally reported in Zendesk https://cockroachdb.zendesk.com/agent/tickets/24877
The Zendesk ticket includes statement bundles from the customer's cluster, and the un-redacted SQL.
This query below has a full scan on
join_requests
returning 1,087,393 rows, then a lookup join onprojects
which is an outer join. This lookup join is where all the time is being spent. Finally, the 820,399 rows from the lookup join are hash joined toteams
. However, the scan onteams
results in 1 row, and the hash join results in 0 rows.If I simply switch the order that
projects
andteams
are listed in the SQL, the subquery returns in 6ms. This scansteams
and results in 1 row, which is lookup joined tojoin_requests
resulting in 0 rows.To Reproduce
Statement bundles of both versions of the original query are included in the Zendesk ticket https://cockroachdb.zendesk.com/agent/tickets/24877
The query described above is just one subquery within a larger SQL. The statement bundles in the Zendesk ticket include the full original SQL.
I checked the
reorder_joins_limit
which was set to the default. When I setreorder_joins_limit = 10
, there was no improvement in the query or plan using the original join order.Expected behavior
I expected the optimizer to consider the join reorder without a SQL modiication.
Environment:
us-east-1
with 12 nodesJira issue: CRDB-45710
gz#24877
The text was updated successfully, but these errors were encountered: