dpf5207 2013-08-02 09:24
浏览 312
已采纳

将unix时间戳存储为无符号整数会有什么好处吗?

I don't want to start another discussion of pro or contra between using DATETIME vs TIMESTAMP vs INT. (I've already read articles like Using MySQL's TIMESTAMP vs storing timestamps directly.)

I sometimes use INT data type to store unix timestamps in database. That's because date and time calculations in my applications where done with unix timestamps often (e.g. for session timeouts and token expiration). Furthermore data selection in database is faster than using DATETIME when I can compare integer values in WHERE clause simply. There are few tables with 10+ million rows (up to 100 million) where this 4 bytes really save storage (on disk and in memory because of smaller indexes) also.

Regarding Y2K38 problem, I assume (and hope so), that UNIX_TIMESTAMP in MySQL an time() in PHP will return 64bit values in future, so there will be no need to change something in the applications itself. The point is, I've stored all these pseudo integer timestamp in MySQL as unsigned integers (I mean INT not BIGINT). Sure, unsigned integer timestamps will overflow in year 2106, but that's a bit more time than 2038.

My question is now: assuming that UNIX_TIMESTAMP itself will work after 2038, could there be any problems in MySQL and/or PHP until 2106 when these timestamps are stored as unsigned integer in MySQL? (Please don't argue with: there will be a lot time to fix that until 2038, I want to clarify this from the point of the application are not touched any longer)

EDIT: Because the question came up: I store only current timestamps in these columns, no birthdates, no future dates. Only current timestamps, so I want to clarify if this would work after 2038.

  • 写回答

1条回答 默认 最新

  • drnzpd579935 2013-08-28 12:15
    关注

    Your assumption surrounding UNIX_TIMESTAMP() is a big one.

    Currently, UNIX_TIMESTAMP returns 0 if you try

    mysql> select unix_timestamp("2038-01-19" );
    +-------------------------------+
    | unix_timestamp("2038-01-19" ) |
    +-------------------------------+
    |                    2147468400 |
    +-------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select unix_timestamp("2038-01-20");
    +------------------------------+
    | unix_timestamp("2038-01-20") |
    +------------------------------+
    |                            0 |
    +------------------------------+
    1 row in set (0.00 sec)
    

    While the storage of INTs longer than 32-bits will work, unless you know something about how the implementation of unix_timestamp(int64) will work, then the question is really more guesswork than facts.

    This implies that any integer arithmetic you do will still be valid with 64-bit ints, so for finding expired sessions (timestamp + timeout < (seconds since 1970 in 64-bits)) will still work. Whether or not you can rely on from_unixtime() and unix_timestamp()-functions depend whether the solution is just to up the ante to 64-bits or if the whole world in the next 20-odd years decide to set a new epoch.

    Nobody knows for sure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?