时间:2021-05-24
MySQL中存储时间通常会用datetime类型,但现在很多系统也用int存储unix时间戳,它们有什么区别?本人总结如下:
int
(1)4个字节存储,INT的长度是4个字节,存储空间上比datatime少,int索引存储空间也相对较小,排序和查询效率相对较高一点点
(2)可读性极差,无法直观的看到数据
TIMESTAMP
(1)4个字节储存
(2)值以UTC格式保存
(3)时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
(4)TIMESTAMP值不能早于1970或晚于2037
datetime
(1)8个字节储存
(2)与时区无关
(3)以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'
随着Mysql性能越来越来高,个人觉得关于时间的存储方式,具体怎么存储看个人习惯和项目需求吧
分享两篇关于int vs timestamp vs datetime性能测试的文章
Myisam:MySQL DATETIME vs TIMESTAMP vs INT 测试仪
CREATE TABLE `test_datetime` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`datetime` FIELDTYPE NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM;机型配置
测试
DATETIME 14111 14010 14369 130000000
TIMESTAMP 13888 13887 14122 90000000
INT 13270 12970 13496 90000000
执行mysql
mysql> select * from test_datetime into outfile ‘/tmp/test_datetime.sql';Query OK, 10000000 rows affected (6.19 sec)mysql> select * from test_timestamp into outfile ‘/tmp/test_timestamp.sql';Query OK, 10000000 rows affected (8.75 sec)mysql> select * from test_int into outfile ‘/tmp/test_int.sql';Query OK, 10000000 rows affected (4.29 sec)alter table test_datetime rename test_int;alter table test_int add column datetimeint INT NOT NULL;update test_int set datetimeint = UNIX_TIMESTAMP(datetime);alter table test_int drop column datetime;alter table test_int change column datetimeint datetime int not null;select * from test_int into outfile ‘/tmp/test_int2.sql';drop table test_int;So now I have exactly the same timestamps from the DATETIME test, and it will be possible to reuse the originals for TIMESTAMP tests as well.
mysql> load data infile ‘/export/home/ntavares/test_datetime.sql' into table test_datetime;
Query OK, 10000000 rows affected (41.52 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0
mysql> load data infile ‘/export/home/ntavares/test_datetime.sql' into table test_timestamp;
Query OK, 10000000 rows affected, 44 warnings (48.32 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 44
mysql> load data infile ‘/export/home/ntavares/test_int2.sql' into table test_int;
Query OK, 10000000 rows affected (37.73 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0
As expected, since INT is simply stored as is while the others have to be recalculated. Notice how TIMESTAMP still performs worse, even though uses half of DATETIME storage size.
Let's check the performance of full table scan:
mysql> SELECT SQL_NO_CACHE count(id) FROM test_datetime WHERE datetime > ‘1970-01-01 01:30:00′ AND datetime < ‘1970-01-01 01:35:00′;+———–+| count(id) |+———–+| 211991 |+———–+1 row in set (3.93 sec)mysql> SELECT SQL_NO_CACHE count(id) FROM test_timestamp WHERE datetime > ‘1970-01-01 01:30:00′ AND datetime < ‘1970-01-01 01:35:00′;+———–+| count(id) |+———–+| 211991 |+———–+1 row in set (9.87 sec)mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > UNIX_TIMESTAMP('1970-01-01 01:30:00′) AND datetime < UNIX_TIMESTAMP('1970-01-01 01:35:00′);+———–+| count(id) |+———–+| 211991 |+———–+1 row in set (15.12 sec)Then again, TIMESTAMP performs worse and the recalculations seemed to impact, so the next good thing to test seemed to be without those recalculations: find the equivalents of those UNIX_TIMESTAMP() values, and use them instead:
mysql> select UNIX_TIMESTAMP('1970-01-01 01:30:00′) AS lower, UNIX_TIMESTAMP('1970-01-01 01:35:00′) AS bigger;+——-+——–+| lower | bigger |+——-+——–+| 1800 | 2100 |+——-+——–+1 row in set (0.00 sec)mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > 1800 AND datetime < 2100;+———–+| count(id) |+———–+| 211991 |+———–+1 row in set (1.94 sec)Innodb:MySQL DATETIME vs TIMESTAMP vs INT performance and benchmarking with InnoDB
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
mysql支持很多字段类型,包括数值类型、日期/时间类型和字符串(字符)类型;在使用时需要考虑到存储空间,存储效率;几种列类型描述使用了下述惯例:·M表示最大显
刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题:在mysql数据库中,有一个datetime类型的字段用于存储记录的日期时间值。py
MySQL存储过程参数有三种类型:in、out、inout。它们各有什么作用和特点呢?一、MySQL存储过程参数(in)MySQL存储过程“in”参数:跟C语言
一、MySQL存储过程参数(in)MySQL存储过程“in”参数:跟C语言的函数参数的值传递类似,MySQL存储过程内部可能会修改此参数,但对in类型参数的修改
MySQL日期类型:日期格式、所占存储空间、日期范围比较。日期类型存储空间日期格式日期范围-----------------------------------