MySQL 中存储微时间差的最佳数据类型

发布于 2024-11-17 12:58:17 字数 553 浏览 4 评论 0原文

我将记录处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

婴鹅 2024-11-24 12:58:17

时间戳用于存储某个时刻,而不是持续时间。 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 will TIME. Go with an integer field.

I also found a similar question: Storing microseconds in MySQL: which workaround?.

标点 2024-11-24 12:58:17

时间戳不会削减它。

TIMESTAMP 列以与 DATETIME 相同的格式显示
列。也就是说,显示宽度固定为19个字符,
格式为“YYYY-MM-DD HH:MM:SS”。

即使您执行几行来捕获“时间时刻”以执行两者的增量(差异),其最佳精度也只能以秒为单位。

我会用整数来做。这样您就可以在毫秒内进行乘数。例如,0.1 毫秒可以在整数列中存储为“1”。由于整数只能存储在整数列中,因此通过使用硬乘数规则,您可以向后计算以确定毫秒。

Timestamp is not going to cut it.

TIMESTAMP columns are displayed in the same format as DATETIME
columns. In other words, the display width is fixed at 19 characters,
and the format is 'YYYY-MM-DD HH:MM:SS'.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文