关于mysql分页查询大数据量offset过大的查询速度变慢

-- 以该查询为例
select * from table where field='' limit n,m

假设该查询走了索引,该查询会查询出前n + 1条数据,根据条件去掉前n条,如果n太大,则会有多次回表操作导致查询效率降低

优化方式

如果单表数据量比较大,可通过减少回表次数提高效率,所以可以对上面的查询语句做下简单的修改

select * from table a inner join (select id from table where where field='' limit n,m) b on  a.id=b.id

如果每页分页数据量不大,也可以用子查询,如果m值太大,不建议使用,可能会导致索引失效

select * from table where id in (select id from (select id from table where where field='' limit n,m) b)

 

posted @ 2020-09-25 11:53  wanglgg  阅读(852)  评论(0编辑  收藏  举报