关于mysql联合索引的⾯试题
今天是我失业的第41天,这段时间⾯试了很多公司,却没有⼏个offer,因为没有学历没有⼊职成功,今天和⼤家分享⼀下今天的⾯试题,关于mysql联合索引的问题。
先跟⼤家描述⼀下⾯试官的问题,表中a,b两个字段创建联合索引,以下哪些sql会使⽤到索引。当然⾯试的时候我是说的对了,但是为了验证⼀下我还是在⾃⼰的电脑上实践了⼀下。
我先是创建了表sql:
CREATE TABLE `test_index` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,header英文
`c` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
mysql面试题sqlKEY `index_ab` (`a`,`b`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
INSERT INTO `test`.`test_index` (`id`, `a`, `b`, `c`) VALUES ('1', '11', '22', '33');一个会动的高级昵称代码
INSERT INTO `test`.`test_index` (`id`, `a`, `b`, `c`) VALUES ('2', '111', '222', '333');
然后添加了⼀下索引:
ALTER TABLE test_index ADD INDEX index_ab (a, b);
matlab画sinx图像⼤家可以先猜测⼀下哪些sql使⽤了索引:
EXPLAIN select * from test_index;
EXPLAIN select * from test_index where a=11;
EXPLAIN select * from test_index where a=11 or b=11;
EXPLAIN select * from test_index where a=11 and b=11;c语言linklist什么意思
EXPLAIN select * from test_index where b=11;
EXPLAIN select * from test_index where b=11 and a=11;
EXPLAIN select * from test_index where a=11 and c=22;
想好哪些使⽤到了索引了吗?下⾯给⼤家公布⼀下答案:
初中以ness结尾的名词
EXPLAIN select * from test_index; -- 没有使⽤索引
EXPLAIN select * from test_index where a=11; -- type ref 使⽤索引
EXPLAIN select * from test_index where a=11 or b=11; -- 没有使⽤索引
EXPLAIN select * from test_index where a=11 and b=11; -- type ref 使⽤索引
EXPLAIN select * from test_index where b=11; -- 没有使⽤索引
EXPLAIN select * from test_index where b=11 and a=11; -- type ref 使⽤索引
EXPLAIN select * from test_index where a=11 and c=22; -- type ref 使⽤索引
给⼤家详解⼀下,第⼀个sql很明显没有使⽤到索引;第⼆个sql,因为mysql索引最左原则所以使⽤到了索引,因为是先创建的a之后才是b;第三个没有使⽤到索引,因为or关键字的原因;第四个sql也不⽤多说了吧,两个都使⽤到了;第五个sql和第⼆条sql差不多,因为使⽤的是后者,所以没有使⽤到索引;第六个sql,依稀记得之前看过上⾯写,这样写的sql会被优化成“EXPLAIN select * from
test_index where a=11 and b=11;” 所以也使⽤到了索引;第⼋条也不⽤说了,只要不是单独使⽤后者外加其他字段都会使⽤索引,个⼈能⼒有限,如果有不对的地⽅还请⼤家指出来,相互学习,谢谢。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。