MySQL 中存储微时间差的最佳数据类型
我将记录处理 REST 请求所需的时间,但我尚未决定哪种数据类型最适合该列。时间差将以毫秒为单位,我预计它不会小于 1 毫秒,但可能会是几秒(当然希望它不会更高!)。
与 按时间存储跨度的最佳方式非常相似在 MySQL 数据库中?,但同时具有更高的精度。
我相信我有两个选择:时间戳或整数。时间戳具有微秒精度,整数可以具有我需要的任何实际精度(只需将时间差异乘以 Y * 1000)。
PHP 中的时间差将通过
$start = microtime(true);
// do something
$runtime = microtime(true) - $start;
哪种数据类型最适合在 MySQL 中存储毫秒级的时间差?
对于这种情况,时间戳和整数的优缺点是什么?
I'm going to be logging the time it takes to process a REST request and I'm undecided on which data type would be best for the column. The time difference would be in the millisecond scale, where I wouldn't expect it to be less than 1ms but could be a couple seconds (certianly hope it won't be higher!).
Very similar to Best way to store span on time in a MySQL database?, but while having a higher precision.
I believe I have two options: timestamp or integer. Timestamp has microsecond precision and integer could have any practical precision I need (would just multiply time diff by Y * 1000).
The time difference will be calculated in PHP by
$start = microtime(true);
// do something
$runtime = microtime(true) - $start;
Which data type would be best to store a time difference on the millisecond scale in MySQL?
What are the pros/cons of timestamp and integer for this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
时间戳用于存储某个时刻,而不是持续时间。
TIMESTAMP
不存储毫秒,TIME
也不会。使用整数字段。我还发现了一个类似的问题:在 MySQL 中存储微秒:哪种解决方法?。
The timestamp is used to store a moment in time, not a duration of time.
TIMESTAMP
doesn't store milliseconds and neither willTIME
. Go with an integer field.I also found a similar question: Storing microseconds in MySQL: which workaround?.
时间戳不会削减它。
即使您执行几行来捕获“时间时刻”以执行两者的增量(差异),其最佳精度也只能以秒为单位。
我会用整数来做。这样您就可以在毫秒内进行
乘数
。例如,0.1 毫秒可以在整数列中存储为“1”。由于整数只能存储在整数列中,因此通过使用硬乘数规则,您可以向后计算以确定毫秒。Timestamp is not going to cut it.
Even if you do several rows to capture "moments in time" to do a delta (difference) of the two, at it's best precision would only be in seconds.
I would do it in integer. This way you can do
multipliers
on miliseconds. For example, .1 milisecond can be stored as "1" in the integer column. Because integers can only be stored in an integer column, by having a hard multiplier rule, you can work backwards to determine milliseconds.