我是SQL Server 2005中执行计划的新手,但这让我感到困惑.
当我运行此代码时......
((SELECT COUNT(DISTINCT StudentID)
FROM vwStudentUnitSchedules AS v
WHERE v.UnitActive = 1
AND v.UnitOutcomeCode IS NULL
AND v.UnitCode = su.UnitCode
AND v.StartDate = su.StartDate
AND v.StudentCampus = st.StudentCampus) - 1) AS ClassSize
Run Code Online (Sandbox Code Playgroud)
为了获得班级规模,它会超时并且一般地运行它,它需要30秒
但是当我用这个微小的修改来运行它时......
((SELECT COUNT(DISTINCT LTRIM(RTRIM(UPPER(StudentID))))
FROM vwStudentUnitSchedules AS v
WHERE v.UnitActive = 1
AND v.UnitOutcomeCode IS NULL
AND v.UnitCode = su.UnitCode
AND v.StartDate = su.StartDate
AND v.StudentCampus = st.StudentCampus) - 1) AS ClassSize
Run Code Online (Sandbox Code Playgroud)
它几乎立即运行.
是因为LTRIM()RTRIM()和UPPER()函数?他们为什么要让事情变得更快?我想这是因为COUNT(DISTINCT是一个按字符从左到右计算的聚合?是StudentID是VARCHAR(10).
谢谢