第一示范因为用了 in 所以效率不高
select top 页面大小 * from MyTable where MyTableID not in (select top (页数*页大小) MyTableID
from MyTable order by id_MyTableID) order byMyTableID
再来看第二个分页代码,因为避免了使用 in,效率比之前的要更高效,推荐使用,但反向排序不好使
select top 页面大小 * fromMyTable
where id > ( select isnull (max(id), 0) from (select top (页面大小 *页数)id fromMyTable order by id ) a)
order by id
第三种,SQL2005支持,需要有唯一编号,推荐使用
select top 25 * from (select ROW_NUMBER() over (order by id) as RowNumber, * from Table) t1 where RowNumber > 25 * 页码
.

本文对比了三种改进的分页SQL查询方法,通过优化避免使用in操作符提升效率,推荐使用ROW_NUMBER()函数实现分页,并提供具体示例代码。
730

被折叠的 条评论
为什么被折叠?



