update call_record set duration=
case duration
when '-1' then '2'
else duration
end
 where line_name='801' order by `time` desc limit 1这个表有10几W数据, duration,line_name, time 都为索引,现在这个语句耗时10几毫秒,我的系统其它语句都是0.3毫秒,请问怎么优化这个语句?优化mysqlcase when

解决方案 »

  1.   

    alter table  call_record  add index(line_name,time)
      

  2.   

    我的系统现在量不大的情况下, 有这个语句cpu占用10%左右, 注销掉这个语句就降为了1%以内,这能忍?
      

  3.   


    贴出你的
    show index from call_record;
    explain select * from call_record where line_name='801' order by `time` desc limit 1;以供分析。
    理论上创建了 index(line_name,time) 应该就已经非常优化了。
      

  4.   

    目测是order by太耗时,
    我多加了保存每个line_name最大的time,
    这样直接类似 where duration=-1 and line_name='801' and time=max_time 就可以了,耗时0.3毫秒内现在我的系统运行起来了也才1-2%cpu,优化成功!谢谢大家