SQL查询在SSRS报告中花费时间

时间:2015-04-06 07:17:42

标签: sql sql-server ssrs-2008 report reporting

我正在使用SSRS报告并使用其中的数据集。当它以较少的数据执行时,结果以秒为单位显示,但是当它在大量数据上运行时,我的查询中提到的大约需要两分钟。请建议天气有问题查询与否。

SELECT TOP 1000 
  VarianceRequestID,
  vr.Created,
  j.FullName AS Job,  
STUFF
(
    (
        SELECT DISTINCT ',' + v.Name
        FROM (SELECT * FROM VarianceRequestDetail WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) vrd 
        INNER JOIN (SELECT * FROM Vendor WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) v  ON v.VendorID = vrd.VendorID
        WHERE vrd.VarianceRequestID = vr.VarianceRequestID
        FOR XML PATH(''), type
    ).value('.', 'varchar(max)'), 1, 1, ''
) AS Vendors
  ,vReas.Name AS VarianceReasonText
  ,VarianceDescription
  , ExecutiveSummary
  ,e.Name AS CreatedBy
  ,ApprovalCode AS Approved
  ,IsProcessed AS Authorized
  ,ApprovedVarianceTotal
  ,CASE CostAllocationType WHEN 0 THEN 'Unknown' WHEN 1 THEN 'True Variance' WHEN  2 THEN 'Unused Budget' WHEN 3 THEN 'Budget Transfer' WHEN 4 THEN 'Budget Cancellation' WHEN  5 THEN 'Unallocated Budget' END AS CostType

    ,(SELECT TOP 1 e.Name FROM (SELECT * FROM LiveWorkflowItem WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) li  
        INNER JOIN (SELECT * FROM LiveWorkflow WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) l ON l.LiveWorkflowID = li.LiveWorkflowID 
        INNER JOIN (SELECT * FROM Employee WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) e ON e.EmployeeID = li.EmployeeID AND li.ItemStatus in (2,6)
        WHERE l.WorkbookID = vr.WorkbookID ORDER BY li.Code DESC) AS ApprovedBy
    ,(SELECT TOP 1 e.Name FROM (SELECT * FROM LiveWorkflow WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) l 
        INNER JOIN (SELECT * FROM Employee WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) e ON e.EmployeeID = l.AuthorizedBy 
        WHERE l.WorkbookID = vr.WorkbookID) AS AuthorizedBy
      FROM (SELECT * FROM VarianceRequest 
      WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) vr 
      LEFT OUTER JOIN (SELECT * FROM Employee WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) e  ON e.EmployeeID = vr.EmployeeID

      left outer join (SELECT * FROM VarianceReason WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) vReas ON vReas.VarianceReasonID = vr.VarianceReasonID
      LEFT OUTER JOIN (SELECT * FROM Job WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) j  ON j.JobID = vr.JobID
      WHERE VarianceType = 0
        AND (ISNULL(0,0) = 0 OR (ISNULL(0,0) = j.CommunityID))
        AND (ISNULL(0,0) = 0 OR (ISNULL(0,0) = j.JobID))
        AND (ISNULL(NULL,0) = 0 OR (ISNULL(NULL,0) = vr.EmployeeID))
        AND (ISNULL(NULL,0) = 0 OR EXISTS(SELECT VendorID FROM (SELECT * FROM VarianceRequestDetail WHERE (TenantID = (SELECT TenantID FROM Tenant WHERE (SQLUserID = SUSER_ID('ReportUser_1_37'))))) AS ve WHERE VarianceRequestID = vr.VarianceRequestID AND  (ISNULL(NULL,0) = VendorID)))AND (ISNULL(NULL,0) = 0 OR (ISNULL(NULL,0) = vr.VarianceReasonID))
        AND vr.Created >= COALESCE(NULL, '1/1/1900') AND vr.Created  <= COALESCE(NULL, '1/1/3000')

1 个答案:

答案 0 :(得分:1)

你只是想要一个是或否答案?是的,我认为您的查询存在问题。

where子句中的“(ISNULL(0,0)= 0 OR ..”是什么?这不总是正确的,因此毫无意义吗?

为什么合并中的日期条件为NULL?不确定索引是否将与这种参数一起使用。该日期格式也不好,使用YYYYMMDD确保它始终被正确解释。

至少所有“内部联接+选择*”看起来都很糟糕。使用精确列而不是*,它可能使SQL Server有可能避免键查找。

在查询中包含那么多表可能会导致优化器超时或者只是为了得到一个非常糟糕的计划,因为统计数据很可能非常复杂。

查看I / O返回的统计信息,这应该可以让您了解问题所在+实际计划和计划缓存可能会有所帮助。通过统计I / O和实际计划(不仅仅是它的图片),可以更容易地指出问题所在。

编辑:选择distinct + XML路径也可能导致问题,但如果没有更多信息则无法确定。