MySql分页查询

⒈使用limit进行分页查询

  例如:

    1.查询前5条记录

1 select * from city LIMIT 5

    2.查询第11条-第25条记录

      11-1 = 10  (索引从0开始)

      25-11+1 = 15 

1 select * from city LIMIT 10,15

    3.每页8条记录,我要看第3页

      (page-1)*size   === (3-1)* 8 = 16

         size  ===   8

1 select * from city LIMIT 16,8

  缺点:全表扫描,速度会很慢 且有的数据库结果集返回不稳定(如某次返回1,2,3,另外的一次返回2,1,3)。limit限制的是从结果集的指定位置处取出n条数据,其余抛弃。

⒉使用索引优化limit速度

  例如:

1 select * from student_score where id >= 10 order by id limit 5

  优点:利用索引扫描速度会快很多,适用于数据量多的情况,order by后最好也是索引列,如果主键是递增的,可以省略order by

 

原文地址:https://www.cnblogs.com/fanqisoft/p/10697250.html