2015-04-06 116 views
0

我使用它SSRS報告使用的數據集。當它執行的數據較少時,結果以秒爲單位顯示,但當它運行在大量數據上時,我的查詢中提到的時間大約低於兩分鐘。請提出一個方法天氣有問題在查詢或不。SQL查詢到一次在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

你只是想要一個是或否的答案嗎?是的,我認爲您的查詢存在問題。

什麼where子句中的「(ISNULL(0,0)= 0或..」?是不是總是如此,因此沒有意義?

爲什麼在一個NULL聚結日期條件?不確定索引是否會與這種參數一起使用,日期格式也不好,請使用YYYYMMDD以確保它始終正確解釋。

至少所有的「inner join + select *」看起來都不好。確切列,而不是*,可能會給SQL Server的可能性,以避免鍵查找。

具有在查詢許多表可能會導致優化Ë以免超時或以其他方式得到一個非常糟糕的計劃,因爲統計數據很可能會變得非常複雜。

看什麼統計數據I/O的回報,這應該給你一個想法是哪裏的問題是+實際的規劃和計劃緩存可能會有所幫助。通過統計I/O和實際計劃(不只是它的圖片),指出問題出在哪裏會更容易。

編輯:另外選擇不同的+ XML路徑可以是造成問題,但沒有更多的信息無法確定。

+0

當然,你應該檢查你在那裏的所有選擇(或最好試圖擺脫其中的一部分)的索引使用 – 2015-04-06 07:34:14

+0

而且你也在SQL中重複了很多相同/相似的提取,你可能應該改爲連接 – 2015-04-06 07:37:07