Skip to content

opt: allow OrderingChoices to imply more orderings#64593

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
rytaft:origin/interesting-ord2
May 4, 2021
Merged

opt: allow OrderingChoices to imply more orderings#64593
craig[bot] merged 1 commit intocockroachdb:masterfrom
rytaft:origin/interesting-ord2

Conversation

@rytaft
Copy link
Copy Markdown
Collaborator

@rytaft rytaft commented May 3, 2021

This commit updates the logic for determining whether an OrderingChoice
can imply another OrderingChoice. The new logic allows columns in a group to be
treated like optional columns after one of the columns is used. For example,
"+1,+2,+3" implies "+(1|2),+3" now returns true. Prior to this commit, it
returned false, which resulted in unnecessary sort operations in some cases.
This commit also updates the corresponding logic for intersecting and finding
the common prefix of two OrderingChoices.

Furthermore, we now use interesting orderings to inform the required ordering
passed down to the children of Select expressions. This makes it more likely
that the child will be able to provide the required ordering.

Informs #33023

Release note (performance improvement): increased the intelligence
of the optimizer around orderings that can be provided by certain
relational expressions when there are equalities between columns.
This can allow the optimizer to remove unnecessary sort operations in
some cases, thus improving performance.

@rytaft rytaft requested review from a team, RaduBerinde and mgartner May 3, 2021 18:57
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@rytaft
Copy link
Copy Markdown
Collaborator Author

rytaft commented May 3, 2021

Ignore the first 3 commits -- they are from #64501.

@rytaft rytaft force-pushed the origin/interesting-ord2 branch 2 times, most recently from 9023b89 to d8a54d9 Compare May 3, 2021 19:30
@rytaft
Copy link
Copy Markdown
Collaborator Author

rytaft commented May 3, 2021

Just copying this from above so it doesn't get lost: ignore the first 3 commits -- they are from #64501.

Also, note that this doesn't fix #33023, because the example in that issue still requires a sort. To remove the sort, we'll need to write canProvideOrdering functions, etc., for With and WithScan, and possibly also derive interesting orderings for With, WithScan, and mutation expressions.

Copy link
Copy Markdown
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice!

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


pkg/sql/opt/ordering/select.go, line 30 at r4 (raw file):

		return props.OrderingChoice{}
	}
	child := parent.(*memo.SelectExpr).Input

This is a creative way to do it! But it deserves an explanation (and we should point to #33023 as well). What we need to get across is that trimColumnGroups will trim the column groups arbitrarily so we are trying to "guide" it into a good choice by using a more specific ordering.


pkg/sql/opt/ordering/select.go, line 34 at r4 (raw file):

	for i := range orders {
		if orders[i].Implies(required) {
			order := orders[i].CommonPrefix(required)

If it implies required why do we need to get the common prefix? (as opposed to just using orders[i])?

Also, maybe we should use Intersects and Intersection here instead (it should allow for more cases)

@rytaft rytaft force-pushed the origin/interesting-ord2 branch from d8a54d9 to 00fbf57 Compare May 3, 2021 20:56
Copy link
Copy Markdown
Collaborator Author

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

TFTR!

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


pkg/sql/opt/ordering/select.go, line 30 at r4 (raw file):

Previously, RaduBerinde wrote…

This is a creative way to do it! But it deserves an explanation (and we should point to #33023 as well). What we need to get across is that trimColumnGroups will trim the column groups arbitrarily so we are trying to "guide" it into a good choice by using a more specific ordering.

Thanks! Done.


pkg/sql/opt/ordering/select.go, line 34 at r4 (raw file):

If it implies required why do we need to get the common prefix? (as opposed to just using orders[i])?

We need to make sure that the required ordering is no stronger than needed. Otherwise, we get errors like this from checkRequired: "panic: provided +7,+8 can be trimmed to +7 (FDs: (6)==(7), (7)==(6))" (added a comment to that effect).

Also, maybe we should use Intersects and Intersection here instead (it should allow for more cases)

If we use Intersects and Intersection, we get those same errors from checkRequired, since Intersection may create a stronger ordering than required.

(I also tried removing that check to see what would happen, and using Intersects and Intersection here doesn't improve many plans -- it actually makes many plans worse).

Copy link
Copy Markdown
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 41 of 50 files at r1, 40 of 49 files at r3, 24 of 25 files at r4, 3 of 3 files at r5, 2 of 2 files at r6.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @rytaft)


pkg/sql/opt/ordering/select.go, line 34 at r4 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

If it implies required why do we need to get the common prefix? (as opposed to just using orders[i])?

We need to make sure that the required ordering is no stronger than needed. Otherwise, we get errors like this from checkRequired: "panic: provided +7,+8 can be trimmed to +7 (FDs: (6)==(7), (7)==(6))" (added a comment to that effect).

Also, maybe we should use Intersects and Intersection here instead (it should allow for more cases)

If we use Intersects and Intersection, we get those same errors from checkRequired, since Intersection may create a stronger ordering than required.

(I also tried removing that check to see what would happen, and using Intersects and Intersection here doesn't improve many plans -- it actually makes many plans worse).

Ah, got it, indeed, we need to trim the interesting ordering.

This commit updates the logic for determining whether an OrderingChoice
can imply another OrderingChoice. The new logic allows columns in a group to be
treated like optional columns after one of the columns is used. For example,
"+1,+2,+3" implies "+(1|2),+3" now returns true. Prior to this commit, it
returned false, which resulted in unnecessary sort operations in some cases.
This commit also updates the corresponding logic for intersecting and finding
the common prefix of two OrderingChoices.

Furthermore, we now use interesting orderings to inform the required ordering
passed down to the children of Select expressions. This makes it more likely
that the child will be able to provide the required ordering.

Informs cockroachdb#33023

Release note (performance improvement): increased the intelligence
of the optimizer around orderings that can be provided by certain
relational expressions when there are equalities between columns.
This can allow the optimizer to remove unnecessary sort operations in
some cases, thus improving performance.
@rytaft rytaft force-pushed the origin/interesting-ord2 branch from 00fbf57 to 970a932 Compare May 4, 2021 14:57
Copy link
Copy Markdown
Collaborator Author

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

bors r+

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

Copy link
Copy Markdown
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

Reviewed 6 of 6 files at r7.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner)

@craig
Copy link
Copy Markdown
Contributor

craig bot commented May 4, 2021

Build succeeded:

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.

3 participants