• Mysql实现分页


    1、Mysql的limit用法

    limit分页公式

    1)limit分页公式:curPage是当前第几页;pageSize是一页多少条记录
    limit (curPage-1)*pageSize,pageSize
    (2)用的地方:sql语句中
    select * from student limit(curPage-1)*pageSize,pageSize;

    总页数公式

    (1)总页数公式:totalRecord是总记录数;pageSize是一页分多少条记录
    int totalPageNum = (totalRecord +pageSize - 1) / pageSize;
    (2)用的地方:前台UI分页插件显示分页码
    (3)查询总条数:totalRecord是总记录数
    SELECT COUNT(*) FROM tablename

    例如:
    select * from orders_history where type=8 limit 100,100;
    select * from orders_history where type=8 limit 1000,100;
    select * from orders_history where type=8 limit 10000,100;
    select * from orders_history where type=8 limit 100000,100;
    select * from orders_history where type=8 limit 1000000,100;

    但是这种查询比较慢,因为:limit 200000,200,需要扫描200200行,如果在一个高并发的应用里,每次查询需要扫描超过20W行,效率十分低下。

    limit m语句

     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条数据。


    3、方法2 虽然扫描的数据量少了,但是在某些需要跳转到多少也得时候就无法实现,这时还是需要用到方法1,既然不能避免,那么我们可以考虑尽量减小m的值,因此我们可以给这条语句加上一个条件限制。是的每次扫描不用从第一条开始。这样就能尽量减少扫描的数据量。

    例如:每页10条数据,当前是第10页,当前条目ID的最大值是109,最小值是100.(当前100-109)
    那么跳到第9页:
    select * from dept where deptno<100 order by deptno desc limit 0,10;   //倒序
    
    那么跳到第8页:
    select * from dept where deptno<100 order by deptno desc limit 10,10;
    
    那么跳到第11页:
    select * from dept where deptno>109 order by deptno asc limit 0,10;


    最后附上参考文档网址:http://www.open-open.com/doc/view/2bda32bf64864e8e965e91686f5309d4
    如有错误,敬请指正,在此提前表示感谢!!!

  • 相关阅读:
    I4-6 Sports and Extreme Sports Teacher:Lamb
    English trip V2-B 24 Biographies Teacher: TAALAN
    git设置下载代理 http or socket5 设置方法
    The "Go" Learning Trip -- 0. Base build
    English trip V2-B 23 Making a Good Impression Teacher: GABRIELE
    English trip V2-B 22 Tell a Story Teacher: Russell
    English trip V2-B 21 Personal History Teacher: Russell
    English trip EM2- PE 5A COMMUNICATION 交流 Teacher:Corrine
    Phonics 自然拼读法 ou ow oi oy au aw oo oo ea Teacher:Lamb
    beta分布 java代码
  • 原文地址:https://www.cnblogs.com/cxy2020/p/12866983.html
Copyright © 2020-2023  润新知