逛V站遇到这个问题,也有点懵,看到评论答案形形色色,还是本人入手试下吧。
1.建一个表:
2.插入10000条数据
DROP PROCEDURE IF EXISTS proc_initData;--如果存在此存储过程则删掉DELIMITER $CREATE PROCEDURE proc_initData()BEGIN DECLARE i INT DEFAULT 1; WHILE i<=10000 DO INSERT INTO test(id,a,b,c) VALUES(i,i,i,i); SET i = i+1; END WHILE;END $CALL proc_initData();
3.建索引
ALTER table test ADD INDEX indextest(a,b,c);
4.测试
explain select * from test where a=1;
explain select * from test where a=1 and b > 2;
explain select * from test where a=1 and b > 2 order by c;
由此可知,的确满足最左匹配会应用索引没有问题,a 等值,b 范畴,ab会应用到索引,范畴后的操作不会应用索引,c不会用到索引,但总体来看还是用到了组合索引。