MySql in子句 效率低下优化

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 后,极大优化效率。效率

相关文章
相关标签/搜索