1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > MySQL优化篇:explain性能分析

MySQL优化篇:explain性能分析

时间:2022-10-16 15:57:28

相关推荐

MySQL优化篇:explain性能分析

文章目录

1、概念2、explain能干什么3、使用方式4、准备工作5、各字段解释5.1、id5.2、select_type5.3、table5.4、type5.5、possible_keys5.6、key5.7、key_len5.8、ref5.9 rows5.10、Extra

1、概念

使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的。分析查询语句或是表结构的性能瓶颈。

2、explain能干什么

表的读取顺序数据读取操作的操作类型那些索引可以使用哪些索引被实际使用表之间的引用每张表有多少行被优化器查询

3、使用方式

使用方法:explain+SQL语句

explain执行后返回的信息如下:

4、准备工作

建立演示的表及示例数据

CREATE TABLE t1(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));CREATE TABLE t2(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));CREATE TABLE t3(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));CREATE TABLE t4(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));INSERT INTO t1(content) VALUES(CONCAT('t1_',FLOOR(1+RAND()*1000)));INSERT INTO t2(content) VALUES(CONCAT('t2_',FLOOR(1+RAND()*1000)));INSERT INTO t3(content) VALUES(CONCAT('t3_',FLOOR(1+RAND()*1000)));INSERT INTO t4(content) VALUES(CONCAT('t4_',FLOOR(1+RAND()*1000)));

5、各字段解释

5.1、id

select查询的序列号,包含一组数字,表示查询中执行select子句或操作表的顺序。

(1)id相同,执行顺序由上至下

mysql> explain select * from t1,t2,t3 where t1.id=t2.id and t2.id=t3.id;+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+| 1 | SIMPLE| t1 | ALL | PRIMARY | NULL | NULL | NULL | 1 | || 1 | SIMPLE| t2 | eq_ref | PRIMARY | PRIMARY | 4 | jdbc.t1.id | 1 | || 1 | SIMPLE| t3 | eq_ref | PRIMARY | PRIMARY | 4 | jdbc.t1.id | 1 | |+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+3 rows in set

(2)id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行

mysql> explain select t1.id from t1 where t1.id in(select t2.id from t2 where t2.id in(select t3.id from t3 where t3.content=''));+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra|+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+| 1 | PRIMARY | t1 | index | NULL| PRIMARY | 4 | NULL | 1 | Using where; Using index || 2 | DEPENDENT SUBQUERY | t2 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using index; Using where || 3 | DEPENDENT SUBQUERY | t3 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+3 rows in set

(3)有相同,也有不同

mysql> explain select t2.* from t2,(select * from t3) as s3 where s3.id=t2.id;+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+| id | select_type | table| type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+| 1 | PRIMARY| <derived2> | system | NULL| NULL | NULL | NULL | 1 | || 1 | PRIMARY| t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | || 2 | DERIVED| t3 | ALL | NULL| NULL | NULL | NULL | 1 | |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+3 rows in set

id如果相同,可以认为是一组,从上往下顺序执行;

在所有组中,id值越大,优先级越高

越先执行衍生=DERIVED

5.2、select_type

select_type代表查询的类型,主要是用于区别普通查询、联合查询、子查询等的复杂查询。

(1)SIMPLE

SIMPLE代表单表查询

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

(2)PRIMARY

查询中若包含任何复杂的子部分,最外层查询被标记为Primary。

mysql> explain select * from (select t1.content from t1) a;+----+-------------+------------+--------+---------------+------+---------+------+------+-------+| id | select_type | table| type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------+--------+---------------+------+---------+------+------+-------+| 1 | PRIMARY| <derived2> | system | NULL| NULL | NULL | NULL | 1 | || 2 | DERIVED| t1 | ALL | NULL| NULL | NULL | NULL | 1 | |+----+-------------+------------+--------+---------------+------+---------+------+------+-------+2 rows in set

(3)DERIVED

在 FROM 列表中包含的子查询被标记为 DERIVED(衍生),MySQL 会递归执行这些子查询, 把结果放在临时表里。

(4)SUBQUERY

在 SELECT 或 WHERE 列表中包含了子查询。

mysql> explain select t2.id from t2 where t2.id =(select t3.id from t3 where t3.id=1);+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+| 1 | PRIMARY| t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index || 2 | SUBQUERY | t3 | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+2 rows in set

(5)DEPENDENT SUBQUERY

在 SELECT 或 WHERE 列表中包含了子查询,子查询基于外层。

mysql> explain select t2.id from t2 where t2.id in(select t3.id from t3 where t3.content='t3_197');+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra|+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+| 1 | PRIMARY | t2 | index | NULL| PRIMARY | 4 | NULL | 1 | Using where; Using index || 2 | DEPENDENT SUBQUERY | t3 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+2 rows in set

注意:以上两种类型,都是where后面的条件,subquery是单个值,dependent subquery是一组值

(6)UNION

若第二个 SELECT 出现在 UNION 之后,则被标记为 UNION;若 UNION 包含在 FROM 子句的子查询中,外层 SELECT将被标记为:DERIVED。

mysql> explain select t2.id,t2.content from t2-> union all-> select t3.id,t3.content from t3;+------+--------------+------------+------+---------------+------+---------+------+------+-------+| id | select_type | table| type | possible_keys | key | key_len | ref | rows | Extra |+------+--------------+------------+------+---------------+------+---------+------+------+-------+| 1 | PRIMARY| t2 | ALL | NULL| NULL | NULL | NULL | 1 | || 2 | UNION | t3 | ALL | NULL| NULL | NULL | NULL | 1 | || NULL | UNION RESULT | <union1,2> | ALL | NULL| NULL | NULL | NULL | NULL | |+------+--------------+------------+------+---------------+------+---------+------+------+-------+3 rows in set

(7)UNION RESULT

从UNION表获取结果的SELECT

5.3、table

显示这一行的数据是关于哪张表的

5.4、type

type是查询的访问类型。是较为重要的一个指标,结果值从最好到最坏依次是:

system > const> eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>ALL

system > const > eq_ref >ref>range> index >ALL

一般来说,得保证查询至少达到range级别,最好能达到ref。

(1)system

表只有一行记录(等于系统表),这是const类型的特列,平时不会出现,这个也可以忽略不计

(2)const

表示通过索引一次就找到了,const用于比较primary key或者unique索引。因为只匹配一行数据,所以很快

如将主键置于where列表中,MySQL就能将该查询转换为一个常量

mysql> explain select * from (select * from t1 where t1.id=1) s;+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+| id | select_type | table| type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+| 1 | PRIMARY| <derived2> | system | NULL| NULL | NULL | NULL | 1 | || 2 | DERIVED| t1 | const | PRIMARY | PRIMARY | 4 || 1 | |+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+

(3)eq_ref

唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描。

mysql> explain select * from t1,t2 where t1.id=t2.id;+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+| 1 | SIMPLE| t1 | ALL | PRIMARY | NULL | NULL | NULL | 1 | || 1 | SIMPLE| t2 | eq_ref | PRIMARY | PRIMARY | 4 | jdbc.t1.id | 1 | |+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+

(4)ref

非唯一性索引扫描,返回匹配某个单独值的所有行。

本质上也是一种索引访问,他返回所有匹配某个单独值的行,然而,他可能会找到多个符合条件的行,所以他应该属于查找和扫描的混合体。

没用索引前:

mysql> explain select * from t1,t2 where t1.content=t2.content;+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra|+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+| 1 | SIMPLE| t1 | ALL | NULL| NULL | NULL | NULL | 1 | || 1 | SIMPLE| t2 | ALL | NULL| NULL | NULL | NULL | 1 | Using where; Using join buffer |+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+

建立索引后:

mysql> mysql> create index idx_content on t2(content);Query OK, 0 rows affectedRecords: 0 Duplicates: 0 Warnings: 0mysql> explain select * from t1,t2 where t1.content=t2.content;+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+--------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra|+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+--------------------------+| 1 | SIMPLE| t1 | ALL | NULL| NULL | NULL | NULL | 1 ||| 1 | SIMPLE| t2 | ref | idx_content | idx_content | 403| jdbc.t1.content | 1 | Using where; Using index |+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+--------------------------+

(5)range

只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引

一般就是在你的where语句中出现了between、<、>、in等的查询

这种范围扫描索引扫描比全表扫描要好,因为它只需要开始于索引的某一点,而结束于另一点,不用扫描全部索引。

mysql> explain select * from t1 where t1.id <10;+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| 1 | SIMPLE| t1 | range | PRIMARY | PRIMARY | 4 | NULL | 1 | Using where |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+1 row in setmysql> explain select * from t1 where t1.id between 1 and 3;+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| 1 | SIMPLE| t1 | range | PRIMARY | PRIMARY | 4 | NULL | 1 | Using where |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+1 row in setmysql>

(6)index

Full Index Scan,Index与ALL的区别为Index类型只遍历索引树。这通常比ALL快,因为索引文件通常比数据文件小。

也就是说虽然all和index都是读全表,但index是从索引中读取的,而all是从磁盘中读取的。

出现index是sql使用了索引但是没有通过索引进行过滤,一般是使用了覆盖索引或者是利用索引进行了排序分组

mysql> explain select * from t1;+----+-------------+-------+------+---------------+------+---------+------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+------+---------------+------+---------+------+------+-------+| 1 | SIMPLE| t1 | ALL | NULL| NULL | NULL | NULL | 1 | |+----+-------------+-------+------+---------------+------+---------+------+------+-------+1 row in setmysql> mysql> explain select id from t1;+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| 1 | SIMPLE| t1 | index | NULL| PRIMARY | 4 | NULL | 1 | Using index |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+1 row in set

(7)all

Full Table Scan,将遍历全表以找到匹配的行。

mysql> mysql> explain select * from t1,t2 where t1.content=t2.content;+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra|+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+| 1 | SIMPLE| t1 | ALL | NULL| NULL | NULL | NULL | 1 | || 1 | SIMPLE| t2 | ALL | NULL| NULL | NULL | NULL | 1 | Using where; Using join buffer |+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+

说明:一般来说,需要保证查询至少达到range级别,最好达到ref级别。

5.5、possible_keys

显示可能应用在这张表中的索引,一个或者多个

查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用

5.6、key

实际使用的索引。如果为NULL,则没有使用索引。

查询中若使用了覆盖索引,则该索引仅出现在key列表中

5.7、key_len

表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度

在不损失精确性的情况下,长度越短越好。

key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出来的

key_len字段能够帮助检查是否充分的利用上了索引。

key_len越长,说明索引使用的越充分。

key_len如何计算,详见key_len计算方法.

5.8、ref

显示索引的哪一列被使用了,如果可能的话,是一个常数。哪些列或常量被用于查找索引列上的值。

5.9 rows

rows列显示MySQL认为执行查询时必须检查的行数。

越少越好

5.10、Extra

其他的额外重要信息。

Using filesort

说明MySQL会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取。MySQL中无法利用索引完成的排序操作成为文件排序

查询中排序的字段,排序字段若通过索引去访问将大大提高排序速度。

Using temporary

使用了临时表保存中间结果,MySQL在对查询结果排序时使用临时表。

常见于排序order by 和分组查询group by

Using index

Using index表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错。

如果同时出现using where,表明索引被用来执行索引键值的查找;

如果没有同时出现using where,表明索引只是用来读取数据而非利用索引执行查找。

利用索引进行了排序或分组。

覆盖索引(Covering Index)

覆盖索引,一说为索引覆盖。

理解方式一:就是select的数据列只用从索引中就能够取得,不必读取数据行,MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件,换句话说查询列要被所创建的索引覆盖

理解方式二:索引是高效找到行的一个方法,但是一半数据库也能使用索引找到一个列的数据,因此他不必读取整个行。毕竟索引叶子节点存储了他们索引的数据;当能通过读取索引就可以得到想要的数据,那就不需要读取行了。一个索引包含了(或覆盖了)满足查询结果的数据就叫做覆盖索引。

注意:

如果要使用覆盖索引,一定要注意select列表中只取出需要的列,不可select *;因为如果将所有字段一起做索引会导致索引文件过大,查询性能下降

Using where

表明使用了where过滤。

Using join buffer

使用了连接缓存。

impossible where

where子句的值总是false,不能用来获取任何元祖。

select tables optimized away

在没有group by子句的情况下,基于索引优化min/max操作或者对于MyISAM存储引擎优化count(*)操作,不必等到执行阶段在进行计算,查询执行计划生成的阶段即完成优化。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。