MySQL数据库时间类型datetime、bigint、timestamp的查询效率比较

沙海 2021年7月15日01:07:43Java评论89字数 2698阅读8分59秒阅读模式
摘要

MySQL数据库时间类型datetime、bigint、timestamp的查询效率比较 点击关注 ? 不装逼的程序员

MySQL数据库时间类型datetime、bigint、timestamp的查询效率比较

点击关注 ? 不装逼的程序员 文章源自JAVA秀-https://www.javaxiu.com/37963.html

数据库中可以用datetime、bigint、timestamp来表示时间,那么选择什么类型来存储时间比较合适呢?文章源自JAVA秀-https://www.javaxiu.com/37963.html

前期数据准备

通过程序往数据库插入50w数据文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 数据表:文章源自JAVA秀-https://www.javaxiu.com/37963.html

CREATE TABLE `users` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `time_date` datetime NOT NULL,  `time_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  `time_long` bigint(20) NOT NULL,  PRIMARY KEY (`id`),  KEY `time_long` (`time_long`),  KEY `time_timestamp` (`time_timestamp`),  KEY `time_date` (`time_date`)) ENGINE=InnoDB AUTO_INCREMENT=500003 DEFAULT CHARSET=latin1

其中time_long、time_timestamp、time_date为同一时间的不同存储格式文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 实体类users文章源自JAVA秀-https://www.javaxiu.com/37963.html

/** * @author hetiantian  * @date 2018/10/21 * */@Builder@Datapublic class Users {    /**     * 自增唯一id     * */    private Long id;    /**     * date类型的时间     * */    private Date timeDate;    /**     * timestamp类型的时间     * */    private Timestamp timeTimestamp;    /**     * long类型的时间     * */    private long timeLong;}
  • dao层接口文章源自JAVA秀-https://www.javaxiu.com/37963.html

/** * @author hetiantian * @date 2018/10/21 * */@Mapperpublic interface UsersMapper {    @Insert("insert into users(time_date, time_timestamp, time_long) value(#{timeDate}, #{timeTimestamp}, #{timeLong})")    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")    int saveUsers(Users users);}
  • 测试类往数据库插入数据文章源自JAVA秀-https://www.javaxiu.com/37963.html

public class UsersMapperTest extends BaseTest {    @Resource    private UsersMapper usersMapper;    @Test    public void test() {        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());        }    }}

sql查询速率测试

  • 通过datetime类型查询:文章源自JAVA秀-https://www.javaxiu.com/37963.html

select count(*) from users where time_date >="2018-10-21 23:32:44" and time_date <="2018-10-21 23:41:22"

耗时:0.171文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过timestamp类型查询文章源自JAVA秀-https://www.javaxiu.com/37963.html

select count(*) from users where time_timestamp >= "2018-10-21 23:32:44" and time_timestamp <="2018-10-21 23:41:22"

耗时:0.351文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过bigint类型查询文章源自JAVA秀-https://www.javaxiu.com/37963.html

select count(*) from users where time_long >=1540135964091 and time_long <=1540136482372  

耗时:0.130s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 结论 在InnoDB存储引擎下,通过时间范围查找,性能bigint  > datetime > timestamp文章源自JAVA秀-https://www.javaxiu.com/37963.html

sql分组速率测试

使用bigint 进行分组会每条数据进行一个分组,如果将bigint做一个转化在去分组就没有比较的意义了,转化也是需要时间的文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过datetime类型分组:文章源自JAVA秀-https://www.javaxiu.com/37963.html

select time_date, count(*) from users group by time_date

耗时:0.176s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过timestamp类型分组:文章源自JAVA秀-https://www.javaxiu.com/37963.html

select time_timestamp, count(*) from users group by time_timestamp

耗时:0.173s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 结论 在InnoDB存储引擎下,通过时间分组,性能timestamp > datetime,但是相差不大文章源自JAVA秀-https://www.javaxiu.com/37963.html

sql排序速率测试

  • 通过datetime类型排序:文章源自JAVA秀-https://www.javaxiu.com/37963.html

select * from users order by time_date

耗时:1.038s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过timestamp类型排序文章源自JAVA秀-https://www.javaxiu.com/37963.html

select * from users order by time_timestamp

耗时:0.933s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 通过bigint类型排序文章源自JAVA秀-https://www.javaxiu.com/37963.html

select * from users order by time_long

耗时:0.775s文章源自JAVA秀-https://www.javaxiu.com/37963.html

  • 结论 在InnoDB存储引擎下,通过时间排序,性能bigint > timestamp > datetime文章源自JAVA秀-https://www.javaxiu.com/37963.html

小结

如果需要对时间字段进行操作(如通过时间范围查找或者排序等),推荐使用bigint,如果时间字段不需要进行任何操作,推荐使用timestamp,使用4个字节保存比较节省空间,但是只能记录到2038年记录的时间有限文章源自JAVA秀-https://www.javaxiu.com/37963.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定