I have a T-SQL query (SQL Server 2012) that does the job, but when I look at the execution plan, I see a sort operator with a warning: "Operator used tempdb to spill data during execution with spill level 1."
The reading I have done so far suggests that if I use an "order by" clause I can eliminate this sort iterator. This isn't an option for me, because I can't do a subquery sort and sorting the outermost query not remove the sort iterator.
I added non-clustered indices where previous execution plans suggested I should.
Is there anything else I could to do address this "tempdb spill" warning? I have no more ideas at this point.
Thank you for any ideas.
USE MIA_2014_15_v1;
GO
/*
Notes:
* The outer query exists so that I can filter by a windowed function (Date_Count).
*/
SELECT q.Campus,
q.Student_ID,
q.Student_Name,
q.DATEIN,
q.TIMEIN,
q.[TIMEOUT],
q.Date_Count
FROM (
SELECT TC_Hours.Campus,
TC_Hours.[Student ID] AS Student_ID,
Students.Student_Name,
TC_Hours.[Date] AS DATEIN,
TC_Hours.[Time In] AS TIMEIN,
TC_Hours.[Time Out] AS TIMEOUT,
count(TC_Hours.[Date]) OVER (
PARTITION BY TC_Hours.Campus,
TC_Hours.[Student ID],
TC_Hours.[Date]
) AS Date_Count
FROM dbo.TC_Hours_District TC_Hours
LEFT JOIN dbo.Base__Student_Name_by_FY Students ON TC_Hours.Campus = Students.Campus
AND TC_Hours.[Student ID] = Students.Student_ID
WHERE (NOT students.Student_Name IS NULL)
AND Students.FY = 'FY15'
) q
WHERE q.Date_Count > 1;
GO