[Mysql series] talk about the "mystery" of explain execution plan


Talk to optimize mysql database, we will talk about the basic explain keywords, to confirm whether the index sql database table to establish and then discuss sql statement or index optimization, etc. - that this article mainly talk about explain understanding. It divided into theory and practice combined.

Theory Part

concept

Let's first understand the explaingrammar and related theoretical knowledge.
Syntax :
EXPLAIN SELECT select_options;

  1. select_optionsIs the query option of the select statement, including from where子句etc.
  2. Executing this statement can analyze the execution of the select statement following EXPLAIN, and can analyze some characteristics of the query table.
    For example: EXPLAIN SELECT * FROM class; the
    execution result is shown in the figure:
    Insert picture description here

The specific meaning of each column in the execution plan is explained as follows:

  • id:
查询的序号,包含一组数字,表示查询中执行select子句或操作表的顺序
1.id相同,执行顺序从上往下
2.id不同,id值越大,优先级越高,越先执行
  • select_type:
查询类型,主要用于区别普通查询,联合查询,子查询等的复杂查询
1.simple ——简单的select查询,查询中不包含子查询或者UNION
2.primary ——查询中若包含任何复杂的子部分,最外层查询被标记
3.subquery——在select或where列表中包含了子查询
4.derived——在from列表中包含的子查询被标记为derived(衍生),MySQL会递归执行这些子查询,把结果放到临时表中
5.union——如果第二个select出现在UNION之后,则被标记为UNION,如果union包含在from子句的子查询中,外层select被标记为derived,故在union中第二个及之后的select。
6.union result:UNION 临时表检索结果的select。
  • table:
输出的行所引用的表
  • partitions:
如果查询基于分区表,将会显示访问的是哪个区。
  • type:
显示连接类型,显示查询使用了何种类型,按照从最佳到最坏类型排序
1.system:表中仅有一行(=系统表)这是const联结类型的一个特例。
2.const:表示通过索引一次就找到,const用于比较primary key或者unique索引。因为只匹配一行数据,所以如果将主键置于where列表中,mysql能将该查询转换为一个常量
3.eq_ref:唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于唯一索引或者主键扫描,常用于连接查询。简单查询不会出现该类型
4.ref:非唯一性索引扫描,返回匹配某个单独值的所有行,本质上也是一种索引访问,是使用普通索引或者唯一性索引的部分前缀,它返回所有匹配某个单独值的行,可能会找多个符合条件的行,属于查找和扫描的混合体
5.range:只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引,一般就是where语句中出现了between,in等范围的查询。这种范围扫描索引扫描比全表扫描要好,因为它开始于索引的某一个点,而结束另一个点,不用全表扫描
6.index:index 与all区别为index类型只遍历索引树。通常比all快,因为索引文件比数据文件小很多。
7.all:遍历全表以找到匹配的行
type常见类型从最优到最差:system > const > eq_ref > ref > range > index > ALL
注意:一般保证查询至少达到range级别,最好能达到ref。
  • possible_keys:
指出MySQL能使用哪个索引在该表中找到行
  • key:
显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。查询中如果使用覆盖索引,则该索引和查询的select字段重叠。
1.要想强制mysql使用或者忽视possible_key列中的索引,在查询中使用force index、use index或者ignore index。
  • key_len:
表示索引中使用的字节数,该列计算查询中使用的索引的长度在不损失精度的情况下,长度越短越好。如果键是NULL,则长度为NULL。该字段显示为索引字段的最大可能长度,并非实际使用长度。
  • ref:
显示索引的哪一列被使用了,如果有可能是一个常数,哪些列或常量被用于查询索引列上的值
  • rows:
根据表统计信息以及索引选用情况,大致估算出找到所需的记录所需要读取的行数
  • filtered:
指返回结果的行占需要读到的行(rows列的值)的百分比。
  • Extra:
包含不适合在其他列中显示,但是十分重要的额外信息
1、Using filesort:说明mysql会对数据适用一个外部的索引排序。而不是按照表内的索引顺序进行读取。MySQL中无法利用索引完成排序操作称为“文件排序”
2、Using temporary:使用了临时表保存中间结果,mysql在查询结果排序时使用临时表。常见于排序order by和分组查询group by。
3、Using index:表示相应的select操作用使用覆盖索引,避免访问了表的数据行。如果同时出现using where,表名索引被用来执行索引键值的查找;如果没有同时出现using where,表名索引用来读取数据而非执行查询动作。
4、Using where :表明使用where过滤
5、using join buffer:使用了连接缓存
6、impossible where:where子句的值总是false,不能用来获取任何元组
7、select tables optimized away:在没有group by子句的情况下,基于索引优化Min、max操作或者对于MyISAM存储引擎优化count(*),不必等到执行阶段再进行计算,查询执行计划生成的阶段即完成优化。
8、distinct:优化distinct操作,在找到第一匹配的元组后即停止找同样值的动作。

Practice Part

use explain_detail;

DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `id` int(11) NOT NULL comment '教师id',
  `teacher_name` varchar(45) DEFAULT NULL comment '姓名',
  `teacher_no` varchar(45) DEFAULT NULL comment '教师编号',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `teacher` (`id`, `teacher_name`, `teacher_no`)
VALUES (1,'溪源a','150921'), (2,'溪源b','201010'), (3,'溪源c','200325');
 
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `id` int(11) NOT NULL AUTO_INCREMENT comment '班级ID',
  `class_name` varchar(10) DEFAULT NULL comment '班级名称',
  PRIMARY KEY (`id`),
  KEY `idx_class_name` (`class_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `class` (`id`, `class_name`)
VALUES (1,'java1'),(2,'java2'),(3,'java3');
 
# 班级教师关系表
DROP TABLE IF EXISTS `class_teacher`;
CREATE TABLE `class_teacher` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL comment '班级ID',
  `teacher_id` int(11) NOT NULL comment '教师ID'
  PRIMARY KEY (`id`),
  KEY `idx_class_teacher_id` (`class_id`,`teacher_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 # 插入数据
INSERT INTO `class_teacher` (`id`, `class_id`, `teacher_id`)
VALUES (1, 1, 1), (2, 1, 2), (3, 2, 1);

id

explain select (select id from teacher limit 1) from class;

Insert picture description here

According to the theoretical knowledge, id值越大执行优先级越高,id值相同则从上往下执行,id为null最后执行。from the ID column in the figure, we see that the first execution of ID=2 is to query the teacher table first.

select_type

  1. simple
    simple select query, the query does not contain subqueries or UNION ;
    Insert picture description here

  2. primary and subquery
    primary: If the query contains any complex subparts , mark the outermost query statement;
    subquery: include in the selector wherelist 子查询, mark the subquery statement;

explain select (select id from teacher) from class;

Insert picture description here

The subquery subquery teacher table, the outer select is primary.

Insert picture description here
This SQL statement can distinguish the execution order of SQL statements based on the ID column.

  1. The derived subqueries contained
    in the from list are marked asderived (derived), and MySQL will execute these subqueries recursively and put the results 临时表in.
explain select * from (select * from teacher limit 1) tmp;

Insert picture description here
According to ID=2, first query the teacher table, then execute the outermost query, and store the result in a temporary table.

  1. union, union result
    union: If the second select appears after the union, it will be marked as union; if the union is included in the subquery of the from clause, the outer select will be marked as derived; so the second in the union And then select.
    union result: select to retrieve the result from the union temporary table.
explain select * from teacher where id = 1 union select * from teacher;

Insert picture description here
id=1 is primary; description is to do outer query, that is, the statement before this SQL statement from;
id=2 is union; description is the query statement after union;
id=null, marked as UNION RESULT, the generated temporary table ; The
two results are merged into union result for select retrieval.
Explain again the id column: 1, 2, null, execution order 2 --> 1 --> null.
First execute select 2, then select 1, and finally execute to retrieve data from the two Cartesian products.

table

The table referenced by the output row;

  1. When from 子句中有子查询time, table column <derivenN>format, showing the current query id = N ** ** dependent query, so the first query execution id = N.
    Insert picture description here
  2. When there is union, the value of UNION RESULT table columns <union1,2>, 1 and 2 show the involvement union select line id.
    Insert picture description here

type

  1. The null
    MySql optimizer can decompose the query statement during the optimization phase, without having to access the table or index during the execution phase.
    Insert picture description here

  2. system

Only a system table data or 衍生表<derived>only the main query a data appear can be ignored, there is not much sense.
Insert picture description here

  1. const
    means that it is found once through the index const用于比较primary key 或者 unique索引(the query type is related to the index type). Because only one line of data needs to be matched , all is fast. If the primary key is placed in the where list, mysql can convert the query to a const.
    Insert picture description here
    Use the primary key index as the condition in the where statement.

  2. eq_ref
    unique index scan, for each index key, only one record in the table matches it. Commonly used in primary key or unique index scans.
    primarykey 或 unique keyAll parts of the index are used in conjunction, and at most only one record that meets the conditions will be returned . This may be the best type of connection other than const, and this type will not appear in a simple select query .
    Insert picture description here
    The id column is all 1. When the id column value is the same, the table is executed from top to bottom. So execute the class_teacher table first, and then execute the class table.


  3. Compared with eq_ref, ref does not use a unique index, but instead uses a normal index. Or 唯一性索引的部分前缀, if the index is compared with a certain value, multiple qualified rows may be found.
    Insert picture description here

  4. range
    using an index column retrieves the specified range is a range where the back of the query (between and, in,>, <,> =).

Insert picture description here

  1. Index
    queries the data in all indexes只有索引树被扫描 ; because index files are usually smaller than data files, they are usually faster than ALL.
    Insert picture description here

Note: For the classtable, the class_name index is created when the table is created above; the same query is used in the teachertable, and the full table is scanned.

  1. all
    MySQL will traverse the entire table to find matching rows. If no index is established or the index is invalid, query the data of the whole table, which should be avoided as much as possible during development.

Insert picture description here

possible_keys

Indicate which indexes MySQL can use to find records in the table . If there is an index on the field involved in the query, the index will be listed, but it may not be used by the query (the index that can be used by the query, if there is no index, it will display null )
Insert picture description here

key

The key column shows the key (index) that MySQL actually decides to use, which must be included in possible_keys.
If no index is selected, the key is NULL. To force MySQL to use or ignore the index in the possible_keys column, use FORCE INDEX, USE INDEX, or IGNORE INDEX in the query.
Insert picture description here

key_len

It represents the index used字节数 , may be the length (the maximum possible length values of the index fields displayed in index key_len used in the query is calculated by the column, not actual length, i.e., based on key_len calculation table definition is obtained, not by the inner Retrieved)
不损失精确性的情况下,长度越短越好, the shorter the length, the higher the efficiency of index verification and matching.

Use 普通索引and 联合索引look at the specific value of key_len respectively below ;
Insert picture description here
use the primary key index, use the number of bytes 4;
Insert picture description here
use the joint index, key_len=8;

Let's expand the calculation rules of key_len:

1)字符串
char(n):n字节长度;
varchar(n)2字节存储字符串长度,如果是utf-8,则长度 3n + 22)数值类型
tinyint:1字节
smallint:2字节
int4字节
bigint:8字节  
3)时间类型
date:3字节
timestamp:4字节
datetime:8字节
如果字段允许为 NULL,需要1字节记录是否为 NULL。(这是为什么会比正常计算多1的原因)。
索引最大长度是768字节,当字符串过长时,MySql会做一个类似左前缀索引的处理,将前半部分的字符提取出来做索引。


ref

Displayed in the key column index, the column or constant used in the table lookup value is generally const or field name.
Insert picture description here

rows

Estimating the number of rows in the result set means that MySQL estimates the number of rows that need to be read to find the required records based on table statistics and index selection.

filtered

Refers to the percentage of rows that return results to the rows that need to be read (rows column value).

Insert picture description here
It can be seen from the figure that rows=3; the specified 溪源anumber of data records is 1, so filtered = 1/3 * 100/100 = 33.33%, with two decimal places.

Insert picture description here
Then why is it 1 here, because the covering index column does not need to be compared with the full table;

Extra

At this point, I finally reach the last column, so hold on~

  1. Using index
    Extra displays Using Index, indicating that the index is used, which is a high performance performance. Generally, the columns appearing in the query are covered by the indexed columns.
    Insert picture description here

  2. Using where
    Extra displays Using where, which means that the index is not used and the query column is not covered by the index column.
    Insert picture description here

  3. Using where Using index
    Extra displays Using whre Using index, which means that the query column is covered by the index column, and the where filter condition is one of the index columns, but not the first index in the leftmost principle. It often appears in joint index scenarios.
    Insert picture description here

  4. NULL
    Extra displays null, indicating that the query column is not covered by the index column, and the where filter condition is the leading column of the index, indicating that the index is used,
    but some fields are not covered by the index column. It must be implemented by "back to the table", so it is not The index is used purely, and it is not completely useless.

Insert picture description here

  1. Using index condition
    Extra shows that Using index condition is similar to Using where, the query column is not completely covered by the index column, and the where condition is a range of the leading column.

Insert picture description here

to sum up

Xiyuan spent three nights finally finishing this article, hoping to help everyone and get everyone's support. If there is something wrong, I hope the big guys will actively correct it.

Everyone remembers
one-click triple connection~

Guess you like

Origin blog.csdn.net/xuan_lu/article/details/109210620