mysql的两种分页偏移量查询写法

MySQL中的limit offset,rows来分段取出每页中需要的数据。但是当数据量足够大的时候,limit条件中的偏移量offset越大就越会导致性能问题,导致查询耗时增加严重。

语法:limit 开始的索引,每页查询的条数

公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数

不同的级别的偏移量查询

-- 发送不同级别偏移量的查询
set profiling=1;
select * from notes limit 100,3;  100:开始的索引  3:偏移量    
select * from notes limit 10000,3;
select * from notes limit 1000000,3;
select * from notes limit 10000000,3;

在大的数据量面前使用where id>offset来代替使用limit offset

-- 同样的效果
select * from notes limit 1000000,3;
 
select * from notes where id>1000000 limit 3;

猜你喜欢

转载自blog.csdn.net/tony_yang6/article/details/108368499