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