在分页遍历全表的程序中,当offset值过大的时候,查询速度很慢。这是因为指定offset值的时候,mysql会去查找记录数为offset+rows, 会导致查找记录数过大。
解决本问题的方案有:
1、采用主键的条件查询where 代替 limit 的 offset 偏移量功能 ,如: where id>offset order by id limit rows, 每页返回记录中最大id值为新offset值;
2、先查询主键索引,使用索引覆盖特性找出每页中id列表,然后子表 join下数据中,返回所需要的列。如:
select * from table_name inner join ( select id from table_name where (user = xxx) limit 10000,10) b