Ok so I'm just wondering if I could get some opinions on this method of paging search results ... ignore the actual search part I just used it for testing to see if it works ... how would you guys rank this on performance wise? I'm looking to use a non-dynamic sql version of paging ... i've heard people say you can do this with temp tables ... well anything you guys might change to boost performance or anything you might want to note about possible down falls of this method?DECLARE @RowsPerPage INT, @Pages INT, @Page INTDECLARE @StartRow INT, @EndRow INTDECLARE @Rows TABLE (RowID INT NOT NULL IDENTITY(1,1), PKID INT)SET @RowsPerPage = 20SET @Pages = NULLSET @Page = 5SET @StartRow = @RowsPerPage * (@Page - 1) + 1SET @EndRow = @StartRow + @RowsPerPage - 1INSERT INTO @Rows (PKID) SELECT ProjectID FROM Projects WHERE Title LIKE 'WWW:%' ORDER BY TitleSELECT @Pages = (COUNT(*) / @RowsPerPage) + 1 FROM @RowsSELECT ProjectID, Title FROM Projects INNER JOIN @Rows ON ProjectID = PKID AND RowID BETWEEN @StartRow AND @EndRow ORDER BY TitleSELECT @Pages AS Pages, @StartRow AS StartRow, @EndRow AS EndRow
Here's the execution plan I'm getting for the Page count and the actual displaying of the rows...quote:
SELECT @Pages = (COUNT(*) / @RowsPerPage) + 1 FROM @Rows |--Compute Scalar(DEFINE:([Expr1003]=[Expr1002]/[@RowsPerPage]+1)) |--Compute Scalar(DEFINE:([Expr1002]=Convert([Expr1004]))) |--Stream Aggregate(DEFINE:([Expr1004]=Count(*))) |--Table Scan(OBJECT:(@Rows))SELECT ProjectID, Title FROM Projects INNER JOIN @Rows ON ProjectID = PKID AND RowID BETWEEN @StartRow AND @EndRow ORDER BY Title |--Sort(ORDER BY:([Projects].[Title] ASC)) |--Bookmark Lookup(BOOKMARK:([Bmk1000]), OBJECT:([stars].[dbo].[Projects])) |--Nested Loops(Inner Join, OUTER REFERENCES:(@Rows.[PKID])) |--Table Scan(OBJECT:(@Rows), WHERE:(@Rows.[RowID]>=[@StartRow] AND @Rows.[RowID]<=[@EndRow])) |--Index Seek(OBJECT:([stars].[dbo].[Projects].[PK__Projects__6FE99F9F]), SEEK:([Projects].[ProjectID]=@Rows.[PKID]) ORDERED FORWARD)