sqlserver中sql语句分页,借助WITH和ROW_NUMBER()实现时大数据量一个优化方式。
比如原sql:
WITH NoPagedTable AS ( SELECT ROW_NUMBER() OVER (order by a_info_time desc) AS rowIndex, * FROM View_a_info_list where a_info_state=1 ) select * from NoPagedTable WHERE rowIndex>=20 and rowIndex<=39优化:将当前页的最大数用top限定,就是只取截止到当前页码下的最大数量,这样对于大数据量下越是靠近1的页码时速度越快,而且最新的数据的热度要比接近尾页的热度高。
sql语句如下:
WITH NoPagedTable AS ( SELECT ROW_NUMBER() OVER (order by a_info_time desc) AS rowIndex, * FROM (select top 39 * from View_a_info_list where a_info_state=1 order by a_info_time desc ) as tableTemp ) select * from NoPagedTable WHERE rowIndex>=20 and rowIndex<=39第3页时,用top来只取前三页的,比如“top 39”这样在大数据量下在浏览起初的页时就会快很多。
该分页sql在下面的文章中有应用,需要的可以前往阅读: