通過了前面的一系列理論,那麼用一個例子去看一下吧。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