常见的三种SQL分页方式



 
  1.  --top not in方式  
  2. select top 条数 *  from tablename  
  3. where Id not in (select top 条数*页数  Id from tablename)  
  4.   
  5.   
  6.   
  7. --ROW_NUMBER() OVER()方式   
  8.  select * from (   
  9.     select *, ROW_NUMBER() OVER(Order by Id ) AS RowNumber from tablename  
  10.   ) as b  
  11.   where RowNumber BETWEEN 当前页数-1*条数 and 页数*条数     
  12.   
  13.   
  14.   
  15. --offset fetch next方式  
  16. --SQL2012以上的版本才支持  
  17. select * from tablename  
  18.  order by Id offset 页数 row fetch next 条数 row only   



 

 

分析:在数据量较大时

top not in方式:查询靠前的数据速度较快

ROW_NUMBER() OVER()方式:查询靠后的数据速度比上一种较快

offset fetch next方式:速度稳定,优于前2种,但sql版本限制2012及以上才可使用

posted @ 2017-06-20 13:13  仰望-星空  阅读(210)  评论(0编辑  收藏  举报