MySQL 深度分页

本文详细探讨了MySQL中的深度分页问题,介绍了默认分页的低效性,索引分页通过利用索引提高查询速度,以及延迟关联通过减少回表次数提升效率。通过测试数据和执行计划分析,展示了不同策略的实际效果。
摘要由CSDN通过智能技术生成


一、深度分页

MySQL 深度分页是指在分页查询数据量比较大的表时,需要访问表中的某一段数据,而这段数据的位置非常靠后,需要通过较大的 offset 来获取目标数据。


二、测试数据

-- 测试表
drop table if exists wide_table;
create table wide_table
(
    id          bigint auto_increment primary key,
    a           varchar(255),
    b           varchar(255),
    c           varchar(255),
    d           varchar(255),
    e           varchar(255),
    f           varchar(255),
    g           varchar(255),
    h           varchar(255),
    i           varchar(255),
    create_time datetime default current_timestamp
);
-- 插入十万条记录
delimiter //

drop procedure if exists insert_data;
create procedure insert_data()
begin
    declare i int default 1;
    while i <= 100000
        do
            insert into wide_table (a, b, c, d, e, f, g, h, i)
            values (i, i, i, i, i, i, i, i, i);
            set i = i + 1;
            if i % 10000 = 0 then
                select i;
            end if;
        end while;
end //

delimiter ;

call insert_data();

三、分页策略

3.1 默认分页

默认分页即通过 limit #{offset}, #{pageSize}limit #{pageSize} offset #{offset} 来进行分页。二者本质上都是全表扫描,MySQL 会依次取出 pageSize 条记录,然后判断其是否在 offset 后,如果不在则舍弃,继续过滤,所以效率低下。

test> select *
      from wide_table
      limit 80000, 100
[2024-01-18 14:55:42] 在 335 ms (execution: 298 ms, fetching: 37 ms) 内检索到从 1 开始的 100 行
test> select *
      from wide_table
      limit 100 offset 80000
[2024-01-18 14:55:43] 在 282 ms (execution: 233 ms, fetching: 49 ms) 内检索到从 1 开始的 100 行
explain
select *
from wide_table
limit 80000, 100;
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+
# |id|select_type|table     |partitions|type|possible_keys|key |key_len|ref |rows |filtered|Extra|
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+
# |1 |SIMPLE     |wide_table|null      |ALL |null         |null|null   |null|99551|100     |null |
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+

explain
select *
from wide_table
limit 100 offset 80000;
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+
# |id|select_type|table     |partitions|type|possible_keys|key |key_len|ref |rows |filtered|Extra|
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+
# |1 |SIMPLE     |wide_table|null      |ALL |null         |null|null   |null|99551|100     |null |
# +--+-----------+----------+----------+----+-------------+----+-------+----+-----+--------+-----+

3.2 索引分页

索引分页即通过索引字段(一般选取主键)的范围查询以及比较操作进行分页,通过应用索引能够大幅缩短查询时间。

test> select *
      from wide_table
      where id between 80000 and 80100
[2024-01-18 15:02:27] 在 224 ms (execution: 184 ms, fetching: 40 ms) 内检索到从 1 开始的 101 行
test> select *
      from wide_table
      where id > 80000
      limit 100
[2024-01-18 14:58:34] 在 218 ms (execution: 185 ms, fetching: 33 ms) 内检索到从 1 开始的 100 行
explain
select *
from wide_table
where id between 80000 and 80100;
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+----+--------+-----------+
# |id|select_type|table     |partitions|type |possible_keys|key    |key_len|ref |rows|filtered|Extra      |
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+----+--------+-----------+
# |1 |SIMPLE     |wide_table|null      |range|PRIMARY      |PRIMARY|8      |null|101 |100     |Using where|
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+----+--------+-----------+

explain
select *
from wide_table
where id > 80000
limit 100;
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+-----+--------+-----------+
# |id|select_type|table     |partitions|type |possible_keys|key    |key_len|ref |rows |filtered|Extra      |
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+-----+--------+-----------+
# |1 |SIMPLE     |wide_table|null      |range|PRIMARY      |PRIMARY|8      |null|39420|100     |Using where|
# +--+-----------+----------+----------+-----+-------------+-------+-------+----+-----+--------+-----------+

3.3 延迟关联

延迟关联主要通过减少回表次数来提高查询效率。

分析下面的执行计划:

  • 第三行执行计划 id 最大最先执行,这是一个对 wide_table 表的索引扫描,只遍历索引而无需回表(可以理解为索引覆盖)。最终查询到了从第 80001 条记录开始的 100 条记录的主键,并将这些主键保存在临时表 wt 中。
  • 第一行执行计划对 wt 进行了全表扫描,获取了主键的查询结果集。
  • 第二行执行计划在 wt 中这 100 条主键记录的基础上,通过等值连接(延迟关联)回表获取到了最终的分页结果。

整个查询过程中只有最终的 100 条记录发生了回表,其余记录都只查询了 id 并被过滤掉了,提高了查询的效率。

test> select *
      from wide_table
      inner join (select id from wide_table limit 80000, 100) as wt
      on wide_table.id = wt.id
[2024-01-18 15:03:36]241 ms (execution: 207 ms, fetching: 34 ms) 内检索到从 1 开始的 100
explain
select *
from wide_table
inner join (select id from wide_table limit 80000, 100) as wt
on wide_table.id = wt.id;
# +--+-----------+----------+----------+------+-------------+-------+-------+-----+-----+--------+-----------+
# |id|select_type|table     |partitions|type  |possible_keys|key    |key_len|ref  |rows |filtered|Extra      |
# +--+-----------+----------+----------+------+-------------+-------+-------+-----+-----+--------+-----------+
# |1 |PRIMARY    |<derived2>|null      |ALL   |null         |null   |null   |null |80100|100     |null       |
# |1 |PRIMARY    |wide_table|null      |eq_ref|PRIMARY      |PRIMARY|8      |wt.id|1    |100     |null       |
# |2 |DERIVED    |wide_table|null      |index |null         |PRIMARY|8      |null |99551|100     |Using index|
# +--+-----------+----------+----------+------+-------------+-------+-------+-----+-----+--------+-----------+
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值