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

Performance improvements - Planner #16631

Merged
merged 6 commits into from
Aug 26, 2024
Merged

Conversation

systay
Copy link
Collaborator

@systay systay commented Aug 22, 2024

Description

This PR focuses on improving the performance of the Vitess planner by analyzing profiler output and implementing optimizations that reduce execution time, memory usage, and allocations. The changes are aimed at making the planner faster and more efficient, especially for complex queries.

Benchmark Results

The benchmarks were run on an Apple M1 Ultra and demonstrate significant improvements across various metrics:

Execution Time (sec/op):

  • The execution time for the planner has been reduced by 22.41% on average.
  • Notable improvements include:
  • OLTP/Gen4-20: -27.45%
  • TPCH/Gen4-20: -36.17%
  • Planner/large_cases.json-gen4-20: -49.41%
  • SelectVsDML/DML_(random_sample,_N=32)-20: -28.01%

Memory Usage (B/op):

  • Memory usage has been reduced by 33.48% on average.
  • Significant reductions were observed in:
  • TPCH/Gen4-20: -55.71%
  • Planner/large_cases.json-gen4-20: -70.67%
  • SelectVsDML/Select_(random_sample,_N=32)-20: -35.78%

Allocations (allocs/op):

  • Allocations have been reduced by 23.43% on average.
  • Key reductions include:
  • TPCH/Gen4-20: -42.77%
  • Planner/large_cases.json-gen4-20: -53.57%
  • SelectVsDML/Select_(random_sample,_N=32)-20: -29.82%
goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/vt/vtgate/planbuilder
cpu: Apple M1 Ultra
                                            │    before    │              ../after               │
                                            │    sec/op    │   sec/op     vs base                │
OLTP/Gen4-20                                   313.3µ ± 5%   227.3µ ± 2%  -27.45% (p=0.000 n=10)
OLTP/Gen4Greedy-20                             299.4µ ± 0%   225.6µ ± 2%  -24.64% (p=0.000 n=10)
OLTP/Gen4Left2Right-20                         299.7µ ± 0%   227.2µ ± 3%  -24.20% (p=0.000 n=10)
TPCC/Gen4-20                                   2.795m ± 0%   2.342m ± 1%  -16.18% (p=0.000 n=10)
TPCC/Gen4Greedy-20                             2.795m ± 0%   2.346m ± 1%  -16.06% (p=0.000 n=10)
TPCC/Gen4Left2Right-20                         2.754m ± 0%   2.292m ± 1%  -16.80% (p=0.000 n=10)
TPCH/Gen4-20                                   18.16m ± 1%   11.59m ± 1%  -36.17% (p=0.000 n=10)
TPCH/Gen4Greedy-20                             18.12m ± 0%   11.45m ± 1%  -36.78% (p=0.000 n=10)
TPCH/Gen4Left2Right-20                         16.30m ± 0%   10.41m ± 2%  -36.11% (p=0.000 n=10)
Planner/from_cases.json-gen4-20                9.378m ± 0%   7.871m ± 1%  -16.07% (p=0.000 n=10)
Planner/from_cases.json-gen4left2right-20      8.584m ± 1%   7.414m ± 0%  -13.63% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20              199.0m ± 0%   189.9m ± 0%   -4.55% (p=0.000 n=10)
Planner/filter_cases.json-gen4left2right-20    198.0m ± 0%   189.3m ± 1%   -4.40% (p=0.000 n=10)
Planner/large_cases.json-gen4-20              1000.6µ ± 0%   506.2µ ± 1%  -49.41% (p=0.000 n=10)
Planner/large_cases.json-gen4left2right-20     622.2µ ± 1%   315.2µ ± 1%  -49.34% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20                15.97m ± 0%   13.71m ± 1%  -14.11% (p=0.000 n=10)
Planner/aggr_cases.json-gen4left2right-20      15.24m ± 0%   13.17m ± 0%  -13.58% (p=0.000 n=10)
Planner/select_cases.json-gen4-20              12.81m ± 0%   11.07m ± 1%  -13.59% (p=0.000 n=10)
Planner/select_cases.json-gen4left2right-20    12.37m ± 1%   10.71m ± 1%  -13.39% (p=0.000 n=10)
Planner/union_cases.json-gen4-20               4.351m ± 0%   3.959m ± 1%   -9.02% (p=0.000 n=10)
Planner/union_cases.json-gen4left2right-20     4.342m ± 0%   3.962m ± 1%   -8.76% (p=0.000 n=10)
SemAnalysis-20                                 68.30m ± 0%   64.25m ± 1%   -5.93% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20      1078.7µ ± 5%   776.6µ ± 1%  -28.01% (p=0.000 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20    2.590m ± 1%   1.771m ± 1%  -31.60% (p=0.000 n=10)
geomean                                        5.596m        4.342m       -22.41%

                                            │    before     │               ../after                │
                                            │     B/op      │     B/op       vs base                │
OLTP/Gen4-20                                   173.4Ki ± 0%    144.9Ki ± 0%  -16.45% (p=0.000 n=10)
OLTP/Gen4Greedy-20                             173.4Ki ± 0%    144.9Ki ± 0%  -16.43% (p=0.000 n=10)
OLTP/Gen4Left2Right-20                         173.4Ki ± 0%    144.9Ki ± 0%  -16.44% (p=0.000 n=10)
TPCC/Gen4-20                                   1.227Mi ± 0%    1.056Mi ± 0%  -13.91% (p=0.000 n=10)
TPCC/Gen4Greedy-20                             1.227Mi ± 0%    1.056Mi ± 0%  -13.90% (p=0.000 n=10)
TPCC/Gen4Left2Right-20                         1.205Mi ± 0%    1.045Mi ± 0%  -13.25% (p=0.000 n=10)
TPCH/Gen4-20                                  12.031Mi ± 0%    5.328Mi ± 0%  -55.71% (p=0.000 n=10)
TPCH/Gen4Greedy-20                            12.032Mi ± 0%    5.328Mi ± 0%  -55.72% (p=0.000 n=10)
TPCH/Gen4Left2Right-20                        11.314Mi ± 0%    4.806Mi ± 0%  -57.52% (p=0.000 n=10)
Planner/from_cases.json-gen4-20                6.173Mi ± 0%    4.504Mi ± 0%  -27.05% (p=0.000 n=10)
Planner/from_cases.json-gen4left2right-20      5.643Mi ± 0%    4.314Mi ± 0%  -23.54% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20              32.24Mi ± 0%    22.39Mi ± 0%  -30.56% (p=0.000 n=10)
Planner/filter_cases.json-gen4left2right-20    32.06Mi ± 0%    22.32Mi ± 0%  -30.39% (p=0.000 n=10)
Planner/large_cases.json-gen4-20               917.1Ki ± 0%    268.9Ki ± 0%  -70.67% (p=0.000 n=10)
Planner/large_cases.json-gen4left2right-20     567.2Ki ± 0%    174.7Ki ± 0%  -69.20% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20                9.879Mi ± 0%    7.183Mi ± 0%  -27.29% (p=0.000 n=10)
Planner/aggr_cases.json-gen4left2right-20      9.464Mi ± 0%    7.001Mi ± 0%  -26.02% (p=0.000 n=10)
Planner/select_cases.json-gen4-20              7.656Mi ± 0%    6.063Mi ± 0%  -20.81% (p=0.000 n=10)
Planner/select_cases.json-gen4left2right-20    7.356Mi ± 0%    5.959Mi ± 0%  -18.99% (p=0.000 n=10)
Planner/union_cases.json-gen4-20               2.661Mi ± 0%    2.215Mi ± 0%  -16.75% (p=0.000 n=10)
Planner/union_cases.json-gen4left2right-20     2.641Mi ± 0%    2.211Mi ± 0%  -16.29% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20       686.7Ki ± 0%    495.4Ki ± 0%  -27.86% (p=0.000 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20   1581.4Ki ± 0%   1015.6Ki ± 0%  -35.78% (p=0.000 n=10)
geomean                                        2.720Mi         1.809Mi       -33.48%

                                            │    before    │              ../after               │
                                            │  allocs/op   │  allocs/op   vs base                │
OLTP/Gen4-20                                   4.050k ± 0%   3.612k ± 0%  -10.81% (p=0.000 n=10)
OLTP/Gen4Greedy-20                             4.050k ± 0%   3.612k ± 0%  -10.81% (p=0.000 n=10)
OLTP/Gen4Left2Right-20                         4.050k ± 0%   3.612k ± 0%  -10.81% (p=0.000 n=10)
TPCC/Gen4-20                                   28.28k ± 0%   25.70k ± 0%   -9.13% (p=0.000 n=10)
TPCC/Gen4Greedy-20                             28.28k ± 0%   25.70k ± 0%   -9.13% (p=0.000 n=10)
TPCC/Gen4Left2Right-20                         27.84k ± 0%   25.45k ± 0%   -8.56% (p=0.000 n=10)
TPCH/Gen4-20                                   234.1k ± 0%   134.0k ± 0%  -42.77% (p=0.000 n=10)
TPCH/Gen4Greedy-20                             234.1k ± 0%   134.0k ± 0%  -42.77% (p=0.000 n=10)
TPCH/Gen4Left2Right-20                         216.4k ± 0%   124.1k ± 0%  -42.68% (p=0.000 n=10)
Planner/from_cases.json-gen4-20                135.8k ± 0%   106.7k ± 0%  -21.41% (p=0.000 n=10)
Planner/from_cases.json-gen4left2right-20      124.5k ± 0%   101.5k ± 0%  -18.49% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20              642.7k ± 0%   562.7k ± 0%  -12.44% (p=0.000 n=10)
Planner/filter_cases.json-gen4left2right-20    638.6k ± 0%   560.7k ± 0%  -12.20% (p=0.000 n=10)
Planner/large_cases.json-gen4-20               22.67k ± 0%   10.52k ± 0%  -53.57% (p=0.000 n=10)
Planner/large_cases.json-gen4left2right-20    13.486k ± 0%   6.206k ± 0%  -53.98% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20                202.6k ± 0%   164.8k ± 0%  -18.66% (p=0.000 n=10)
Planner/aggr_cases.json-gen4left2right-20      193.6k ± 0%   160.3k ± 0%  -17.23% (p=0.000 n=10)
Planner/select_cases.json-gen4-20              166.0k ± 0%   140.4k ± 0%  -15.45% (p=0.000 n=10)
Planner/select_cases.json-gen4left2right-20    159.5k ± 0%   137.4k ± 0%  -13.85% (p=0.000 n=10)
Planner/union_cases.json-gen4-20               58.41k ± 0%   52.06k ± 0%  -10.87% (p=0.000 n=10)
Planner/union_cases.json-gen4left2right-20     57.95k ± 0%   51.89k ± 0%  -10.45% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20       13.87k ± 0%   10.27k ± 0%  -25.98% (p=0.000 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20    33.14k ± 0%   23.26k ± 0%  -29.82% (p=0.000 n=10)
geomean                                        59.29k        45.40k       -23.43%

Related Issue(s)

#16789

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

Copy link
Contributor

vitess-bot bot commented Aug 22, 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 Aug 22, 2024
@github-actions github-actions bot added this to the v21.0.0 milestone Aug 22, 2024
@systay systay added Component: Query Serving Type: Performance and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Aug 22, 2024
Copy link

codecov bot commented Aug 22, 2024

Codecov Report

Attention: Patch coverage is 87.27273% with 7 lines in your changes missing coverage. Please review.

Project coverage is 68.93%. Comparing base (e6843dc) to head (3c2f215).
Report is 6 commits behind head on main.

Files Patch % Lines
go/vt/vtgate/semantics/dependencies.go 41.66% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16631      +/-   ##
==========================================
- Coverage   68.99%   68.93%   -0.06%     
==========================================
  Files        1562     1562              
  Lines      200754   200758       +4     
==========================================
- Hits       138508   138393     -115     
- Misses      62246    62365     +119     

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

@systay systay marked this pull request as ready for review August 26, 2024 08:58
Comment on lines -64 to -66
if len(operators) < 2 {
panic("incorrect count of inputs for FkCascade")
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should put all these checks behind a debug mode? 🤔 Would that help us in debugging when a user reports a query is not working as intended? Also, won't removing these make some plans pass that would otherwise panic that could return wrong rows?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed the code so that we now only use SetInputs() on the output of GetInputs(). Is that enough you think?

Copy link
Member

@frouioui frouioui left a comment

Choose a reason for hiding this comment

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

These improvements are really impressive given the small change set, congrats! 🎉

@systay systay merged commit a54d5ec into vitessio:main Aug 26, 2024
129 checks passed
@systay systay deleted the quick-planner branch August 26, 2024 19:52
@harshit-gangal
Copy link
Member

Good Riddance to calling Clone in rewriters

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.

4 participants