一、简述
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);