Skip to content

Remove redundant branches in the OR list in filters #58998

@time-and-fate

Description

@time-and-fate

Enhancement

Though users don't deliberately write redundant expressions in the SQL, there are still redundant expressions, such as those from generated SQLs.

In our current implementation, we have RemoveDupExprs() and PropagateConstant() for AND lists. However, the correspondence for the OR list is lacking. So, there could be redundant filters in the OR list in the final plan, which can sometimes cause an inefficient plan.

For example:

create table t(a int, b int, c int, index ia(a), index ib(b), index ic(c));
explain select * from t where a = 1 or a = 1 or b = 2 or b = 2 or c = 3 or c = 3;
> explain select * from t where a = 1 or a = 1 or b = 2 or b = 2 or c = 3 or c = 3;
+--------------------------------+---------+-----------+----------------------+---------------------------------------------+
| id                             | estRows | task      | access object        | operator info                               |
+--------------------------------+---------+-----------+----------------------+---------------------------------------------+
| IndexMerge_15                  | 29.97   | root      |                      | type: union                                 |
| ├─IndexRangeScan_8(Build)      | 10.00   | cop[tikv] | table:t, index:ia(a) | range:[1,1], keep order:false, stats:pseudo |
| ├─IndexRangeScan_9(Build)      | 10.00   | cop[tikv] | table:t, index:ia(a) | range:[1,1], keep order:false, stats:pseudo |
| ├─IndexRangeScan_10(Build)     | 10.00   | cop[tikv] | table:t, index:ib(b) | range:[2,2], keep order:false, stats:pseudo |
| ├─IndexRangeScan_11(Build)     | 10.00   | cop[tikv] | table:t, index:ib(b) | range:[2,2], keep order:false, stats:pseudo |
| ├─IndexRangeScan_12(Build)     | 10.00   | cop[tikv] | table:t, index:ic(c) | range:[3,3], keep order:false, stats:pseudo |
| ├─IndexRangeScan_13(Build)     | 10.00   | cop[tikv] | table:t, index:ic(c) | range:[3,3], keep order:false, stats:pseudo |
| └─TableRowIDScan_14(Probe)     | 29.97   | cop[tikv] | table:t              | keep order:false, stats:pseudo              |
+--------------------------------+---------+-----------+----------------------+---------------------------------------------+

Metadata

Metadata

Assignees

Labels

sig/plannerSIG: Plannertype/enhancementThe issue or PR belongs to an enhancement.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions