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.

@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!

Copy link
Collaborator Author

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball and @mgartner)

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.

LGTM. Nicely done!

└── c:3

opt expect=SplitDisjunction
SELECT c, a FROM t157073 WHERE c = 5 AND (b < 9990 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.

So in this case, we split the disjunction but those two branches could not be index-accelerated, so we pick a full scan instead. Is that correct? Might be good to leave a short note explaining that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's correct. I added a note (and a few more test cases I thought of).

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
Copy link
Collaborator Author

michae2 commented Nov 17, 2025

TFTR!

bors r=mgartner

@craig
Copy link
Contributor

craig bot commented Nov 17, 2025

@craig craig bot merged commit 33ebee8 into cockroachdb:master Nov 17, 2025
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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