planner: use addedRows for out of range and add skew risk ratio | tidb-test=pr/2646
This is an automated cherry-pick of #62363
What problem does this PR solve?
Issue Number: ref #62093 close #55199
Problem Summary:
What changed and how does it work?
Range predicates outside of the histogram bucket range face a similar risk to ranges within a bucket, because we do not have collected stats we can rely on for the cardinality estimation. So, this change uses the tidb_opt_risk_range_skew_ratio variable to allow users or external processes to adjust how much skew is considered in out of range estimations. This also adjusts and simplifies the out of range estimation function to rely more on addedRows (the difference between realTimeRowCount and the histogram count) than histogram row count itself.
Previously, when estimating a predicate outside of our histogram bucket range, we would calculate a percentage, TotalPercent, and multiply it with histogram row count. In the case that addedRows was significant, we would multiply TotalPercent and addedRows and take the larger of the two estimations. Finally, we would ensure our estimate didn't go over ModifyCount. Part of the concern with using histogram row count is that one-sided ranges, such as a > 0, could easily have a TotalPercent = 50%, leading to large out of range estimations. It seems more reasonable for out of range cardinality to be correlated with addedRows rather than histogram row count, no matter the scenario. So, to simplify the logic, the estimation is changed to be TotalPercent * addedRows * 0.5 by default (the 0.5 accounts for the assumption that only half of newly added rows are added outside our histogram range).
To account for underestimation due to skew, since it is possible that all of our newly added rows are within our queried range, we can use the tidb_opt_risk_range_skew_ratio variable. Based on the value of this variable, we account for more or less skew in our estimate. For a default value of 0, there is no change to the original estimate, and for a value of 1, our out of range estimation = worst case skew = addedRows.
Check List
Tests
- [x] 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
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by: fixdb Once this PR has been reviewed and has the lgtm label, please assign yudongusa for approval. For more information see the Code Review Process. Please ensure that each of them provides their approval before proceeding.
The full list of commands accepted by this bot can be found here.
- ~~OWNERS~~ [fixdb]
- ~~pkg/planner/OWNERS~~ [fixdb]
- pkg/sessionctx/variable/OWNERS
- ~~pkg/statistics/OWNERS~~ [fixdb]
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
[LGTM Timeline notifier]
Timeline:
2025-12-09 02:38:16.727574868 +0000 UTC m=+922241.541352430: :ballot_box_with_check: agreed by fixdb.
Codecov Report
:x: Patch coverage is 68.96552% with 9 lines in your changes missing coverage. Please review.
:warning: Please upload report for BASE (release-8.5@07dd0cb). Learn more about missing BASE report.
Additional details and impacted files
@@ Coverage Diff @@
## release-8.5 #64784 +/- ##
================================================
Coverage ? 49.7175%
================================================
Files ? 1576
Lines ? 446756
Branches ? 0
================================================
Hits ? 222116
Misses ? 204641
Partials ? 19999
| Flag | Coverage Δ | |
|---|---|---|
| integration | 49.7175% <68.9655%> (?) |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Components | Coverage Δ | |
|---|---|---|
| dumpling | ∅ <0.0000%> (?) |
|
| parser | ∅ <0.0000%> (?) |
|
| br | 34.3626% <0.0000%> (?) |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
/retest
/unhold
/retest
@ti-chi-bot: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:
| Test name | Commit | Details | Required | Rerun command |
|---|---|---|---|---|
| idc-jenkins-ci-tidb/unit-test | aa60835d0839d27840a6be216b765f0e03f3678b | link | true | /test unit-test |
| idc-jenkins-ci-tidb/mysql-test | aa60835d0839d27840a6be216b765f0e03f3678b | link | true | /test mysql-test |
Full PR test history. Your PR dashboard.
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. I understand the commands that are listed here.
This cherry pick PR is for a release branch and has not yet been approved by triage owners.
Adding the do-not-merge/cherry-pick-not-approved label.
To merge this cherry pick:
- It must be approved by the approvers firstly.
- AFTER it has been approved by approvers, please wait for the cherry-pick merging approval from triage owners.
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.
@ti-chi-bot: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:
| Test name | Commit | Details | Required | Rerun command |
|---|---|---|---|---|
| fast_test_tiprow | aa60835d0839d27840a6be216b765f0e03f3678b | link | true | /test fast_test_tiprow_for_release |
Full PR test history. Your PR dashboard.
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. I understand the commands that are listed here.