给students表中插入数据
INSERT INTO students(sname,age,score,time) VALUES('小明',22,100,now());
INSERT INTO students(sname,age,score,time) VALUES('小红',23,80,now());
INSERT INTO students(sname,age,score,time) VALUES( '小绿',24,80, now());
INSERT INTO students(sname,age,score,time)VALUES('小黑',23,70,now());
创建联合索引
alter table students add index idx_sname_age_score(sname,age,score) ;
联合索引从左到右的顺序为sname,age,score,如果where之后的查询语句,破坏索引的顺序,就会出现索引失效。如:
explain select * from students where age = 22 and score = 100;
如:使用left函数
explain select * from students where left(sname,2) = "小明";
联合索引sname,age,score,只能使用sname,age
explain select * from students where sname="小明" and age > 22 and score = 100;
sname索引无法使用,会进行全表查询
explain select * from students where sname !="小明";
explain select * from students where sname is null;
explain select * from students where sname is not null;
lexplain select * from students where sname like "%明";
explain select *from students where sname = 123;
explain select * from students where sname="小明" or age = 22;
链接: 索引优化