MySQL查询语句性能分析

性能分析

通过Explain进行性能分析

Explain是什么

查看执行计划

Explain的作用

  • 查看表的读取顺序
  • 查看数据读取操作的操作类型
  • 查看可能使用到的索引
  • 查看实际情况x天哪些索引被使用
  • 查看表之间的引用
  • 查看每张表有多少行数据被查询

Explain使用方法

通过在sql语句最前方加上关键字explain,如:

mysql> explain select * from test03;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | test03 | ALL  | NULL          | NULL | NULL    | NULL |    5 |       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.09 sec)

执行计划包含的信息

image-20211019191534386

字段解释

id

主要反映表的加载顺序

1.id相同时

执行顺序由上到下

mysql> explain select * from tbl_emp e,tbl_dept d where e.deptId = d.id;
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
| id | select_type | table | type | possible_keys | key        | key_len | ref          | rows | Extra       |
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
|  1 | SIMPLE      | d     | ALL  | PRIMARY       | NULL       | NULL    | NULL         |    5 |             |
|  1 | SIMPLE      | e     | ref  | fk_dept_id    | fk_dept_id | 5       | deptemp.d.id |    1 | Using where |
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
2 rows in set (0.00 sec)

所以先查询表d后查询表e

2.id不同时

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

mysql> explain select tbl_emp.* from tbl_emp where deptId in (select id from tbl_dept);
+----+--------------------+----------+-----------------+---------------+---------+---------+------+------+-------------+
| id | select_type        | table    | type            | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+--------------------+----------+-----------------+---------------+---------+---------+------+------+-------------+
|  1 | PRIMARY            | tbl_emp  | ALL             | NULL          | NULL    | NULL    | NULL |    8 | Using where |
|  2 | DEPENDENT SUBQUERY | tbl_dept | unique_subquery | PRIMARY       | PRIMARY | 4       | func |    1 | Using index |
+----+--------------------+----------+-----------------+---------------+---------+---------+------+------+-------------+
2 rows in set (0.00 sec)

所以先查询tbl_dept表后查询tbl_emp表

3.id相同和不同同时存在时

先执行id大的SQL语句,id相同的话从上到下执行。

select_type

(1) SIMPLE(简单SELECT,不使用UNION或子查询等)

(2) PRIMARY(子查询中最外层查询,查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY)

(3) UNION(UNION中的第二个或后面的SELECT语句)

(4) DEPENDENT UNION(UNION中的第二个或后面的SELECT语句,取决于外面的查询)

(5) UNION RESULT(UNION的结果,union语句中第二个select开始后面所有select)

(6) SUBQUERY(子查询中的第一个SELECT,结果不依赖于外部查询)

(7) DEPENDENT SUBQUERY(子查询中的第一个SELECT,依赖于外部查询)

(8) DERIVED(派生表的SELECT, FROM子句的子查询)

(9) UNCACHEABLE SUBQUERY(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)

table

即表名

type

访问类型

常用的类型有: ALL、index、range、 ref、eq_ref、const、system、NULL(从左到右,性能从差到好)

  • NULL: MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。

  • system :表只有一行记录,等于系统表,这是const类型的特例,基本不会出现。

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

  • eq_ref:唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。

  • ref:非唯一性索引扫描,返回匹配某个单独值的所有行。(通常将type优化成ref就很好了)

  • range:只检索给定范围的行,使用一个索引来选择行.

  • index: Full Index Scan,index与ALL区别为index类型只遍历索引树.

  • all:全表扫描。(尽量避免出现)

system

单表且一行记录

const
mysql> explain select * from tbl_emp where id = 1;
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | tbl_emp | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
eq_ref

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

ref

表示用到了索引,查到多条记录

name = 'zhangsan’的有多条记录

mysql> explain select * from t1 where name = 'zhangsan';
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | t1    | ref  | idx_t1_name   | idx_t1_name | 63      | const |    2 | Using where |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
range

查询条件是一个范围

mysql> explain select * from t1 where id between 1 and 2;
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key       | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
|  1 | SIMPLE      | t1    | range | idx_t1_id     | idx_t1_id | 5       | NULL |    2 | Using where |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
1 row in set (0.00 sec)
index

查询的id刚好是索引

mysql> explain select id from t1;
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key       | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
|  1 | SIMPLE      | t1    | index | NULL          | idx_t1_id | 5       | NULL |    3 | Using index |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
1 row in set (0.00 sec)
all

全表扫描

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 |    3 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
possible_keys

理论上要使用的索引

key

实际使用的索引,没有使用到索引时显示null

在查询中,如果使用了覆盖索引,name这个索引只出现在key列表中而不出现在possible_keys中

覆盖索引(就是select的数据列只用从索引中就能够取得,不必从数据表中读取)

例如下方的查询,查询的条件id刚好是一个索引。

mysql> explain select id from t1;
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key       | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
|  1 | SIMPLE      | t1    | index | NULL          | idx_t1_id | 5       | NULL |    3 | Using index |
+----+-------------+-------+-------+---------------+-----------+---------+------+------+-------------+
1 row in set (0.00 sec)
key_len

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

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

ref

根据where查看哪些列或者常量被使用了

-- where后条件是一个常量,所以ref为const
mysql> explain select * from tbl_emp e where e.deptId = 1;
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | e     | ref  | fk_dept_id    | fk_dept_id | 5       | const |    3 | Using where |
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
-- where后查询条件是d.id,所以ref为deptemp.d.id(其中deptemp为数据库名,d为表名,id是字段名)
mysql> explain select * from tbl_emp e,tbl_dept d where e.deptId = d.id ;
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
| id | select_type | table | type | possible_keys | key        | key_len | ref          | rows | Extra       |
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
|  1 | SIMPLE      | d     | ALL  | PRIMARY       | NULL       | NULL    | NULL         |    5 |             |
|  1 | SIMPLE      | e     | ref  | fk_dept_id    | fk_dept_id | 5       | deptemp.d.id |    1 | Using where |
+----+-------------+-------+------+---------------+------------+---------+--------------+------+-------------+
2 rows in set (0.00 sec)
rows

显示每张表有多少行被优化器查询

-- 说明t1表一共有3行数据
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 |    3 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
Extra

该列包含MySQL解决查询的详细信息,有以下几种情况:

  • Using index:表示相应的查询操作使用了覆盖索引,避免访问了表的数据行,效率很好。如果同时出现了Using where,表明索引被用来执行索引键值的查找;如果没有出现Using where,表明索引用来读取数据而非执行查找操作。

  • Using temporary:表示MySQL需要使用临时表来存储结果集,常见于排序和分组查询,常见 group by ; order by

  • Using filesort:当Query中包含 order by 操作,而且无法利用索引完成的排序操作称为“文件排序”

  • Using where:不用读取表中所有信息,仅通过索引就可以获取所需数据,这发生在对表的全部的请求列都是同一个索引的部分的时候,表示mysql服务器将在存储引擎检索行后再进行过滤

  • Using join buffer:改值强调了在获取连接条件时没有使用索引,并且需要连接缓冲区来存储中间结果。如果出现了这个值,那应该注意,根据查询的具体情况可能需要添加索引来改进能。

  • Impossible where:这个值强调了where语句会导致没有符合条件的行(通过收集统计信息不可能存在结果)。

  • Select tables optimized away:这个值意味着仅通过使用索引,优化器可能仅从聚合函数结果中返回一行

  • No tables used:Query语句中使用from dual 或不含任何from子句

Using filesort

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

mysql> explain select * from article where category_id = 1 order by title;
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+-----------------------------+
| id | select_type | table   | type | possible_keys   | key             | key_len | ref   | rows | Extra                       |
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+-----------------------------+
|  1 | SIMPLE      | article | ref  | idx_article_cvt | idx_article_cvt | 4       | const |    2 | Using where; Using filesort |
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+-----------------------------+
1 row in set (0.00 sec)

由于索引包含三个字段category_id,views和title,查询时跳过了views,mysql就会产生一次文件排序,降低查询性能。

Using temporary
mysql> explain select * from article where category_id = 1 group by title;
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+----------------------------------------------+
| id | select_type | table   | type | possible_keys   | key             | key_len | ref   | rows | Extra                                        |
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+----------------------------------------------+
|  1 | SIMPLE      | article | ref  | idx_article_cvt | idx_article_cvt | 4       | const |    2 | Using where; Using temporary; Using filesort |
+----+-------------+---------+------+-----------------+-----------------+---------+-------+------+----------------------------------------------+
1 row in set (0.00 sec)

使用group by 要按索引顺序,不然会产生临时表,降低性能。

Using index

表示效率很好,因为只执行了索引键值的查找。

mysql> explain select views from article ;
+----+-------------+---------+-------+---------------+-----------------+---------+------+------+-------------+
| id | select_type | table   | type  | possible_keys | key             | key_len | ref  | rows | Extra       |
+----+-------------+---------+-------+---------------+-----------------+---------+------+------+-------------+
|  1 | SIMPLE      | article | index | NULL          | idx_article_cvt | 265     | NULL |    3 | Using index |
+----+-------------+---------+-------+---------------+-----------------+---------+------+------+-------------+
1 row in set (0.00 sec)
Using where

表明使用了where过滤

Using join buffer

使用了连接缓存

impossible where

where不起作用,比如 select…where sex = m and sex = f;

select tables optimized

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值