You'll have to edit this to implement your input parameters for user's filtering and sort options, but the general principle will apply.. I used this technique with SQL 2000 in 2000/2001 timeframe with 90M record table to deliver fast paging for 150-200k row resultsets. Since only the key is in the temp table, it is a very narrow, very small temp tabl;e, and performance is fast, (and it only has to read the main table index, not the table itself, for this step) Then, when actually generating data from main table for actual (smaller) return resultset (only @PageSize rows) the query only has to read a very few records...
Create Procedure GetPagedData
@Page Integer = 1,
@PageSize Integer = 100,
@UsersFilteringCOnditions,
@UsersSortOptions
As
Set NoCount On
Declare @Start Integer,
Declare @End Integer
Declare @NumRecs Integer
-- Declare a temp table variable to hold all the pk values...
Declare @Keys Table (rowNum integer Identity Primary Key NotNull,
keyVal Integer Not Null)
-- Insert all the Primary Keys into the temp table variable...
Insert @keys(keyVal)
Select PrimaryKey From MyMillionRowTable
Where UsersFilterConditionsAreTrue
Order By UsersSortOptions
-- Then, select from your big table only the data
-- from the rows for the page the user wants
Select @NumRecs = Count(*) From Keys
Set @End = @Page * @PageSize
Set @Start = @End + 1 - @PageSize
Select {Insert ColumnListHere}
From MyMillionRowTable T
Join @Keys K On K.KeyVal = T.PrimaryKey
Where K.rowNum Between @Start And @End