MySql in子句 效率低下優化優化
背景:spa
更新一張表中的某些記錄值,更新條件來自另外一張含有200多萬記錄的表,效率極其低下,耗時高達幾分鐘。code
update clear_res set candelete=0 where resid in ( select distinct resourceid from att_attentionresult where important=0 );
耗時 365sblog
優化後io
update clear_res set candelete=0 where resid in ( select resourceid from ( select distinct resourceid from att_attentionresult where important=0 ) as tmp );
耗時 1.41sclass
總結:對於where xxx in 子句效率極其低下問題,通過in的子句外包裝一層select xxx from( ... )as tmp 後,極大優化效率。效率