iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高
  • 636
分享到

Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高

2024-04-02 19:04:59 636人浏览 安东尼
摘要

目录# 后数据准备# sql查询速率测试# sql分组速率测试# sql排序速率测试# 小结数据库中可以用datetime、bigint、timestamp来表示时间,那么选择什么类

数据库中可以用datetime、bigint、timestamp来表示时间,那么选择什么类型来存储时间比较合适呢?

# 后数据准备

通过程序往数据库插入50w数据

数据表:


CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time_date` datetime NOT NULL,
  `time_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `time_long` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `time_long` (`time_long`),
  KEY `time_timestamp` (`time_timestamp`),
  KEY `time_date` (`time_date`)
) ENGINE=InnoDB AUTO_INCREMENT=500003 DEFAULT CHARSET=latin1

其中time_long、time_timestamp、time_date为同一时间的不同存储格式

实体类users



@Builder
@Data
public class Users {
    
    private Long id;

    
    private Date timeDate;

    
    private Timestamp timeTimestamp;

    
    private long timeLong;
}

dao层接口



@Mapper
public interface UsersMapper {
    @Insert("insert into users(time_date, time_timestamp, time_long) value(#{timeDate}, #{timeTimestamp}, #{timeLong})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int saveUsers(Users users);
}

测试类往数据库插入数据


public class UsersMapperTest extends BaseTest {
    @Resource
    private UsersMapper usersMapper;

    @Test
    public void test() {
        for (int i = 0; i < 500000; i++) {
            long time = System.currentTimeMillis();
            usersMapper.saveUsers(Users.builder().timeDate(new Date(time)).timeLong(time).timeTimestamp(new Timestamp(time)).build());
        }
    }
}

生成数据代码方至GitHubhttps://github.com/TiantianUpup/sql-test/ 如果不想用代码生成,而是想通过sql文件倒入数据,文末附sql文件网盘地址。

# sql查询速率测试

通过datetime类型查询:


select count(*) from users where time_date >="2018-10-21 23:32:44" and time_date <="2018-10-21 23:41:22"

耗时:0.171

通过timestamp类型查询


select count(*) from users where time_timestamp >= "2018-10-21 23:32:44" and time_timestamp <="2018-10-21 23:41:22"

耗时:0.351

通过bigint类型查询


select count(*) from users where time_long >=1540135964091 and time_long <=1540136482372

耗时:0.130s

结论 在InnoDB存储引擎下,通过时间范围查找,性能bigint > datetime > timestamp

# sql分组速率测试


使用bigint 进行分组会每条数据进行一个分组,如果将bigint做一个转化在去分组就没有比较的意义了,转化也是需要时间的

通过datetime类型分组:


select time_date, count(*) from users group by time_date

耗时:0.176s

通过timestamp类型分组:


select time_timestamp, count(*) from users group by time_timestamp

耗时:0.173s

结论 在InnoDB存储引擎下,通过时间分组,性能timestamp > datetime,但是相差不大

# sql排序速率测试

通过datetime类型排序:


select * from users order by time_date

耗时:1.038s

通过timestamp类型排序


select * from users order by time_timestamp

耗时:0.933s

通过bigint类型排序


select * from users order by time_long

耗时:0.775s

结论:在InnoDB存储引擎下,通过时间排序,性能bigint > timestamp > datetime

# 小结

如果需要对时间字段进行操作(如通过时间范围查找或者排序等),推荐使用bigint,如果时间字段不需要进行任何操作,推荐使用timestamp,使用4个字节保存比较节省空间,但是只能记录到2038年记录的时间有限。

文中sql文件网盘地址: 链接: Https://pan.baidu.com/s/1cCRCxtTlPriXMERGsbnb_A 提取码: hbq2

到此这篇关于Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高的文章就介绍到这了,更多相关数据库datetime、bigint、timestamp内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高

本文链接: https://www.lsjlt.com/news/133221.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作