V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
RiseAgainst
V2EX  ›  MySQL

存储时间的字段用int还是用date,time?

  •  
  •   RiseAgainst · Aug 26, 2012 · 10970 views
    This topic created in 5000 days ago, the information mentioned may be changed or developed.
    看了一些程序的数据库,有些用date,datetime类型存储时间
    有些用int存储unix时间戳 这两种区别是什么?实际应用中改怎么选择?
    5 replies    1970-01-01 08:00:00 +08:00
    Js
        1
    Js  
       Aug 26, 2012   ❤️ 1
    signed int和timestamp没区别, 4字节,1970.1.1~2038.1.19

    datetime和上面的区别不大,8字节, 没范围

    date就是纯年月日的日期, 3字节

    time就是纯时分秒的时间, 3字节
    lukydd
        2
    lukydd  
       Aug 26, 2012
    没什么特殊需求就用unix时间戳吧...
    AlloVince
        3
    AlloVince  
       Aug 26, 2012   ❤️ 1
    datetime和date是人直接可以识别的格式,方便DBA,时间戳还需要再进行一次转换,只是存储空间占用稍小。

    性能上看看这篇评测,使用转换函数进行检索时性能会有一个比较大的下滑,当然你可以选择用非MySQL的方式转换时间,这会带来额外的工作量。
    http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/

    我在项目中一般都会用datetime,时区统一为UTC
    yoyicue
        4
    yoyicue  
       Aug 26, 2012   ❤️ 1
    亦在项目中一般使用 datetime,时区统一为 UTC
    一般函数 return 也是 datetime
    优势:
    方便和 pytz 做时区转换
    方便可读的时间加减法
    breestealth
        5
    breestealth  
       Aug 26, 2012
    用int型就是典型的时间换空间的做法。而且不能使用数据库提供的时间计算函数,在某些情况下比较麻烦。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6078 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 02:50 · PVG 10:50 · LAX 19:50 · JFK 22:50
    ♥ Do have faith in what you're doing.