使用 MySQL 的 TIMESTAMP 与直接存储时间戳

发布于 2024-11-29 06:23:35 字数 729 浏览 1 评论 0原文

我对于以 MySQL 的 TIMESTAMP 格式保存日期和时间值与以自定义 UNSIGNED INT 格式保存日期和时间值感到左右为难。这里主要考虑的是检索速度、PHP 中适当的范围计算以及偶尔格式化为人类可读值。

每种类型所需的存储空间及其范围:

DATETIME        8 bytes  '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP       4 bytes  '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
UNSIGNED INT    4 bytes  (Maximum Value 4294967295)

我根本不需要DATETIME的范围。我在 TIMESTAMP 和 UNSIGNED INT 之间左右为难。

支持 UNSIGNED INT 的论点:

  • UNIX 时间戳 4294967295 转换为 Sun, 07 Feb 2106 06:28:15 GMT,这比 TIMESTAMP 更好,对我来说足够好
  • 直接在 PHP 中比较这些时间戳会比通过 strtotime 转换 TIMESTAMP 更快() 然后比较它们

TIMESTAMP 给我的唯一优势是当我从手动修改 mysql 表并需要“查看”它们。

是否有任何令人信服的理由使用 TIMESTAMP 而不是 UNSIGNED INT?

I'm in a dilemma about saving date and time values in MySQL's TIMESTAMP format vs in a custom UNSIGNED INT format. The main considerations here are speed of retrieval, appropriate range calculations in PHP and occasional formatting into human readable values.

The storage space required for each type and their ranges:

DATETIME        8 bytes  '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP       4 bytes  '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
UNSIGNED INT    4 bytes  (Maximum Value 4294967295)

I dont need the range of DATETIME at all. I'm torn between TIMESTAMP and UNSIGNED INT.

Arguments in favor of UNSIGNED INT:

  • A UNIX timestamp of 4294967295 converts to Sun, 07 Feb 2106 06:28:15 GMT which is more than TIMESTAMP and good enough for me
  • Comparing these timestamps directly in PHP would be faster rather than converting TIMESTAMPs via strtotime() and then comparing them

The only advantage TIMESTAMP would give me is when I'm reading in the values from the mysql table manually and need to 'see' them.

Is there any compelling reason to use TIMESTAMP and not an UNSIGNED INT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡的优雅 2024-12-06 06:23:35

TIMESTAMP 的参数

  • 它隐式地以 UTC 时区存储数据。无论您的会话时区是什么。如果您需要使用不同的时区,这很有用。
  • 您可以使用 DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP 拥有自动时间戳列(MySQL 5.6.5 之前每个表仅一列)
  • 您可以使用 datetime 函数进行日期比较、添加、减法、范围查找等,无需使用 FROM_UNIXTIME() 函数 - 这将使编写可以使用索引的查询变得更容易
  • 在 PHP 中

    <前><代码>>>日期('Ymd h:i:s',4294967295);
    '1969-12-31 11:59:59'

    所以范围实际上是相同的

当 UNIX_TIMESTAMP() 用于 TIMESTAMP 列时,该函数
直接返回内部时间戳值,没有隐式
“字符串到 Unix 时间戳”转换

Arguments for TIMESTAMP

  • It implicitly stores data in UTC time zone. No matter what your session time-zone is. Useful if you need to use different time zones.
  • You can have automated timestamping columns using DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP (one column per table only until MySQL 5.6.5)
  • You can use datetime function for date comparison, addition, subtraction, range lookup etc, without the need to use FROM_UNIXTIME() function - it will make it easier to write queries that can use indexes
  • In PHP

    >> date('Y-m-d h:i:s',4294967295);
    '1969-12-31 11:59:59'
    

    so the range is in fact the same

When UNIX_TIMESTAMP() is used on a TIMESTAMP column, the function
returns the internal timestamp value directly, with no implicit
“string-to-Unix-timestamp” conversion

厌倦 2024-12-06 06:23:35

TIMESTAMP 的唯一真正用途是当您希望在更新行时自动更新该字段(这是该字段的默认行为),或者当数据存储要求非常严格以至于每行 4 个字节确实会产生影响时你。

实际上,比较应该在 DATETIME 和 UNSIGNED INT 之间进行,我推荐 DATETIME 因为:

  • 您可以使用 MySQL 的本机日期/时间函数按日期范围等进行选择。
  • 将这些日期选择为 UNIX 时间戳以便于格式化是非常容易的在 PHP 中:SELECT UNIX_TIMESTAMP(field) FROM table,无需选择原始值并使用 strtotime
  • 如果需要,可以更轻松地直接读取和编辑数据库中的字段(正如你所指出的)。
  • 在我看来,对日期范围没有限制

,第二点确实消除了以整数存储的任何理由。

The only real use for TIMESTAMP is when you want that field to be updated automatically when the row is updated (which is the default behaviour for that field), or when data storage requirements are so strict that 4 bytes per row really makes a difference to you.

Really the comparison should be between DATETIME and UNSIGNED INT, and I'd recommend DATETIME because:

  • You can use MySQL's native date/time functions for selecting by date ranges etc.
  • It is trivially easy to select these dates out as UNIX timestamps for easy formatting in PHP: SELECT UNIX_TIMESTAMP(field) FROM table, no need to select out the raw value and use strtotime
  • Easier to read and edit the fields in your database directly if you need to (as you pointed out).
  • No limitations on date range

Point two alone really removes any reason to store in integers, in my opinion.

花心好男孩 2024-12-06 06:23:35

这可能不是一个“科学”的答案,但我总是发现 MySql 在 TIMESTAMP 列上处理转换、算术、比较等的方式令人困惑。 UNSIGNED INT 列更加直接,我总是知道会发生什么。

PS 也许支持 TIMESTAMP 列的另一件事是它能够在每次更新或插入后自动设置为当前时间,但这并不是您不能没有的东西。

This might not be a "scientific" answer but I always find the way MySql handles conversion, arithmetics, comparsion, etc... on TIMESTAMP columns confusing. An UNSIGNED INT column is much more straight forward and I always know what to expect.

P.S. Perhaps one other thing in favor of TIMESTAMP column is its ability to be automatically set to current time after each update or insert but that is not something you can't live without.

归途 2024-12-06 06:23:35

一如既往,这取决于您需要保存什么。
例如,如果您正在使用某些 API 中的数据,并且它以数字形式向您发送时间(秒,这在市场数据中很常见),那么将其存储为 unsigned int 而不是每次都转换为字符串会更容易、更快在插入之前。

As always it depends on what you need to save.
For example if you are consuming data from some API and it sends to you the time as a number (seconds, this is common in market data) then could be easier and faster just to store it as unsigned int instead of converting everytime to a string before inserting it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文