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

Optimize sqlutils.ToSqlite3Dialect for queries + DML/DDLs #17113

Conversation

timvaillancourt
Copy link
Contributor

@timvaillancourt timvaillancourt commented Oct 29, 2024

Description

This PR adds some slight optimisations to sqlutils.ToSqlite3Dialect, a function we've noticed to take up a significant amount of CPU time in vtorc when it watches many instances, primarily due to large insert queries needing to be parsed by regexp filters

Profile SVG (need to enlarge):
profile001

All this change does is assumes inserts happen more frequently than alter/create. Moving inserts to be checked sooner and separating read queries causes them to have to go through fewer regex matches (still a lot 🙈). The expense of queries that are not insert, alter or create is unchanged.

On Slack's vtorc instances this gave us about a 15-20% reduction in user-CPU used by vtorc vs the previous avg

In the benchmark I added in the PR the difference is more like 8-9% for big inserts and 18-19% for reads

Before

goos: darwin
goarch: amd64
pkg: vitess.io/vitess/go/vt/external/golib/sqlutils
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkToSqlite3Dialect_Insert1000
BenchmarkToSqlite3Dialect_Insert1000-16    	       1	1870643229 ns/op
BenchmarkToSqlite3Dialect_Select
BenchmarkToSqlite3Dialect_Select-16        	    3541	    330667 ns/op
PASS
ok  	vitess.io/vitess/go/vt/external/golib/sqlutils	4.660s

After

goos: darwin
goarch: amd64
pkg: vitess.io/vitess/go/vt/external/golib/sqlutils
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkToSqlite3Dialect_Insert1000
BenchmarkToSqlite3Dialect_Insert1000-16    	       1	1711349181 ns/op
BenchmarkToSqlite3Dialect_Select
BenchmarkToSqlite3Dialect_Select-16        	    4436	    270223 ns/op
PASS
ok  	vitess.io/vitess/go/vt/external/golib/sqlutils	4.528s

Related Issue(s)

Resolves #17114

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

@timvaillancourt timvaillancourt added Type: Performance Component: VTorc Vitess Orchestrator integration labels Oct 29, 2024
Copy link
Contributor

vitess-bot bot commented Oct 29, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Oct 29, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Oct 29, 2024
@timvaillancourt timvaillancourt removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Oct 29, 2024
Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
@timvaillancourt timvaillancourt changed the title Optimize sqlutils.ToSqlite3Dialect for inserts Optimize sqlutils.ToSqlite3Dialect for queries + DML/DDLs Oct 29, 2024
@mattlord
Copy link
Contributor

@GuptaManan100 do we still need this library at all? vtorc only supports SQLite now so I would think that we would use the SQLite dialect everywhere.

@timvaillancourt
Copy link
Contributor Author

timvaillancourt commented Oct 29, 2024

@GuptaManan100 do we still need this library at all? vtorc only supports SQLite now so I would think that we would use the SQLite dialect everywhere.

@mattlord this is what I asked/proposed in this thread 👍. I'm hoping we can remove dialect translation and just use sqlite dialect

Also I think external/sqlutils could be replaced with something a bit less crufty - there are similar projects that can reflect database/sql to map/structs for example. For now this is some duct tape

EDIT: an alternative I've enjoyed using is https://github.com/jmoiron/sqlx, but not sure how dependency-heavy it is. I'm 95% sure it will handle anything external/sqlutils provides and it's an active project. I can make that RFC and PRs if there is agreement

Signed-off-by: Tim Vaillancourt <[email protected]>
@dbussink
Copy link
Contributor

@mattlord this is what I asked/proposed in this thread 👍. I'm hoping we can remove dialect translation and just use sqlite dialect

I think removing the translation and writing queries directly that work with sqlite is a good idea indeed. It removes this whole layer of translation that doesn't really serve any purpose anymore.

Also I think external/sqlutils could be replaced with something a bit less crufty - there are similar projects that can reflect database/sql to map/structs for example. For now this is some duct tape

I think it's a good idea to clean this up (also fine incrementally). It's all leftovers from how it was basically vendored but only parts have been modernized to match how other parts of Vitess work. So I think thinks kind of cleanup is also very welcome.

Copy link

codecov bot commented Oct 29, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 4 lines in your changes missing coverage. Please review.

Project coverage is 67.17%. Comparing base (fa5a2c5) to head (9e82f8b).
Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/external/golib/sqlutils/sqlite_dialect.go 78.94% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17113      +/-   ##
==========================================
- Coverage   67.17%   67.17%   -0.01%     
==========================================
  Files        1571     1571              
  Lines      252246   252258      +12     
==========================================
+ Hits       169458   169465       +7     
- Misses      82788    82793       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@timvaillancourt timvaillancourt removed the NeedsIssue A linked issue is missing for this Pull Request label Oct 29, 2024
@GuptaManan100
Copy link
Member

Yes, @timvaillancourt we can get rid of all other dialects other than sqlite. We don't allow any other backing database in VTOrc.

@timvaillancourt
Copy link
Contributor Author

Closing in favour of #17124 which removes the translation entirely

@timvaillancourt timvaillancourt deleted the vtorc-sqlutils-ToSqlite3Dialect-insertFirst branch October 31, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: VTorc Vitess Orchestrator integration Type: Performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Optimize sqlutils.ToSqlite3Dialect for queries + DML/DDLs
4 participants