当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL组合索引与最左匹配原则详解

MySQL组合索引与最左匹配原则详解

2019年03月19日  | 移动技术网IT编程  | 我要评论
前言 之前在网上看到过很多关于mysql联合索引最左前缀匹配的文章,自以为就了解了其原理,最近面试时和面试官交流,发现遗漏了些东西,这里自己整理一下这方面的内容。

前言

之前在网上看到过很多关于mysql联合索引最左前缀匹配的文章,自以为就了解了其原理,最近面试时和面试官交流,发现遗漏了些东西,这里自己整理一下这方面的内容。

什么时候创建组合索引?

当我们的where查询存在多个条件查询的时候,我们需要对查询的列创建组合索引

为什么不对没一列创建索引

  • 减少开销
  • 覆盖索引
  • 效率高

减少开销:假如对col1、col2、col3创建组合索引,相当于创建了(col1)、(col1,col2)、(col1,col2,col3)3个索引
覆盖索引:假如查询select col1, col2, col3 from 表名,由于查询的字段存在索引页中,那么可以从索引中直接获取,而不需要回表查询

效率高:对col1、col2、col3三列分别创建索引,mysql只会选择辨识度高的一列作为索引。假设有100w的数据,一个索引筛选出10%的数据,那么可以筛选出10w的数据;对于组合索引而言,可以筛选出100w*10%*10%*10%=1000条数据

最左匹配原则

假设我们创建(col1,col2,col3)这样的一个组合索引,那么相当于对col1列进行排序,也就是我们创建组合索引,以最左边的为准,只要查询条件中带有最左边的列,那么查询就会使用到索引

创建测试表

create table `student` (
 `id` int(11) not null,
 `name` varchar(10) not null,
 `age` int(11) not null,
 primary key (`id`),
 key `idx_id_name_age` (`id`,`name`,`age`)
) engine=innodb default charset=utf8

填充100w测试数据

drop procedure pro10;
create procedure pro10()
begin
	declare i int;
	declare char_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
	declare return_str varchar(255) default '';
	declare age int;
	set i = 1;
	while i < 5000000 do
		set return_str = substring(char_str, floor(1 + rand()*62), 8);
		set i = i+1;
		set age = floor(rand() * 100);
		insert into student(id, name, age) values(i, return_str, age);
	end while;
end;

call pro10();

场景测试

explain select * from student where id = 2;

可以看到该查询使用到了索引

explain select * from student where id = 2 and name = 'defghijk';

可以看到该查询使用到了索引

explain select * from student where id = 2 and name = 'defghijk' and age = 8;

可以看到该查询使用到了索引

explain select * from student where id = 2 and age = 8;

可以看到该查询使用到了索引

explain select * from student where name = 'defghijk' and age = 8;

可以看到该查询没有使用到索引,类型为index,查询行数为4989449,几乎进行了全表扫描,由于组合索引只针对最左边的列进行了排序,对于name、age只能进行全部扫描

explain select * from student where name = 'defghijk' and id = 2;

explain select * from student where age = 8 and id = 2;

explain select * from student where name = 'defghijk' and age = 8 and id = 2;

可以看到如上查询也使用到了索引,id放前面和放后面查询到的结果是一样的,mysql会找出执行效率最高的一种查询方式,就是先根据id进行查询

总结

如上测试,可以看到只要查询条件的列中包含组合索引最左边的那一列,不管该列在查询条件中的位置,都会使用索引进行查询。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网