Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Parameterized View - Index Question

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2003-10-21 : 15:23:24
Here's a Parameterized View


DROP FUNCTION dbo.FN_CourseCredits
GO

CREATE FUNCTION dbo.FN_CourseCredits ( @CourseID INT )
RETURNS Table
AS
RETURN (

SELECT A.* -- Return only the recordset from courserecords
FROM (
SELECT * -- This select gets ALL course records withing a group. A user MIGHT have more than 1
FROM dbo.CourseRecords CR
WHERE CR.CourseID IN (SELECT CourseID FROM dbo.UDF_CreditGroup(@CourseID))
) A
INNER JOIN ( -- Get the earliest course record for each user in this credit group
SELECT UserID, MIN(CRecordFinish) as CRecordFinish
FROM dbo.CourseRecords CR
INNER JOIN dbo.Courses C ON C.CourseID = CR.CourseID
WHERE CR.CRecordFinish > C.CourseStartDate
AND CR.CourseID IN (SELECT CourseID FROM dbo.UDF_CreditGroup(@CourseID))
GROUP BY UserID
) B ON B.UserID = A.UserID AND B.CRecordFinish = A.CRecordFinish

)
GO


My problem is the column 'UserID' is indexed in the original table. Judging by the execution plan, when I used the Parameterized View above, UserID must not be indexed any longer because the execution plan shows a Lazy Spool where UserID is referenced on a JOIN.

Is this a fact of life that I've got to live with or is there a workaround?

Sam

SamC
White Water Yakist

3467 Posts

Posted - 2003-10-21 : 15:55:11
I don't know if I should say oops! or what, but I restructured the query and the lazy spool went away.

Sam
Go to Top of Page
   

- Advertisement -