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

Faster Prepared Statement Execution by Using Raw SQL for Caching #17777

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

harshit-gangal
Copy link
Member

@harshit-gangal harshit-gangal commented Feb 13, 2025

Description

This PR changes the way Vitess handles prepared statements by modifying the plan cache lookup mechanism. Instead of normalizing and parsing the SQL string before looking up a cached execution plan, we now use the raw SQL string as provided in the PREPARE statement. This eliminates unnecessary parsing, improves lookup efficiency, and reduces overhead in query execution.

Problem Statement

Currently, when a client issues a PREPARE statement, vtgate:

  1. Parses the SQL query.
  2. Normalizes it to a canonical form.
  3. Uses the normalized query string to check for an existing cached execution plan.

What to do About it

Instead of parsing and normalizing the query before checking the plan cache, we will:
• Directly store the original SQL string from the PREPARE statement in the cache.
• Use this unmodified SQL string for cache lookups.
• Only parse the query when the plan is not found in the cache.

Benchmark

goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/test/endtoend/preparestmt
cpu: Apple M1 Max
                                        │ pfp-main.txt │            pfp-new.txt            │
                                        │    sec/op    │   sec/op     vs base              │
PreparedStmt/Insert-4                      494.3µ ± 3%   482.4µ ± 1%  -2.41% (p=0.002 n=6)
PreparedStmt/Select-4                      301.6µ ± 2%   288.4µ ± 3%  -4.35% (p=0.004 n=6)
PreparedStmt/Join_Select:Simple_Route-4    292.8µ ± 1%   269.9µ ± 1%  -7.81% (p=0.002 n=6)
PreparedStmt/Update-4                      344.9µ ± 2%   336.1µ ± 2%  -2.54% (p=0.009 n=6)
PreparedStmt/Delete-4                      311.4µ ± 0%   302.6µ ± 1%  -2.80% (p=0.002 n=6)
geomean                                    342.1µ        328.4µ       -4.01%

Related Issue(s)

Checklist

  • 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

Copy link
Contributor

vitess-bot bot commented Feb 13, 2025

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 Feb 13, 2025
@harshit-gangal harshit-gangal added the Skip CI Skip CI actions from running label Feb 13, 2025
@github-actions github-actions bot added this to the v22.0.0 milestone Feb 13, 2025
@harshit-gangal harshit-gangal added Component: Query Serving Type: Performance and removed Skip CI Skip CI actions from running NeedsWebsiteDocsUpdate What it says NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 14, 2025
Copy link

codecov bot commented Feb 14, 2025

Codecov Report

Attention: Patch coverage is 83.56164% with 84 lines in your changes missing coverage. Please review.

Project coverage is 67.44%. Comparing base (b05df12) to head (7949d15).

Files with missing lines Patch % Lines
go/vt/vtgate/executor.go 93.23% 9 Missing ⚠️
go/vt/vtgate/planbuilder/plan_test_vindex.go 0.00% 8 Missing ⚠️
go/vt/vtgate/fakerpcvtgateconn/conn.go 0.00% 7 Missing ⚠️
go/cmd/vtgateclienttest/services/errors.go 0.00% 6 Missing ⚠️
go/mysql/conn.go 57.14% 6 Missing ⚠️
go/vt/vtgate/plugin_mysql_server.go 0.00% 6 Missing ⚠️
go/cmd/vtgateclienttest/services/echo.go 0.00% 5 Missing ⚠️
go/vt/sqlparser/analyzer.go 37.50% 5 Missing ⚠️
go/cmd/vtgateclienttest/services/fallback.go 0.00% 4 Missing ⚠️
go/vt/vtgate/executorcontext/vcursor_impl.go 89.47% 4 Missing ⚠️
... and 15 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17777      +/-   ##
==========================================
- Coverage   67.45%   67.44%   -0.01%     
==========================================
  Files        1594     1594              
  Lines      259064   259042      -22     
==========================================
- Hits       174760   174721      -39     
- Misses      84304    84321      +17     

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

@harshit-gangal harshit-gangal force-pushed the prepare-fastpath branch 2 times, most recently from ffdc432 to aa4830b Compare February 17, 2025 07:15
@systay systay changed the title [WIP] Prepare Execute fastpath Faster Prepared Statement Execution by Using Raw SQL for Caching Feb 17, 2025
@systay systay removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request labels Feb 17, 2025
@systay systay force-pushed the prepare-fastpath branch 2 times, most recently from 5bdb210 to e5681c2 Compare February 18, 2025 10:13
@harshit-gangal harshit-gangal added the Benchmark me Add label to PR to run benchmarks label Feb 21, 2025
Copy link
Contributor

vitess-bot bot commented Feb 21, 2025

Hello! 👋

This Pull Request is now handled by arewefastyet. The current HEAD and future commits will be benchmarked.

You can find the performance comparison on the arewefastyet website.

@harshit-gangal harshit-gangal marked this pull request as ready for review February 25, 2025 16:40
harshit-gangal and others added 29 commits February 27, 2025 20:21
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Harshit Gangal <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
…hed and uncached plan, moved prepare path to it

Signed-off-by: Harshit Gangal <[email protected]>
Signed-off-by: Harshit Gangal <[email protected]>
…tting plan without parsing and normalizing

Signed-off-by: Harshit Gangal <[email protected]>
Signed-off-by: Harshit Gangal <[email protected]>
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.

Prepare statement execution optimization
2 participants