select*from ( select*,ROW_NUMBER() OVER (ORDERBY id) as rank from dbo.[User] ) as t where t.rank between1800000and1800010 -- 或者 t.rank >1800000 and t.rank <=1800010
用时2106ms;
** 用法2:**
使用Top 优化,TOP函数让子查询每次仅返回必要的结果。
1 2 3 4 5 6 7
SELECT*FROM ( SELECT TOP (180001*10) ROW_NUMBER() OVER (ORDERBY id) AS RowNum, * FROM dbo.[User] WHERE1=1 ) AS tempTable WHERE RowNum >1800000 ORDERBY RowNum
推荐
OFFSET FETCH子句分页
在SQL Server 2012 及以后的版本中终于出现了类似MySQL中LIMIT的写法了,那就是 OFFSET-FETCH 。
1 2 3 4 5 6 7 8 9
DECLARE@pageIndexINT DECLARE@pageSizeINT
SET@pageIndex=180001 SET@pageSize=10
SELECT*FROM dbo.[User] ORDERBY id OFFSET ( @pageSize* ( @pageIndex-1 )) ROWS FETCH NEXT @pageSizeROWSONLY