Home >Database >Mysql Tutorial >The role of EXPLAIN in Mysql

The role of EXPLAIN in Mysql

一个新手
一个新手Original
2017-10-17 10:04:301671browse

1. MYSQL index

Index (Index): A data structure that helps Mysql obtain data efficiently. Used to improve search efficiency, it can be compared to a dictionary. It can be simply understood as a sorted and fast search data structure.
The role of the index: to facilitate querying and sorting (so adding an index will affect the where statement and order by sorting statement).
In addition to the data, the database also maintains data structures that satisfy specific search algorithms, and these data structures reference the data in some way. This allows advanced search algorithms to be implemented on these data structures. These data structures are indexes.
The index itself is also very large and cannot be stored entirely in memory, so the index is often stored on disk in the form of an index file.
The index we usually refer to is generally a B-tree index unless otherwise specified. (Clustered index, compound index, prefix index, and unique index are all B+ tree indexes by default). In addition to B-tree indexes, there are also hash indexes.

Advantages: A. Improve data retrieval efficiency and reduce database IO costs
B. Sort data through index columns, reducing data sorting costs and CPU consumption.
Disadvantages: A. The index is also a table. This table saves the primary key and index fields and points to the records of the entity table, so the index also takes up space.
B. When performing INSERT, UPDATE, and DELETE operations on the table, MYSQL will not only update the data, but also save the corresponding information about the index column fields added to the index file each time it is updated.
In the actual production environment, we need to analyze step by step, optimize and establish the optimal index, and optimize our query conditions.

Classification of indexes: 1. Single-value index An index only contains one field, and a table can have multiple single-column indexes.
2. Unique index The value of the index column must be unique, but null values ​​are allowed.
3. Composite index An index contains multiple columns
It is recommended to create no more than 5 indexes for a table

Syntax:

Create 1. CREATE [UNIQUE] INDEX indexName ON myTable (columnName(length));
2. ALTER myTable Add [UNIQUE] INDEX [indexName] ON (columnName(length));
Delete: DROP INDEX [indexName] ON myTable;
View: SHOW INDEX FROM table_name\G;

2. The role of EXPLAIN

##EXPLAIN: Simulate how the Mysql optimizer executes SQL query statements to know how Mysql processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.


mysql> explain select * from tb_user;+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | tb_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+

(1)id column:


(1)、id 相同执行顺序由上到下
mysql> explain  
    -> SELECT*FROM tb_order tb1    -> LEFT JOIN tb_product tb2 ON tb1.tb_product_id = tb2.id    -> LEFT JOIN tb_user tb3 ON tb1.tb_user_id = tb3.id;+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+| id | select_type | table | type   | possible_keys | key     | key_len | ref                       | rows | Extra |+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+|  1 | SIMPLE      | tb1   | ALL    | NULL          | NULL    | NULL    | NULL                      |    1 | NULL  ||  1 | SIMPLE      | tb2   | eq_ref | PRIMARY       | PRIMARY | 4       | product.tb1.tb_product_id |    1 | NULL  ||  1 | SIMPLE      | tb3   | eq_ref | PRIMARY       | PRIMARY | 4       | product.tb1.tb_user_id    |    1 | NULL  |+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+(2)、如果是子查询,id序号会自增,id值越大优先级就越高,越先被执行。
mysql> EXPLAIN    -> select * from tb_product tb1 where tb1.id = (select tb_product_id from  tb_order tb2 where id = tb2.id =1);+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+|  1 | PRIMARY     | tb1   | const | PRIMARY       | PRIMARY | 4       | const |    1 | NULL        ||  2 | SUBQUERY    | tb2   | ALL   | NULL          | NULL    | NULL    | NULL  |    1 | Using where |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+(3)、id 相同与不同,同时存在

mysql> EXPLAIN 
    -> select * from(select * from tb_order tb1 where tb1.id =1) s1,tb_user tb2 where s1.tb_user_id = tb2.id;+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+| id | select_type | table      | type   | possible_keys | key     | key_len | ref   | rows | Extra |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+|  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL  |    1 | NULL  ||  1 | PRIMARY     | tb2        | const  | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  ||  2 | DERIVED     | tb1        | const  | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+derived2:衍生表   2表示衍生的是id=2的表 tb1

(2) select_type column: Operation type of data reading operation 1. SIMPLE: Simple select query, SQL does not contain subqueries or UNION.
2. PRIMARY: The query contains complex subquery parts, and the outermost query is marked as PRIMARY
3. SUBQUERY: The subquery is included in the select or WHERE list
4. DERIVED: In FROM The subqueries contained in the list will be marked as DERIVED (derived table), and MYSQL will recursively execute these subqueries and put the result set into the zero-time table.
 5. UNION: If the second SELECT appears after UNION, it will be marked as UNION; if UNION is included in the subquery of the FROM clause, the outer SELECT will be marked as DERIVED
 6. UNION RESULT: Select the result obtained from the UNION table

(3) table column: Which table does the row of data relate to

(4) type column: access type from best to worst system > const > eq_ref > ref > range > index > ALL

  1、system:表只有一条记录(等于系统表),这是const类型的特例,平时业务中不会出现。
  2、const:通过索引一次查到数据,该类型主要用于比较primary key 或者unique 索引,因为只匹配一行数据,所以很快;如果将主键置于WHERE语句后面,Mysql就能将该查询转换为一个常量。
  3、eq_ref:唯一索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或者唯一索引扫描。
  4、ref:非唯一索引扫描,返回匹配某个单独值得所有行,本质上是一种索引访问,它返回所有匹配某个单独值的行,就是说它可能会找到多条符合条件的数据,所以他是查找与扫描的混合体。
  5、range:只检索给定范围的行,使用一个索引来选着行。key列显示使用了哪个索引。一般在你的WHERE 语句中出现between 、66f7a98850431fb8c0d9470a35d9d294 、in 等查询,这种给定范围扫描比全表扫描要好。因为他只需要开始于索引的某一点,而结束于另一点,不用扫描全部索引。
  6、index:FUll Index Scan 扫描遍历索引树(扫描全表的索引,从索引中获取数据)。
  7、ALL 全表扫描 从磁盘中获取数据  百万级别的数据ALL类型的数据尽量优化。

(五)possible_keys列:显示可能应用在这张表的索引,一个或者多个。查询涉及到的字段若存在索引,则该索引将被列出,但不一定被查询实际使用。
(六)keys列:实际使用到的索引。如果为NULL,则没有使用索引。查询中如果使用了覆盖索引,则该索引仅出现在key列表中。覆盖索引:select 后的 字段与我们建立索引的字段个数一致。

(七)ken_len列:表示索引中使用的字节数,可通过该列计算查询中使用的索引长度。在不损失精确性的情况下,长度越短越好。key_len 显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出来的。
(八)ref列:显示索引的哪一列被使用了,如果可能的话,是一个常数。哪些列或常量被用于查找索引列上的值。
(九)rows列(每张表有多少行被优化器查询):根据表统计信息及索引选用的情况,大致估算找到所需记录需要读取的行数。

(十)Extra列:扩展属性,但是很重要的信息。

1、 Using filesort(文件排序):mysql无法按照表内既定的索引顺序进行读取。
 mysql> explain select order_number from tb_order order by order_money;
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | tb_order | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
row in set (0.00 sec)
说明:order_number是表内的一个唯一索引列,但是order by 没有使用该索引列排序,所以mysql使用不得不另起一列进行排序。
2、Using temporary:Mysql使用了临时表保存中间结果,常见于排序order by 和分组查询 group by。
mysql> explain select order_number from tb_order group by order_money;
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | tb_order | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
row in set (0.00 sec)
3、Using index 表示相应的select 操作使用了覆盖索引,避免访问了表的数据行,效率不错。
如果同时出现Using where ,表明索引被用来执行索引键值的查找。
如果没有同时出现using where 表明索引用来读取数据而非执行查找动作。
mysql> explain select order_number from tb_order group by order_number;
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
| id | select_type | table    | type  | possible_keys      | key                | key_len | ref  | rows | Extra       |
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
|  1 | SIMPLE      | tb_order | index | index_order_number | index_order_number | 99      | NULL |    1 | Using index |
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
row in set (0.00 sec)
4、Using where 查找
5、Using join buffer :表示当前sql使用了连接缓存。
6、impossible where :where 字句 总是false ,mysql 无法获取数据行。
7、select tables optimized away:
8、distinct:

The above is the detailed content of The role of EXPLAIN in Mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn