Mysql 大分页优化

栏目: 数据库 · Mysql · 发布时间: 7年前

内容简介:看到数据库报警,查看慢 sql 日志看到执行了 90 秒,扫描了 1329172 行。先解决大分页的问题(

看到数据库报警,查看慢 sql 日志看到

select id, content from zixun_article where status = 1 order by id asc limit 1328000, 1000

执行了 90 秒,扫描了 1329172 行。

两个问题

  • 大分页问题
  • 索引问题

先解决大分页的问题( 这个经验是高级数据库专家之前分享的 )修改后

select a2.id, a2.content from (select id from zixun_article where status = 1 order by id asc limit 1309000, 1000) a1, zixun_article a2 where a1.id=a2.id;

我看到 status 上是没有索引的,所以直接改成

select a2.id, a2.content,a2.status from (select id from zixun_article order by id asc limit 1309000, 1000) a1, zixun_article a2 where a1.id=a2.id;

然后再到业务代码里面去根据 status 去做筛选。

总结

这种数据量很大的表,应该是先做一个子查询查出 id(只会在索引里面扫描),然后关联查询,这样扫描的行数是限定的。而不会扫描表前面所有的行。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP 5

Learning PHP 5

David Sklar / O'Reilly / July, 2004 / $29.95

Learning PHP 5 is the ideal tutorial for graphic designers, bloggers, and other web crafters who want a thorough but non-intimidating way to understand the code that makes web sites dynamic. The book ......一起来看看 《Learning PHP 5》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换