作为开发者必须掌握的mysql操作sql语句优化-showprocesslistandexplain(三)

2014-11-24 10:18:28 · 作者: · 浏览: 1
果键是NULL,则长度为NULL。 ref 显示使用哪个列或常数与key一起从表中选择行。 rows 显示MySQL认为它执行查询时必须检查的行数。多行之间的数据相乘可以估算要处理的行数。 filtered 显示了通过条件过滤出的行数的百分比估计值。 Extra

该列包含MySQL解决查询的详细信息

  • Distinct:MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。
  • Not exists:MySQL能够对查询进行LEFT JOIN优化,发现1个匹配LEFT JOIN标准的行后,不再为前面的的行组合在该表内检查更多的行。
  • range checked for each record (index map: #):MySQL没有发现好的可以使用的索引,但发现如果来自前面的表的列值已知,可能部分索引可以使用。
  • Using filesort:MySQL需要额外的一次传递,以找出如何按排序顺序检索行。
  • Using index:从只使用索引树中的信息而不需要进一步搜索读取实际的行来检索表中的列信息。
  • Using temporary:为了解决查询,MySQL需要创建一个临时表来容纳结果。
  • Using where:WHERE 子句用于限制哪一个行匹配下一个表或发送到客户。
  • Using sort_union(...), Using union(...), Using intersect(...):这些函数说明如何为index_merge联接类型合并索引扫描。
  • Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

一.select_type的说明

1.UNION:

当通过union来连接多个查询结果时,第二个之后的select其select_type为UNION。

  1. mysql> explain select * from t_order where order_id=100 union select * from t_order where order_id=200;
  2. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
  5. | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
  6. | 2 | UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
  7. | NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |
  8. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
  9. 3 rows in set (0.34 sec)

2.DEPENDENT UNION与DEPENDENT SUBQUERY:

当union作为子查询时,其中第二个union的select_type就是DEPENDENT UNION。
第一个子查询的select_type则是DEPENDENT SUBQUERY。

  1. mysql> explain select * from t_order where order_id in (select order_id from t_order where order_id=100 union select order_id from t_order where order_id=200);
  2. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
  5. | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | Using where |
  6. | 2 | DEPENDENT SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  7. | 3 | DEPENDENT UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  8. | NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |
  9. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
  10. 4 rows in set (0.03 sec)

3.SUBQUERY:

子查询中的第一个select其select_type为SUBQUERY。

  1. mysql> explain select * from t_order where order_id=(select order_id from t_order where order_id=100);
  2. +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
  5. | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
  6. | 2 | SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
  7. +----+-------------+---------+-------+------