文章目录
解决mysql的超大分页
-
减少load的数据,或者加快load数据的速度。使用子查询、索引覆盖减少加载的数据量。
select * from table where age > 20 limit 1000000,10 -- 加载1000000条数据只取10条,速度很慢 select * from table where id in (select id from table where age > 20 limit 1000000,10) -- 虽然load了一百万条数据但使用了索引覆盖,要查询的所有字段都在索引中,速度快。 -- 假如是无条件并且id是连续的可以采用如下方法 select * from table where id >1000000 limit 10
-
从需求的角度减少这种请求。
-
使用内连接进行延迟关联
select a.* from t5 a inner join (select id from t5 order by text limit 1000000, 10) b on a.id=b.id; -- 先在索引列上完成分页操作,在获取需要的列。
-
记录上次查询结束的位置(避免从头查,抛弃大量的数据)