zoukankan      html  css  js  c++  java
  • mysql 分页

    一、简述

    1、直接使用limit分页,越到后面越慢
    2、数据库的id不是连续的,因此不能使用between and
    3、数据库的索引稍微快一点,但索引的遍历依旧很慢
        select * from tb1 where id in(select id from tb1)

    二、方案

    1、只有上一页和下一页(效率最高)
    a)、关键
        记住这一页的最大值和最小值
    b)、实现
    max # 最大值
    min  # 最小值
    下一页:
    select * from tb1 where id > max limit 10;
    上一页
    select * from tb1 where id < min order by id desc limit 10;
    2、中间有页码
    5, [6], 7, 8, 9
    a)、关键
        页码增量和当前页的最大值和最小值
    b)、实质
    max # 最大值 100
    min # 最小值
    n # 页码增量 3x10
    下第n页:
    select * from test where id in(select foo.id from (select B.id from (select id from test where id>100 limit 30) as B order by B.id desc limit 10) as foo) order by id;
    上第n页:
    select * from test where id in(select foo.id from (select B.id from (select id from test where id<100 order by id desc limit 30) as B order by B.id asc limit 10) as foo);
  • 相关阅读:
    程序崩溃访问非法内存
    C# IP转换,时间转换
    _heap_alloc_dbg 崩溃
    TaskIcon 系统任务栏图标
    zlib 压缩使用
    桌面清理工具
    CMMI知识库(精简版)
    JAVA程序员面试题集合
    OracleDECODE用法
    Oracle索引重建
  • 原文地址:https://www.cnblogs.com/wt7018/p/11116694.html
Copyright © 2011-2022 走看看