您好我需要一些帮助来优化此代码,目前运行 SQL 查询需要 38 秒,将其加载为视图需要 23 秒。这是背景 - 当成员使用链接时重定向表记录并记录他们去哪里、何时返回以及处于何种状态。项目表管理我需要的每个项目信息。目前,我确实有第三个表,它保存每个项目的计数,每次将记录添加到重定向表时都会更新该计数,但是计数可能有点不可靠。服务器每小时运行一次查询以修复/验证计数。
有没有什么好方法来计算列而不必使用 sum(if(xxx,1,0)) ?
Select projects.ID as ID,cid,name as name,state as status,
sum(if(status="complete",1,0)) as complete,cpc,
cpc*ss as mmkingaku,
cpc*sum(if(status="complete",1,0)) as total,
sum(if(status="screenout",1,0)) as screenout,
sum(if(status="quotafull",1,0)) as quotafull,
sum(if(status="short",1,0)) as short,
sum(if(status="gate",1,0)) as gate,
sum(if(status is null,1,0)) as empty,
sum(if(status="complete",1,0))/(sum(if(status="complete",1,0))+sum(if(status="screenout",1,0)))*100 as IR
from redirects,projects
where redirects.rid=projects.rid and state<>"test" group by name order by cid desc