select id from news where cate = 1 order by id desc limit 500000 ,10 查询时候 20 秒
好恐惧的速率!!应用第一节《百万数据mysql数据测试环境引见》学问举行优化:
select * from news where cate = 1 and id > (select id from news where cate = 1 order by id desc limit 500000,1 ) order by id desc limit 0,10 查询时候 15 秒
优化结果不明显,前提带来的影响照样很大!在如许的情况下不管我们怎样去优化sql语句就没法处理运转效力题目。那末换个思绪:竖立一个索引表,只纪录文章的id、分类信息,我们将文章内容这个大字段支解出去。
表 news2 [ 文章表 引擎 myisam 字符集 utf-8 ] ------------------------------------------------- idint11主键自动增添 cateint11索引
在写入数据时将2张表同步,查询是则可以运用news2 来举行前提查询:
select * from news where cate = 1 and id > (select id from news2 where cate = 1 order by id desc limit 500000,1 ) order by id desc limit 0,10
注重前提 id > 背面运用了news2 这张表!
运转时候 1.23秒,我们可以看到运转时候缩减了近20倍!!数据在10万摆布是查询时候可以保持在0.5秒摆布,是一个逐渐靠近我们可以容忍的值!
然则1秒关于服务器来讲依然是一个不能接收的值!!另有什么可以优化的方法吗??我们尝试了一个巨大的变化:
将 news2 的存储引擎改变成innodb,实行结果是惊人的!
select * from news where cate = 1 and id > (select id from news2 where cate = 1 order by id desc limit 500000,1 ) order by id desc limit 0,10
只需要 0.2秒,异常棒的速率。为何会有怎样大的差异呢?请寓目下一篇 mysql存储引擎详解。
以上就是百万数据下mysql前提查询及分页查询的注重事项的细致内容,更多请关注ki4网别的相干文章!