Skip to content

planner: fix index range intersection for in-list and other predicates #58029

Merged
ti-chi-bot[bot] merged 1 commit intopingcap:masterfrom
ghazalfamilyusa:range_intersection_extension
Dec 10, 2024
Merged

planner: fix index range intersection for in-list and other predicates #58029
ti-chi-bot[bot] merged 1 commit intopingcap:masterfrom
ghazalfamilyusa:range_intersection_extension

Conversation

@ghazalfamilyusa
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #57694

Problem Summary:

This is a limitation for optimal multi-column index range derivation. It is similar to #54166 but different where point ranges are derived from IN list on the prefix column. A simple example is below where the range is based on "t1.a1 IN ( 44, 70, 76)" and not the full predicate "t1.a1 IN ( 44, 70, 76) AND (t1.a1 > 70 OR (t1.a1 = 70 AND t1.b1 > 41))"

CREATE TABLE t1 (
  a1 int,
  b1 int,
  c1 varbinary(767) DEFAULT NULL,
  KEY twoColIndex (a1,b1)
)

EXPLAIN  format = brief
SELECT 1
     FROM t1 FORCE INDEX (twoColIndex)
     WHERE
       t1.a1 IN ( 44, 70, 76)
       AND (t1.a1 > 70 OR (t1.a1 = 70 AND t1.b1 > 41))
--------------

id      estRows task    access object   operator info
Projection      10.07   root            1->Column#5
└─IndexReader   10.10   root            index:Selection
  └─Selection   10.10   cop[tikv]               or(gt(test.t1.a1, 70), and(eq(test.t1.a1, 70), gt(test.t1.b1, 41)))
    └─IndexRangeScan    30.00   cop[tikv]       table:t1, index:twoColIndex(a1, b1)     range:[44,44], [70,70], [76,76]

The optimal solution is below where the range intersection of all predicates is: (70 41,70 +inf], [76,76]

id      estRows task    access object   operator info
Projection      43.33   root            1->Column#5
└─IndexReader   54.17   root            index:IndexRangeScan
  └─IndexRangeScan      54.17   cop[tikv]       table:t1, index:twoColIndex(a1, b1)     range:(70 41,70 +inf], [76,76]

We already have the logic of intersection through prior 54166 PR and it is computed for this case as well. The problem is that we do not choose the intersection for this case.

What changed and how does it work?

The fix is to recognize this case by comparing the conjunctive normal form, or CNF result (applying all predicates) and IN list ranges and pick the most selective. The CNF result is more selective if the covers a superset of filters. In the above example, the CNF result access conditions is "t1.a1 IN ( 44, 45) AND (t1.a1 > 70 OR (t1.a1 = 70 AND t1.b1 > 41))" which is a superset of the IN list access conditions "

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

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 5, 2024
@tiprow
Copy link
Copy Markdown

tiprow Bot commented Dec 5, 2024

Hi @ghazalfamilyusa. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

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.

@fixdb fixdb requested review from qw4990 and winoros December 5, 2024 21:43
@fixdb fixdb changed the title Optimizer: Fix index range intersection for inlist and other predicates planner: fix index range intersection for in list and other predicates Dec 5, 2024
@ghazalfamilyusa ghazalfamilyusa changed the title planner: fix index range intersection for in list and other predicates planner: fix index range intersection for in-list and other predicates Dec 5, 2024
@ghazalfamilyusa ghazalfamilyusa force-pushed the range_intersection_extension branch from 1798d42 to 8661177 Compare December 6, 2024 00:01
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.9687%. Comparing base (9812d85) to head (61274f3).
Report is 37 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #58029        +/-   ##
================================================
+ Coverage   73.1839%   73.9687%   +0.7847%     
================================================
  Files          1671       1675         +4     
  Lines        460731     471156     +10425     
================================================
+ Hits         337181     348508     +11327     
+ Misses       102827     101849       -978     
- Partials      20723      20799        +76     
Flag Coverage Δ
integration 43.8313% <23.8095%> (?)
unit 72.8339% <100.0000%> (+0.5287%) ⬆️

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

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 46.2693% <ø> (+0.2649%) ⬆️

@ghazalfamilyusa ghazalfamilyusa changed the title planner: fix index range intersection for in-list and other predicates Optimizer: fix index range intersection for in-list and other predicates Dec 6, 2024
@ghazalfamilyusa ghazalfamilyusa changed the title Optimizer: fix index range intersection for in-list and other predicates planner: fix index range intersection for in-list and other predicates Dec 6, 2024
Comment thread pkg/util/ranger/detacher.go Outdated
Comment thread pkg/util/ranger/detacher.go Outdated
@ghazalfamilyusa ghazalfamilyusa force-pushed the range_intersection_extension branch from 8661177 to 61274f3 Compare December 6, 2024 18:10
@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 10, 2024
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Dec 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: qw4990, winoros

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

The pull request process is described here

Details 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

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 10, 2024
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Dec 10, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-12-10 06:20:59.173926521 +0000 UTC m=+333049.262729061: ☑️ agreed by qw4990.
  • 2024-12-10 13:33:29.589299483 +0000 UTC m=+358999.678102025: ☑️ agreed by winoros.

@tiprow
Copy link
Copy Markdown

tiprow Bot commented Dec 10, 2024

@ghazalfamilyusa: 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 61274f3 link true /test fast_test_tiprow

Full PR test history. Your PR dashboard.

Details

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.

@ti-chi-bot ti-chi-bot Bot merged commit 2a1f646 into pingcap:master Dec 10, 2024
ClamChowderTiDB pushed a commit to ClamChowderTiDB/tidb that referenced this pull request Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

further improvement of pagination query

3 participants