数据库的分页方式
数据库的分页方式,优化加快了服务端数据的反馈;这里总结了常见的数据库的分页模式示例:
1、最简单便利的数据库: MYSQL
select * from TABLE limit (curPage-1)*pageSize,pageSize
示例:
(1)select * from Table order by deptno desc limit 3,3; //数据多了,效率低
(2)select * from dept where deptno >10 order by deptno asc limit n;//下一页
select * from dept where deptno <60 order by deptno desc limit n; //上一页
这种方式不管翻多少页只需要扫描n条数据。
2、SQL SERVER 注意版本的区别
示例:
–SQL2012+的OFFSET分页方式
select number
from spt_values
where type='p'
order by number offset 10 rows fetch next 5 rows only;
–SQL2005+的ROW_NUMBER分页方式
select number from (
select number,row_number() over(order by number) as num
from spt_values where type='p'
) t where num between 11 and 15
order by number asc
–SQL2000+的TOP分页方式
select number from (
select top 5 number from (
select top 15 number from spt_values where type='p'
order by number asc
) t
order by number desc
) t
order by number asc
3、ORACLE分页规律总结:每页显示m条数据,查询第n页数据
示例:
select * from (
select rownum r , e. * from Table e where rownum<=m*n
) t
where r>m*n-m ;