How MySQL stores data in milliseconds

Source: Internet
Author: User

A considerable number of new MySQL users have encountered the same problem: storage and display in milliseconds. Because MySQL only provides DATETIME, TIMESTAMP, TIME, DATE, and year time types, and the minimum unit of DATETIME and TIMESTAMP is second, there is no function for storing milliseconds. However, MySQL can recognize the millisecond part of the time. In addition, we have multiple ways to obtain the millisecond part, such as the function: microsecond.

Here is a simple example to store the parts before and after seconds.
For applications that use the time field as the primary key, we can create the following table for conversion:

mysql> create table mysql_microsecond ( log_time_prefix timestamp not 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)


You can use VARCHAR to store all the time fields or store a HASH to ensure performance!
There are many methods, depending on how your application is used properly.

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.