php - MySQL replacement for sum(if(xxx,1,0)) -
hi need optimizing code, takes 38 seconds run sql query, , 23 load view. here's background - redirects table records when member uses link , records go, , when return , status. projects table manages per project information need. have third table keeps per project count updated each time record added redirects table, counts can little unreliable. every hour server runs query fix/verify counts.
is there way count columns without having use sum(if(xxx,1,0)) ?
select projects.id id,cid,name name,state status, sum(if(status="complete",1,0)) complete,cpc, cpc*ss mmkingaku, cpc*sum(if(status="complete",1,0)) total, sum(if(status="screenout",1,0)) screenout, sum(if(status="quotafull",1,0)) quotafull, sum(if(status="short",1,0)) short, sum(if(status="gate",1,0)) gate, sum(if(status null,1,0)) empty, sum(if(status="complete",1,0))/(sum(if(status="complete",1,0))+sum(if(status="screenout",1,0)))*100 ir redirects,projects redirects.rid=projects.rid , state<>"test" group name order cid desc
sql performance not due calculations in select clause. need @ from , group by clauses.
do tables have appropriate indexes? should have index on redirects.rid, projects.rid, or both. in fact, these should composite indexes, including state , test (wherever appropriate).
the group by can performance hog in mysql. how data in each table?
Comments
Post a Comment