时间:2021-07-01 10:21:17 帮助过:37人阅读
里面有90w数据,都是随机的时间.
先看没有索引的全表扫描
1 :
select COUNT(*) from 20130107date
where c_date BETWEEN DATE(‘20110101‘) and DATE(‘20110102‘)
这个需要1.54s
2:
select COUNT(*) from 20130107date
where c_date_long BETWEEN UNIX_TIMESTAMP(‘20110101‘) and UNIX_TIMESTAMP(‘20110102‘)
这个是2.3s
但是可以这样搞
3 :
select UNIX_TIMESTAMP(‘20110101‘),UNIX_TIMESTAMP(‘20110102‘);
得到结果1293811200和1293897600
然后
select COUNT(*) from 20130107date
where c_date_long BETWEEN 1293811200 and 1293897600;
发现变成了0.61s
1和2的差距还可以说是比较int和比较timestamp的差距,那么2和3的差距呢?难道多出来的时间是每一条记录都要evaluate UNIX_TIMESTAMP(‘20110102’)?
然后用索引
select COUNT(*) from 20130107date
where idx_date_long BETWEEN UNIX_TIMESTAMP(‘20110101‘) and UNIX_TIMESTAMP(‘20110102‘);
select COUNT(*) from 20130107date
where idx_date BETWEEN ‘20110101‘ and ‘20110102‘
毫无悬念,两个基本都是瞬时的.
版权声明:本文为博主原创文章,未经博主允许不得转载。
mysql timestamp和long存储时间效率比较
标签:mysql timestamp long