当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL explain获取查询指令信息原理及实例

MySQL explain获取查询指令信息原理及实例

2020年06月14日  | 移动技术网IT编程  | 我要评论

沢田莉爱,日本旅游签证时间,赵红霞个人资料

explain用于获取查询执行计划信息,

一、语法

只需要在select前加上explain即可,如:

mysql> explain select 1;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra     |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 1 | simple   | null | null    | null | null     | null | null  | null | null |   null | no tables used |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+

二、explain中的信息

1、id:表示select所属的行。id越大,则执行顺序越高,id相同时,从上到下执行。

2、select_type:显示对应行是简单还是复杂查询

1)simple:简单查询,意味着不包含子查询和union

2)subquery:表示是个子查询

3)derived:用来表示包含在form子句的子查询中的select

4)union:

3、table:表示访问哪个表

4、partitions:访问分区

5、type:关联类型,就是如何查找表中的行。

1)all:全表扫描。为了查找数据必须从头带尾的扫描所有的数据(limit关键字不会扫描所有数据)

2)index:索引扫描。这个跟全表扫描一样,只是扫描表时按索引次序而不是行,主要优点是避免了排序,最大缺点是承担按索引次序读取整个表的开销。

3)range:范围扫描。就是一个有限制的索引扫描,它开始于索引的某一点,不用遍历全部索引。

4)ref:索引访问。它返回所有匹配某个单值的行。只有使用非唯一性所有或者唯一性所有的非唯一性前缀时才会发生。

5)eq_ref:使用这个索引查找,最多返回一条记录,如果主键索引和唯一性索引。

6)const,system:当mysql能对查询的某一部分进行优化并将其转换成一个常量时,就会使用这些访问类型

6、possible_keys:显示查询可以用哪些所有

7、key:mysql决定采用哪个索引来优化这个该表的访问,如果这个索引没有出现在possible_keys中,它可能选择了一个覆盖索引,如果没有使用索引,这个值为null

8 、key_len:索引的字节数,越短越好。一般来说key_len等于索引列字段类型长度,如int是4字节,bigint是8字节,date是3个字节,datetime是8个字节;如果索引列是字符串类型,则需要考虑他的字符集,utf8每个字符占3个字段,可变类型(varchar)额外需要2个字节;如果索引列可为空,则额外需要1个字段。

9、ref:

10、rows:mysql估计为了找到所需的行而要读取的行

11、filtered:返回结果的行数占读取行数的百分比(估算),值越大越好

12、extra:显示不适合其他列但也重要的信息,常见得值有:

1)using index:表示使用覆盖索引,以避免访问表

2)using where:mysql服务器将在存储引擎检索行后再进行过滤

3)using temporary:表示mysql对查询结果排序时会使用一个临时表。

三、示例

示例1:

mysql> explain select * from bd_dept;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | simple   | bd_dept | null    | all | null     | null | null  | null |  3 |  100.00 | null |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

可以看出该语句进行了全表扫描,没有用到索引

示例2:

mysql> explain select * from bd_dept where id=1;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key   | key_len | ref  | rows | filtered | extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | simple   | bd_dept | null    | const | primary    | primary | 4    | const |  1 |  100.00 | null |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+

该语句用到了主键索引,只需扫描一条记录就可以得到结果,int类型占4个字节,所以ken_len=4。

示例3:

mysql> explain select * from bd_dept where dept_code='01';
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key    | key_len | ref  | rows | filtered | extra |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
| 1 | simple   | bd_dept | null    | const | dept_code   | dept_code | 32   | const |  1 |  100.00 | null |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+

dept_code是一个唯一性索引字段,字段类型为varchar(10),不为空,所以索引长度为10*3+2=33。

示例4:

mysql> explain select * from bd_dept where create_date>'2020-04-29';
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | extra         |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| 1 | simple   | bd_dept | null    | range | create_date  | create_date | 4    | null |  1 |  100.00 | using index condition |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+

create_date是date类型,普通索引字段,可为空,查询条件为大于,所以关联类型为range,索引长度为3+1=4。

示例5:

mysql> explain select a.id, a.dept_name, b.dept_name parent_name from bd_dept a inner join bd_dept b on a.id=b.parent_id;
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref         | rows | filtered | extra    |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------------+
| 1 | simple   | b   | null    | all  | null     | null  | null  | null         |  3 |  100.00 | using where |
| 1 | simple   | a   | null    | eq_ref | primary    | primary | 4    | zhi_test.b.parent_id |  1 |  100.00 | null    |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------------+

可以看出mysql先执行一个全表扫描,再通过主键进行关联

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网