Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: inline constant values for FK and uniqueness checks #65093

Closed
wants to merge 1 commit into from

Conversation

mgartner
Copy link
Collaborator

In FK and uniqueness checks, WithScans that buffer the mutation's input
are replaced with Values expression when inserted values are constant.
This is especially beneficial for REGIONAL BY ROW tables where the
crdb_region column is a computed column dependent on a FK. Because the
constant values are inlined, the optimizer is not able to reduce a FK
check that scans multiple regions with a FK check that checks only a
single region.

Informs #63882

Release note (performance improvement): Previously, foreign key checks
performed for inserts into REGIONAL BY ROW tables always searched the
parent table in all regions to check for FK violations. Now, only a
single region is searched if constant values are inserted and the
crdb_region column is a computed column dependent on the FK column.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner
Copy link
Collaborator Author

mgartner commented May 12, 2021

Ok, this was more of a doozy than I thought it would be. Please review carefully. I used "Informs" instead of "Fixes" in the commit because I think there's probably more work to be done here. But I think this is a good start. For example, can we do something similar for DELETES on parent tables? What about FK cascades?

@mgartner mgartner force-pushed the fk-check-const-values branch 2 times, most recently from a8d258b to 79293dc Compare May 13, 2021 16:01
Copy link
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.

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


pkg/sql/opt/xform/testdata/external/tpcc-later-stats, line 165 at r2 (raw file):

 └── f-k-checks
      └── f-k-checks-item: order(o_w_id,o_d_id,o_c_id) -> customer(c_w_id,c_d_id,c_id)
           └── anti-join (cross)

I think this will prevent the use of "insert fast path", which is an important optimization. We should probably add some execbuilder tests for these. I'm not quite sure how to integrate these two things.

@mgartner mgartner force-pushed the fk-check-const-values branch 2 times, most recently from 6851027 to ebe1373 Compare May 22, 2021 00:11
@mgartner
Copy link
Collaborator Author

This is ready for a look now. Radu and I discussed how this breaks the insert fast path for some cases, but we'll address that in a future PR. The fix is not yet obvious.

I also made a slight tweak so that DEFAULT and computed insert values will be inlined, assuming they are normalized to constant values in mb.insertExpr.

Copy link
Collaborator

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

:lgtm:

Nice! I'm just a bit worried about merging this before we have a fix to the fast path in case it causes a TPC-C regression...

Reviewed 7 of 11 files at r1, 10 of 11 files at r2, 7 of 7 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)


pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit, line 749 at r2 (raw file):

    └── • error if rows
        │
        └── • norows

nice simplification!


pkg/sql/opt/optbuilder/mutation_builder.go, line 1350 at r3 (raw file):

// checking for FK and uniqueness violations. It returns either a WithScan that
// iterates over the input to the mutation operator, or a Values expression with
// constant mutation values inlined. produces constant values

nit: "produces constant values" looks it was left over


pkg/sql/opt/xform/testdata/external/tpcc, line 162 at r3 (raw file):

 └── f-k-checks
      └── f-k-checks-item: order(o_w_id,o_d_id,o_c_id) -> customer(c_w_id,c_d_id,c_id)
           └── anti-join (cross)

do you think we'll see a regression in TPC-C if we merge this without fixing the fast path issue?

In FK and uniqueness checks, WithScans that buffer the mutation's input
are replaced with Values expression when inserted values are constant.
This is especially beneficial for `REGIONAL BY ROW` tables where the
`crdb_region` column is a computed column dependent on a FK. Because the
constant values are inlined, the optimizer is not able to reduce a FK
check that scans multiple regions with a FK check that checks only a
single region.

Informs cockroachdb#63882

Release note (performance improvement): Previously, foreign key checks
performed for inserts into `REGIONAL BY ROW` tables always searched the
parent table in all regions to check for FK violations. Now, only a
single region is searched if constant values are inserted and the
`crdb_region` column is a computed column dependent on the FK column.
Copy link
Collaborator Author

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

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


pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit, line 749 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nice simplification!

🎉


pkg/sql/opt/optbuilder/mutation_builder.go, line 1350 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: "produces constant values" looks it was left over

Done.


pkg/sql/opt/xform/testdata/external/tpcc, line 162 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

do you think we'll see a regression in TPC-C if we merge this without fixing the fast path issue?

I would assume we would see a regression. I'm fine with leaving this PR unmerged until we can come up with a fix for the fast path issue.

Copy link
Collaborator

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

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


pkg/sql/opt/xform/testdata/external/tpcc, line 162 at r3 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I would assume we would see a regression. I'm fine with leaving this PR unmerged until we can come up with a fix for the fast path issue.

Sounds good -- I think it would be good to hold off merging in that case.

@mgartner
Copy link
Collaborator Author

Closing in favor of #77943.

@mgartner mgartner closed this Mar 17, 2022
@mgartner mgartner deleted the fk-check-const-values branch March 17, 2022 21:59
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.

4 participants