MySQL如何使用索引

一、初始化测试数据
创建一个测试用的表

create table dept(
id int primary key auto_increment ,
deptName varchar(32) not null unique,
salary decimal(12,6) not null,
remark varchar(256),
createDate date);

在表中插入一万条数据

BEGIN

Declare i int default 0;
while(i<=10000) do
begin
select i;
set i=i+1;
insert into dept(deptName,salary,createDate,remark) values(i,20000,now(),‘test’);
end ;
End while;
END

二、通过EXPLAIN分析SQL的执行计划
在这里插入图片描述

type:表示MySQL在表中找到所需行的方式,或者叫访问类型,常见类型如下(从左到右,性能由差到好)

ALL index range ref eq_ref const,system NULL

ALL: 全表扫描

index: 索引全扫描

range:索引范围扫描

ref:使用非唯一索引扫描

eq_ref:使用唯一索引扫描

const,system:单表中最多只有一个匹配行

以上:通过explain分析type是ALL 是性能最差的一种 以下:开始优化。

三、MySQL中BTREE索引的用法
普通的BTREE索引

优化前 explain select remark from dept where deptName = ?; type ALL

增加索引 ALTER TABLE dept ADD INDEX index_remark (remark);

优化测试 explain select remark from dept where deptName = ?; type ref

联合BTREE索引

增加联合索引 ALTER TABLE dept ADD INDEX index_all (deptName,remark,createDate);

explain select deptName,remark,createDate from dept where deptName=‘2’ and remark = 'test’and createDate =‘2018-07-22’; type ref

但是explain select deptName,remark,createDate from dept where remark = 'test’and createDate =‘2018-07-22’; type index

——type从ref(使用非唯一索引扫描)变成了index(索引全扫描),

上面两个SQL 索引相同 Type不同是因为 BTREE索引中匹配最左前缀 比如给col1+col2+col3字段上加联合索引能够被包含

col1+col2 、col1+col2+col3、col1+col3使用(顺序没有影响 比如 where col1=? and col2=? 和 where col2=? and col1=? 结果一样)

使用联合索引 相当于一下子创建了如上的三个索引,这就是联合索引的好处

四、存在索引但不能使用索引的经典场景
例句:explain select deptName,remark,createDate from dept where deptName=‘2’ and remark = 'test’and createDate =‘2018-07-22’; type ref

下面是反面教材:

1.以%开头的LIKE查询不能使用BTREE索引

explain select deptName,remark,createDate from dept where deptName like’%2’ and remark = 'test’and createDate =‘2018-07-22’; type index

2.数据类型出现隐式转换时也不能使用索引

explain select deptName,remark,createDate from dept where deptName =2 and remark = 'test’and createDate =‘2018-07-22’; type index

3.复合索引时 不符合最左匹配原则(上面已经提到)

explain select deptName,remark,createDate from dept where remark = 'test’and createDate =‘2018-07-22’; type index

复合索引最左匹配原则的成因:

Mysql创建复合索引的规则是首先会对复合索引最左边,也就是第一个字段的索引进行排序,

在第一个字段排序的基础上,在对第二个字段进行排序,所以直接使用第二个字段是没有顺序的

4.用or分隔开的条件,如果or前的条件中的列有索引,后面的列中没有索引,那么涉及到的索引都不会使用到

explain select deptName,remark,createDate from dept where deptName =‘2’ and remark = 'test’and createDate =‘2018-07-22’ or salary =200; type ALL

五、Order By语句
  order by 字段混合使用DESC ASC 不会使用索引

select * from table order by key1 desc,key2 asc (尽量不要出现这种混合排序)

Where条件过滤的关键字和Order by中所使用的不同 不会使用索引

select * from table where key2 = ? order by key1 (order by 出现的关键字 尽量 在where条件中也出现)

六、优化Group By语句
  当我们的SQL语句需要Group By分组时:

explain分析SQL的时候 Extra那一列有时候会出现 using Filesort表示需要额外的排序 如果业务没有要求排序 我们可以通过禁止排序, 加上 Order By NULL 有时候可以去掉using filesort

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值