问题:数据库中可以用 datetime、bigint、timestamp 来表示时间,那么选择什么类型来存储时间比较合适呢?

前期数据准备

通过程序往数据库插入 50w 数据

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 为同一时间的不同存储格式

实体类 users

/**

* @author hetiantian

* @date 2018/10/21

* */

@Builder

@Data

public class Users{

/**

* 自增唯一id

* */

private Long id;

/**

* date类型的时间

* */

private Date timeDate;

/**

* timestamp类型的时间

* */

private Timestamp timeTimestamp;

/**

* long类型的时间

* */

private long timeLong;

}

dao 层接口

/**

* @author hetiantian

* @date 2018/10/21

* */

@Mapper

public 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);

}

测试类往数据库插入数据

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());

}

}

}

生成数据代码方至 github:http://github.com/TiantianUpup/sql-test/ 如果不想用代码生成,而是想通过 sql 文件倒入数据,附 sql 文件网盘地址:http://pan.baidu.com/s/1Qp9x6z8CN6puGfg-eNghig

sql 查询速率测试

通过 datetime 类型查询:

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

耗时:0.171

通过 timestamp 类型查询

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

耗时:0.351

通过 bigint 类型查询

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

耗时:0.130s

结论 在 InnoDB 存储引擎下,通过时间范围查找,性能 bigint > datetime > timestamp

sql 分组速率测试

使用 bigint 进行分组会每条数据进行一个分组,如果将 bigint 做一个转化在去分组就没有比较的意义了,转化也是需要时间的

通过 datetime 类型分组:

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

耗时:0.176s

通过 timestamp 类型分组:

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

耗时:0.173s

结论 在 InnoDB 存储引擎下,通过时间分组,性能 timestamp > datetime,但是相差不大

sql 排序速率测试

通过 datetime 类型排序:

select * from users order by time_date

耗时:1.038s

通过 timestamp 类型排序

select * from users order by time_timestamp

耗时:0.933s

通过 bigint 类型排序

select * from users order by time_long

耗时:0.775s

结论 在 InnoDB 存储引擎下,通过时间排序,性能 bigint > timestamp > datetime

小结

如果需要对时间字段进行操作 (如通过时间范围查找或者排序等),推荐使用 bigint,如果时间字段不需要进行任何操作,推荐使用 timestamp,使用 4 个字节保存比较节省空间,但是只能记录到 2038 年记录的时间有限。

mysql 时间戳 bigint_MySQL时间类型datetime、bigint及timestamp的查询效率相关推荐

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

    以下文章来源方志朋的博客,回复"666"获面试宝典 来源:https://juejin.im/post/6844903701094596615 数据库中可以用datetime.bi ...

  2. 我脸都问绿了!二面竟然被问到 MySQL 时间类型 datetime、bigint 及 timestamp 的查询效率。。。

    " 数据库中可以用datetime.bigint.timestamp来表示时间,那么选择什么类型来存储时间比较合适呢? 前期数据准备 通过程序往数据库插入50w数据 数据表: CREATE ...

  3. datetime mysql 查询_mysql数据库时间类型datetime、bigint、timestamp的查询效率比较

    数据库中可以用datetime.bigint.timestamp来表示时间,那么选择什么类型来存储时间比较合适呢? 前期数据准备 通过程序往数据库插入50w数据 数据表: CREATE TABLE ` ...

  4. MySql中的时间类型datetime,timestamp,date,year比较

    MySQL日期类型.日期格式.存储空间.日期范围比较. 日期类型        存储空间       日期格式                 日期范围 ------------ ---------  ...

  5. mysql 数据库时间类型 datetime 数据在页面上显示一串数字, 用函数date_format( )转换格式 ;

    mysql 数据库时间类型 datetime 转换格式 数据库时间字段datetime可以使用date_format( )函数进行时间的转换. 数据库时间字段类型: 数据库查询结果: 没转换前页面显示 ...

  6. MySQL时间戳和unix时间戳区别、MySQL中的时间类型

    文章目录 1 时间戳 1.1 unix时间戳(unix timestamp) 1.2 MySQL时间戳(timestamp) 2 MySQL中timestamp和datetime的不同点 3 MySQ ...

  7. MySQL中的时间类型

    时间是一类重要的数据,MySQL中有多种关于时间的类型可以选择.这篇文章主要介绍MySQL中的时间类型,主要参考MySQL文档:https://dev.mysql.com/doc/refman/8.0 ...

  8. mysql中的时间类型和比较大小

    mysql中的时间类型和比较大小 一.时间类型 二.时间比较 一.时间类型 mysql有五大时间类型: datetime 可以存储年月日时分秒类型的时间,如果在设计字段的时候可以设置获取当前时间,插入 ...

  9. mysql 创建时间类型,MySQL日期和时间类型

    搜索热词 MysqL中存储日和和时间,使用日期和时间类型. 提供的包括YEAR.DATE.TIME.DATETIME和TIMESTAMP. YEAR 占用:1字节 取值范围:1901~2155 日期格 ...

  10. mysql小日期时间类型_MySQL之日期时间类型

    mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 DA ...

最新文章

  1. php sql alert,SQL ALTER
  2. UIView 使用NSLayoutConstraint 代码
  3. Linux_LVM、RAID_RHEL7
  4. ubuntu上玩3D,把状态栏面板栏给玩没了
  5. mysql创建外键级联更新_MySQL中利用外键实现级联删除、更新
  6. mybatis和spring整合时这个报错,应该这样解决!
  7. FileOutputStream为false时候注意的问题
  8. 【华为云技术分享】在 K8S 大规模场景下 Service 性能如何优化?
  9. Transformers Assemble(PART II)
  10. vue 运行报错Module build failed: Error: Node Sass does not yet support your current environment: Windows
  11. 阿里云数据库RDS MySQL 物理全备文件数据恢复至自建数据库Mysql 5.7中
  12. 其实,我只是一个工程师
  13. 清、浊、爆破音的时域与频域特性
  14. C:\Windows\System32\drivers\etc中的文件说明
  15. 用Visio做工作流程图
  16. 股票F10关键字过滤工具:13年年报10转10股票一览,截止2014年3月10日 共67只
  17. Rosetta基础3:ligand docking
  18. 显卡测试(具体步骤)
  19. 基于AntDesign UI的自定义表单
  20. 【】oracle 11g 新特性

热门文章

  1. Python备份文件实现以及备份大文件出错解决方案
  2. 《Unity_5.X_3D游戏开发技术详解与典型案例》pdf
  3. eclipse从SVN检出的项目大面积报错怎么办
  4. CentOS6.6安装cobbler
  5. Windows7 64位下SDK Manager.exe无法运行问题解决方法
  6. 《Windows via C/C++》学习笔记 —— 用户模式的“线程同步”之“条件变量”
  7. 知识,因为美丽而传播
  8. 孟子模型 | 当Bert炼丹不是玄学而是哲学
  9. 说来你可能不信,穿越后男狐狸逼我好好学习…
  10. 【每日算法Day 102】美团 AI 平台算法工程师面试编程题