With the addition of parens in the subquery, results were returned immediately in the app.
This runs slow when called by RS2005 in a vb.net / aspx web app:
SELECT
c.TeacherID, u.FName + ' ' + u.lname as Teacher, count(sb.behaviorID) as BxCount,
sb.behaviorID, b.BehaviorName, std.GradeID, gl.GradeLevel
FROM
StudentBehaviors sb
join
Classes c on sb.classid = c.classid
join
StudentDetails std on sb.studentID = std.StudentID and std.RecordIsActive=1
join
users u on c.TeacherID = u.UserID
join
Behaviors b on sb.behaviorID = b.BehaviorID
join
GradeLevels gl on std.GradeID = gl.GradeLevelID
WHERE
sb.classdate between @StartDate and @EndDate
and c.schoolid = @SchoolID
and std.GradeID=@GradeLevel
GROUP BY
c.TeacherID, sb.behaviorID, b.BehaviorName, u.lname, u.FName,
std.GradeID, gl.GradeLevel
ORDER BY
u.LName, sb.behaviorID
This runs fast:
select a.teacherid, a.teacher,a.bxcount, a.behaviorid,a.behaviorname,a.gradeid, a.gradelevel
from (
SELECT c.TeacherID, u.FName + ' ' + u.lname as Teacher, count(sb.behaviorID) as BxCount,
sb.behaviorID, b.BehaviorName, std.GradeID, gl.GradeLevel
FROM StudentBehaviors sb
join Classes c on sb.classid = c.classid
join StudentDetails std on sb.studentID = std.StudentID and std.RecordIsActive=1
join users u on c.TeacherID = u.UserID
join Behaviors b on sb.behaviorID = b.BehaviorID
join GradeLevels gl on std.GradeID = gl.GradeLevelID
WHERE sb.classdate between @StartDate and @EndDate
and c.schoolid = @SchoolID
and std.GradeID=@GradeLevel
group by c.TeacherID, sb.behaviorID, b.BehaviorName, u.lname, u.FName, std.GradeID, gl.GradeLevel
) a
order by a.teacher, a.behaviorid
These run at the same speed in a query windows in SQL Server Management Studio. Why the difference? Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…