Untuk tabel sebesar itu, gunakan Common-Table Expression (CTE) dan ROW_NUMBER; gunakan fungsi kecil untuk menghitung catatan yang akan dikembalikan berdasarkan @PageNumber
dan @PageSize
variabel (atau apa pun yang Anda ingin menyebutnya). Contoh sederhana dari salah satu prosedur tersimpan kami:
-- calculate the record numbers that we need
DECLARE @FirstRow INT, @LastRow INT
SELECT @FirstRow = ((@PageNumber - 1) * @PageSize) + 1,
@LastRow = ((@PageNumber - 1) * @PageSize) + @PageSize
;
WITH CTE AS
(
SELECT [Fields]
, ROW_NUMBER() OVER (ORDER BY [Field] [ASC|DESC]) as RowNumber
FROM [Tables]
WHERE [Conditions, etc]
)
SELECT *
-- get the total records so the web layer can work out
-- how many pages there are
, (SELECT COUNT(*) FROM CTE) AS TotalRecords
FROM CTE
WHERE RowNumber BETWEEN @FirstRow AND @LastRow
ORDER BY RowNumber ASC