数据库设计 - 考虑到数据库的效率,有必要把所有的时间datetime都设置为timestamp吗?

查看:104
本文介绍了数据库设计 - 考虑到数据库的效率,有必要把所有的时间datetime都设置为timestamp吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

各种表从10行数据到100万行数据
虽然timestamp的缺点只支持到2035年,但是在这个缺点忽略不计的情况下,是否每个表都应该把datetime设置为timestamp呢?
从维护的角度考虑是否一些小表可以保留datetime呢

解决方案

你需要考虑到 timestamp 潜在的时区问题:

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored

根据 MySQL 官方文档, TIMESTAMP 在存入时, 会根据当前时区转换为 UTC 时间, 而在取出也会根据当前时区将 UTC 时间转换为 TIMESTAMP. 因此如果你的程序的时区有变化时, 那么写入和读出的 TIMESTAMP 就不一样了.

如果没有上面的时区的问题, 那么我觉得 TIMESTAMP 比较好一些.

这篇关于数据库设计 - 考虑到数据库的效率,有必要把所有的时间datetime都设置为timestamp吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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