数据量在15万左右,我这个SQL跑的时间比较长,能否优化一下,谢谢。
select TIMESTAMPDIFF(SECOND,a.time,b.time) as online_time,a.time as login_time,b.time as logout_time,a.cid,a.pid from
   (select time,cid,pid from eventstats where event = 'login') as a,
   (select time,cid,pid from eventstats where event = 'logout') as b
   where a.cid = b.cid

解决方案 »

  1.   

    select TIMESTAMPDIFF(SECOND,a.time,b.time) as online_time,a.time as login_time,b.time as logout_time,a.cid,a.pid 
    from eventstats a , eventstats b
    where a.cid = b.cid
    and a.event = 'login'
    and b.event = 'logout'
      

  2.   

    另外检查一下你的索引。建议你可以把 show index from eventstats 贴出来。
      

  3.   

    eventstats 1 i_time 1 time A \N \N \N BTREE