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

statistics: use another way to merge topn #47765

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

winoros
Copy link
Member

@winoros winoros commented Oct 18, 2023

What problem does this PR solve?

Issue Number: ref #50761

Problem Summary:

What is changed and how it works?

We use the property that items inside both the TopN and the histogram are ordered to speed up the process.

And use a heuristic cutting:

  • We record the avg num per distinct value of each histogram notNullCount / ndv_in_hist.
  • Then for a topn item, we can use the mentioned property to know the TopNs that contain the item.
  • Then we can easily know in which partition the item occurs in the histogram.
  • The maximum possible occurrence of this item is the sum occurrence of the affected TopNs + the avg num per distinct value of each affected histogram
  • If the maximum possible occurrence is still smaller than the currently maintained smallest global TopN item, we don't need to insert this one into the heap and test it.

In this way, the speed is improved while the CPU is saved.
img_v2_3e92f289-37de-4fd4-9b33-2a35f25c9d5g
The CPU usage. Previous VS This pull.

Running tool: /DATA/disk4/yiding/go/bin/go test -benchmem -run=^$ -tags intest,deadlock -bench ^BenchmarkMergePartTopN2GlobalTopNWithHists$ github.com/pingcap/tidb/pkg/statistics/handle/globalstats

goos: linux
goarch: amd64
pkg: github.com/pingcap/tidb/pkg/statistics/handle/globalstats
cpu: Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz
BenchmarkMergePartTopN2GlobalTopNWithHists/Size100-72         	     159	   7249785 ns/op	   34714 B/op	     113 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size1000-72        	       9	 117386869 ns/op	  100136 B/op	    1013 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size2000-72        	       4	 272247218 ns/op	  173416 B/op	    2013 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size5000-72        	       2	 787035728 ns/op	  393256 B/op	    5013 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size10000-72       	       1	1919716271 ns/op	  759656 B/op	   10013 allocs/op
PASS
ok  	github.com/pingcap/tidb/pkg/statistics/handle/globalstats	17.839s

Running tool: /DATA/disk4/yiding/go/bin/go test -benchmem -run=^$ -tags intest,deadlock -bench ^BenchmarkMergePartTopN2GlobalTopNWithHists$ github.com/pingcap/tidb/pkg/statistics/handle/globalstats

goos: linux
goarch: amd64
pkg: github.com/pingcap/tidb/pkg/statistics/handle/globalstats
cpu: Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz
BenchmarkMergePartTopN2GlobalTopNWithHists/Size100-72         	     122	  10071203 ns/op	  177982 B/op	      30 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size1000-72        	       6	 186292872 ns/op	  176418 B/op	      28 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size2000-72        	       3	 398591599 ns/op	  178269 B/op	      31 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size5000-72        	       1	1436609512 ns/op	  179432 B/op	      33 allocs/op
BenchmarkMergePartTopN2GlobalTopNWithHists/Size10000-72       	       1	4363627038 ns/op	  177336 B/op	      31 allocs/op
PASS
ok  	github.com/pingcap/tidb/pkg/statistics/handle/globalstats	18.587s
n=100,            7249785/10071203=72%
n=1000         117386869/186292872=63%
n=2000         272247218/398591599=68%
n=5000        787035728/1436609512=55%
n=10000      1919716271/4363627038=44%

The mem usage is also in an acceptable range.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@winoros winoros added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 18, 2023
@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 18, 2023
@winoros winoros force-pushed the merge-topn-one-pass branch from dfddf2e to 54fc562 Compare October 18, 2023 20:18
@winoros winoros force-pushed the merge-topn-one-pass branch from 54fc562 to e0e2588 Compare October 18, 2023 21:34
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 18, 2023
@winoros winoros force-pushed the merge-topn-one-pass branch from 702a07f to 547f9c6 Compare October 19, 2023 19:02
@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 17, 2023
Copy link

ti-chi-bot bot commented Nov 17, 2023

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: Patch coverage is 82.51121% with 39 lines in your changes missing coverage. Please review.

Project coverage is 73.6702%. Comparing base (41c3b01) to head (4efdb94).
Report is 10 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #47765        +/-   ##
================================================
+ Coverage   73.0683%   73.6702%   +0.6018%     
================================================
  Files          1687       1719        +32     
  Lines        466567     476392      +9825     
================================================
+ Hits         340913     350959     +10046     
+ Misses       104711     103715       -996     
- Partials      20943      21718       +775     
Flag Coverage Δ
integration 45.6218% <70.8520%> (?)
unit 72.2819% <82.5112%> (+0.0216%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 44.7575% <ø> (-0.5773%) ⬇️

@winoros winoros added sig/planner SIG: Planner and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jan 26, 2024
@hawkingrei hawkingrei self-requested a review January 28, 2024 13:54
@winoros
Copy link
Member Author

winoros commented Jan 29, 2024

And some codes like the in-place updates for the heap are optimized for the memory.

@Rustin170506 Rustin170506 self-requested a review February 1, 2024 02:07
@winoros
Copy link
Member Author

winoros commented Feb 5, 2024

/retest

pkg/statistics/handle/globalstats/topn.go Show resolved Hide resolved
globalTopN.Sort()
return &globalTopN, remainedTopNs, hists, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Mark as deprecated

@hawkingrei
Copy link
Member

/retest

Copy link
Member

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

Just a quick run through the code, I'm still trying to understand the business logic of it. I'll look at it a couple more times soon to try and understand it.

pkg/statistics/cmsketch_util.go Outdated Show resolved Hide resolved
pkg/statistics/cmsketch_util.go Outdated Show resolved Hide resolved
pkg/statistics/handle/globalstats/global_stats_test.go Outdated Show resolved Hide resolved
pkg/statistics/handle/globalstats/topn.go Show resolved Hide resolved
pkg/statistics/handle/globalstats/topn.go Outdated Show resolved Hide resolved
pkg/statistics/handle/globalstats/topn.go Outdated Show resolved Hide resolved
pkg/statistics/handle/globalstats/topn.go Outdated Show resolved Hide resolved
Copy link
Member

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

Looks good to me. But please move checkTheCurAndMoveForward out as a wrapper and add more comments. Thank you for working on this. Approve in advance.

affectedHist = append(affectedHist, int(histPos))
}
// Hacking skip.
if uint32(len(finalTopNs)) >= n {
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure which one we prefer. len(finalTopNs) vs. finalTopNs.Len(). But I guess it doesn't matter.

pkg/statistics/handle/globalstats/topn.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Feb 22, 2024
Copy link

ti-chi-bot bot commented Feb 22, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-02-22 09:58:29.271545344 +0000 UTC m=+523998.019168506: ☑️ agreed by hi-rustin.

@hawkingrei
Copy link
Member

/test all

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 28, 2024
Copy link

ti-chi-bot bot commented Sep 28, 2024

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

ti-chi-bot bot commented Jan 24, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Rustin170506

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@winoros winoros removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants