Mysql 笔记--分页优化

表结构 xxxx(id,title,content,url,status) id为主键,自动递增

普通分页

select * from xxxx where status = 1 limit 1000,100;

这样查询在数据量比较小,同时查询前面的数据的时候是很快的.但是如果表的数量持续增大(这里的测试数据有589530条数据),同时需要查询后面分页的数据,如:

select * from xxxx where status = 1 limit 235500,500;

执行上面这条语句,就会很慢. 这里测试结果是 8-9s的样子

优化分页

利用id主键,如下:

select * from xxxx where id >=(select id from xxxx where status=1 limit 235500,1) and status = 1 limit 500;

上面这条语句的执行结果是 0.24s
效率一下提升40多倍

发布了84 篇原创文章 · 获赞 66 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/u012358984/article/details/44835657
今日推荐