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: fix init topn not sorted #34234

Merged
merged 4 commits into from
Apr 26, 2022

Conversation

time-and-fate
Copy link
Member

@time-and-fate time-and-fate commented Apr 26, 2022

What problem does this PR solve?

Issue Number: close #34216 ref #32758

Problem Summary: Please see the issue.

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (please see the comments below)
  • No code

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

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Apr 26, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • qw4990
  • winoros

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 26, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Apr 26, 2022

@time-and-fate
Copy link
Member Author

time-and-fate commented Apr 26, 2022

The behavior of InitStats() is different in unit tests and in real TiDB. In unit tests, the batch size for SQL execution is 1, which makes the bug not reproducible in unit tests. And the internal structure of stats is not exposed to the outside. This bug needs tidb to be restarted or join a new cluster to trigger. So it's also not convenient to add integration tests. So I did 2 manual tests:

  1. I used the debugger to verify that the TopN is sorted after the PR.
  2. I used an internal tool to test the p-error[1] distribution of cardinality estimation of JOB workload + IMDB dataset (that's also how we found this bug). The test result shows that cardinality estimation is improved:

[1]: We use the p-error definition from "Preventing bad plans by bounding the impact of cardinality estimation errors."

LabelCountP50P90P95P99Max
Overallbefore2686126.0001068187.0002625728.00039921236.0001538186904.000
after26594.0001104.6854765.634290965.0007313673.000
Exact estimationbefore226-----
after216-----
Overestimationbefore72129.4306083.11162117.131733610.8891275947.000
after149117.0993614.05528894.973962682.2737313673.000
Underestimationbefore17392736.0001934074.0006158732.00047732163.0001538186904.000
after9521.59156.523237.0002678.56844896.225

Note that there are some bad overestimations cases after this PR. They are caused by:

  1. The bug makes some estimations become 0 wrongly.
  2. Actual row counts of some join results happen to be 0.
  3. Then the estimations for those cases become worse after this change.

But I think it's OK to accept that and fix this bug, because:

  1. It's just wrong, it just coincides with the correct row count.
  2. It's not reliable, it only happens when a tidb is restarted or a new tidb joins the cluster. After an ANALYZE, the estimation becomes as expected again.
  3. For most cases, the estimation becomes better.

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 26, 2022
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Apr 26, 2022
@qw4990
Copy link
Contributor

qw4990 commented Apr 26, 2022

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 6f70fc8

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 26, 2022
@ti-chi-bot ti-chi-bot merged commit e158017 into pingcap:master Apr 26, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Apr 26, 2022

TiDB MergeCI notify

✅ Well Done! New fixed [1] after this pr merged.

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/common-test 🔴 failed 1, success 11, total 12 6 min 8 sec Existing failure
idc-jenkins-ci-tidb/integration-ddl-test ✅ all 6 tests passed 7 min 5 sec Fixed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 10 min Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 29 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 4 min 58 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 4 min 48 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 4 min 18 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 31 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

@winoros winoros added needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-6.0 labels Apr 28, 2022
@winoros
Copy link
Member

winoros commented Apr 28, 2022

/run-cherry-picker release-5.4
/run-cherry-picker release-6.0

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 28, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-6.0 in PR #34316

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 28, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-5.4 in PR #34317

ti-chi-bot pushed a commit that referenced this pull request Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

initStatsTopN4Chunk() doesn't sort TopN for statistics sometimes
6 participants