我有一个我正在尝试运行的查询。现在,我正在使用开始和结束日期参数。大约50秒后,我总共得到33行,我觉得这是一段非常多的时间。删除参数时,返回1582行;这只需要53秒才能返回。
我完全不知道该做什么。我试过取出任何冗余连接(有几个);这没有减少查询返回时间。如果可能的话,能够在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
答案 0 :(得分:1)
首先,让我们扼杀期望与现实。听起来好像你逻辑上假设Where语句会让你的查询花费更少的时间。从逻辑上讲,这似乎是有道理的,这不是SQL Server的运作方式。
的操作顺序SQL Server实际上完成了完整查询然后通过WHERE参数限制它。如果您对此数据库中的表具有管理访问权限,我会考虑至少添加索引:
一个好的经验法则是日期和ID应该被编入索引,一般来说,你可以合理地定位到where语句中的任何字段。