有一张表里面已经有几千万条数据了?
一般查询的话应该有常用的语句的。
比如常见查询为:
select * from factdata where user='a' and module='b' and dtime between '2012-11-01 00:10:00' and '2012-11-01 00:11:10';
那么你这时候需要在factdata表上建立(user,module,dtime)的联合索引。
alter table factdata add index i_merge(`user`,`module`,`dtime`);
你可以执行
explain select * from factdata where user='a' and module='b' and dtime between '2012-11-01 00:10:00' and '2012-11-01 00:11:10';
查看建立索引前面的返回的结果。
假如没有索引的话,explain会显示返回查询全表的数据自然会很慢了。
假如用到了索引的话,可以快速的找到需要查询的区间里的数据,往往需要查询的数据量是全表的1/100,1/1000,那么这时候花费的时间就是1/100,1/1000了。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有