阿里分页优化

分页的传统写法:select * from sbtest1 order by id limit M,N。
它存在的问题点:需要扫描大量无效数据后,再返回请求数据,成本很高。
根据业务需求,有以下三种解决方法:
1)select * from sbtest1 where id > #max_id# order by id limit n;
适用顺序翻页的场景,每次记录上一页#max_id#带入下一次查询中。
2)select * from sbtest1 as a inner join (select id from sbtest1 order by id limit m, n) as b
on a.id = b.id order by a.id;
适用只按照id进行分页,无Where条件。
3)select * from sbtest1 as a inner join (select id from sbtest1where col=xxxx order by id limit m, n) as b
on a.id = b.id order by a.id;
适用于带Where条件,同时按照ID顺序分页。此时,需要在Where条件上创建二级索引。

你可能感兴趣的:(阿里分页优化)