FirstFirst
查询靠前的数据速度最快查询靠前的数据速度最快
//top not in 方式
select top 条数 * from tablename where Id not in (select top 条数*页数 Id from tablename)
(select top (当前的页数-1))x每页显示的记录数 id from topic order by id desc)order by id desc
SecondSecond
查询靠后的数据速度最快查询靠后的数据速度最快
//ROW_NUMBER() OVER()方式
select * from
(
select *, ROW_NUMBER() OVER(Order by Id ) AS RowNumber from tablename
) as b
where RowNumber Between 当前页数-1*条数 and 页数*条数
ThirdThird
速度比较稳定,集合前两者的一些优点速度比较稳定,集合前两者的一些优点
with temptbl as(
SELECT ROW_NUMBER() OVER (ORDER BY id desc)AS 行号, * from table
)
SELECT * FROM temptbl where 行号 between Num1(开始行号) and Num2(结束行号)
FourthFourth
速度最稳定,集合前三者的优点,但是SQL版本为2012及以上才能使用速度最稳定,集合前三者的优点,但是SQL版本为2012及以上才能使用
//offset fetch next方式
//SQL2012以上的版本才支持
select * from tablename order by Id offset 页数 row fetch next 条数 row only