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

sql: foreign key batch checking per statement #26786

Closed
3 tasks
emsal0 opened this issue Jun 18, 2018 · 14 comments
Closed
3 tasks

sql: foreign key batch checking per statement #26786

emsal0 opened this issue Jun 18, 2018 · 14 comments
Assignees
Labels
A-sql-fks A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. C-performance Perf of queries or internals. Solution not expected to change functional behavior.

Comments

@emsal0
Copy link
Contributor

emsal0 commented Jun 18, 2018

Currently, foreign key checks are run for every row being inserted. We could make optimizations by making foreign key checks at the end of each statement, batching together all of the checks corresponding to a single statement.

@BramGruneir @knz @jordanlewis

"if you insert 10 rows in a single statement, 8 of which have the same value for a column that has a foreign key relationship to another table, you shouldn’t have to check that relationship 8 times"

  • Benchmarks need to be made that test the performance of foreign key checks for multi-row statements and other statements that deal with foreign key checks
  • The code change itself: change the way that some functions in fk.go do the foreign key checks, and possibly how those functions are called
  • Confirm that the code change actually results in better performance on the benchmark
@emsal0 emsal0 self-assigned this Jun 18, 2018
@jordanlewis
Copy link
Member

See #15157 if you haven't already, which has some prior discussion about this issue as well as some potential other strategies for dealing with it.

@BramGruneir
Copy link
Member

One thing to note, is that when batching together for each statement, make sure that moving them to batching per transaction or even reverting to per row is possible. That way deferrable can be implemented afterwards.

@tbg
Copy link
Member

tbg commented Jun 18, 2018

Oh, I missed this issue when creating #26795. Feel free to incorporate what you like into this one and close it.

@knz
Copy link
Contributor

knz commented Jun 18, 2018 via email

@knz
Copy link
Contributor

knz commented Jun 18, 2018 via email

@BramGruneir
Copy link
Member

@knz, I agree.

And of course we need to be cognizant of the missing functionality. Regardless, we need to ensure that we never run out of memory.

@knz knz added C-performance Perf of queries or internals. Solution not expected to change functional behavior. A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. labels Jul 9, 2018
@knz knz added this to the 2.1 milestone Jul 23, 2018
@BramGruneir BramGruneir assigned BramGruneir and unassigned emsal0 Sep 11, 2018
@BramGruneir BramGruneir modified the milestones: 2.1, 2.2 Sep 11, 2018
@BramGruneir
Copy link
Member

The work on this did not make it into 2.1, but should be done in the 2.2 time frame.

@nvanbenschoten
Copy link
Member

I just spent some time looking at traces from new_order transactions in TPC-C. On a cluster with a low amount of load, the transaction takes around 31ms. The traces revealed that, on average, about 9ms of the transaction are spent performing redundant foreign key lookups that could be eliminated if we collapsed foreign key checks across rows in the same statement. Put another way, 9 of the 23 kv batches issued by the transaction were superfluous and could be completely avoided by addressing this issue. That's an estimated 27% savings on the most common txn in TPC-C.

@knz
Copy link
Contributor

knz commented Mar 29, 2019 via email

@knz
Copy link
Contributor

knz commented Apr 1, 2019

cc @awoods187 please refer to nathan's comment above

@awoods187
Copy link
Contributor

It's on the roadmap

@knz
Copy link
Contributor

knz commented Apr 1, 2019

@awoods187 I was more thinking about copy-pasting his explanation into airtable, given that the initial motivation was one of correctness and use cases, and here we have a new argument that's performance-oriented.

@jordanlewis
Copy link
Member

This will get done as part of the optimizer's work on foreign key planning, so I'm moving to planning project for tracking.

@RaduBerinde
Copy link
Member

Opt-driven FK checks are now enabled on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-fks A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. C-performance Perf of queries or internals. Solution not expected to change functional behavior.
Projects
None yet
Development

No branches or pull requests

9 participants