I have a table in my database containing about 100000 records. I have to select 10 records from it by row number from n to n+10. I have used following way to get records-
ALTER PROCEDURE getGalleryImagesByPaging
@startIndex int,
@endIndex int
AS
BEGIN
WITH gallery as(
Select ImageId,Caption, ROW_NUMBER() over (Order by id desc) as RN from imagegalleries where ishome=1 and IsActive=1
)
Select ImageId, Caption From gallery Where RN Between @startIndex AND @endIndex;
END
GO
but I think it is very expensive query because it selects all the elements first then select records by row number. Please suggest me the better way to do it.
dbo.imagegalleriesand list of indexes.