怎样用mysql语句实现分页

怎样用mysql语句实现分页,第1张

--1.最常用的分页select * from content order by id desc limit 0, 10--limit是MySQL中特有的分页语法,用法如下:--举例:select * from tableName limit 5--返回前5行select * from tableName limit 0,5--同上,返回前5行select * from tableName limit 5,10--返回6-15行

直接用limit start, count分页语句, 也是我程序中用的方法:

select * from product limit start, count

当起始页较小时,查询没有性能问题,我们分别看下从10, 100, 1000, 10000开始分页的执行时间(每页取20条), 如下:

select * from product limit 10, 20 0.016秒

select * from product limit 100, 20 0.016秒

select * from product limit 1000, 20 0.047秒

select * from product limit 10000, 20 0.094秒

我们已经看出随着起始记录的增加,时间也随着增大, 这说明分页语句limit跟起始页码是有很大关系的,那么我们把起始记录改为40w看下(也就是记录的一般左右) select * from product limit 400000, 20 3.229秒

再看我们取最后一页记录的时间

select * from product limit 866613, 20 37.44秒

难怪搜索引擎抓取我们页面的时候经常会报超时,像这种分页最大的页码页显然这种时

间是无法忍受的。

从中我们也能总结出两件事情:

1)limit语句的查询时间与起始记录的位置成正比

2)mysql的limit语句是很方便,但是对记录很多的表并不适合直接使用。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8455136.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-16
下一篇2023-04-16

发表评论

登录后才能评论

评论列表(0条)

    保存