Mysql Data type optimization

Source: Internet
Author: User

One, date and time type

MySQL can use many types to hold date and time values.

Such as: Year and date. The minimum time granularity that MySQL can store is seconds (MARIADB supports microsecond-level time types). But MySQL can also use subtle levels of granularity for temporary operations, and we'll show you how to circumvent this storage limit.

Datetime

This type can hold a wide range of values, from 1001 to 9999, with a precision of seconds. It encapsulates the date and time in an integer formatted as YYYYMMMDDHHHMMSS, regardless of the time zone. Use 8 bytes of storage space.

By default, MySQL displays datetime values in a sortable, unambiguous format, such as "2015-10-24 09:57:33". This is the date and time representation method defined by the ANSI standard.

TIMESTAMP

Like its name, the timestamp type holds the number of seconds since January 1, 1970 0 o'clock (Greenwich Mean Time), which is the same as the Unix timestamp. Timestamp uses only 4 bytes of storage space, so it has a much smaller range than DateTime: It can only be stored for 1970-2038 years.

If you store or access data in more than one time zone, the behavior of Timestamp and datetime will be very different.

The value provided by timestamp is related to the time zone, which just retains the date and time of the text representation.

You can also specify the behavior of add and update for timestamp, and the default for the timestamp column is not NULL

In addition to special behavior, you should always use timestamp as much as possible, because it is more efficient than datetime space Sometimes people will store the Unix timestamp as an integer value, but this will not bring any benefit. The format of saving timestamps with integers is often inconvenient, so this is not recommended.


What if you need to store a smaller granularity date and time than the second?

MySQL does not currently provide the appropriate data type, but it can be stored by using its own format: You can use the bigint type to store a micro-level timestamp, or use a double to store the decimal part after the second. Either way, you can, or you can use MARIADB instead of MySQL




Mysql Data type optimization

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.