MySql in子句 效率低下優化


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 后,極大優化效率。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM