各位帮忙看下,下面的sql能不能优化一下,现在运行这个sql很慢select t.datrpt as 日期,sum(t.intimei) as 累计用户量,sum(i.intimei)  as 新增用户量,sum(a.intactive) as 活跃用户量 
from tb_rpt_hudee_appid_imei_total_day t,tb_base_hudee_app_distribution d,tb_rpt_hudee_appid_imei_day i,tb_rpt_hudee_appid_imei_active_day a
where t.varappid=d.varappid
and i.varappid=d.varappid
and a.varappid=d.varappid
and d.flag='1'
and t.datrpt>=DATE_SUB(CURDATE(),INTERVAL 7 day)
and t.datrpt=i.datrpt
and t.datrpt=a.datrpt
GROUP BY 日期
ORDER BY 日期 desc

解决方案 »

  1.   

    1、说实话,没看懂你这个连接条件的意义,这连一下,那连一下的,还用的where,对可读性造成了极大的不便
    2、如果连接没问题的话,那么就是在连接条件和group by条件上建索引了,其他没什么好优化的
    3、你这date_sub看起来貌似是mysql的,这里是mssql,你走错地方了
      

  2.   

    varappid, datrpt, 日期加索引, 调整一下以下语句的顺序,怎样调整,通过自己观察。where t.varappid=d.varappid
    and i.varappid=d.varappid
    and a.varappid=d.varappid
    and d.flag='1'
    and t.datrpt>=DATE_SUB(CURDATE(),INTERVAL 7 day)
    and t.datrpt=i.datrpt
    and t.datrpt=a.datrpt