ROW_NUMBER分页的注意事项

时间:2022-05-30 20:23:01

  之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据。

  like this:

select * from
(
select row_number() over(order by LogID desc) as rnum,* from B_ShortProtectLog where addtime>='2014-9-1' and addtime<'2014-10-1'
) as tLog where rnum between 1 and 20;

  但是对于大表查询的话如果用ROW_NUMBER这里应该使用ROW_NUMBER的SELECT只查询出主键就可以了,其他的信息取出分页数据的主键之后,连表查询就可以了。

  类似:

with tempLog as
(
select row_number() over(order by LogID desc) as rnum,LogID from B_ShortProtectLog where addtime>='2014-9-1' and addtime<'2014-10-1'
) select tLog.* from tempLog left join B_ShortProtectLog as tLog on tempLog.LogID=tLog.LogID where rnum between 50001 and 50020;