以前學習sql server的時候,不知在那兒得知,先在一個很大的table中篩選資料,得到一個暫存的小檔,再用此檔去進行其他運算,如join,效率會比較好。在mysql中,我使用類似的作法,就是先用子查詢,得出一個暫存的table,再進行處理。
EXPLAIN SELECT a . * , b . seq , b . weight AS tWeight , b . loss
FROM batch_detail a
LEFT OUTER JOIN scription_main b ON a . prodSeq = b . seq
WHERE a . accSeq = 1
AND a . bNo = 3
第二個表使用子查詢,可以看到mysql對子查詢用where篩選,先產生一個暫存檔derived2(DERIVED是一個子查詢的型態)。再和table b join。
EXPLAIN SELECT a . * , b . seq , b . weight AS tWeight , b . loss
FROM ( SELECT prodSeq , weight AS totWeight
FROM batch_detail
WHERE accSeq = 1
AND bNo = 3 ) a
LEFT OUTER JOIN scription_main b ON a . prodSeq = b . seq