当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql中explain作用详解

Mysql中explain作用详解

2017年12月07日  | 移动技术网IT编程  | 我要评论
一、mysql的索引 索引(index):帮助mysql高效获取数据的一种数据结构。用于提高查找效率,可以比作字典。可以简单理解为排好序的快速查找的数据结构。

一、mysql的索引

索引(index):帮助mysql高效获取数据的一种数据结构。用于提高查找效率,可以比作字典。可以简单理解为排好序的快速查找的数据结构。

索引的作用:便于查询和排序(所以添加索引会影响where 语句与 order by 排序语句)。

在数据之外,数据库还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用数据。这样就可以在这些数据结构上实现高级查找算法。这些数据结构就是索引。

索引本身也很大,不可能全部存储在内存中,所以索引往往以索引文件的形式存储在磁盘上。

我们平时所说的索引,如果没有特别指明,一般都是b树索引。(聚集索引、复合索引、前缀索引、唯一索引默认都是b+树索引),除了b树索引还有哈希索引。

优点:

a、提高数据检索效率,降低数据库的io成本
b、通过索引列对数据进行排序,降低了数据排序成本,降低了cpu的消耗。

缺点:

a、索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录,所以索引也是占用空间的。
b、对表进行insert、update、delete操作时,mysql不仅会更新数据,还要保存一下索引文件每次更新添加了索引列字段的相应信息。

在实际的生产环境中我们需要逐步分析,优化建立最优的索引,并要优化我们的查询条件。

索引的分类:

1、单值索引 一个索引只包含一个字段,一个表可以有多个单列索引。
2、唯一索引 索引列的值必须唯一,但允许有空值。
3、复合索引 一个索引包含多个列

一张表建议建立5个之内的索引

语法:

1、create [unique] index indexname on mytable (columnname(length));
2、alter mytable add [unique] index [indexname] on (columnname(length));

删除:drop index [indexname] on mytable;

查看: show index from table_name\g;

二、explain 的作用

explain :模拟mysql优化器是如何执行sql查询语句的,从而知道mysql是如何处理你的sql语句的。分析你的查询语句或是表结构的性能瓶颈。

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 |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+

(一)id列:

(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

(二)select_type列:数据读取操作的操作类型

  1、simple:简单的select 查询,sql中不包含子查询或者union。
  2、primary:查询中包含复杂的子查询部分,最外层查询被标记为primary
  3、subquery:在select 或者where 列表中包含了子查询
  4、derived:在from列表中包含的子查询会被标记为derived(衍生表),mysql会递归执行这些子查询,把结果集放到零时表中。
  5、union:如果第二个select 出现在union之后,则被标记位union;如果union包含在from子句的子查询中,则外层select 将被标记为derived
  6、union result:从union表获取结果的select

(三)table列:该行数据是关于哪张表

(四)type列:访问类型  由好到差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 、< 、> 、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 |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
1 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 |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
1 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 |
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
1 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:

总结

以上就是本文关于mysql中explain作用详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:mysql子查询和嵌套查询优化实例解析几个比较重要的mysql变量oracle sql语句优化技术要点解析等,如有不足之处,欢迎留言指出,小编会及时回复大家并进行改正。感谢朋友们对本站的支持!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网