首先创建一个表
create table employee
(
id bigint unsigned auto_increment comment '主键ID'
primary key,
employee_no char(5) null comment '员工编号',
name varchar(30) not null comment '员工姓名',
job varchar(30) not null comment '工作',
salary decimal(7, 2) not null comment '薪水',
employee_id_leader int not null comment '领导ID',
dept_id int not null comment '部门ID',
latitude point null comment '经纬度',
constraint unique_no
unique (employee_no)
)
comment '员工表';
再往表里添加一些数据
insert into employee (id, employee_no, name, job, salary, employee_id_leader, dept_id, latitude)
VALUES (1,'e1001','张三丰','总经理',50000.00,1,2,null),
(2,'e1002','李四','开发',30400.00,1,1,null),
(3,'e1003','王五','UI',23000.00,2,1,null),
(4,'e1004','赵六','销售',30000.00,1,2,null);
1.查询一个表,employee表,在前面添加EXPLAIN返回数据,并查看结果
1.1简单查询
EXPLAIN select * from employee;
未加添加的查询结果,select_type(简单查询),表示这条sql不包含子查询或者union等;
1.2嵌套子查询
EXPLAIN select (select id from employee a where id = 1) from employee b;
总结:id越大,查询优先级越高,同时select_type返回subquery(子查询) 说明a表是一个子查询,而b表返回primary(复杂查询,最外层查询)意思就是该查询语句包含子查询等操作,然后最外面的select称之为复杂查询。
1.3衍生查询
EXPLAIN select * from (select * from employee where id = 1) a;
1.4联合查询
EXPLAIN select id,name,dept_id from employee union all select * from dept;
EXPLAIN select 1 union all select 1;
如果没有使用表,Extra会显示no table used,没有表被使用
1.5综上所述:
id列
id列的编号是 select 的序列号,有几个 select 就有几个id,并且id的顺序是按 select 出现的顺序增长的。 id列越大执行优先级越高,id相同则从上往下执行。
select_type列
simple、primary、subquery、derived、union
table列
表示explain的一行正在访问那个表。
type列
这一列表示关联类型或访问类型,即MySQL决定如何查找表中的行,查找数据行记录的大概范围。 依次从最优到最差分别为:system > const > eq_ref > ref > range > index > ALL 一般来说,得保证查询达到range级别,最好达到ref。
const, system
mysql能对查询的某部分进行优化并将其转化成一个常量(可以看show warnings 的结果)。用于 primary key 或 unique key 的所有列与常数比较时,所以表最多有一个匹配行,读取1次,速度比较快。system是 const的特例,表里只有一条元组匹配时为system。
EXPLAIN select * from (select * from employee where id = 1) a;
eq_ref
primary key 或 unique key 索引的所有部分被连接使用 ,最多只会返回一条符合条件的记录。这可能是在 const 之外最好的联接类型了,简单的 select 查询不会出现这种 type。
EXPLAIN select a.id,a.employee_no from employee a left join dept b on b.id = a.dept_id;
ref
相比 eq_ref,不使用唯一索引,而是使用普通索引或者唯一性索引的部分前缀,索引要和某个值相比较,可能会找到多个符合条件的行。
range
范围扫描通常出现在 in(), between ,> ,<, >= 等操作中。使用一个索引来检索给定范围的行
EXPLAIN select * from dept where id > 1;
index,all
EXPLAIN select id,employee_no from employee;
EXPLAIN select * from employee; -- All
possible_keys列
这个表示该条sql可能会用到的索引。
key列
表示该条sql会用到那个索引
key_len列
这一列显示了mysql在索引里使用的字节数,通过这个值可以算出具体使用了索引中的哪些列。
key_len 计算规则
字符串,char(n)和varchar(n),n均代表字符数,
而不是字节数,如果是utf-8,一个数字 或字母占
1个字节,一个汉字占3个字节
char(n):如果存汉字长度就是 3n 字节。
varchar(n):如果存汉字则长度是 3n + 2 字节,
加的2字节用来存储字符串长度,因为 varchar是变长
字符串。
tinyint:1字节
smallint:2字节
int:4字节
bigint:8字节
时间类型date:3字节
timestamp:4字节
datetime:8字节
如果字段允许为 NULL,需要1字节记录是否为 NULL
索引最大长度是768字节,当字符串过长时,mysql会
做一个类似左前缀索引的处理,将前半部分的字符提取
出来做索引。
ref列
这一列显示了在key列记录的索引中,表查找值所用到的列或常量,常见的有:const(常量),字段名(例:employee.id)
rows列
这一列是mysql估计要读取并检测的行数,注意这个不是结果集里的行数。
Extra列
Using index—覆盖索引
覆盖索引的意思是只需通过二级索引就能满足该sql的查询条件以及查询结果,不需要通过二级索引去寻找主键索引。
EXPLAIN select id from employee;
Using where—where条件未被索引覆盖到
其实也就是没有使用到索引,用的是全表扫描。
EXPLAIN select id from employee where name = '2';
Using index condition—未完全用索引
比如我们一般建立一个联合索引,有三个字段,但是现在我们的where条件里只用到了第一个或者前两个字段,这种就是Using index condition,也叫用了,但没有完全用索引。
Using temporary—使用了临时表
mysql需要创建一张临时表来处理查询。出现这种情况一般是要进行优化的,首先是想到用索引来优化,意思就是给返回的数据添加上索引,如下所示,给name字段加上索引:
EXPLAIN select distinct name from employee;
Using filesort—文件排序
将用外部排序而不是索引排序,数据较小时从内存排序,否则需要在磁盘完成排序。这种情况下一 般也是要考虑使用索引来优化的。
这个其实也是让order by后面的字段走索引就行了。
如下所示,给name字段添加索引:
EXPLAIN select * from employee order by name;
Select tables optimized away—聚合函数访问字段
EXPLAIN select max(id) from employee;