SQL查询需要很长时间,具体取决于选项

时间:2015-04-22 15:29:18

标签: sql

我正在处理这个存储过程,事情就是使用其中一个选项需要5分钟才能运行,但是所有其他选项只需要5秒钟。

DECLARE @rptType varchar(75)
SET @rptType = 'Audits'

    SELECT CLT.[cltNum] AS [Client Number] ,CLT.[cltEng] AS [Engagement] ,CLT.[cltName] AS [Client Name] , CLT.[CSPLname] AS [Assurance Partner], CLT.[cmgLName], e.[DDEventDesc] AS [Project Type], Budget.[CBudProjectDesc] AS [Project Description], DUE.[CDTargetAmount] AS [Budget], MIN(WIP.[wdate]) [1st Date], CAST(SUM(WIP.[wRate]*WIP.[wHours]) AS 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]) AS [Last InvoiceDate], due.[CDDateDelivered] AS [Project OTD Date]From [sab].[dbo].[WIP] AS WIP
    Join [sab].[dbo].Clients AS CLT
      ON WIP.[wcltID] = CLt.[ID]
    Join [sab].[dbo].[cltdue] AS DUE
      ON DUE.[CDID] = WIP.[wDue]
    join [sab].[dbo].[DDEvents] AS E
      ON DUE.[cdEventID] = e.[ID]
    join [sab].[dbo].[Invoice] AS Inv
      ON WIP.[wInvNum] = INV.[invNumber]
    Join [sab].[dbo].[AcctsRec] AS AR
      ON INV.[invNumber] = AR.[ARApplyTo]
    Join [sab].[dbo].[ClientBuckets] AS Bucket
      ON CLT.ID = Bucket.ID
    JOIN [sab].[dbo].[cltBudget] AS Budget 
      ON clt.ID = cBudCltID 



    WHERE (@rptType = 'Audit - Payroll' AND e.[DDEventDesc] = 'Audit - Payroll' AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) ) OR
          (@rptType = 'Audits' 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')
                                                        AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)
                                                       ) OR
          (@rptType = 'Reviews & Comps' AND e.[DDEventDesc] IN  ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc') AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)) OR
          (@rptType = 'Assur Tax Returns' AND 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')  AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) )

    GROUP BY 
         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

'审计'是一个需要很长时间的审计。

2 个答案:

答案 0 :(得分:0)

假设问题是当@rpType是" Audits"时,我注意到的情况与此情况不同的是你在列" [ID]"来自表[sab]。[dbo]。[DDEvents]。

有许多事情可能导致此速度显着降低。要检查的第一件事是是否已在列[ID]上创建索引。如果没有索引,那么这就是它花费这么长时间的原因。

另一种可能性是它只是返回了更多的数据,所以花更长的时间是正常的。

作为旁注,而不是过度使用" OR"在这一部分:

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')

您可以使用" IN":

AND e.[ID] IN ('132', '133',' 134', '135', '139', '140', '142', '178')

差异不应影响性能 - 但它确实使代码更清晰。

答案 1 :(得分:0)

深入研究这一点,我发现有些人比其他人花费更长时间的原因是因为分组。当我将组更改为聚合函数时,它将时间缩短到3秒。

新SQL:

DECLARE @rptType varchar(75)
SET @rptType = 'Audits'

SELECT CLT.[cltNum] AS [Client Number] ,
       CLT.[cltEng] AS [Engagement] ,
       CLT.[cltName] AS [Client Name] , 
       CLT.[CSPLname] AS [Assurance Partner], 
       CLT.[cmgLName] , 
       e.[DDEventDesc] AS [Project Type], 
       Budget.[CBudProjectDesc] AS [Project Description], 
       sum(DUE.[CDTargetAmount]) AS [Budget],
        MIN(WIP.[wdate]) [1st Date], 
        CAST(SUM(WIP.[wRate]*WIP.[wHours]) AS 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]) AS [Last InvoiceDate], 
        Max(due.[CDDateDelivered]) AS [Project OTD Date]

From [sab].[dbo].[WIP] AS WIP
Join [sab].[dbo].Clients AS CLT
  ON WIP.[wcltID] = CLt.[ID]
Join [sab].[dbo].[cltdue] AS DUE
  ON DUE.[CDID] = WIP.[wDue]
join [sab].[dbo].[DDEvents] AS e
  ON DUE.[cdEventID] = e.[ID]
join [sab].[dbo].[Invoice] AS Inv
  ON WIP.[wInvNum] = INV.[invNumber]
Join [sab].[dbo].[AcctsRec] AS AR
  ON INV.[invNumber] = AR.[ARApplyTo]
Join [sab].[dbo].[ClientBuckets] AS Bucket
  ON CLT.ID = Bucket.ID
JOIN [sab].[dbo].[cltBudget] AS Budget 
  ON clt.ID = cBudCltID 



WHERE (@rptType = 'Audit - Payroll' AND e.[DDEventDesc] = 'Audit - Payroll' AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) ) OR
      (@rptType = 'Audits' AND e.[ID] IN (132, 133, 134, 135, 139, 140, 142, 178) AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)) OR
      (@rptType = 'Reviews & Comps' AND e.[DDEventDesc] IN  ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc') AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)) OR
      (@rptType = 'Assur Tax Returns' AND 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')  AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) )

GROUP BY 
     CLT.[cltnum],
     CLT.[clteng],
     CLT.[cltName],
     CLT.[CSPLname],     
     Budget.[CBudProjectDesc],
     e.[DDEventDesc],
     CLT.[CMGLname]
Having sum(Bucket.[cinvar])>0