当前位置: 移动技术网 > IT编程>开发语言>PHP > MySQL时间字段究竟使用INT还是DateTime的说明

MySQL时间字段究竟使用INT还是DateTime的说明

2019年04月17日  | 移动技术网IT编程  | 我要评论

慕容心,安阳兰天商情,iphone x 预约

今天解析dedecms时发现deder的mysql时间字段,都是用

`senddata` int(10) unsigned not null default '0';

随后又在网上找到这篇文章,看来如果时间字段有参与运算,用int更好,一来检索时不用在字段上转换运算,直接用于时间比较!二来如下所述效率也更高。

归根结底:用int来代替data类型,更高效。

环境:

windows xp
php version 5.2.9
mysql server 5.1

第一步、创建一个表date_test(非定长、int时间)

create table `test`.`date_test` (
`id` int not null auto_increment ,
`start_time` int not null ,
`some_content` varchar( 255 ) not null ,
primary key ( `id` )
) engine = innodb;

第二步、创建第二个表date_test2(定长、int时间)

create table `test`.`date_test2` (
`id` int not null auto_increment ,
`start_time` int not null ,
`some_content` char( 255 ) not null ,
primary key ( `id` )
) engine = innodb;

第三步、创建第三个表date_test3(varchar、datetime时间)

create table `test`.`date_test3` (
`id` int not null auto_increment ,
`start_time` datetime not null ,
`some_content` varchar( 255 ) not null ,
primary key ( `id` )
) engine = innodb;

第四步、创建第四个表date_test3(char、datetime时间)

create table `test`.`date_test4` (
`id` int not null auto_increment ,
`start_time` datetime not null ,
`some_content` char( 255 ) not null ,
primary key ( `id` )
) engine = innodb;

ok,现在我们开始做测试,环境是php,先向各个表插入一百万条数据。插入的时候分200次,每次进库5000条。

表一执行记录:页面运行时间: 26.5997889042 秒,插入的时候发现一个有趣的现象:select count( id ) from `date_test` where 1 的结果是100w,而直接select * from `date_test`却是1,000,374条结果。(后来看到这是一个可能接近的值,请参看mysql faq 3.11)。

表二执行记录:页面运行时间: 62.3908278942 秒,这次记录是1,000,066条。

表三执行记录:页面运行时间: 30.2576560974 秒,这次的是1,000,224条。

表四执行记录:页面运行时间: 67.5393900871 秒,这次的是:1,000,073条。

现在把四个表的start_time字段一一加上索引。

测试四个表的更新,分别update 100条记录,并记录时间:

表一:页面运行时间: 2.62180089951 秒(非定长,int时间)

表二:页面运行时间: 2.5475358963 秒(定长,int时间)

表三:页面运行时间: 2.45077300072 秒(varchar,datetime时间)

表四:页面运行时间: 2.82798409462 秒(char,datetime时间)

测试四个表的读取,分别select 100条随机记录,以主键id为条件查询,并记录时间:

表一:页面运行时间: 0.382651090622 秒(非定长,int时间)

表二:页面运行时间: 0.542181015015 秒(定长,int时间)

表三:页面运行时间: 0.334048032761 秒(varchar,datetime时间)

表四:页面运行时间: 0.506206989288 秒(char,datetime时间)

测试四个表的读取,分别select 10条随机记录,以star_time为条件查询,并记录时间:

表一:页面运行时间: 30.1972880363 秒(非定长,int时间)

表二:页面运行时间: 65.1926910877 秒(定长,int时间)

表三:页面运行时间: 39.7210869789 秒(varchar,datetime时间)

表四:页面运行时间: 70.4632740021 秒(char,datetime时间)

因为量比较小,所以我们默认即使是微小的变化,也是有意义的。

结论:

大数据量下,如果存在大量的select * from table where 时间>xx这样的查询,在mysql5.1时使用int换datetime是有意义的。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网