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')
當然,你應該檢查你在那裏的所有選擇(或最好試圖擺脫其中的一部分)的索引使用 – 2015-04-06 07:34:14
而且你也在SQL中重複了很多相同/相似的提取,你可能應該改爲連接 – 2015-04-06 07:37:07