order by id))
order by id
但这个存储过程有一个致命的缺点,就是它含有not in字样。虽然我可以把它改造为:
select top 页大小 *
from table1
where not exists
(select * from (select top (页大小*页数) * from table1 order by id) b where b.id=a.id )
order by id
即,用not exists来代替not in,但我们前面已经谈过了,二者的执行效率实际上是没有区别的。
既便如此,用top 结合not in的这个方法还是比用游标要来得快一些。
虽然用not exists并不能挽救上个存储过程的效率,但使用sql server中的top关键字却是一个非常明智的选择。因为分页优化的最终目的就是避免产生过大的记录集,而我们在前面也已经提到了top的优势,通过top 即可实现对数据量的控制。