mysql select_type simple_mysql explain
前言
数据准备
mysql优化器在数据量不同的情况下,也会到结果产生影响
create table `user`
(
`id` bigint(20) unsigned not null auto_increment,
`name` varchar(64) default null,
`age` bigint(20) unsigned default '0',
`param` varchar(32) default null,
`a` int(11) default '0',
`b` int(11) default '0',
`c` int(11) default '0',
primary key (`id`),
key `idx_a_b_c_d` (`a`, `b`, `c`),
key `idx_age` (`age`),
key `idx_name` (`name`)
);
create table `order_info`
(
`id` bigint(16) unsigned not null auto_increment,
`ref_user_id` bigint(20) unsigned not null,
`serial_no` varchar(32) not null,
primary key (`id`),
key `uk_user_id` (`ref_user_id`)
);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (1, 'xiaoming', 11, 'a', 1, 2, 3);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (2, 'xiaohong', 23, 'b', 21, 21, 21);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (3, 'liuqiangdong', 45, 'c', 56, 23, 23);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (4, 'mayun', 123, 'd', 45, 12, 3);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (5, 'leijun', 5, 'e', 12, 322, 1);
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (1, 1, '2');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (2, 2, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (3, 3, '5');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (4, 2, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (5, 4, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (6, 5, '6');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (7, 4, '3');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
参数
含义
id
查询的标识符
select_type
SELECT 查询的类型.
table
查询的是哪个表
partitions
匹配的分区
type
判断是什么扫描查询 比如:ALL,Index,Rank
possible_keys
可能选用的索引
key
确切使用到的索引
key_len
索引长度(通过观察这个可以判断联合索引使用了几列,很有用)
ref
哪个字段或常数与 key 一起被使用
rows
显示此查询一共扫描了多少行. 这个是一个估计值.
filtered
表示此查询条件所过滤的数据的百分比
extra
额外的信息
1、select_type
select_type
说明
SIMPLE
简单查询
UNION
联合查询
SUBQUERY
子查询
UNION RESULT
联合查询的结果
PRIMARY
最外层查询
1.1、SIMPLE简单查询
解释:此查询不包含 UNION 查询或子查询
explain select * from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
1.2、UNION联合查询
解释:表示此查询是 UNION 的第二或随后的查询
explain select * from user union select * from user ;
id = 1 PRIMARY 外层查询
id = 2 UNION 联合查询
id = 3 UNION RESULT 很明显为联合查询的结果
1
2
3
4
5
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
PRIMARY
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
2
UNION
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
NULL
UNION RESULT
NULL
ALL
NULL
NULL
NULL
NULL
NULL
NULL
Using temporary
1.3、SUBQUERY子查询
explain
select *
from order_info o
where id > (select b.id from user b where b.id = 1);
1、第一个 select 为 PRIMARY 最外层查询
2、第二个 select 为 SUBQUERY 子查询
1
2
3
4
5
6
7
8
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
PRIMARY
o
NULL
ALL
PRIMARY
NULL
NULL
NULL
7
85.71
Using where
2
SUBQUERY
b
NULL
const
PRIMARY
PRIMARY
8
const
1
100
Using index
1.4、UNION RESULT联合查询的结果
解释:在1.2中介绍过了
1.5、PRIMARY最外层查询
解释:在1.2和1.3中介绍过
2、type
解释:它提供了判断查询是否高效的重要依据依据. 通过type字段, 我们判断此次查询是全表扫描还是索引扫描等,要和Extra同时观察会更好
性能:ALL < index < range ~ index_merge < ref < eq_ref < const < system
2.1、ALL
解释:全表扫描
explain select * from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
2.2、index
解释:表示全索引扫描 (索引覆盖)和ALL类似
1、index: 表示全索引扫描, 和 ALL 类型类似, 只不过 ALL 类型是全表扫描,而 index 类型则仅仅扫描所有的索引, 而不扫描数据. 其实就是讲 查询条件 写上索引的字段
2、index类型通常出现在:所要查询的数据直接在索引树中就可以获取到, 而不需要扫描数据.当是这种情况时, Extra 字段 会显示Using index
3、index类型的查询虽然不是全表扫描, 但是它扫描了所有的索引, 因此比 ALL 类型的稍快.
explain select name from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
index
NULL
idx_name
195
NULL
5
100
Using index
2.3、range
解释:索引范围内查询,通过索引字段范围获取表中部分数据记录. 这个类型通常出现在 =, <>, >, >=, , BETWEEN, IN() 操作中
explain select * from user where id > 2;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
range
PRIMARY
PRIMARY
8
NULL
3
100
Using index condition
2.4、INDEX_MERGE
解释:合并索引,使用多个单列索引搜索
explain select id from user where id = 2 or name = 'xiaoming';
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
index_merge
PRIMARY,idx_name
PRIMARY,idx_name
8,195
NULL
2
100
Using union(PRIMARY,idx_name); Using where
2.5、REF
解释:根据索引查找一个或多个值
explain select id from user where name = 'xiaoming';
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ref
idx_name
idx_name
195
const
1
100
NULL
2.6、eq_ref
解释:连接join查询时,使用primary key 或 unique类型,其实就是说索引唯一的关联查询
explain
select *
from order_info o
join user u on u.id = o.ref_user_id;
1
2
3
4
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
o
NULL
ALL
uk_user_id
NULL
NULL
NULL
7
100
NULL
1
SIMPLE
u
NULL
eq_ref
PRIMARY
PRIMARY
8
hlj_sql.o.ref_user_id
1
100
NULL
2.7、const
解释:针对主键或唯一索引的等值查询扫描,只有一行
explain select id from user where id = 1;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
const
PRIMARY
PRIMARY
8
const
1
100
Using index
2.8、system
解释:表中仅仅有一条数据,这个是特殊的const查询
3、possible_keys
可能用到的索引,看 4
4、key
**解释:表示 MySQL 在查询时, 真实使用到的索引, **
即使有些索引在possible_keys中出现, 但是并不表示此索引会真正地被 MySQL 使用到. MySQL 在查询时具体使用了哪些索引, 由key字段决定…
下面这个条件中使用了 索引id 和 联合索引 ref_user_id
实际上我们只使用了 索引id 进行查询,所以 key是id ,possible_keys 是id和 ref_user_id
explain select id from order_info where id = 1 and ref_user_id = 1;
1
2
3
4
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
order_info
NULL
const
PRIMARY,uk_user_id
PRIMARY
8
const
1
100
NULL
5、key_len
解释: 使用索引字节长度,这个字段可以评估联合索引是否完全被使用
5.1、字符串
类型
索引长度
char(n)
n
varchar(n)
如果是 utf8,3 n + 2
varchar(n)
如果是 utf8mb4 ,则是 4 n + 2 字节.
5.2、数值类型:
类型
索引长度
TINYINT
1
SMALLINT
2
MEDIUMINT
3
INT
4
BIGINT
8
float
4
double
8
decimal
5.3、时间类型
类型
长度
year
1
date
4
time
3
datetime
8
timestamp
4
6、rows
显示此查询一共扫描了多少行. 这个是一个估计值.
7、Extra
解释 : 额外信息,优化器会在索引存在的情况下,通过符合 RANGE 范围的条数和总数的比例来选择是使用索引还是进行全表遍历,
具体案例例具体分析,不要把这里想复杂了,就是一个额外的信息而已
名词解释:
回表:表示即使使用索引筛选了,但是查询的字段不是全部都是索引列
Extra
说明
NULL
查询的不全都是索引
using index
使用覆盖索引的时候就会出现
using index condition
查询条件是索引的一个范围
using where
查询条件包含普通的条件
Using filesort
排序 不能通过索引达到排序效果
using index>using where>using index condition,如果不需要回表查询数据,效率上应该比较快的
mysql select_type simple_mysql explain相关推荐
- explain ref_你必须要掌握的MySQL命令:explain
mysql中的explain命令可以用来查看sql语句是否使用了索引,用了什么索引,有没有做全表扫描.可以帮助我们优化查询语句. explain出来的信息有10列,文章主要介绍type.key.Ext ...
- MySQL查询优化之explain的深入解析
在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...
- MySQL高级之explain详解
MySQL高级之explain详解 文章目录 MySQL高级之explain详解 一.expalin命令详解 1.使用方式 2.结果显示 3.主要的字段信息 4.作用 二.id字段 三.select_ ...
- mysql执行计划explain介绍_Mysql执行计划EXPLAIN详解
分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.explai ...
- MySQL 优化之 EXPLAIN 关键字
MySQL查询优化之explain的深入解析 0. 准备 首先执行如下的 sql 语句: CREATE TABLE IF NOT EXISTS `article` (`id` int(10) unsi ...
- MySQL查询分析器EXPLAIN或DESC用法
MySQL查询分析器EXPLAIN或DESC用法 一.简单例子: MySQL可以通过EXPLAIN或DESC来查看并分析SQL语句的执行情况 mysql> desc one_and_two_kn ...
- mysql 笛卡尔积影响_SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积
SQL优化 MySQL版 -分析explain SQL执行计划 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 首先我们先创建一个数据库,数据库中分别写三张表来存储数据; course: ...
- mysql生成树状执行计划_SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积
SQL优化 MySQL版 -分析explain SQL执行计划 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 首先我们先创建一个数据库,数据库中分别写三张表来存储数据; course: ...
- Mysql中的explain查看执行计划
Mysql中的explain查看执行计划 1.explain是什么? 查看执行计划. 2.怎么使用? explain + sql语句. 3.执行计划包含的信息: id,select_type,tabl ...
- MYSQL执行计划EXPLAIN
实际项目开发中,由于我们不知道实际查询的时候数据库里发生了什么事情,数据库软件是怎样扫描表.怎样使用索引的,因此,我们能感知到的就只有 sql语句运行的时间,在数据规模不大时,查询是瞬间的,因此,在写 ...
最新文章
- HDU - 1150 Machine Schedule(最小点覆盖-二分图最大匹配)
- python将一个列表里面的某类元素取出来_03|Python列表常见操作
- NBU客户端安装(linux和windows)
- zookeeper 负载均衡 概念笔记
- 未经专业销密的计算机设备,DOC1-题
- coreseek mysql_centos+php+coreseek+sphinx+mysql之一coreseek安装篇
- 7.过渡案例:①进度条 ②小米图标翻转(父盒子一定要加初始值!!!)
- 大行bya412和java513_真实解密大行bya412和k3区别在哪里?哪款最好?老司机透漏评测...
- SpringBoot 配置文件加载优先级
- The root link base_link has an inertia specified in the URDF, but KDL does not support a root ...
- ssms远程服务器地址,SSMS无法连接到远程服务器,崩溃
- 基于STM32单片机智能RFID刷卡汽车位锁设计(论文
- 基于web的模型管理系统的设计
- 如何判断一个数是否是NaN
- 纯JS实现懒加载+瀑布流布局
- n皇后问题回溯法-迭代实现
- matlab 可视化界面,[转载]Matlab 的可视化界面设计(上)
- html全选替换,网站上如何实现全选与反选
- Tekla Structures Design Suite 2020中文版
- Microsoft visio 2019 professional 安装
热门文章
- 玩机教程----安卓机型解锁system分区 任意修改删除系统文件
- [Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞
- 这下爽了,居然有人通过这个把Linux的命令提示符给整明白了
- 写了一个内核模块实现简单的类似ps命令.
- 怎么在微云服务器找一个文件,微云文件在哪里打开_怎么快速找到微云文件
- 超级符号就是超级创意_超级食物
- 计算机网络的组成与分类
- 复现 ASPCMS企业建站系统Cookies欺骗漏洞
- h2o api java_h2o 准备
- 华东理工大学计算机图形答案,华东理工大学信息科学与工程学院硕士研究生导师张静...