1. 程式人生 > >MySql in子句 效率低下優化(親測有效,從200秒變1秒)

MySql in子句 效率低下優化(親測有效,從200秒變1秒)

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

 

https://www.cnblogs.com/hdwang/p/4749152.html