Skip to content

Commit

Permalink
planner: rewrite LIKE as range for expression index (#24250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Howie59 committed May 24, 2021
1 parent 9d8935e commit 23ce657
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,28 @@ explain format = 'brief' select c0 from t0;
id estRows task access object operator info
TableReader 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t0 keep order:false, stats:pseudo
-- TableRead
drop table if exists tbl1;
create table tbl1 (id int unsigned not null auto_increment primary key, s int, index((md5(s))));
insert into tbl1 (id) select null;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
insert into tbl1 (id) select null from tbl1;
update tbl1 set s=id%32;
explain format = 'brief' select count(*) from tbl1 where md5(s) like '02e74f10e0327ad868d138f2b4fdd6f%';
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(Column#6)->Column#4
└─IndexReader 1.00 root index:StreamAgg
└─StreamAgg 1.00 cop[tikv] funcs:count(1)->Column#6
└─IndexRangeScan 250.00 cop[tikv] table:tbl1, index:expression_index(md5(`s`)) range:["02e74f10e0327ad868d138f2b4fdd6f","02e74f10e0327ad868d138f2b4fdd6g"), keep order:false, stats:pseudo
select count(*) from tbl1 use index() where md5(s) like '02e74f10e0327ad868d138f2b4fdd6f%';
count(*)
64
8 changes: 8 additions & 0 deletions cmd/explaintest/t/explain_generate_column_substitute.test
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,11 @@ explain format = 'brief' select c0 from t0; -- TableRead
drop table if exists t0;
create table t0(c0 double, c1 float as (c0) unique);
explain format = 'brief' select c0 from t0; -- TableRead

drop table if exists tbl1;
create table tbl1 (id int unsigned not null auto_increment primary key, s int, index((md5(s))));
insert into tbl1 (id) select null; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1; insert into tbl1 (id) select null from tbl1;
update tbl1 set s=id%32;
explain format = 'brief' select count(*) from tbl1 where md5(s) like '02e74f10e0327ad868d138f2b4fdd6f%';
select count(*) from tbl1 use index() where md5(s) like '02e74f10e0327ad868d138f2b4fdd6f%';

6 changes: 6 additions & 0 deletions planner/core/rule_generate_column_substitute.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func (gc *gcSubstituter) substitute(ctx context.Context, lp LogicalPlan, exprToC
tryToSubstituteExpr(expr, lp.SCtx(), candidateExpr, tp, x.Schema(), column)
}
}
case ast.Like:
expr := &sf.GetArgs()[0]
tp = sf.GetArgs()[1].GetType().EvalType()
for candidateExpr, column := range exprToColumn {
tryToSubstituteExpr(expr, lp.SCtx(), candidateExpr, tp, x.Schema(), column)
}
}
}
case *LogicalProjection:
Expand Down

0 comments on commit 23ce657

Please sign in to comment.