sqlserver performs pagination query

Three kinds of paging query statements in SqlServer
  Let’s talk about it first, there are two places for sorting the query data (1, sorting before paging. 2, sorting after querying the current page data)

The first one,
  1. First query all data ids before the current page number

select top ((当前页数-1)*每页数据条数) id from 表名

2. Query the first few items of all the data again, but the id is not in the previously found data

 select top 每页数据条数 * from 表名 where id not in ( select top 

((当前页数-1)*每页数据条数) id from 表名 )

3. After querying all the data on the current page, sort according to a column of data

  select * from (

      select top 每页数据条数 * from 表名 where id not in 

(select top ((当前页数-1)*每页数据条数) id from 表名)

    ) as b order by 排序列名 desc

4. Of course, if you want to modify the sorting column, you can also query (the default is to sort by id asc, we can change it to other columns)

 select top 每页数据条数 * from 表名 where id not in (select top ((2-

1)*5) id from wg_users order by 排序列名 desc) order by 排序列名 desc

The sorting column name here must use the same column, otherwise, the pagination query will find duplicate data or less data, because of the disordered sorting

The second type, ROW_NUMBER() pagination
  1. Use the ROW_NUMBER() function to first add a column of serial numbers to all the queried data (that is, add a column of 1, 2, 3, 4, 5 to the data... this, be sure not to remove the one that starts later Alias ​​[I call it b here])

 select * from (select ROW_NUMBER() OVER(Order by id) AS RowNumber,* 

from 表名) as b

2. Then, according to the serial number added before, use mathematics to calculate the current page is the data from x to y

select * from (select ROW_NUMBER() OVER(Order by id) AS RowNumber,* 

from 表名) as b  where b.RowNumber BETWEEN (当前页数-1)*每页数据条数

+1 and 当前页数*每页数据条数order by 排序列名 desc

3. The sort column of this pagination statement is here (the default is to sort by the id column here, you can change it if necessary, you can write [order by id, creatTime, name, …] for multiple sort columns)

 select * from (select ROW_NUMBER() OVER(Order by 排序列名) AS 

RowNumber,* from 表名) as b

The third, the paging method of Offset and Fetch - only applicable to SqlServer2012 and above (Offset and Fetch can be used not only for paging, but also for other functions, but I don't know it, and I just know that it can be used in paging)
  1. Compared with the second type of paging, this kind of paging has advantages in both performance and grammar, but the advantages are not obvious (for reference only, see other people's tests, Mengxin said that he does not know how to compare -_-!!)

 select * from 表名 order by 排序列名 offset (当前页数-1)*每页数据条


数 row fetch next 每页数据条数 row only

2. Reorder the data of the current page (if you don’t need it, you don’t need to add this part)

 select * from (

      select * from 表名 order by 排序列名 offset (当前页数-1)*

每页数据条数 row fetch next 每页数据条数 row only

    ) as b order by 排序列名 desc

Guess you like

Origin blog.csdn.net/qq_37213281/article/details/123704778