Storing microseconds in MySQL: which workaround?

喜你入骨 提交于 2019-12-04 03:14:20

If you say that the most popular queries are time base, I would recomend going with a single column that stores the time as in your first option.

You could pick your own epoch for the application, and work from there.

This should simplify the queries that needs to be written when searching for the time intervals.

Also have a look at 10.3.1. The DATETIME, DATE, and TIMESTAMP Types

However, microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded. Conversion of TIME or DATETIME values to numeric form (for example, by adding +0) results in a double value with a microseconds part of .000000

MySQL will support microseconds, see MySQL 5.6.4 changelog:

Fractional Seconds Handling

Incompatible Change: MySQL now permits fractional seconds for TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision. To define a column that includes a fractional seconds part, use the syntax type_name(fsp), where type_name is TIME, DATETIME, or TIMESTAMP, and fsp is the fractional seconds precision. For example:

CREATE TABLE t1 (t TIME(3), dt DATETIME(6)); The fsp value, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)

The following items summarize the implications of this change. See also Section 10.3.5, “Fractional Seconds in Time Values”.

How about splitting the date parts into a date-only part, and microseconds from midnight? There are less than 2^64 microseconds in the day. Then cluster the table on {date, microsecond}.

I would guess, though I don't know your data, that certain queries would be fine with day-level accuracy -- 'experiments in 1964' doesn't need to worry about microseconds.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!