通过了前面的一系列理论,那么用一个例子去看一下吧。mysql
EXPLAIN SELECT t3.emp_no,t3.first_name,(select t4.last_name from temployees t4 where t4.emp_no=t3.emp_no) AS last_name from (select t1.emp_no,t1.first_name from temployees t1 where t1.emp_no in (10001,10002)) t3 where t3.emp_no=10002 UNION select t2.emp_no,t2.first_name,t2.last_name from temployees t2 where t2.emp_no=10001
这是一段很诡异的代码,没人会这样写,在此只是做为分析处理。sql
咱们看下结果:code
那么按照咱们前面的分析,来看一下它的执行顺序是啥,暂时就不去看它的一个索引利用率了。blog
在该系列九中有一个结论,当id越大越先执行。索引
那么看一下吧:io
首先是4,加载执行t2。这里咱们能够看出其实mysql运行是的下的代码,而不是union上面的。ast
而后是2,这时候也就是去加载t4,执行子查询,子查询发现了须要表t1,实际上是t3,可是t3是临时表在这里没有显示。select
最后去加载t1进行查询,将结果返回给t4,t4执行完返回结果。nio
最后一步就是合并结果。im