欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL存储时间类型选择的问题讲解

程序员文章站 2022-06-13 12:10:27
mysql中存储时间通常会用datetime类型,但现在很多系统也用int存储unix时间戳,它们有什么区别?本人总结如下: int (1)4个字节存储,int的长度是...

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;

机型配置

  • kip-locking
  • key_buffer = 128m
  • max_allowed_packet = 1m
  • table_cache = 512
  • sort_buffer_size = 2m
  • read_buffer_size = 2m
  • read_rnd_buffer_size = 8m
  • myisam_sort_buffer_size = 8m
  • thread_cache_size = 8
  • query_cache_type = 0
  • query_cache_size = 0
  • thread_concurrency = 4

测试

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接