MySQL deals with milliseconds and microseconds, MySQL gets milliseconds!

Source: Internet
Author: User

Many friends migrating to MySQL from Oracle or SQL SERVER encounter the same problem, that is, the storage and display of milliseconds. Since MySQL only provides datetime, TIMESTAMP, time, date, year, and the minimum unit of datetime and TIMESTAMP is seconds, there is no function to store the millisecond level. But MySQL can recognize the milliseconds in time. And we have a number of ways to get milliseconds, such as functions: Microsecond and so on.

Let me give you a simple example to store the parts before and after the second.
For the application of the Time field as the primary key, we can create the following tables for the corresponding conversions:

mysql> CREATE TABLE Mysql_microsecond (log_time_prefix timestampnot null default 0, Log_time_suffix mediumint not Null default 0) engine Innnodb;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> ALTER TABLE Mysql_microsecond add primary key (Log_time_prefix, log_time_suffix);
Query OK, 0 rows affected (0.01 sec)
records:0 duplicates:0 warnings:0

mysql> Set @a = Convert (Concat (now (), '. 222009 '), datetime);
Query OK, 0 rows Affected (0.00 sec)

mysql> INSERT INTO Mysql_microsecond select Date_format (@a, '%y-%m-%d%h-%i-%s '), Date_format (@a, '%f ');
Query OK, 1 row Affected (0.00 sec)
Records:1 duplicates:0 warnings:0

mysql> SELECT * from Mysql_microsecond;
+---------------------+-----------------+
| Log_time_prefix | Log_time_suffix |
+---------------------+-----------------+
|          2009-08-11 17:47:02 | 222009 |
+---------------------+-----------------+
1 row in Set (0.00 sec)


Either use varchar to store all the time fields, or store a hash to ensure performance!
A lot of ways to see how your application is reasonable.


MySQL newer version (MySQL 6.0.5), also has not produced a microsecond function, now () can only be accurate to seconds. There is also no date-time type stored in MySQL with milliseconds or microseconds.

But, oddly enough, MySQL already has a function of extracting (extract) microseconds. For example:

Select Microsecond (' 12:00:00.123456 ');               --123456select microsecond (' 1997-12-31 23:59:59.000010 '); --10
Select Extract (microsecond from ' 12:00:00.123456 ');  --123456select Extract (microsecond from ' 1997-12-31 23:59:59.000010 '); --10
Select Date_format (' 1997-12-31 23:59:59.000010 ', '%f '); --000010

Still, you want to get a millisecond, microsecond, or something in the application-level program for MySQL. If you get time in your application that contains microseconds: 1997-12-31 23:59:59.000010, when MySQL is stored, you can design two fields: C1 datetime, C2 mediumint, respectively, and hold the date and microsecond. Why not use char to store it? The char type requires bytes, and datetime + Mediumint is only 11 (8+3) bytes.

Millisecond, microsecond noun explanation:
Milliseconds: Millisecond--1 per thousand sec microseconds: microsecond--one out of 10,000 seconds 1 seconds = 1000 milliseconds; 1 milliseconds = 1000 microseconds


MySQL deals with milliseconds and microseconds, MySQL gets milliseconds!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.