码迷,mamicode.com
首页 > 数据库 > 详细

mysql执行计划分析

时间:2016-08-03 13:24:33      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

本实验employees数据库从https://launchpad.net/test-db/下载

1.id列

若一个select语句中要连接多个数据表,执行计划会根据连接的表数进行显示,并赋予相同的id。下面示例只有一个select语句,但连接多个表,此时id值不会增加,拥有相同id:

root@localhost:mysql3316.sock  11:58:51 [employees]>explain extended select e.emp_no,e.first_name,s.from_date,s.salary from employees e,salaries s where e.emp_no=s.emp_no limit 10;
+----+-------------+-------+------+----------------+---------+---------+--------------------+--------+----------+-------+
| id | select_type | table | type | possible_keys  | key     | key_len | ref                | rows   | filtered | Extra |
+----+-------------+-------+------+----------------+---------+---------+--------------------+--------+----------+-------+
|  1 | SIMPLE      | e     | ALL  | PRIMARY        | NULL    | NULL    | NULL               | 299290 |   100.00 | NULL  |
|  1 | SIMPLE      | s     | ref  | PRIMARY,emp_no | PRIMARY | 4       | employees.e.emp_no |      4 |   100.00 | NULL  |
+----+-------------+-------+------+----------------+---------+---------+--------------------+--------+----------+-------+
2 rows in set, 1 warning (0.00 sec)

相反,下列查询执行计划中,整个查询语句由3个select语句组成,所以执行计划的各记录拥有不同id值:

root@localhost:mysql3316.sock  12:12:35 [employees]>explain select( (select count(*) from employees) + (select count(*) from departments) ) as total_count;
+----+-------------+-------------+-------+---------------+-----------+---------+------+--------+----------------+
| id | select_type | table       | type  | possible_keys | key       | key_len | ref  | rows   | Extra          |
+----+-------------+-------------+-------+---------------+-----------+---------+------+--------+----------------+
|  1 | PRIMARY     | NULL        | NULL  | NULL          | NULL      | NULL    | NULL |   NULL | No tables used |
|  3 | SUBQUERY    | departments | index | NULL          | dept_name | 122     | NULL |      9 | Using index    |
|  2 | SUBQUERY    | employees   | index | NULL          | PRIMARY   | 4       | NULL | 299290 | Using index    |
+----+-------------+-------------+-------+---------------+-----------+---------+------+--------+----------------+
3 rows in set (0.00 sec)

 

mysql执行计划分析

标签:

原文地址:http://www.cnblogs.com/xxmysql/p/5732355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!