关于:Sql Server查询性能

时间:2014-04-23 12:11:30

标签: database-performance

在我的项目中,将所有数据捕获到temp并根据用户请求过滤条件,从临时表中过滤数据并显示给用户。

但是,在将所有数据提取到临时表时,有时需要花费大约28秒才需要1分钟,因为此应用程序变得非常慢并且会导致性能问题。

我包含获取数据并插入临时表的主查询供您参考,请帮助我提高此查询的性能。

SQL查询:

SELECT   
  CP.ClientKey, CP.Client, CP.ProjectKey, CP.Project, CI.ClientMarket,  
  CI.ClientReference, CI.[Filename], CI.UploadedDate, CI.ReceivedDate,   
  CI.OverallStatus, CI.NoOfPages,  
  (SELECT MIN(cp.StartDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL1.Id) AS L1StartDate,   
  (SELECT MAX(cp.EndDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL1.Id) AS L1EndDate,   
  (SELECT SUM(DATEDIFF(mi, cp.StartDatetime, cp.EndDatetime)) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL1.Id) AS L1TimeTaken,   
  ELUL1.FirstName + ' ' + ELUL1.LastName + '_' + ELUL1.UserName AS L1Resource,   
  CL1.Description L1StatusComment, CMIL1.NumberOfDxCode AS L1NumberOfDxCodes,   
  (SELECT MIN(cp.StartDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL2.Id) AS L2StartDate,   
  (SELECT MAX(cp.EndDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL2.Id) AS L2EndDate,   
  (SELECT SUM(DATEDIFF(mi, cp.StartDatetime, cp.EndDatetime)) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL2.Id) AS L2TimeTaken,   
  ELUL2.FirstName + ' ' + ELUL2.LastName + '_' + ELUL2.UserName AS L2Resource,  
  CL2.Description L2StatusComment, CMIL2.NumberOfDxCode AS L2NumberOfDxCodes,  
  (SELECT MIN(cp.StartDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL3.Id) AS L3StartDate,   
  (SELECT MAX(cp.EndDatetime) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL3.Id) AS L3EndDate,   
  (SELECT SUM(DATEDIFF(mi, cp.StartDatetime, cp.EndDatetime)) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL3.Id) AS L3TimeTaken,   
  ELUL3.FirstName + ' ' + ELUL3.LastName + '_' + ELUL3.UserName AS L3Resource,  
  CL3.Description L3StatusComment, CMIL3.NumberOfDxCode AS L3NumberOfDxCodes,  
  CI.Id AS ChartId,  
  (SELECT   
       COUNT(ca.Id)   
   FROM   
       ChartAudit ca   
   INNER JOIN 
       ChartMoreInfo cmi WITH(NOLOCK) ON ca.ChartMoreInfoId = cmi.Id  
   WHERE   
       cmi.ChartId = CI.Id) AS AuditCount,  
  cp.ClientReferenceLabel,  
  CMIL1.Id AS L1ChartMoreInfoId,  
  CMIL2.Id AS L2ChartMoreInfoId,  
  CMIL3.Id AS L3ChartMoreInfoId,  
  (SELECT COUNT(cp.Id) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL1.Id) AS L1ProductionTimeCount,  
  (SELECT COUNT(cp.Id) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL2.Id) AS L2ProductionTimeCount,  
  (SELECT COUNT(cp.Id) FROM ChartProduction cp WHERE cp.ChartMoreInfoId = CMIL3.Id) AS L3ProductionTimeCount,  
  CMIL1.LevelStatusId AS L1StatusId, CMIL2.LevelStatusId AS L2StatusId, CMIL3.LevelStatusId AS L3StatusId,  
  CMIL1.CompletionDate L1CompletionDate, CMIL1.InvalidDate L1InvalidDate, CMIL1.HoldDate L1HolDate,  
  CMIL2.CompletionDate L2CompletionDate, CMIL2.InvalidDate L2InvalidDate, CMIL2.HoldDate L2HolDate,  
  CMIL3.CompletionDate L3CompletionDate, CMIL3.InvalidDate L3InvalidDate, CMIL3.HoldDate L3HolDate  
INTO  
  #RetTable  
FROM   
  ChartInfo CI WITH (NOLOCK)  
INNER JOIN 
  ClientProject CP ON CP.Id = CI.ClientProjectId  
LEFT OUTER JOIN ChartMoreInfo CMIL1 WITH (NOLOCK) ON   
    CMIL1.ChartId = CI.Id AND CMIL1.LevelNumber = 1  
   LEFT OUTER JOIN EmployeeLevelUser ELUL1 ON   
    ELUL1.EmployeeLevelId = CMIL1.LevelUpdatedBy  
   LEFT OUTER JOIN Comments CL1 ON  
    CMIL1.LevelStatusCommentId = CL1.Id  
   LEFT OUTER JOIN ChartMoreInfo CMIL2 WITH (NOLOCK) ON   
    CMIL2.ChartId = CI.Id AND CMIL2.LevelNumber = 2  
   LEFT OUTER JOIN EmployeeLevelUser ELUL2 ON   
    ELUL2.EmployeeLevelId = CMIL2.LevelUpdatedBy  
   LEFT OUTER JOIN Comments CL2 ON  
    CMIL2.LevelStatusCommentId = CL2.Id  
   LEFT OUTER JOIN ChartMoreInfo CMIL3 WITH (NOLOCK) ON   
    CMIL3.ChartId = CI.Id AND CMIL3.LevelNumber = 3  
   LEFT OUTER JOIN EmployeeLevelUser ELUL3 ON   
    ELUL3.EmployeeLevelId = CMIL3.LevelUpdatedBy  
   LEFT OUTER JOIN Comments CL3 ON  
    CMIL3.LevelStatusCommentId = CL3.Id  
 WHERE   
  CI.ClientProjectId = @clientProjectId 

Thanks,
Suresh

0 个答案:

没有答案