解决like '%字符串%'时索引不被使用的方法

样例如下:
1.创建表
create table t1(
Id int not null PRIMARY key ,
Name varchar(20) not NULL DEFAULT ‘’,
age int not null default 0,
Email varchar(20) not null DEFAULT ‘’
);

2.EXPLAIN优化分析:
EXPLAIN select * from t1 where name like ‘%aa%’;
EXPLAIN select Name from t1 where name like ‘%aa%’;

3.解决方案
– 覆盖索引
alter table t1 add index index_NameAge(Name,age);

你可能感兴趣的:(数据库)