mysql的分页比较简单,只需要limit offset,length就可以获取数据了,但是当offset和length比较大的时候,mysql明显性能下降
* 1.子查询优化法
先找出第一条数据,然后大于等于这条数据的id就是要获取的数
缺点:数据必须是连续的,可以说不能有where条件,where条件会筛选数据,导致数据失去连续性
实验下
Sql代码
- mysql> set profiling=1
- Query OK, 0 rows affected (0.00 sec
3.4. mysql> select count(*) from Member
- +———-
- | count(*)
- +———-
- | 169566
- +———-
- 1 row in set (0.00 sec
11.12. mysql> pager grep !~
- PAGER set to ‘grep !~-
14.15. mysql> select * from Member limit 10, 100
- 100 rows in set (0.00 sec
17.18. mysql> select * from Member where MemberID >= (select MemberID from Member limit 10,1) limit 100
- 100 rows in set (0.00 sec
20.21. mysql> select * from Member limit 1000, 100
- 100 rows in set (0.01 sec
23.24. mysql> select * from Member where MemberID >= (select MemberID from Member limit 1000,1) limit 100
- 100 rows in set (0.00 sec
26.27. mysql> select * from Member limit 100000, 100
- 100 rows in set (0.10 sec
29.30. mysql> select * from Member where MemberID >= (select MemberID from Member limit 100000,1) limit 100
- 100 rows in set (0.02 sec
32.33. mysql> nopage
- PAGER set to stdou
35.36.37. mysql> show profiles\
- ************************ 1. row ***********************
- Query_ID:
- Duration: 0.0000330
- Query: select count(*) from Membe
42.43. ************************ 2. row ***********************
- Query_ID:
- Duration: 0.0016700
- Query: select * from Member limit 10, 10
- ************************ 3. row ***********************
- Query_ID:
- Duration: 0.0011240
- Query: select * from Member where MemberID >= (select MemberID from Member limit 10,1) limit 10
51.52. ************************ 4. row ***********************
- Query_ID:
- Duration: 0.0026320
- Query: select * from Member limit 1000, 10
- ************************ 5. row ***********************
- Query_ID:
- Duration: 0.0013400
- Query: select * from Member where MemberID >= (select MemberID from Member limit 1000,1) limit 10
60.61. ************************ 6. row ***********************
- Query_ID:
- Duration: 0.0995670
- Query: select * from Member limit 100000, 10
- ************************ 7. row ***********************
- Query_ID:
- Duration: 0.0244770
- Query: select * from Member where MemberID >= (select MemberID from Member limit 100000,1) limit 100
从结果中可以得知,当偏移1000以上使用子查询法可以有效的提高性能。
* 2.倒排表优化法
倒排表法类似建立索引,用一张表来维护页数,然后通过高效的连接得到数据
缺点:只适合数据数固定的情况,数据不能删除,维护页表困难
具体请看, http://blog.chinaunix.net/u/29134/showart_1333566.html
* 3.反向查找优化法
当偏移超过一半记录数的时候,先用排序,这样偏移就反转了
缺点:order by优化比较麻烦,要增加索引,索引影响数据的修改效率,并且要知道总记录
,偏移大于数据的一
引
limit偏移算法
正向查找: (当前页 – 1) * 页长
反向查找: 总记录 – 当前页 * 页长
做下实验,看看性能如何
总记录数:1,628,77
每页记录数: 4
总页数:1,628,775 / 40 = 4072
中间页数:40720 / 2 = 20360
第21000
正向查找SQL:
Sql代码
- SELECT * FROM
abc
WHEREBatchID
= 123 LIMIT 839960, 40
时间:1.8696 秒
反向查找sql:
Sql代码
- SELECT * FROM
abc
WHEREBatchID
= 123 ORDER BY InputDate DESC LIMIT 788775, 40
时间:1.8336 秒
第30000
正向查找SQL:
Sql代码
- SELECT * FROM
abc
WHEREBatchID
= 123 LIMIT 1199960, 40
时间:2.6493 秒
反向查找sql:
Sql代码
- SELECT * FROM
abc
WHEREBatchID
= 123 ORDER BY InputDate DESC LIMIT 428775, 40
时间:1.0035 秒
注意,反向查找的结果是是降序desc的,并且InputDate是记录的插入时间,也可以用主键联合索引,但是不方便。
* 4.limit限制优化法
把limit偏移量限制低于某个数。。超过这个数等于没数据,我记得alibaba的dba说过他们是这样做的
* 5.只查索引法
http://willko.iteye.com/blog/670120
总结:limit的优化限制都比较多,所以实际情况用或者不用只能具体情况具体分析了。页数那么后,基本很少人看的。。。
- 相关文章推荐:
- python调用mysql存储过程并获取返回值
- python操作mysql数据库的例子
- python MySQLdb的安装和使用
- 本文来自:爱好Linux技术网
- 本文链接:http://www.ahlinux.com/python/9260.html