MySQL - 以小时和分钟为单位的简单时间的最佳数据类型 [英] MySQL - best data type for simple time with hours and minutes

查看:120
本文介绍了MySQL - 以小时和分钟为单位的简单时间的最佳数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 MySQL 5 数据库中表示任务的到期时间.
然后,我需要存储这样的时间:
_ 18
_ 9:15
_ 12:00

I need to represent the due time for tasks in my MySQL 5 database.
Then, I need to store times like these:
_ 18
_ 9:15
_ 12:00

现在我处于两难境地,是使用时间数据类型还是仅使用 4 位整数,因为我只需要存储小时和分钟.
在第二种情况下,我会存储:
_ 1800
_ 0915
_ 1200

Now I am in dilemma whether to use the time datatype or just a 4-digit integer as I just need to store hours and minutes.
In the second case, I would store:
_ 1800
_ 0915
_ 1200

这两种方法的含义是什么?
考虑到最重要的要求是在庞大的数据集(数百万行)中实现高性能,您会实施哪种解决方案?

What are the implication of both the approaches?
Which solution would you implement given that the most important requirement is high performance in a huge dataset (millions of rows)?

应用程序代码是用 PHP 编写的.

The application code is written in PHP.

我想到了一个重要的观点.如果我使用整数,当我显示时间(插入分号)时,我需要用 PHP 进行一些字符串操作,这可能会使使用整数的增益无效

I was thinking of an important point. If I use integers, I need to do some string manipulation with PHP when I display the time (to insert the semicolon) that probably nullify the gain with using integers

谢谢,

推荐答案

使用 time 数据类型.主要好处是可以重用 MySQL 中嵌入的现有时间函数.

Use time datatype. Main benefit will be reusing existing time function embedded in MySQL.

使用 smallint(只有 2 个字节)在空间方面可能会更有效,但您将失去使用现有函数对语义为 time 的字段进行操作的能力 值.我可以将格式化函数、时差、时区视为您可以立即利用 time 数据类型的场景示例.

It will probably be more efficient in terms of space using a smallint (only 2 bytes) but you will loose the ability to use existing functions to operate on fields whose semantic is a time value. I can think of formatting functions, time difference, time zones as examples of scenarios where you could take immediate advantage of the time datatype.

这篇关于MySQL - 以小时和分钟为单位的简单时间的最佳数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆