MySQL 开发 10000 篇新闻的小型系统怎么写都可以,用各种框架可以实现快速开发,但是当数据量到了10万、100万、1000万,它的性能还能那么高吗? 小小的失误可能造成整个系统出现瓶颈,甚至让系统无法正常运行!
数据表 collect ( id, title ,info ,vtype) 4个字段,
title 用定长,
info 用text,
id 自增,
vtype 是 tinyint,设置索引。
这是一个基本的新闻系统的简单模型,现在往里面填充数据,填充 10 万篇新闻 。
最后 collect 10万条记录,数据库表占用硬盘 1.6GB 。
看这条sql语句:
select id,title from collect limit 1000,10; // 查询时间 0.01秒
select id,title from collect limit 90000,10;
9万条开始分页,查询需要 8-9 秒哪出问题了?
其实要优化这条数据,网上已经给出了答案,看下面的 SQL 语句:
select id from collect order by id limit 90000,10; // 0.04秒
因为 id 主键做索引当然快。
select id,title from collect where id >= (select id from collect order by id
limit 90000,1) limit 10;
这就是用了 id 做索引的结果,如果 SQL 复杂那么一点点就完了。<