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

planner: support expand IN expr #35699

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

Conversation

Yriuns
Copy link

@Yriuns Yriuns commented Jun 23, 2022

What problem does this PR solve?

Issue Number: close #34882

Problem Summary:

Provide a hint that allows optimizer to expand the in expression.

A selection like a = 0 AND b = 1 AND c IN (2, 3) will be convert to something like this:

a = 0 AND b = 1 AND c = 2
UNION ALL
a = 0 AND b = 1 AND c = 3

So that the we can utilize the index better, e.g., TopN push down can be optimized as LimitN.

What is changed and how it works?

For WHERE clause with IN expressions, we divide the expressions list into non-IN expressions list and IN expressions list. For the lists of IN expressions, we do a Cartesian product. Each element of the Cartesian product result set is merged with the lists of non-IN expressions to obtain a new WHERE clause. Finally, we use a UNION ALL to merge all these new Selection operators.

An example:
Untitled-2022-06-08-1524

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
mysql> CREATE TABLE `t` (
    ->   `id` bigint(22) NOT NULL AUTO_INCREMENT,
    ->   `a` bigint(20) NOT NULL DEFAULT '0',
    ->   `b` bigint(20) NOT NULL DEFAULT '0',
    ->   `c` bigint(20) NOT NULL DEFAULT '0',
    ->   `d` bigint(20) NOT NULL DEFAULT '0',
    ->   UNIQUE KEY `uk_id` (`id`),
    ->   KEY `a_b_c_id` (`a`,`b`,`c`,`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=1;

-- without hint
mysql> explain SELECT id, a, b, c, d FROM t WHERE a = 0 AND b = 1 AND c IN (2, 3) ORDER BY id ASC LIMIT 10;
+----------------------------------+---------+-----------+--------------------------------------+--------------------------------------------------------------------+
| id                               | estRows | task      | access object                        | operator info                                                      |
+----------------------------------+---------+-----------+--------------------------------------+--------------------------------------------------------------------+
| TopN_9                           | 0.00    | root      |                                      | test.t.id, offset:0, count:10                                      |
| └─IndexLookUp_20                 | 0.00    | root      |                                      |                                                                    |
|   ├─TopN_19(Build)               | 0.00    | cop[tikv] |                                      | test.t.id, offset:0, count:10                                      |
|   │ └─IndexRangeScan_17          | 0.00    | cop[tikv] | table:t, index:a_b_c_id(a, b, c, id) | range:[0 1 2,0 1 2], [0 1 3,0 1 3], keep order:false, stats:pseudo |
|   └─TableRowIDScan_18(Probe)     | 0.00    | cop[tikv] | table:t                              | keep order:false, stats:pseudo                                     |
+----------------------------------+---------+-----------+--------------------------------------+--------------------------------------------------------------------+
5 rows in set (0.00 sec)

-- with hint
mysql> explain SELECT /*+ in_expansion() */ id, a, b, c, d FROM t WHERE a = 0 AND b = 1 AND c IN (2, 3) ORDER BY id ASC LIMIT 10;
+------------------------------------------+---------+-----------+--------------------------------------+----------------------------------------------------+
| id                                       | estRows | task      | access object                        | operator info                                      |
+------------------------------------------+---------+-----------+--------------------------------------+----------------------------------------------------+
| TopN_17                                  | 0.00    | root      |                                      | test.t.id, offset:0, count:10                      |
| └─Union_22                               | 0.00    | root      |                                      |                                                    |
|   ├─Limit_27                             | 0.00    | root      |                                      | offset:0, count:10                                 |
|   │ └─Projection_40                      | 0.00    | root      |                                      | test.t.id, test.t.a, test.t.b, test.t.c, test.t.d  |
|   │   └─IndexLookUp_39                   | 0.00    | root      |                                      |                                                    |
|   │     ├─Limit_38(Build)                | 0.00    | cop[tikv] |                                      | offset:0, count:10                                 |
|   │     │ └─IndexRangeScan_36            | 0.00    | cop[tikv] | table:t, index:a_b_c_id(a, b, c, id) | range:[0 1 2,0 1 2], keep order:true, stats:pseudo |
|   │     └─TableRowIDScan_37(Probe)       | 0.00    | cop[tikv] | table:t                              | keep order:false, stats:pseudo                     |
|   └─Limit_45                             | 0.00    | root      |                                      | offset:0, count:10                                 |
|     └─Projection_58                      | 0.00    | root      |                                      | test.t.id, test.t.a, test.t.b, test.t.c, test.t.d  |
|       └─IndexLookUp_57                   | 0.00    | root      |                                      |                                                    |
|         ├─Limit_56(Build)                | 0.00    | cop[tikv] |                                      | offset:0, count:10                                 |
|         │ └─IndexRangeScan_54            | 0.00    | cop[tikv] | table:t, index:a_b_c_id(a, b, c, id) | range:[0 1 3,0 1 3], keep order:true, stats:pseudo |
|         └─TableRowIDScan_55(Probe)       | 0.00    | cop[tikv] | table:t                              | keep order:false, stats:pseudo                     |
+------------------------------------------+---------+-----------+--------------------------------------+----------------------------------------------------+
14 rows in set (0.00 sec)
  • No code

As we can see, after we use IN_EXPANSION() hint, the TopN_19 to tikv converts to 2 Limit to tikv. with-hint plan only needs to scan at most 10 + 10 records, but without-hint plan needs to scan and sort all the records of ranges [0 1 2,0 1 2], [0 1 3,0 1 3]. If there are a lot of records of these ranges, we can save a lot of uncessary cost.

I also conduct a micro benchmark(1 concurrency of client) in my own server. Here is the result:

CREATE TABLE `t` (
  `id` bigint(22) NOT NULL AUTO_INCREMENT,
  `a` bigint(22) NOT NULL,
  `b` bigint(22) NOT NULL,
  `c` bigint(22) NOT NULL,
  `d` bigint(22) NOT NULL,
  KEY `a_b_c_d_id` (`a`,`b`,`c`,`d`, `id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=

SELECT *
FROM t
WHERE a = 14 AND b IN (3, 4, 7, 8, 11, 12, 13) AND c = 1 AND d IN (0, 20)
ORDER BY id asc
LIMIT 0, 20

Without hint

number of records that satisfy filter condition QPS avg latency TP99 TiDB CPU
1000 277.01 3.6 7.17 150%
10000 65.23 15.33 26.58 30%
100000 6.28 159.32 170.48 <10%

With hint

number of records that satisfy filter condition QPS avg latency TP99 TiDB CPU
1000 140.45 7.12 11.87 740%
10000 136.05 7.35 12.30 740%
100000 134.05 7.46 12.30 750%

The latency is quite stable, and the more records, the more time we save.

Side effects

  • Performance regression: Consumes more CPU of tidb, but save CPU of tikv
  • 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

@Yriuns Yriuns requested a review from a team as a code owner June 23, 2022 14:01
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

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 release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 23, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Jun 23, 2022

CLA assistant check
All committers have signed the CLA.

@ti-chi-bot
Copy link
Member

Welcome @Yriuns!

It looks like this is your first PR to pingcap/tidb 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to pingcap/tidb. 😃

@Yriuns
Copy link
Author

Yriuns commented Jun 23, 2022

/cc @winoros

@ti-chi-bot ti-chi-bot requested a review from winoros June 23, 2022 14:12
@Yriuns Yriuns force-pushed the in-expansion branch 3 times, most recently from 835b5ff to a228b6c Compare June 23, 2022 16:13
Yriuns and others added 2 commits June 24, 2022 20:46
Co-authored-by: Chengpeng Yan <41809508+Reminiscent@users.noreply.github.com>
@Yriuns
Copy link
Author

Yriuns commented Jun 24, 2022

/cc @time-and-fate

@Yriuns Yriuns requested a review from Reminiscent June 24, 2022 15:40
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 8, 2022
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 10, 2022
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 14, 2022
@ti-chi-bot
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit operator can not be pushed down to tikv when using IN clause
4 participants