1.使用场景

获取执行计划命令:在select 命令前加上explain 或 desc

explain select 或
desc select

1.语句执行之前 :防患于未然
2.出现慢语句时 :亡羊补牢

2. 执行计划查看

3.重点关注指标说明

table : 		发生在那张表的执行计划
type  :		查询的类型
					全表扫描 : ALL
					索引扫描 :idnex,range,ref,eq_ref,connst(system),NULL        *****
		
possible_keys:  可能用到的索引
key    :		此次查询走的索引名
key_len : 		索引覆盖长度,评估联合索引应用长度。                            *****
rows  : 		扫描了表中的多少行
Extra : 		额外的一些信息                                                ****

4. type

2.4 type 
(1) ALL       :  全表扫描 
mysql> desc select * from city;
mysql> desc select * from city where 1=1 ;
mysql> desc select * from city where population=42;
mysql> desc select * from city where countrycode !='CHN';
mysql> desc select * from city where countrycode not in ('CHN','USA');
mysql> desc select * from city where countrycode like '%CH%';


(2) index     : 全索引扫描 
mysql> desc select countrycode from city;

(3) range     : 索引范围扫描(最常见)
>   <   >=  <=  like  
in or

mysql> desc select  *  from city where id<10;
mysql> desc select * from city where countrycode like 'CH%';


mysql> desc select * from city where countrycode  in ('CHN','USA');

改写: 

desc 
select * from city where countrycode='CHN'
union all 
select * from city where countrycode='USA'

(4) ref  辅助索引等值查询
desc 
select * from city where countrycode='CHN';


(5) eq_ref 多表关联查询中,非驱动表的连接条件是主键或唯一键
desc 
select 
city.name,
country.name ,
city.population 
from city 
join country 
on city.countrycode=country.code
where city.population<100;

(6) connst(system) :主键或者唯一键等值查询
mysql> desc select * from city where id=10;


(7) NULL  索引中获取不到数据 
mysql> desc select * from city where id=100000;

  • 在实际生产中我们尽量避免,全表扫描,全索引扫描,以及索引范围扫描,以提高查询效率,和减少资源使用。
  • 我们在多表关联查询当中,最好保证非驱动表为主键或者是唯一键,最不济为普通主键,以高效率

5.key_len 详细说明

1. 作用:判断联合索引覆盖长度

2.最大长度的计算方法
idx(a,b,c) ========> a(10)+b(20)+c(30)

(1). 影响计算的条件
字符集: utf8mb4
数字类型
tinyint        1 Bytes
int            4 Bytes
bigint         8 Bytes

字符串类型
char(5)      20 Bytes
varchar(5)   20 Bytes + 2Bytes (字符串长度)

没有not null : 会多出来一个字节存储是否为空

字符集
测试表:

create table keyt (
id int not null primary key auto_increment,
num int not null, 
num1 int ,
k1 char(10) not null ,
k2 char(10) , 
k3 varchar(10) not null ,
k4 varchar(10)
)charset=utf8mb4;

num :  4 
num1:  5
k1  :  40 
k2  :  41
k3  :  42 
k4  :  43

2.5.3 联合索引应用"道道"   *****
-- 建立联合索引时,最左侧列,选择重复值最少的列.
alter table keyt add index idx(a,b,c);

-- 例子:
-- 哪些情况可以完美应用以上索引.
desc select *from student where xname='张三' and xage=11 and xgender='m';
desc select *from student where xage=11 and xgender='m' and xname='张三' ;
desc select *from student where xgender='m' and xname='张三' and xage=11 ;

-- 影响到联合索引应用长度的.
-- 缺失 联合索引最左列,不走任何索引
mysql> desc select *from student where xage=11 and xgender='m'  ;
-- 缺失中间部分,只能走丢失部分之前的索引部分
mysql> desc select *from student where xname ='张三'  and xgender='m'  ;
-- 查询条件中,出现不等值查询(> ,< ...like )
mysql> desc select *from student where xname ='张三' xage<18 and xgender='m'  ;
联合索引应用长度到不等值列截断了.
-- 多子句
按照 select 子句顺序创建联合索引.

posted on 2019-12-23 16:47  杨港澳  阅读(188)  评论(0)    收藏  举报