本实验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)
时间: 2024-11-05 18:31:11