简单的分页查询实现

以前写过N次分页查询,今天新遇到分页查询时,我竟然重新思考了五分钟;所以写个笔记吧
(以下单表测试,连表根据自己需要自由发挥);
开始奥利给:
方法1: ROW_NUMBER()函数(根据我的弱小无助的经验和测试用例,在大量数据时,查询页码靠后时,效率较高,几毫秒)
select top 5 * from 
     (select ROW_NUMBER() over(order by t.id asc)  as numId,t.* from Test t )
      t2 where t2.numId>0

方案2:在测试用例中,查询前几页时,效率却有自己的优势。

 select * from Test t1,
      (
         select top 5 ID from 
         (
           select  top 20 ID,name  from Test order by ID asc
          )t2 order by ID 
       ) t3
       where t1.ID=t3.id  order by t1.ID

实现效果

 


原文地址:https://www.cnblogs.com/yuanshuo/p/13753700.html