SQL query taking a long time depending on the option -


i'm working on stored proc , thing 1 of options take 5 minutes run, of others takes 5 seconds.

declare @rpttype varchar(75) set @rpttype = 'audits'      select clt.[cltnum] [client number] ,clt.[clteng] [engagement] ,clt.[cltname] [client name] , clt.[csplname] [assurance partner], clt.[cmglname], e.[ddeventdesc] [project type], budget.[cbudprojectdesc] [project description], due.[cdtargetamount] [budget], min(wip.[wdate]) [1st date], cast(sum(wip.[wrate]*wip.[whours]) decimal(34,2)) [billable wip], cast( sum(ar.[arprogress])as decimal(34,2)) [progress], cast((sum(wip.[wrate]*wip.[whours]) - sum(ar.[arprogress]))as decimal(34,2)) [net wip],  cast(sum(bucket.[cinvar])as decimal(34,2)) [ar balence], max(inv.[invdate]) [last invoicedate], due.[cddatedelivered] [project otd date]from [sab].[dbo].[wip] wip     join [sab].[dbo].clients clt       on wip.[wcltid] = clt.[id]     join [sab].[dbo].[cltdue] due       on due.[cdid] = wip.[wdue]     join [sab].[dbo].[ddevents] e       on due.[cdeventid] = e.[id]     join [sab].[dbo].[invoice] inv       on wip.[winvnum] = inv.[invnumber]     join [sab].[dbo].[acctsrec] ar       on inv.[invnumber] = ar.[arapplyto]     join [sab].[dbo].[clientbuckets] bucket       on clt.id = bucket.id     join [sab].[dbo].[cltbudget] budget        on clt.id = cbudcltid         (@rpttype = 'audit - payroll' , e.[ddeventdesc] = 'audit - payroll' , ar.[artype] = 1 , (clt.[cmaster]=1 or clt.[cinvindiveng] = 0) ) or           (@rpttype = 'audits' , (e.[id] =   '132' or e.[id] = '133' or e.[id] = '134' or e.[id] = '135' or e.[id] = '139' or e.[id] = '140' or e.[id] = '142' or e.[id] = '178')                                                         , ar.[artype] = 1 , (clt.[cmaster]=1 or clt.[cinvindiveng] = 0)                                                        ) or           (@rpttype = 'reviews & comps' , e.[ddeventdesc] in  ('audit - review', 'audit -comp/disc','audit - comp w/o disc') , ar.[artype] = 1 , (clt.[cmaster]=1 or clt.[cinvindiveng] = 0)) or           (@rpttype = 'assur tax returns' , e.[ddeventdesc] in ('5500','720-pcori','8955-ssa','campaign report','corporate (1120-pol)','lm-1','lm-2','lm-3','lm-4','lm-10','lm-30','non-profit (990)','non-profit (990 ez)','non-profit (990-n)','non-profit (990-t)','schedule c letters','section 104d letter')  , ar.[artype] = 1 , (clt.[cmaster]=1 or clt.[cinvindiveng] = 0) )      group           clt.[cltnum],          clt.[clteng],          clt.[cltname],          clt.[csplname],          e.[ddeventdesc],          budget.[cbudprojectdesc],          due.[cdtargetamount],          due.[cddatedelivered],          due.[cddatereceived],           clt.[cmglname]     having sum(bucket.[cinvar])>0 

'audits' 1 taking long.

presuming issue when @rptype "audits", thing notice different in case query on column "[id]" table [sab].[dbo].[ddevents].

there number of things cause run slower. first thing check whether or not index has been created on column [id]. if no index, why taking long.

another possibility is returning lot more data, taking longer normal.

as side note, instead of excessive use of "or" in part:

and (e.[id] =   '132' or e.[id] = '133' or e.[id] = '134' or e.[id] = '135' or e.[id] = '139' or e.[id] = '140' or e.[id] = '142' or e.[id] = '178') 

you use "in":

and e.[id] in ('132', '133',' 134', '135', '139', '140', '142', '178') 

the difference shouldn't affect performance--but make code cleaner.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -