There are already many paging query methods, and I am also a member here.
SQL Server has a set rowcount setting, which means that the command processing stops processing the command after responding to the specified number of rows, we can use it to implement high-performance paging query in a 10 million row-level data table. Let's talk about the implementation method:
1. Assume that there is an indexed primary key field ID (integer type) in the table. We will retrieve data by PAGE based on this field.
2. Place the page size in @ pagesize.
3. The current page number is stored in @ currentpage.
4. How can we make the record pointer quickly scroll to the row starting with the data we want to fetch? This is the key! With set rowcount, we can easily implement it.
5. If we successfully scroll the record pointer to the row starting with the data we want to fetch, and then record the value of the Record ID field in that row, then, using Top and conditions, we can easily get the data of a specified page. Of course, with set rowcount, do we still use top?
Let's see how set rowcount helps us:
Declare @ ID Int
Declare @ Moverecords Int
-- @ currentpage and @ pagesize are input parameters
set @ moverecords = @ currentpage * @ pagesize + 1
--The following two rows are used to quickly scroll to the row of the data we want to fetch and record the ID.
Set Rowcount @ Moverecords
Select @ ID=IDFromTable1Order ByID
Set Rowcount @ Pagesize
-- Most hate to reduce the trouble *, but here for convenience, temporarily use
Select * From Table1 Where ID > = @ ID Order By ID
Set Rowcount 0
You can try it out. In a table with 100th million records, you can flip pages to 100 pages (entries per page) to see how fast it is!