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

Optimal ranges cannot be built in some case #41572

Closed
xuyifangreeneyes opened this issue Feb 20, 2023 · 3 comments · Fixed by #41661
Closed

Optimal ranges cannot be built in some case #41572

xuyifangreeneyes opened this issue Feb 20, 2023 · 3 comments · Fixed by #41661
Assignees
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 affects-7.1 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@xuyifangreeneyes
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table t(id int, a varchar(100), b int, c int, index idx_ab(a, b));
explain select id, c from t use index (idx_ab) where ((a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0)) and c != 0;

2. What did you expect to see? (Required)

mysql> explain select id, c from t use index (idx_ab) where ((a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0)) and c != 0;
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| id                              | estRows | task      | access object               | operator info                                                                     |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| Projection_4                    | 0.20    | root      |                             | test.t.id, test.t.c                                                               |
| └─IndexLookUp_8                 | 0.20    | root      |                             |                                                                                   |
|   ├─IndexRangeScan_5(Build)     | 0.30    | cop[tikv] | table:t, index:idx_ab(a, b) | range:["t" 1,"t" 2], ["w" 0,"w" 0], keep order:false, stats:pseudo |
|   └─Selection_7(Probe)          | 0.20    | cop[tikv] |                             | ne(test.t.c, 0)                                                                   |
|     └─TableRowIDScan_6          | 0.30    | cop[tikv] | table:t                     | keep order:false, stats:pseudo                                                    |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

3. What did you see instead (Required)

mysql> explain select id, c from t use index (idx_ab) where ((a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0)) and c != 0;
+------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| id                           | estRows | task      | access object               | operator info                                                                                                                     |
+------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Projection_4                 | 0.20    | root      |                             | test.t.id, test.t.c                                                                                                               |
| └─IndexLookUp_9              | 0.20    | root      |                             |                                                                                                                                   |
|   ├─Selection_7(Build)       | 0.20    | cop[tikv] |                             | or(and(eq(test.t.a, "t"), eq(test.t.b, 1)), or(and(eq(test.t.a, "t"), eq(test.t.b, 2)), and(eq(test.t.a, "w"), eq(test.t.b, 0)))) |
|   │ └─IndexRangeScan_5       | 20.00   | cop[tikv] | table:t, index:idx_ab(a, b) | range:["t","t"], ["w","w"], keep order:false, stats:pseudo                                                                        |
|   └─Selection_8(Probe)       | 0.20    | cop[tikv] |                             | ne(test.t.c, 0)                                                                                                                   |
|     └─TableRowIDScan_6       | 0.20    | cop[tikv] | table:t                     | keep order:false, stats:pseudo                                                                                                    |
+------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)

4. What is your TiDB version? (Required)

master

@xuyifangreeneyes xuyifangreeneyes added type/bug The issue is confirmed as a bug. sig/planner SIG: Planner labels Feb 20, 2023
@xuyifangreeneyes xuyifangreeneyes changed the title Accurate ranges cannot be built in some case Optimal ranges cannot be built in some case Feb 20, 2023
@xuyifangreeneyes
Copy link
Contributor Author

It looks like a corner case since the following cases work:

mysql> explain select id, c from t use index (idx_ab) where ((a = 't' and b = 1) or (a = 't' and b = 3) or (a = 'w' and b = 0)) and c != 0;
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| id                              | estRows | task      | access object               | operator info                                                                     |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| Projection_4                    | 0.20    | root      |                             | test.t.id, test.t.c                                                               |
| └─IndexLookUp_8                 | 0.20    | root      |                             |                                                                                   |
|   ├─IndexRangeScan_5(Build)     | 0.30    | cop[tikv] | table:t, index:idx_ab(a, b) | range:["t" 1,"t" 1], ["t" 3,"t" 3], ["w" 0,"w" 0], keep order:false, stats:pseudo |
|   └─Selection_7(Probe)          | 0.20    | cop[tikv] |                             | ne(test.t.c, 0)                                                                   |
|     └─TableRowIDScan_6          | 0.30    | cop[tikv] | table:t                     | keep order:false, stats:pseudo                                                    |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

mysql> explain select id, c from t use index (idx_ab) where (a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0);
+---------------------------------+---------+-----------+-----------------------------+--------------------------------------------------------------------+
| id                              | estRows | task      | access object               | operator info                                                      |
+---------------------------------+---------+-----------+-----------------------------+--------------------------------------------------------------------+
| Projection_4                    | 2.60    | root      |                             | test.t.id, test.t.c                                                |
| └─IndexLookUp_7                 | 3.25    | root      |                             |                                                                    |
|   ├─IndexRangeScan_5(Build)     | 3.25    | cop[tikv] | table:t, index:idx_ab(a, b) | range:["t" 1,"t" 2], ["w" 0,"w" 0], keep order:false, stats:pseudo |
|   └─TableRowIDScan_6(Probe)     | 3.25    | cop[tikv] | table:t                     | keep order:false, stats:pseudo                                     |
+---------------------------------+---------+-----------+-----------------------------+--------------------------------------------------------------------+
4 rows in set (0.01 sec)

mysql> explain select id, c from t use index (idx_ab) where ((a = 't' and b = 1) or (a = 's' and b = 2) or (a = 'w' and b = 0)) and c != 0;
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| id                              | estRows | task      | access object               | operator info                                                                     |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
| Projection_4                    | 0.20    | root      |                             | test.t.id, test.t.c                                                               |
| └─IndexLookUp_8                 | 0.20    | root      |                             |                                                                                   |
|   ├─IndexRangeScan_5(Build)     | 0.30    | cop[tikv] | table:t, index:idx_ab(a, b) | range:["s" 2,"s" 2], ["t" 1,"t" 1], ["w" 0,"w" 0], keep order:false, stats:pseudo |
|   └─Selection_7(Probe)          | 0.20    | cop[tikv] |                             | ne(test.t.c, 0)                                                                   |
|     └─TableRowIDScan_6          | 0.30    | cop[tikv] | table:t                     | keep order:false, stats:pseudo                                                    |
+---------------------------------+---------+-----------+-----------------------------+-----------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

@xuyifangreeneyes xuyifangreeneyes self-assigned this Feb 20, 2023
@xuyifangreeneyes xuyifangreeneyes added type/enhancement The issue or PR belongs to an enhancement. and removed type/bug The issue is confirmed as a bug. labels Feb 20, 2023
@xuyifangreeneyes
Copy link
Contributor Author

xuyifangreeneyes commented Feb 20, 2023

When building ranges, here is the code path: DetachCondAndBuildRangeForIndex -> detachCondAndBuildRangeForCols -> detachCNFCondAndBuildRangeForIndex -> extractIndexPointRangesForCNF. In extractIndexPointRangesForCNF, we call DetachCondAndBuildRangeForIndex for (a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0) and get ["t" 1,"t" 2], ["w" 0,"w" 0]. Since the ranges are not all points, pointRes returned by extractIndexPointRangesForCNF is nil, which is the root cause for the issue.

Screen Shot 2023-02-20 at 4 02 33 PM

@xuyifangreeneyes
Copy link
Contributor Author

When we call DetachCondAndBuildRangeForIndex for (a = 't' and b = 1) or (a = 't' and b = 2) or (a = 'w' and b = 0), it would merge consecutive ranges, i.e., convert ["t" 1,"t" 1], ["t" 2,"t" 2], ["w" 0,"w" 0] to ["t" 1,"t" 2], ["w" 0,"w" 0]. We should not merge consecutive ranges here because we hope to generate point ranges here. Not merging consecutive ranges can fix the issue.

@xuyifangreeneyes xuyifangreeneyes added type/bug The issue is confirmed as a bug. and removed type/enhancement The issue or PR belongs to an enhancement. labels Feb 20, 2023
@ti-chi-bot ti-chi-bot added may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.0 may-affects-6.1 may-affects-6.2 may-affects-6.3 may-affects-6.4 may-affects-6.5 may-affects-6.6 labels Feb 22, 2023
@xuyifangreeneyes xuyifangreeneyes removed may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. labels Feb 22, 2023
@chrysan chrysan removed affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-6.0 affects-6.2 affects-6.3 affects-6.4 affects-6.6 affects-7.0 labels Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 affects-7.1 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants