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

util: skip always false DNF #51901

Merged
merged 6 commits into from
Mar 21, 2024
Merged

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Mar 19, 2024

What problem does this PR solve?

Issue Number: close #40997

Problem Summary:
For example:

explain
select
  *
from
  t71706696 force index(dt_2)
where
  (
    (
      dt = '20210112'
      and db_id = '62812'
      and tbl_id > '228892694'
    )
    or (
      dt = '20210112'
      and db_id = '62813'
      and tbl_id <= '226785696'
    )
    or (
      dt = '20210112'
      and db_id > '62812'
      and db_id < '62813'
    )
  )

In the where clause we have CNF contains some DNF and the last DNF is always false. But it will cause the optimizer to fall back to a full table scan.

The final plan looks like:

+-------------------------------+----------+-----------+------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                            | estRows  | task      | access object                                  | operator info                                                                                                                                                                                |
+-------------------------------+----------+-----------+------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IndexLookUp_8                 | 0.67     | root      |                                                |                                                                                                                                                                                |
| ├─Selection_7(Build)          | 0.67     | cop[tikv] |                                                | or(and(eq(test.t71706696.dt, "20210112"), and(eq(test.t71706696.db_id, 62812), gt(test.t71706696.tbl_id, 228892694))), or(and(eq(test.t71706696.dt, "20210112"), and(eq(test.t71706696.db_id, 62813), le(test.t71706696.tbl_id, 226785696))), and(eq(test.t71706696.dt, "20210112"), and(gt(test.t71706696.db_id, 62812), lt(test.t71706696.db_id, 62813))))) |
| │ └─IndexFullScan_5           | 10000.00 | cop[tikv] | table:t71706696, index:dt_2(dt, db_id, tbl_id) | keep order:false, stats:pseudo                                                                                                                                                                                |
| └─TableRowIDScan_6(Probe)     | 0.67     | cop[tikv] | table:t71706696                                | keep order:false, stats:pseudo                                                                                                                                                                                |
+-------------------------------+----------+-----------+------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Actually, we can ignore this always false filter and do an index scan.

What changed and how does it work?

From the code details, we should ignore the empty range here:

ranges := res.Ranges

In the detachCNFCondAndBuildRangeForIndex we will return directly from here if the range is empty:

if emptyRange {

But we didn't consider it in the detachDNFCondAndBuildRangeForIndex.

So I skip it here:
https://github.com/hi-rustin/tidb/blob/3f2468115cf4089d1bf47da3da4d06ca2dd31850/pkg/util/ranger/detacher.go#L740

			// If CNF item always false, we can return ignore this CNF item.
			if len(ranges) == 0 {
				continue
			}

I also updated the TestIndexMergeIssue49605 test case. Because in this case we have an empty range: BETWEEN _UTF8MB4'WVz' AND _UTF8MB4'G#駧褉ZC領*lov'. So that it doesn't use index merge anymore.

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.

增强 TiDB 对总是 false 的 DNF 的处理,直接忽略这种 filter
Enhance TiDB's handling of DNFs that are always false by simply ignoring such filters

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 19, 2024
Copy link

codecov bot commented Mar 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.2386%. Comparing base (9b6db9d) to head (1a1e545).
Report is 1671 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #51901         +/-   ##
=================================================
- Coverage   70.7435%   55.2386%   -15.5050%     
=================================================
  Files          1477       1592        +115     
  Lines        438550     616970     +178420     
=================================================
+ Hits         310246     340806      +30560     
- Misses       108884     252967     +144083     
- Partials      19420      23197       +3777     
Flag Coverage Δ
integration 36.9547% <100.0000%> (?)
unit 70.9688% <100.0000%> (+0.4257%) ⬆️

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

Components Coverage Δ
dumpling 53.9957% <ø> (ø)
parser ∅ <ø> (∅)
br 50.8987% <ø> (+5.0702%) ⬆️

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 20, 2024
@hawkingrei hawkingrei self-requested a review March 20, 2024 03:05
@hawkingrei

This comment was marked as outdated.

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 20, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 20, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Copy link
Member Author

@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.

🔢 Self-check (PR reviewed by myself and ready for feedback.)

require.Regexp(t, ".*IndexFullScan.*", res.Rows()[2][0])
require.Len(t, res.Rows(), 2)
require.Regexp(t, ".*IndexReader.*", res.Rows()[0][0])
require.Regexp(t, ".*IndexRangeScan.*", res.Rows()[1][0])

res = tk.MustQuery("explain format = 'brief' select col1 from UK_GCOL_VIRTUAL_18588 use index(UK_COL1) " +
"where col1 between -1696020282760139948 and -2619168038882941276 or col1 < -4004648990067362699;")
Copy link
Member Author

Choose a reason for hiding this comment

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

-1696020282760139948 > -2619168038882941276. So this is an empty range.

INSERT INTO `t` VALUES (2065948,1999,_binary '8jxN','rf',-54,-5656,'1987-07-03',259254,'7me坨'),(-8248164,2024,_binary 'zA5A','s)DAkX3',-93,-12983,'2027-12-18',299573,'LUf咲'),(-6131509,2023,_binary 'xdex#Y2','1th%h',-51,19149,'2013-10-28',428279,'矷莒X'),(7545837,1998,_binary 'PCVO','&(lJw6',30,4093,'1987-07-03',736235,'腏@TOIJ'),(-7449472,2029,_binary 'B7&jrl','EjbFfX!',80,-7590,'2011-11-03',765580,'堮ZQF_'),(-7176200,1988,_binary 'tiPglv7mX_#','CnCtNb',-25,NULL,'1987-07-03',842956,'Gq羣嗳殓'),(-115168,2036,_binary 'BqmX$-4It','!8#dvH',82,18787,'1991-09-20',921706,'椉2庘v'),(6665100,1987,_binary '4IJgk0fr4','(D',-73,28628,'1987-07-03',1149668,'摔玝S渉'),(-4065661,2021,_binary '8G%','xDO39xw#',-107,17356,'1970-12-20',1316239,'+0c35掬-阗'),(7622462,1990,_binary '&o+)s)D0','kjoS9Dzld',84,688,'1987-07-03',1403663,'$H鍿_M~'),(5269354,2018,_binary 'wq9hC8','s8XPrN+',-2,-31272,'2008-05-26',1534517,'y椁n躁Q'),(2065948,1982,_binary '8jxNjbksV','g$+i4dg',11,19800,'1987-07-03',1591457,'z^+H~薼A'),(4076971,2024,_binary '&!RrsH','7Mpvk',-63,-632,'2032-10-28',1611011,'鬰+EXmx'),(3522062,1981,_binary ')nq#!UiHKk8','j~wFe77ai',50,6951,'1987-07-03',1716854,'J'),(7859777,2012,_binary 'PBA5xgJ&G&','UM7o!u',18,-5978,'1987-07-03',1967012,'e)浢L獹'),(2065948,2028,_binary '8jxNjbk','JmsEki9t4',51,12002,'2017-12-23',1981288,'mp氏襚');
explain format='brief' SELECT /*+ AGG_TO_COP() STREAM_AGG()*/ (NOT (`t`.`i`>=_UTF8MB4'j筧8') OR NOT (`t`.`i`=_UTF8MB4'暈lH忧ll6')) IS TRUE,MAX(`t`.`e`) AS `r0`,QUOTE(`t`.`i`) AS `r1` FROM `t` WHERE `t`.`h`>240817 OR `t`.`i` BETWEEN _UTF8MB4'WVz' AND _UTF8MB4'G#駧褉ZC領*lov' GROUP BY `t`.`i`;
select count(*) from (SELECT /*+ AGG_TO_COP() STREAM_AGG()*/ (NOT (`t`.`i`>=_UTF8MB4'j筧8') OR NOT (`t`.`i`=_UTF8MB4'暈lH忧ll6')) IS TRUE,MAX(`t`.`e`) AS `r0`,QUOTE(`t`.`i`) AS `r1` FROM `t` WHERE `t`.`h`>240817 OR `t`.`i` BETWEEN _UTF8MB4'WVz' AND _UTF8MB4'G#駧褉ZC領*lov' GROUP BY `t`.`i`) derived;
explain format='brief' SELECT /*+ AGG_TO_COP() */ (NOT (`t`.`i`>=_UTF8MB4'j筧8') OR NOT (`t`.`i`=_UTF8MB4'暈lH忧ll6')) IS TRUE,MAX(`t`.`e`) AS `r0`,QUOTE(`t`.`i`) AS `r1` FROM `t` WHERE `t`.`h`>240817 OR `t`.`i` BETWEEN _UTF8MB4'WVz' AND _UTF8MB4'G#駧褉ZC領*lov' GROUP BY `t`.`i`;
Copy link
Member Author

Choose a reason for hiding this comment

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

mysql> select 1 where _UTF8MB4'WVz' > _UTF8MB4'G#駧褉ZC領*lov';
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

mysql> select 1 where _UTF8MB4'WVz' < _UTF8MB4'G#駧褉ZC領*lov';
Empty set (0.00 sec)

@Rustin170506 Rustin170506 changed the title util: skip always false CNF util: skip always false DNF Mar 21, 2024
@Rustin170506
Copy link
Member Author

/unhold

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 21, 2024
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Mar 21, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Copy link

ti-chi-bot bot commented Mar 21, 2024

@hawkingrei: Your lgtm message is repeated, so it is ignored.

In response to this:

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.

@Rustin170506 Rustin170506 added the sig/planner SIG: Planner label Mar 21, 2024
Copy link

ti-chi-bot bot commented Mar 21, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, hawkingrei

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:
  • OWNERS [AilinKid,hawkingrei]

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 Mar 21, 2024
Copy link

ti-chi-bot bot commented Mar 21, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-03-20 03:05:26.020780706 +0000 UTC m=+1432353.043027096: ☑️ agreed by hawkingrei.
  • 2024-03-21 09:41:18.392882341 +0000 UTC m=+1542505.415128732: ☑️ agreed by AilinKid.

@ti-chi-bot ti-chi-bot bot merged commit a04fed9 into pingcap:master Mar 21, 2024
23 checks passed
@Rustin170506 Rustin170506 deleted the rustin-patch-CNF branch March 21, 2024 10:42
@ti-chi-bot ti-chi-bot added the needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. label May 20, 2024
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #53413.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 20, 2024
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
ti-chi-bot bot pushed a commit that referenced this pull request May 21, 2024
@Rustin170506 Rustin170506 added the needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. label Oct 30, 2024
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #56993.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Oct 30, 2024
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
ti-chi-bot bot pushed a commit that referenced this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unexpected full range scan
4 participants