通常存储时间用datetime类型,现在很多系统也用int存储时间,它们有什么区别?个人更喜欢使用int这样对于日期计算时比较好哦,下面我们一起来看到底那种会好些。

int

().4个字节存储,INT的长度是4个字节,存储空间上比datatime少,int索引存储空间也相对较小,排序和查询效率相对较高一点点
()可读性极差,无法直观的看到数据,可能让你很恼火 TIMESTAMP ()4个字节储存
()值以UTC格式保存
()时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
()TIMESTAMP值不能早于1970或晚于2037 datetime ()8个字节储存
()与时区无关 ()以'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() 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 =
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 8M
thread_cache_size =
query_cache_type =
query_cache_size =
thread_concurrency = 测试 DATETIME
TIMESTAMP
INT 执行mysql mysql> select * from test_datetime into outfile ‘/tmp/test_datetime.sql’;
Query OK, rows affected (6.19 sec) mysql> select * from test_timestamp into outfile ‘/tmp/test_timestamp.sql’;
Query OK, rows affected (8.75 sec) mysql> select * from test_int into outfile ‘/tmp/test_int.sql’;
Query OK, 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, rows affected (41.52 sec)
Records: Deleted: Skipped: Warnings: mysql> load data infile ‘/export/home/ntavares/test_datetime.sql’ into table test_timestamp;
Query OK, rows affected, warnings (48.32 sec)
Records: Deleted: Skipped: Warnings: mysql> load data infile ‘/export/home/ntavares/test_int2.sql’ into table test_int;
Query OK, rows affected (37.73 sec)
Records: Deleted: Skipped: Warnings: 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 > ‘-- ::′ AND datetime < ‘-- ::′;
+———–+
| count(id) |
+———–+
| |
+———–+
row in set (3.93 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_timestamp WHERE datetime > ‘-- ::′ AND datetime < ‘-- ::′;
+———–+
| count(id) |
+———–+
| |
+———–+
row in set (9.87 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > UNIX_TIMESTAMP(’-- ::′) AND datetime < UNIX_TIMESTAMP(’-- ::′);
+———–+
| count(id) |
+———–+
| |
+———–+
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(’-- ::′) AS lower, UNIX_TIMESTAMP(’-- ::′) AS bigger;
+——-+——–+
| lower | bigger |
+——-+——–+
| | |
+——-+——–+
row in set (0.00 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > AND datetime < ;
+———–+
| count(id) |
+———–+
| |
+———–+
row in set (1.94 sec)

Mysql存储日期类型用int、timestamp还是datetime?的更多相关文章

  1. MySQL各种日期类型与整型(转)

    日期类型 存储空间 日期格式 日期范围 datetime 8 bytes YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 t ...

  2. 07、MySQL—时间日期类型

    时间日期类型 1.Date 日期类型:系统使用三个字节来存储数据,对应的格式为:YYYY-mm-dd,能表示的范围是从1000-01-01 到9999-12-12,初始值为0000-00-00 2.T ...

  3. Java中日期类型和mysql中日期类型进行整合

      1. java与mysql中日期.时间类型总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mysql(版本:5.1.50)的时间日期类型如下:   da ...

  4. MySQL 的日期类型有5个,分别是: date、time、year、datetime、timestamp。

    类型 字节 格式 用途 是否支持设置系统默认值 date 3 YYYY-MM-DD 日期值 不支持 time 3 HH:MM:SS 时间值或持续时间 不支持 year 1 YYYY 年份 不支持 da ...

  5. MySQL的日期类型

    -- MySQL 中有多种数据类型可以用于日期和时间的表示,不同的版本可能有所差异,表 3-2 中-- 列出了 MySQL 5.0 中所支持的日期和时间类型.-- 表 3-2 MySQL 中的日期和时 ...

  6. Mysql 中日期类型bigint和datetime互转

    MySql数据库中字段类型bigint 长度是10位的 mysql> select (from_unixtime(1554047999))as datatime;+--------------- ...

  7. mysql date and time type ---- mysql 时间&日期 类型详解

    mysql 中支持用多种方式来表示时间与日期 一.mysql 中能表示时间与日期的数据类型: 1.表示年 ) -- 最好不要用这个数据类型.对于年份的取值中有[1901 --> 2155] + ...

  8. mysql基础 日期类型

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

    1.首先是我们分析datetime长度是8个字节,INT的长度是4个字节,存储空间上比datatime少. 2.int存储索引的空间也比datetime少,排序效率高,查询速度比较快. 3.方便计算, ...

随机推荐

  1. “人少也能办大事”---K2 BPM老客户交流会

    主题:固铂轮胎工作流项目分享-K2 SmartForm下的工作流快速开发 嘉宾:王彦(固铂轮胎IT资深经理) 国内业务规模越来越大,流程越来越复杂,跨部门跨组织的流程纸质审批非常复杂,内控的要求越来越 ...

  2. CSSOM视图模式(CSSOM View Module)相关整理(转载)

    原文地址 http://www.zhangxinxu.com/wordpress/?p=1907 一.Window视图属性 这些属性可以hold住整个浏览器窗体大小.微软则将这些API称为“Scree ...

  3. ssh原理

     客户端向服务器端发出连接请求 服务器端向客户端发出自己的公钥 客户端使用服务器端的公钥加密通讯密钥然后发给服务器端 如果通讯过程被截获,由于窃听者即使获知公钥和经过公钥加密的内容,但不拥有私 ...

  4. Xrun 将 app 转化为 IPA

    xcodebuild命令行打包,在使用xcodebuild编译后发现有些东西有些临时性质的东西,依然存在,搜索了一些资料,找到有clean的命令:在之前打包都是生成app文件,将app打包成ipa文件 ...

  5. 一篇介绍jquery很好的

    本文基于jQuery1.7.1版本,是对官方API的整理和总结,完整的官方API见http://api.jquery.com/browser/ 0.总述 jQuery框架提供了很多方法,但大致上可以分 ...

  6. hdu 2030

    PS:原本这道题就空了好久...今天才去查了下汉字机内码... 然后才知道了. 1—— 一个汉字在字符串中是以两个负的字符形式存储,所以本题只要把字符串中负字符的个数找出来,再 除以2 就OK了. 2 ...

  7. (spring-第14回【IoC基础篇】)国际化信息

    国际化又称为本地化. 当你把手机的language由中文切换到英文时,你的微信也相应改用英语,这就是i18n国际化.一般来说,应用软件提供一套不同语言的资源文件,放到特定目录中,应用根据不同语言的操作 ...

  8. win下隐藏任务栏

    C# 隐藏任务栏开始按钮 关闭shell 分类: .NET C# 2011-07-14 15:26 789人阅读 评论(1) 收藏 举报 shell任务c#stringnulluser 一.隐藏任务栏 ...

  9. Balance_01背包

    Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...

  10. 解决:子元素设置margin-top,父元素也受影响的问题

    <!doctype html><html> <head> <meta charset="UTF-8"> <title>子 ...