我有一個查詢,我試圖運行。現在,我正在使用開始和結束日期參數。大約50秒後,我總共獲得了33排,我覺得這是非常多的時間。當我刪除參數時,返回1582行;這隻需要53秒即可返回。SQL Server查詢需要超過50秒才能執行
我完全喪失了做什麼。我曾嘗試刪除任何多餘的連接(有幾個);這沒有減少查詢返回時間。如果可能的話,能夠在30秒內返回查詢會很好。謝謝。
DECLARE @StartDate DATETIME
SET @StartDate = '2017-04-30 00:00:00.000'
SET @EndDate = '2017-07-31 08:00:00.000'
SELECT
a.PrimaryEventID,
b.Event_Name as 'PrimaryEventName',
b.Event_StartTime as 'EventDate',
e.UserLastName+', '+e.UserFirstName as 'Operator',
f.Activity_MetaDataFieldValue as 'Type',
'Status' = Case
When c.EventStatus = 0 then 'Tent'
When c.EventStatus = 1 then 'Appr'
When c.EventStatus = 2 then 'Pend'
Else 'No Status Found'
End,
CONVERT(varchar(20), a.PrimaryEventID)+'|'+CONVERT(varchar(20), e.UserId)
as 'JoinColumn',
b1.Activity_MetaDataTypeID
FROM
views.mcd_DWH_LinkedEvents a
Left JOIN dwh.mcd_DWH_Events b ON a.PrimaryEventID = b.Event_ID
Left JOIN rawViews.mcd_DWH_ActivitiesBasicData b1 on b.EventActivity_ID = b1.Activity_ID
Left JOIN dwh.mcd_DWH_Events c ON a.LinkedEventID = c.Event_ID
Left JOIN dwh.mcd_FWT_UserEventAssociations_Detailed_Manager d ON a.LinkedEventID = d.Event_ID
Left JOIN views.mcd_FWT_UsersBasicData e ON d.User_ID = e.UserID
Left JOIN views.mcd_FWT_ActivitiesExtraDetails f ON c.EventActivity_ID = f.Activity_ID and f.Activity_MetaDataFieldName = 'Type'
Left JOIN views.mcd_FWT_TrainingProgramsBasicData g ON c.EventTrainingProgram_ID = g.TrainingProgramID
Left JOIN views.mcd_FWT_MPlansBasicData h ON g.TrainingProgram_MPlanID = h.MPlanID
WHERE d.Activity_TypeID = 57
and b.Event_StartTime between @StartDate and @EndDate
and (f.Activity_MetaDataFieldValue = 'Qual' or f.Activity_MetaDataFieldValue = 'Run' or f.Activity_MetaDataFieldValue = 'Assess')
GROUP by a.PrimaryEventID, b.Event_Name,b.Event_StartTime, h.MPlanName, f.Activity_MetaDataFieldValue, e.UserLastName, e.UserFirstName, c.EventStatus, e.UserId, b1.Activity_MetaDataTypeID
你在加入的列上有索引嗎?並且'b.Event_StartTime'也是一個DATETIME字段(即:它是否與您用於查詢的變量的數據類型相同?如果不是,則不會使用該索引) – RToyo
當您查看查詢計劃它建議任何索引?如果我不得不猜測,那麼你可能每個人都需要花費幾秒鐘的時間,這加起來。 – SteveB
在沒有看到執行計劃的情況下,我們無能爲力。 – DavidG