limit 分页优化方法
1.子查询优化法
先找出第一条数据,然后大于等于这条数据的id就是要获取的数据 缺点:数据必须是连续的,可以说不能有where条件,where条件会筛选数据,导致数据失去连续性 实验下 mysql> set profi=1; Query OK, 0 rows affected (0.00 sec) mysql> select count(*) from Member; +———-+ | count(*) | +———-+ | 169566 | +———-+ 1 row in set (0.00 sec) mysql> pager grep !~- PAGER set to ‘grep !~-‘ mysql> select * from Member limit 10, 100; 100 rows in set (0.00 sec) mysql> select * from Member where MemberID >= (select MemberID from Member limit 10,1) limit 100; 100 rows in set (0.00 sec) mysql> select * from Member limit 1000, 100; 100 rows in set (0.01 sec) mysql> select * from Member where MemberID >= (select MemberID from Member limit 1000,1) limit 100; 100 rows in set (0.00 sec) mysql> select * from Member limit 100000, 100; 100 rows in set (0.10 sec) mysql> select * from Member where MemberID >= (select MemberID from Member limit 100000,1) limit 100; 100 rows in set (0.02 sec) mysql> nopager PAGER set to stdout mysql> show profilesG *************************** 1. row *************************** Query_ID: 1 Duration: 0.00003300 Query: select count(*) from Member *************************** 2. row *************************** Query_ID: 2 Duration: 0.00167000 Query: select * from Member limit 10, 100 *************************** 3. row *************************** Query_ID: 3 Duration: 0.00112400 Query: select * from Member where MemberID >= (select MemberID from Member limit 10,1) limit 100 *************************** 4. row *************************** Query_ID: 4 Duration: 0.00263200 Query: select * from Member limit 1000, 100 *************************** 5. row *************************** Query_ID: 5 Duration: 0.00134000 Query: select * from Member where MemberID >= (select MemberID from Member limit 1000,1) limit 100 *************************** 6. row *************************** Query_ID: 6 Duration: 0.09956700 Query: select * from Member limit 100000, 100 *************************** 7. row *************************** Query_ID: 7 Duration: 0.02447700 Query: select * from Member where MemberID >= (select MemberID from Member limit 100000,1) limit 100 从结果中可以得知,当偏移1000以上使用子查询法可以有效的提高性能。 2.倒排表优化法 倒排表法类似建立索引,用一张表来维护页数,然后通过高效的连接得到数据 缺点:只适合数据数固定的情况,数据不能删除,维护页表困难 3.反向查找优化法 当偏移超过一半记录数的时候,先用排序,这样偏移就反转了 缺点:order by优化比较麻烦,要增加索引,索引影响数据的修改效率,并且要知道总记录数 ,偏移大于数据的一半 引用 limit偏移算法: 正向查找: (当前页 – 1) * 页长度 反向查找: 总记录 – 当前页 * 页长度 做下实验,看看性能如何 总记录数:1,628,775 每页记录数: 40 总页数:1,628,775 / 40 = 40720 中间页数:40720 / 2 = 20360 第21000页 正向查找SQL: Sql代码 SELECT * FROM `abc` WHERE `BatchID` = 123 LIMIT 839960, 40 时间:1.8696 秒 反向查找sql: Sql代码 SELECT * FROM `abc` WHERE `BatchID` = 123 ORDER BY InputDate DESC LIMIT 788775, 40 时间:1.8336 秒 第30000页 正向查找SQL: Sql代码 1.SELECT * FROM `abc` WHERE `BatchID` = 123 LIMIT 1199960, 40 SELECT * FROM `abc` WHERE `BatchID` = 123 LIMIT 1199960, 40 时间:2.6493 秒 反向查找sql: Sql代码 1.SELECT * FROM `abc` WHERE `BatchID` = 123 ORDER BY InputDate DESC LIMIT 428775, 40 SELECT * FROM `abc` WHERE `BatchID` = 123 ORDER BY InputDate DESC LIMIT 428775, 40 时间:1.0035 秒 注意,反向查找的结果是是降序desc的,并且InputDate是记录的插入时间,也可以用主键联合索引,但是不方便。 4.limit限制优化法 把limit偏移量限制低于某个数。。超过这个数等于没数据,我记得alibaba的dba说过他们是这样做的