Skip to content

Conversation

@michae2
Copy link
Collaborator

@michae2 michae2 commented Nov 7, 2025

The SplitDisjunction rules did not consider disjunctions over expressions referencing the same column interesting, because normally these disjunctions can become multiple spans in the same constrained scan. There is one special case, however, where disjunctions over the same column might be interesting: if the table has multiple partial indexes with different predicates referencing that column. In that case we might be able to use a different partial index for each side of the disjunction.

Fixes: #157073

Release note (performance improvement): This commit teaches the optimizer to split disjunctions on the same column into unions when there are multiple partial indexes with different predicates referencing that column.

The SplitDisjunction rules did not consider disjunctions over
expressions referencing the same column interesting, because normally
these disjunctions can become multiple spans in the same constrained
scan. There is one special case, however, where disjunctions over the
same column might be interesting: if the table has multiple partial
indexes with different predicates referencing that column. In that case
we might be able to use a different partial index for each side of the
disjunction.

Fixes: cockroachdb#157073

Release note (performance improvement): This commit teaches the
optimizer to split disjunctions on the same column into unions when
there are multiple partial indexes with different predicates referencing
that column.
@michae2 michae2 requested review from a team, DrewKimball and mgartner November 7, 2025 20:20
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

// If the current expression ColSet matches leftColSet (and we're not using
// the exception for multiple referencing partial index predicates) add the
// expr to the left group. Otherwise, add it to the right group.
if leftColSet.Equals(cols) && !c.multiplePartialIndexesReferencing(sp, leftColSet) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could be stricter and keep a set of the partial indexes with predicates implied by the left exprs, and when examining an expr we can add it to right exprs if its colset is equal to leftColSet and it implies at least one partial index predicate not implied by the left expressions.

----

opt expect=SplitDisjunction
SELECT c, a FROM t157073 WHERE c = 5 AND (b < 0 OR b >= 9990)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests with multiple adjacent disjunctions would be interesting to see.

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.

sql/opt: explore splitting disjunctions of same column in some cases

3 participants