用TIMESTAMP类型取代INT和DATETIME
1、首先是我们分析datetime长度是8个字节,INT的长度是4个字节,存储空间上比datatime少。
2、int存储索引的空间也比datetime少,排序效率高,查询速度比较快。
3、方便计算,unix时间戳有点就是方便计算。
网上还有还一个TIMESTAMP 和INT比较的例子,timestamp效率比int不会差,而且timestamp有自动更新功能
现在我们来对比下 TIMESTAMP 和 INT UNSIGNED 以及 DATETIME 这3种类型到底谁更好。
mysql> CREATE TABLE `t` (
`d1` int(10) unsigned NOT NULL default '',
`d2` timestamp NOT NULL default CURRENT_TIMESTAMP,
`d3` datetime NOT NULL,
KEY `d2` (`d2`),
KEY `d1` (`d1`),
KEY `d3` (`d3`)
);
时间戳 1199116800 表示 2008-01-01 这个时间点。然后运行存储过程,大概填充几十万条记录后,中止执行,因为上面的存储过程是个死循环,所以需要人工中止。
来看看到底有多少条记录了,以及索引情况:
mysql> select count(*) from t;
+----------+
| count(*) |
+----------+
| 924707 |
+----------+
mysql> analyze table t;
+--------+---------+----------+-----------------------------+
| Table | Op | Msg_type | Msg_text |
+--------+---------+----------+-----------------------------+
| test.t | analyze | status | Table is already up to date |
+--------+---------+----------+-----------------------------+
mysql> show index from t;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t | 1 | d2 | 1 | d2 | A | 924707 | NULL | NULL | | BTREE | |
| t | 1 | d1 | 1 | d1 | A | 924707 | NULL | NULL | | BTREE | |
| t | 1 | d3 | 1 | d3 | A | 924707 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2. 对比
2.1 只检索一条记录
mysql> explain select * from t where d1 = 1199579155;
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| 1 | SIMPLE | t | ref | d1 | d1 | 4 | const | 1 | |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
mysql> explain select * from t where d2 = '2008-01-06 08:25:55';
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| 1 | SIMPLE | t | ref | d2 | d2 | 4 | const | 1 | |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
mysql> explain select * from t where d3 = '2008-01-06 08:25:55';
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| 1 | SIMPLE | t | ref | d3 | d3 | 8 | const | 1 | |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
2.2 范围检索
mysql> explain select * from t where d1 >= 1199894400;
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | t | range | d1 | d1 | 4 | NULL | 121961 | Using where |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
mysql> explain select * from t where d2 >= from_unixtime(1199894400);
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | t | range | d2 | d2 | 4 | NULL | 121961 | Using where |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
mysql> explain select * from t where d3 >= from_unixtime(1199894400);
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | t | range | d3 | d3 | 8 | NULL | 120625 | Using where |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
小结:从上面的2次对比中可以看到,对 d1 或 d2 字段检索时的索引长度都是 4,因为 TIMESTAMP 实际上是 4字节 的 INT 值。因此,实际应用中,基本上完全可以采用 TIMESTAMP 来代替另外2种类型了,并且 TIMESTAMP 还能支持自动更新成当前最新时间,何乐而不为呢?
用TIMESTAMP类型取代INT和DATETIME的更多相关文章
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
一.TIMESTAMP 显示格式:YYYY-MM-DD HH:MM:SS 时间范围:[ '1970-01-01 00:00:00'到'2037-12-31 23:59:59'] TIMESTAMP D ...
- mysql 查询 int类型日期转换成datetime类型
数据库日期类型是int类型的,该查询结果是datetime类型的 SELECT from_unixtime( `时间列名` ) FROM 表名 如果原来类型是datetime类型,查询结果要是int类 ...
- mysql:数据库保存时间的类型——int和datetime的区别
我们都知道,时间保存在数据库中,可以选择使用两种类型,一种是int,一种是datetime 那么,它们两个有什么区别呢?要怎么用呢? 现在和小仓鼠一起来探讨一下 1.int和datetime的使用区别 ...
- .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题
最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...
- mysql java 类型对照 int
Java MySQL数据类型对照 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang.S ...
- java 反射: 当Timestamp类型的属性值为null时,设置默认值
import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- TIMESTAMP类型字段在SQL Server和MySQL中的含义和使用
公众号上转的满天飞的一篇文章,MySQL优化相关的,无意中瞄到一句“尽量使用TIMESTAMP而非DATETIME”,之前对TIMESTAMP也不太熟悉,很少使用,于是查了一下两者的区别. 其实,不管 ...
- 使用.NET向webService传double、int、DateTime 服务器得到的数据时null的问题(转http://blog.csdn.net/slimboy123/article/details/4366701)
用C#.NET调用Java开发的WebService时,先在客户端封装的带有int属性的对象,当将该对象传到服务器端时,服务器端可以得到string类型的属性值,却不能得到int类型.double和D ...
- MySQL的timestamp类型自动更新问题
今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...
随机推荐
- Linux 目录操作和4中文件拷贝效率测试
/*1.用户输入任意目录名称,显示该目录下的文件列表信息,包括文件类型,文件权限,文件大小,文件名称2.拷贝用户输入的文件到当前目录下3.第二点功能,使用4种方式完成,并比较说明效率*/ /* str ...
- js 使用技巧 - [近几年工作中的经验总结的技巧]
1.如果 ajax 返回单一的 json 格式,接收方需要这样再格式化一下赋值: var str = eval("(" + msg + ")"); 开发引用: ...
- EXTJS 4.2 资料 控件之Grid Columns 列renderer 绑定事件
columns: [ { header: '序号', xtype: 'rownumberer', align: 'center', width: 100 }, { header: 'CompanyId ...
- CoInitialize()、CoInitializeEx()和AfxOleInit()区别联系
CoInitialize()和AfxOleInit() 都是初始化COM库,不同之处在与: OLE是建立在COM之上的技术,层次比COM要高.AfxOleInit()调用的是OleInitialize ...
- Extjs-4.2.1(二)——使用Ext.define自定义类
鸣谢:http://www.cnblogs.com/youring2/archive/2013/08/22/3274135.html --------------------------------- ...
- jsp的静态包含与动态包含:<%@ include file="" %>和<jsp:include page=""></jsp:include>区别与分析
<%@ include file="" %>是将文件原封不动的copy进现有的文件中,像是拼接好后,再编译成为servlet运行. <jsp:include pa ...
- datagridview 右键选中行 并弹出菜单
private void dataGridView_OLUsers_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { i ...
- 团体程序设计天梯赛-练习集L1-020. 帅到没朋友
L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...
- uva 10986
ford 超时 使用优先队列的Dijkstra 算法 //#include <cstdio> //#include <cstring> //#include <algo ...
- ECNU1104-BFS
简单的BFS题意:多起点多终点 /* 简单的BFS 题意:多起点多终点 */ #include<stdio.h> #include<string.h> #include< ...