数据库案例分析

一张mysql大数据表有几千万数据,但有一自增id字段,且为主键,要遍历此表的所有数据,写出有效的方法和sql(禁止使用limit n,m)。


案例分析:数据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用text, id 是逐渐,vtype是tinyint,vtype是索引。10万数据的效果。
select id,title from collect limit 1000,10; 基本上0.01秒就OK
select id,title from collect limit 90000,10; 8-9秒完成
select id from collect order by id limit 90000,10;  0.04秒就OK。


为什么?因为用了id主键做索引当然快。
改进方法:select id,title from collect where id>=(select id from collect order by id limit 90000,1) limit 10;


select id from collect where vtype=1 order by id limit 90000,10; 很慢,用了8-9秒!
vtype 做了索引了啊?怎么会慢呢?vtype做了索引是不错,你直接 
select id from collect where vtype=1 limit 1000,10; 是很快的,基本上0.05秒,可是提高90倍,从9万开始,那就是0.05*90=4.5秒的速度了。

猜你喜欢

转载自blog.csdn.net/u014230945/article/details/78111290