【mySQL】MYSQL数据库时间字段INT,TIMESTAMP,DATETIME性能效率比较

目录

MYSQL数据库时间字段INT,TIMESTAMP,DATETIME性能效率比较

mysql的日期存储字段比较int,datetime,timestamp区别


MYSQL数据库时间字段INT,TIMESTAMP,DATETIME性能效率比较


文章:https://www.jianshu.com/p/27c807ed6974

文章2:https://blog.csdn.net/adsadadaddadasda/article/details/78933784

mysql的日期存储字段比较int,datetime,timestamp区别

1、首先是我们分析datetime长度是8个字节,INT的长度是4个字节,存储空间上比datatime少。

2、int存储索引的空间也比datetime少,排序效率高,查询速度比较快。

3、方便计算,unix时间戳有点就是方便计算。

总结了一下timestamp和datetime的区别:

1、timestamp有范围限制1970 ~ 2037

2、timestamp支持default current_timestamp 来设置默认自动当前时间

3、timestamp支持on update current_timestamp 来设置更新时自动当前时间

4、timestamp时区相关,存储时以UTC时间保持,查询时转换为当前时区,

即如果在东8区的08:00:00分保存的数据,在东9区看到的是09:00:00

5、timestamp 4个字节存储(实际上就是int),datetime 8个字节

6、如果timestamp的值超出范围,mysql不会报错

7、如果是自动更新模式,手动修改数据导致timestamp字段更新

8、同时有两个timestamp字段默认值为current_timestamp会报错

9、如果mysql在maxdb模式下,timestamp等同于datetime,也就是说timestamp

受服务器模式影响

网上还有还一个TIMESTAMP 和INT比较的例子,timestamp效率比int不会差,而且timestamp有自动更新功能

相同点
TIMESTAMP列的显示格式与datetime列相同。
换句话说,显示宽度固定在19字符,并且格式为YYYY-MM-DD HH:MM:SS。

不同点
datetime
以”YYYY-MM-DDHH:MM:SS”格式检索和显示datetime值。支持的范围为”1000-01-01 00:00:00″到”9999-12-31 23:59:59″
TIMESTAMP值不能早于1970或晚于2037
TIMESTAMP
1、4个字节储存(Time stamp value is stored in 4 bytes)
2、值以UTC格式保存( it stores the number of milliseconds)
3、时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
datetime
1、8个字节储存(8 bytes storage)
2、实际格式储存(Just stores what you have stored and retrieves thesamething which you have stored.)
3、与时区无关(It has nothing to deal with the TIMEZONEandConversion.)

猜你喜欢

转载自blog.csdn.net/bandaoyu/article/details/89249840