百度面试官:数据库中datetime、bigint、timestamp来表示时间,选择谁来存储时间效率最高呢?
大家好,我是D哥
点击关注下方公众号,Java面试资料 都在这里
作者:哒波甜
来源:https://juejin.cn/post/6844903701094596615
数据库中可以用datetime、bigint、timestamp来表示时间,那么选择什么类型来存储时间比较合适呢?
# 后数据准备
通过程序往数据库插入50w数据
数据表:
CREATETABLE`users` (
`id`int(11) NOTNULL AUTO_INCREMENT,
`time_date` datetime NOTNULL,
`time_timestamp`timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,
`time_long`bigint(20) NOTNULL,
PRIMARY KEY (`id`),
KEY`time_long` (`time_long`),
KEY`time_timestamp` (`time_timestamp`),
KEY`time_date` (`time_date`)
) ENGINE=InnoDB AUTO_INCREMENT=500003DEFAULTCHARSET=latin1
其中time_long、time_timestamp、time_date为同一时间的不同存储格式
实体类users
/**
* @author hetiantian
* @date 2018/10/21
* */
publicclass Users {
/**
* 自增唯一id
* */
private Long id;
/**
* date类型的时间
* */
privateDate timeDate;
/**
* timestamp类型的时间
* */
private Timestamp timeTimestamp;
/**
* long类型的时间
* */
private long timeLong;
}
dao层接口
/**
* @author hetiantian
* @date 2018/10/21
* */
publicinterfaceUsersMapper{
"insert into users(time_date, time_timestamp, time_long) value(#{timeDate}, #{timeTimestamp}, #{timeLong})") (
true,keyProperty = "id",keyColumn = "id") (useGeneratedKeys =
intsaveUsers(Users users);
}
测试类往数据库插入数据
publicclassUsersMapperTestextendsBaseTest{
private UsersMapper usersMapper;
publicvoidtest(){
for (int i = 0; i < 500000; i++) {
long time = System.currentTimeMillis();
usersMapper.saveUsers(Users.builder().timeDate(new Date(time)).timeLong(time).timeTimestamp(new Timestamp(time)).build());
}
}
}
生成数据代码方至github:https://github.com/TiantianUpup/sql-test/ 如果不想用代码生成,而是想通过sql文件倒入数据,文末附sql文件网盘地址。
# sql查询速率测试
通过datetime类型查询:
selectcount(*) fromuserswhere time_date >="2018-10-21 23:32:44"and time_date <="2018-10-21 23:41:22"
耗时:0.171
通过timestamp类型查询
selectcount(*) fromuserswhere time_timestamp >= "2018-10-21 23:32:44"and time_timestamp <="2018-10-21 23:41:22"
耗时:0.351
通过bigint类型查询
selectcount(*) fromuserswhere time_long >=1540135964091and time_long <=1540136482372
耗时:0.130s
结论 在InnoDB存储引擎下,通过时间范围查找,性能bigint > datetime > timestamp
# sql分组速率测试
使用bigint 进行分组会每条数据进行一个分组,如果将bigint做一个转化在去分组就没有比较的意义了,转化也是需要时间的
通过datetime类型分组:
select time_date, count(*) fromusersgroupby time_date
耗时:0.176s
通过timestamp类型分组:
select time_timestamp, count(*) fromusersgroupby time_timestamp
耗时:0.173s
结论 在InnoDB存储引擎下,通过时间分组,性能timestamp > datetime,但是相差不大
# sql排序速率测试
通过datetime类型排序:
select * fromusersorderby time_date
耗时:1.038s
通过timestamp类型排序
select * fromusersorderby time_timestamp
耗时:0.933s
通过bigint类型排序
select * fromusersorderby time_long
耗时:0.775s
结论:在InnoDB存储引擎下,通过时间排序,性能bigint > timestamp > datetime
# 小结
如果需要对时间字段进行操作(如通过时间范围查找或者排序等),推荐使用bigint,如果时间字段不需要进行任何操作,推荐使用timestamp,使用4个字节保存比较节省空间,但是只能记录到2038年记录的时间有限。
文中sql文件网盘地址: https://pan.baidu.com/s/1vjxCyTih8YXGJPUwbU2oqA 提取码: g9c4
最后,D哥也建了一个技术群,主要探讨一些新的技术和开源项目值不值得去研究及IDEA使用的“骚操作”,有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群。
▲长按扫描
关键词
数据
数据库
效率
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。