MySQL索引篇
1、索引介绍
索引是帮助MySQL高效获取数据的数据结构。好比是一本书前面的目录,能加快数据库的查询速度。
1.1优势:
--检索,可以提高数据检索的效率,降低数据库的IO成本。
--排序,通过索引列对数据进行排序,降低数据排序的成本,减少CPU的消耗。
1.2劣势:
索引会占据磁盘空间索引虽然会提高查询效率,但是会降低更新表的效率**。
比如每次对表进行增删改操作MySQL不仅要保存数据,还有保存或者更新对应的索引文件。
2、索引分类
按照数据结构:主要包含B+树和Hash索引。
应用层次来分:普通索引,唯一索引,复合索引。
3、索引的使用
3.1索引创建
3.1.1普通索引
即一个索引只包含单个列,一个表可以有多个单列索引。
alter table table_name add index index_name (column) ;
3.1.2唯一索引
索引列的值必须唯一,但允许有空值。
alter table table_name add unique index index_name(column);
3.1.3全文索引
alter table table_name add fulltext index_name(column);
3.1.4组合索引
多列值组成一个索引,专门用于组合搜索,其效率大于索引合并。
alter table table_name add index index_name_age (name,age)) ; 删除索引 DROP INDEX index_name ON table查看索引SHOW INDEX FROM table`
3、索引原理分析
3.1 索引存储结构
索引是在存储引擎中实现的,也就是说不同的存储引擎,会使用不同的索引。
MyISAM和InnoDB存储引擎:只支持BTREE索引,也就是说默认使用BTREE,不能够更换。
MEMORY/HEAP存储引擎:支持HASH和BTREE索引。
3.2 B树和B+树
数据结构示例网站:
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
B树是为了磁盘或其它存储设备而设计的一种多叉平衡查找树。
3.2.1 B和B+的区别
B树和B+树的最大区别在于非叶子节点是否存储数据的问题。
B树是非叶子节点和叶子节点都会存储数据。
B+树只有叶子节点才会存储数据,而且存储的数据都是在一行上,而且这些数据都是有指针顺序指向。
3.3聚簇索引
是一种数据存储方式,将数据存储与索引放到了一块,找到索引也就找到了数据,如果有主键,MySQL将使用主键作为聚簇索引,如果没有的话则会隐式定义一个主键作为聚簇索引。
3.4 非聚簇索引
不是聚簇索引,就是非聚簇索引。以非主键创建的索引;非聚集索引在叶子节点存储的是主键和索引列;使用非聚簇索引查询数据,会查询到叶子上的主键,再根据主键查到数据(这个过程叫做回表)。
聚簇索引和非聚簇索引的区别?
在Innodb下主键索引是聚集索引,在Myisam下主键索引是非聚集索引。
InnoDB会使用聚簇索索引来保存数据,而非聚簇索引的目的仅仅是加快查询速度。
聚簇索引一个表只能有一个,而非聚簇索引一个表可以存在多个,而且只能由用户自己添加,InnoDB默认并不会创建任何非聚簇索引。。
聚簇索引存储记录是物理上连续存在,而非聚簇索引是逻辑上的连续,物理存储并不连续。
使用非聚簇索引查询数据一定会用到聚簇索引,但反过来却不存在。
4.索引使用场景
4.1 哪些情况需要创建索引
1、主键自动建立唯一索引
2、频繁作为查询条件的字段应该创建索引 where。
3、多表关联查询中,关联字段应该创建索引 on 两边都要创建索引。
4、查询中排序的字段,应该创建索引 B+tree有顺序。
5、统计或者分组字段,应该创建索引。
4.2 哪些情况不需要创建索引
1、表记录太少;索引是要有存储的开销。
2、频繁更新;索引要维护。
3、查询字段使用频率不高。
4.3 组合索引的使用:
例如组合索引(a,b,c),组合索引的生效原则是
从前往后依次使用生效,如果中间某个索引没有使用,那么断点前面的索引部分起作用,断点后面的索引没有起作用。
比如:
where a=3 and b=4 and c=5;这种三个索引顺序使用中间没有断点,全部发挥作用。
where c=5 and b=4 and a=3;全部发挥作用,条件顺序在查询之前会被mysql自动优化,效果跟上一句一样。
where a=3 and c=5;这种情况下b就是断点,a发挥了效果,c没有效果。
where b=3 and c=4;这种情况下a就是断点,在a后面的索引都没有发挥作用,这种写法联合索引没有发挥任何效果。
where a=3 and b>7 and c=3;(>范围值就算是断点)a用到了,b也用到了,c没有用到。
where a>4 and b=7 and c=9;(>范围值就算是断点)a用到了,b没有使用,c没有使用。
where a=3 order by b;a用到了索引,b在结果排序中也用到了索引的效果,a下面任意一段的b是排好序的。
where b=3 order by a;b没有用到索引,排序中a也没有发挥索引效果。

为什么使用组合索引?
由多个字段组成的索引,使用顺序就是创建的顺序
ALTER TABLE 'table_name' ADD INDEX index_name(col1,col2,col3)
5.索引实战
5.1案例表
--用户表
create table tuser(
id int primary key,
loginname varchar(100),
name varchar(100),
age int,
sex char(1),
dep int,
address varchar(100)
);
--部门表
create table tdep(
id int primary key,
name varchar(100)
);
--地址表
create table taddr(
id int primary key,
addr varchar(100)
);
--创建普通索引
mysql> alter table tuser add index idx_dep(dep);
--创建唯一索引
mysql> alter table tuser add unique index idx_loginname(loginname);
--创建组合索引
mysql> alter table tuser add index idx_name_age_sex(name,age,sex);
--创建全文索引
mysql> alter table taddr add fulltext ft_addr(addr);
5.2查看执行计划
explain出来的信息有10列,分别是
id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra
5.2.1 id
每个SELECT语句都会自动分配的一个唯一标识符。
5.2.2 select_type(重要)
查询类型,主要用于区别普通查询、联合查询(union、union all)、子查询等复杂查询。
simple
表示不需要union操作或者不包含子查询的简单select查询。有连接查询时,外层的查询为simple,且只有一个
explain select * from tuser;
primary
一个需要union操作或者含有子查询的select,位于最外层的单位查询的select_type即为primary。且只有一个
explain select (select name from tuser) from tuser ;
subquery
除了from字句中包含的子查询外,其他地方出现的子查询都可能是subque
explain select * from tuser where id = (select max(id) from tuser);
dependent subquery
与dependent union类似,表示这个subquery的查询要受到外部表查询的影响
explain select id,name,(select name from tdep a where a.id=b.dep) from tuser b;
union
union连接的两个select查询,第一个查询是PRIMARY,除了第一个表外,第二个以后的表select_type 都是union
explain select * from tuser where sex='1' union select * from tuser where sex='2';
dependent union
与union一样,出现在union 或union all语句中,但是这个查询要受到外部查询的影响
explain select * from tuser where sex in (select sex from tuser where sex='1' union select sex from tuser where sex='2');
5.2.3 table
显示的查询表名,如果查询使用了别名,那么这里显示的是
5.2.4 type(重要)
依次从好到差:
system,const,eq_ref,ref,fulltext,ref_or_null,unique_subquery,index_subquery,range,index_merge,index,all
除了all之外,其他的type都可以使用到索引,除了index_merge之外,其他的type只可以用到一个索引,因为优化器会选用最优索引一个
system
表中只有一行数据或者是空
const(重要)
使用唯一索引或者主键,返回记录一定是1行记录的等值where条件时,通常type是const。
explain select * from tuser where id=1;
explain select * from tuser where loginname = '1';
eq_ref(重要)
连接字段主键或者唯一性索引。
此类型通常出现在多表的join查询, 表示对于前表的每一个结果, 都只能匹配到后表的一行结果. 并且查询的比较操作通常是 '=', 查询效率较高。
explain select a.id from tuser a left join tdep b on a.dep=b.id;
ref(重要)
针对非唯一性索引,使用等值(=)查询非主键。或者是使用了最左前缀规则索引的查询。
-- 非唯一索引
explain select * from tuser where dep=1;
-- 联合索引 abc 最左前缀a
explain select * from tuser where name = '1';
fulltext
全文索引检索,要注意,全文索引的优先级很高,若全文索引和普通索引同时存在时,mysql不管代价,优先选择使用全文索引。
explain select * from taddr where match(addr) against('abc');
range(重要)
索引范围扫描,常见于使用>,<,is null,between ,in ,like等运算符的查询中。
explain select * from tuser where id>1;
-- like 'a%'前缀索引
explain select * from tuser where name like 'a%';
-- like '%a' 不使用索引
explain select * from tuser where loginname like '%a';
index_merge
表示查询使用了两个以上的索引,最后取交集或者并集,常见and,or的条件使用了不同的索引。
由于要读取所个索引,性能可能大部分时间都不如range
index(重要)
条件是出现在索引树中的节点的。可能没有完全匹配索引。
索引全表扫描,把索引从头到尾扫一遍,常见于使用索引列就可以处理不需要读取数据文件的查询、可以使用索引排序或者分组的查询。
-- 单索引
explain select loginname from tuser;
-- 组合索引
explain select age from tuser;
all(重要)
这个就是全表扫描数据文件,然后再在server层进行过滤返回符合要求的记录。
explain select * from tuser;
5.2.5 possible_keys
此次查询中可能选用的索引,一个或多个。
5.2.6 key
查询真正使用到的索引。
5.2.7 key_len
用于处理查询的索引长度,如果是单列索引,那就整个索引长度算进去,如果是多列索引,那么查询不一定都能使用到所有的列,具体使用到了多少个列的索引,这里就会计算进去,没有使用到的列,这里不会计算进去。
另外,key_len只计算where条件用到的索引长度,而排序和分组就算用到了索引,也不会计算到key_len。
5.2.8 ref
- 如果是使用的常数等值查询,这里会显示const
- 如果是连接查询,被驱动表的执行计划这里会显示驱动表的关联字段
- 如果是条件使用了表达式或者函数,或者条件列发生了内部隐式转换,这里可能显示为func
5.2.9 rows
这里是执行计划中估算的扫描行数,不是精确值(InnoDB不是精确的值,MyISAM是精确的值,主要原
因是InnoDB里面使用了MVCC并发机制)
5.2.10 extra(重要)
这个列包含不适合在其他列中显示单十分重要的额外的信息,这个列可以显示的信息非常多
using filesort(重要)
排序时无法使用到索引时,就会出现这个。常见于order by和group by语句中
说明MySQL会使用一个外部的索引排序,而不是按照索引顺序进行读取。MySQL中无法利用索引完成的排序操作称为“文件排序”
explain select * from tuser order by address;
using index(重要)
查询时不需要回表查询,直接通过索引就可以获取查询的数据。
- 表示相应的SELECT查询中使用到了覆盖索引(Covering Index) ,避免访问表的数据行,效率不错!
- 如果同时出现Using Where ,说明索引被用来执行查找索引键值。
- 如果没有同时出现Using Where ,表明索引用来读取数据而非执行查找。
explain select name,age,sex from tuser ;
using union:
表示使用or连接各个使用索引的条件时,该信息表示从处理结果获取并集。
using sort_union和using sort_intersection
与using union类似,只是他们是出现在用and和or查询信息量大时,先查询主键,然后进行排序合并后,才能读取记录并返回。
using temporary
表示使用了临时表存储中间结果。MySQL在对查询结果order by和group by时使用临时表
临时表可以是内存临时表和磁盘临时表
distinct 在select部分使用了distinct关键字 (索引字段)
explain select distinct a.id from tuser a left join tdep b on a.dep=b.id;
explain select name from tuser group by name order by id;
using where(重要)
表示存储引擎返回的记录并不是所有的都满足查询条件,需要在server层进行过滤。
查询条件address不是索引
explain select * from tuser where address='1';
-- 索引失效
explain select * from tuser where age=1;
explain select * from tuser where id in(1,2);
6、索引失效分析
loginname唯一索引
dep普通索引
name,age,sex联合索引
6.1 联合索引,查询时的条件列不是联合索引中的第一个列,索引失效。
-- 带头索引死:
explain select * from tuser where age=23;
-- 中间索引断:
explain select * from tuser where name='aa' and sex='1';
6.2 在索引列上使用mysql的内置函数,或者运算,会索引失效。
explain select * from tuser where loginname='1';
explain select * from tuser where left(loginname,1)='1';
6.3 索引字段上不要使用不等
索引字段上使用(!或者 < >)判断时,会导致索引失效而转向全表扫描注:主键索引会使用范围索引,辅助索引会失效
explain select * from tuser where loginname!='10001';
explain select * from tuser where loginname<>'10001';
6.4 范围条件右边的列失效
不能继续使用索引中范围条件(bettween、<、>、in等)右边的列
-- 俩索引正常
explain select * from tuser where loginname='10001' and name='fuyu-1';
-- 全部失效
explain select * from tuser where name='fuyu-1' and age>20 and loginname='10001';
6.5 索引字段使用like不以通配符开头
索引字段使用like以通配符开头(‘%字符串’)时,会导致索引失效而转向全表扫描
-- 非失效案例
explain select * from tuser where name like 'a%';
-- 失效案例
explain select * from tuser where name like '%a';
6.6 联合索引字段使用or 会索引失效,单独索引不会失效
联合索引字段使用 or 时,会导致索引失效而转向全表扫描
-- 失效案例
explain select * from tuser where name='fuyu-100000' or age=27
-- 正常案例 loginname唯一索引
explain select * from tuser where loginname = '110000' or name='fuyu-100000'
-- 不失效,因为俩字段都是单独的索引
explain select * from tuser where dep='10087' or loginname = '110000'
6.7 索引字段字符串要加单引号
索引字段是字符串,但查询时不加单引号,会导致索引失效而转向全表扫描
-- 非失效案例
explain select * from tuser where name='123';
-- 失效案例
explain select * from tuser where name=123;
6.8 左右连接查询关联的字段编码格式不一样,可能导致索引失效。
字段字符集编码是utf8mb4,而另一个字段字符集编码为utf8。
6.9 主键字段上不可以使用null 索引字段可以使用is null,但不能使用is not null
主键 不能is null
索引字段 可为空,is null 走索引,is not null 不走索引
索引字段 不为空,is null is not null 都不走索引
-- 失效案例
explain select * from tuser where id is null;
explain select * from tuser where id is not null;
explain select * from tuser where dep is not null;

浙公网安备 33010602011771号