2009-04-27 58 views
1

進行解釋其對一個查詢之後:在聯合查詢的MySQL解釋

explain 
select name from t1 where name like '%smthing%' 
UNION ALL 
select name from t2 where name like '%smthing%' 
UNION ALL 
select name from t3 where name like '%smthing%' 
UNION ALL 
select name from t4 where name like '%smthing%' 

通過的4代表的UNION由我得到這樣的:

id select_type table   type possible_keys key  key_len ref   rows Extra     
1 PRIMARY  t1    index (NULL)   name 152  (NULL)  337 Using where; Using index 
2 UNION   t2    index (NULL)   name 152  (NULL) 3842 Using where; Using index 
3 UNION   t3    index (NULL)   name 452  (NULL)  204 Using where; Using index 
4 UNION   t4    index (NULL)   name 452  (NULL) 8269 Using where; Using index 
(NULL) UNION RESULT <union1,2,3,4> ALL  (NULL)   (NULL) (NULL) (NULL) (NULL) 

當聯合的各成分進行說明,類型是「INDEX」,但是聯合結果的類型是「ALL」。 這是什麼原因導致這種行爲? 謝謝

+0

也許你可以粘貼你的查詢,並在問題中解釋計劃的結果? – Avi 2009-04-27 12:36:35

回答

2

嘗試使用UNION ALL而不是UNION - 否則MySQL將嘗試刪除任何重複的行(大概減去表indecies,因爲它正在查看結果集)。

此外,你有UNDER結果的ORDER BY或WHERE子句?再次,這將在結果集上執行,而不是在表級別上執行。