MySQL 日期时间和时间戳字段是否比 Unix 时间戳整数更适合 PHP 应用程序?
我正在阅读一篇文章,其中显示了一些非常好的信息和基准,关于三种不同的 MySQL 日期/时间存储选项的性能。
MySQL DATETIME vs TIMESTAMP vs INT 性能以及使用 MyISAM 进行基准测试
在阅读本文时,您开始意识到使用整数只是一种浪费,您应该使用 MySQL Datetime 或 Timestamp 列类型。
然而,在文章的最后,他又做了一个不使用 MySQL 函数的测试,您突然发现,在按 unix 时间戳搜索时,直接 INT 的速度是两个 MySQL 选项的 2 倍。
所以我突然明白了 - 呃,PHP 应用程序都使用什么? time()! 几乎每个 PHP 应用程序的逻辑都基于 Unix Epoch。这意味着大多数对特定时间结果的查询都是基于 time() 开始的,然后转换为使用 MySQL 的字段。
这给我留下了以下内容:
存储为 INT 的 Unix 时间戳是 速度更快、占用空间更小、工作效率更高 本地基于 PHP 的 time() 计算。
MySQL日期类型更适合 来自MySQL的操作和逻辑 适合
目前 Unix 和 MySQL 时间戳仅有效到 2037 这意味着你必须使用 较大日期的日期时间字段 未来。
像
date = NOW()
这样的 MySQL 命令可能会滞后 使用复制会导致数据不一致。
所以将其应用到现实生活中我们看到了答案 考虑到大多数真正的 DBA 会使用像 PostgreSQL 这样更好的引擎,这些结果是否存在?
但是,大多数达到使用数据库逻辑级别的应用程序可能会使用 PostgreSQL。这意味着我们所有其他程序员只使用 MySQL 作为我们数据的存储罐(你知道这是真的),这使得保持字段小、快,UNIX INT 看起来就像它实际上是最好的选择。
那么你们觉得怎么样?
时间戳真的比 MySQL 日期字段更适合 PHP 应用程序吗?
I was reading over an article that shows some really good information and benchmarks about how well the three different MySQL date/time storage options perform.
MySQL DATETIME vs TIMESTAMP vs INT performance and benchmarking with MyISAM
While reading the article you start to get the idea that using ints are just a waste and you should instead go with MySQL Datetime or Timestamp column types.
However, towards the end of the article he does one more test not using MySQL functions and you suddenly see that straight INT's are 2x as fast as the two MySQL options when searching by unix timestamps.
So it suddenly dawned on me - duh, what do PHP apps all use? time()! Almost every php application bases their logic off of the Unix Epoch. Which means that most queries for results in a certain time start off based on time() and then are converted to work with MySQL's fields.
This leaves me with the following:
Unix Timestamps stored as INT's are
faster, take less space, and work
natively with PHP's time() based
calculations.MySQL Date types are more suited to
operations and logic from the MySQL
side.For the time being both Unix And
MySQL Timestamps only work until
2037 which means that you must use a
datetime field for larger dates in
the future.MySQL commands like
date = NOW()
can lag when
using replication causing data inconsistencies.
So applying this to real life we see that answer
that these results given that most really DBA's would use a better engine like PostgreSQL - is there arny
However, most apps that would be to the level of using DB logic would probably go with PostgreSQL. Which means that all the rest of us programmers only use MySQL for a storage tank for our data (you know it's true) which makes keeping the fields as small, fast, UNIX INT's seem like it is actually the best option.
So what do you guys think?
Are timestamps really more suited to PHP apps than the MySQL date fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MySQL 的日期格式不存在 2038 年问题。
MySQL 的日期从 1000 年到 9999 年都是可靠的,而 Unix 时间戳在 2038 年之后或 1902 年之前可能会出错,除非系统中的所有内容都是 64 位。
但是,如果您使用 PHP,这可能没有实际意义:PHP 在其大部分日期和时间函数中使用 unix 时间戳来表示日期和时间,除非您使用 64 位版本,否则它将具有相同的限制。
您将使用专门用于此目的的字段类型。
如果您关心的话。将日期作为 unix 时间戳放入 INT 字段并不具有自描述性;如果不以适当的方式转换数据,您就无法查看数据。但这对你来说可能没有什么区别。
另一方面,考虑到您正在使用 PHP,一旦您将时间转换为 PHP,您就必须将其转换回 Unix 时间戳才能对其进行任何有用的操作,因为对于 PHP 来说,Unix 时间戳是本国的。
编辑:
当我写这个答案时,我没有使用 PHP 的 DateTime 类。使用 DateTime 类消除了使用 Unix 时间戳的任何需要,并消除了 32 位/64 位问题。感谢查尔斯下面的评论指出了使用它的好方法。
MySQL's date format has no year 2038 problem.
MySQL's dates are reliable from the year 1000 to the year 9999, whereas Unix timestamps can screw up after 2038 or before 1902 unless everything in your system is 64-bit.
If you're using PHP, however, this can be moot: PHP uses unix timestamps for dates and times throughout most of its date and time functions and unless you are using a 64-bit build it will have the same limitation.
You'd be using the field type that was intended for this purpose.
If you care. Putting date into an INT field as a unix timestamp is not as self-describing; you can't look at the data without converting it in the appropriate way. But that may make no difference to you.
The flip side of this, given that you're using PHP, is that once you get the time into PHP you'd have to convert it back to a Unix timestamp anyway to do anything useful with it, because to PHP, Unix timestamps are native.
Edit:
Back when I wrote this answer, I didn't use PHP's DateTime class. Using the DateTime class removes any need to use Unix timestamps, and removes the 32-/64-bit issues. Thanks to Charles' comment below for pointing out a good way to use this.
我总是喜欢以 mySQL 格式存储日期,因为它使查询中的比较更简单。 mySQL 也有一些很棒的日期格式选项:
http://www.dan.co.uk/mysql-date-format/< /a>
抱歉,我应该补充一点,我真的不知道哪个速度效率更高,这是您问题的重要部分。
I always prefer to store dates in mySQL format as it makes comparisons simpler in your queries. mySQL has some great date formatting options too:
http://www.dan.co.uk/mysql-date-format/
Sorry, I should add, I really don't know about which is more efficient speed-wise which was an important part of your question.
我喜欢将所有逻辑保留在单个高级域中(即用 php 编写的应用程序)。 MySQL 是一个储罐——它应该保留。我更喜欢使用诸如 http://www.symfony-project.org/plugins/ 之类的类sfDateTime2Plugin 然后 ->dump() 或 ->get() 为适当的格式。在应用程序域中编写(和扩展)高级操作比使用静态 mysql 接口更快、更容易。
PostgreSQL 的接口在 MySQL 上进行了清理。但我们在这里仍然讨论 MySQL,因为它很流行。这就提出了一个重要的考虑因素。在编写代码或设计系统时,遵守约定通常是有意义的,即使它的计算效率低于其他鲜为人知的选项。这很重要,因为它有利于另一种效率——其他人的可读性。通常,与计算效率低下相比,可读性和可理解性低效率会导致更大的业务支出(和时间)。
不过我完全赞成尝试 INT。请尝试一下并写下您的发现。
干杯
I like to keep all of the logic in a single, high-level domain (that being the app written in php). MySQL is a storage tank--as it should stay. I prefer to use a class such as http://www.symfony-project.org/plugins/sfDateTime2Plugin and then ->dump() or ->get() to the appropriate format anyways. It's much faster and easier to write (and extend) high level manipulations in the application domain than it is using the static mysql interface.
PostgreSQL's interface cleans up on MySQL. But we're still talking about MySQL here because it's popular. Which brings up an important consideration. When writing code or designing systems, often it makes sense to observe the convention, even if it is less computationally efficient than other less known options. This is important because it favours a different kind of efficiency--readability for others. Often readability and understandability inefficiencies account for larger business expenses (and time) than do computational inefficiencies.
I'm all for trying INTs though. Please give it a shot and write about your findings.
Cheers
使用 MySQL 的各种时间和日期格式可以实现使用 Unix 时间戳难以进行的查询。
一个示例是根据特定周(周数)过滤数据,或者在添加或删除特定时间范围后使用数据库中的值。
MySQL 有一些很棒的函数用于时间和日期操作 可以很好地处理日期、日期时间和时间格式。
我们的大多数网站都使用 PHP/MySQL,并且我们自动创建数据库到 PHP 对象,从 PHP 更改为 MySQL 格式的代码非常简单:
MySQL 到 PHP:
strtotime() for datetime
mktime() 用于获取时间和日期
Using MySQL's various time and date formats allows for queries that would be difficult using Unix timestamps.
An example would be filtering data based on a particular week (week number), or using a value in the database after adding or removing a certain timeframe from it.
MySQL has some great functions for time and date manipulation which work well with the date,datetime, and time formats.
We use PHP/MySQL for most of our sites, and we automate database to PHP object creation, the code to change from PHP to MySQL formats is very simple:
MySQL to PHP:
strtotime() for datetime
mktime() for time and date
很好的开放性问题。我看你是个完美主义者。我也是。
但就像编程和生活中的几乎所有事情一样,这取决于它如何适合您的问题。
如果性能确实至关重要,则应使用 UNIX 时间戳。
但我真的不认为是这样。我告诉你为什么。这是因为我与 Rasmus Lerdorf 有着相同的观点。 PHP 是一种脚本语言,它为中小型企业带来了许多便利。
对于真正关键/大型应用程序,可扩展性和性能非常重要,您根本不应该使用 PHP + MySQL。
Java 或 C++ 是更好的解决方案。我想这里的大多数人都会问“PHP 出了什么问题,你这个混蛋?!”。嗯,实际上很多事情。我曾担任过一段时间的性能测试人员,我说开发人员应该记住,您最喜欢的语言并不总是解决每个问题的最佳解决方案。
让我给你举个例子。关键的数学/物理应用程序。只需要一个数字即可进行现象分析。您可以在 Shell 脚本和 C 上完成此操作。C 的性能会好得多。看,选择最适合您的问题的语言和工具就是您的正确答案。
让我们回到 MySQL、PHP 和数据类型。如果您正在使用这些,我想应用程序不是那么大,也不是充满业务规则(如果它那么大,您会考虑一些编译语言,如果它是如此关键,您应该考虑使用 PostgreSQL 或 Oracle)。
在这种情况下,重要的是构建应用程序的速度。如果您这样做,我认为一个好的开始方法是将表单字段基于数据库元数据。这可以帮助您自动化表单构建。在这种情况下,我建议使用本机数据库类型。
Nice and open question. I see you are a perfeccionist. So am I.
But as almost everything in programming and life, it depends on how it would fit to your problem.
If performance is really critical, you should use UNIX timestamps.
But I really don't think it is the case. I tell you why. It is because I share the same point of view as Rasmus Lerdorf. PHP is a scripting language, which brings many facilities to small to medium business.
For really really critical/big apps, where scalability and performance really matters, you should not use PHP + MySQL at all.
Java or C++ are the better solutions. I think most guys here will ask "What is wrong with PHP, your bastard?!". Well, many things actually. I worked as a performance tester for a while, and I say that developers should keep in mind that your favorite language is not always the best solution for every problem.
Let me give you and example. A critical math/physics application. Just need a number for a phenomenon analysis. You can do it on Shell Script and C. C will perform far way better. See, choosing the most appropriate language and tools to fit your problem is the answer to your correct answer.
Let's get back on MySQL, PHP and the data types. If you are using those, I suppose the application is not so big, nor full of business rules (if it is that big, you would consider some compiled languages, and if it is so critical, you should consider using PostgreSQL or Oracle).
And in this case, what matters is the speed to build up the application. If you do so, I think a good way to start is base your form fields on database metadata. This can help you to automate the form building. And in this case, I recommend using native database types.
如果您使用 INT 而不是 DATETIME,您将失去按日期、小时或时间进行 GROUP 的灵活性,从而对间隔进行不同的操作。
您可以使用函数 FROM_UNIXTIME 进行 INT 处理,但您的查询将不可读。
使用 INT 代替 DATE 会使您的编程成本是使用 DATE 时的 3 倍。您的安全执行时间不足以支付编程成本。硬件比复杂的编程更便宜。
一旦我们犯了这个错误,并将日期保存在 INT 中。半年后,我们决定对约30处部位进行耐火处理,以便于维护。
If you use INT instead of DATETIME you lose flexibility in GROUP by date, hour or time, making different manipulation with intervals.
You can make it with INT using function FROM_UNIXTIME, but your query's will be unreadable.
Using INT instead of DATE makes your programing cost x3 then your work with DATE. You safe executing time not enough to cover programming cost. Hardware is more cheaper then complex programming.
Once we have made this mistake and holds date in INT. After half year we decide to refractory about 30 sites for easy maintenance.