Mysql the timestamp (timestamp) and explain solutions to problems of 2038

Copyright: If you please indicate the source, the exchange of learning: [email protected] https://blog.csdn.net/weter_drop/article/details/89924451

Time stamp refers GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) until now the total number of seconds.

Deploying a production environment with various versions of MySQL, including MySQL 5.5 / 5.6 / 5.7 N small three major version and version, since the MySQL upward compatibility is poor, resulting in the same SQL behave differently in different versions, from below several aspects in detail timestamp data type.

Timestamp data access

In the above-described three major MySQL version, the default time stamp (Timestamp) of the range type '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC , data accurate to the second level, which comprises in the range from about 2.2 billion value, so the type INT uses 4 bytes to store internal MySQL timestamp data:
1, when storing the time stamp data, first time the local region to convert UTC time zone, time zone UTC time and then converted to the format of milliseconds INT (using UNIX_TIMESTAMP function), then stored in the database.
2, the time stamp when reading data, the conversion value in milliseconds for the first-time format INT (using FROM_UNIXTIME function) UTC time, and then converted to the local time zone, and finally back to the client.

In MySQL version 5.6.4 and later, the time stamp type data may be highest accuracy microseconds (millionths of a second), the data type is defined as a timestamp (N), N in the range of 0-6, the default is 0, the required precision of milliseconds is set to timestamp (3), such as the need to precisely set the microsecond timestamp (6), the cost data to improve the accuracy of its internal storage space is becoming larger, but not changing the minimum timestamp type and maximum ranges.

Timestamp field defined

Timestamp field defined primarily affects two types of operations:

  1. When a record timestamp field contains DEFAULT CURRENT_TIMESTAMP, then the data specific time stamp field value as the current time is not inserted as a recording

  2. Update records, the timestamp field contains ON UPDATE CURRENT_TIMESTAMP, not specified as a specific time data update records the timestamp field value is set to the current time

PS1: CURRENT_TIMESTAMP indication CURRENT_TIMESTAMP () function to get the current time, similar to NOW () function

According to the above two types of operations, the timestamp column can have four combinations of definitions, meanings are:

  1. When a field is defined as a timestamp, it indicates that the field will not be automatically set to the current time when the inserts and updates.

  2. When the field is defined as the timestamp DEFAULT CURRENT_TIMESTAMP, represents the current time is given field specified value insertion and not only, not modified and not when the specified value before updating.

  3. When the field is defined as the timestamp ON UPDATE CURRENT_TIMESTAMP, it indicates that the field is inserted and not at the specified value is assigned "0000-00-00 00:00:00" is updated to the current time value when updating is not specified.

  4. When the field is defined as the timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, the specified value is expressed when the field is inserted or updated, were assigned to the current time.

PS1: construction of the table statement and the execution of final tables in MySQL statement will create differences, it is recommended to use SHOW CREATE TABLE TB_XXX get a table has been created to build the table statement.

MySQL timestamp field in each version of the differences in use

  1. In MySQL 5.5 and previous versions, only one time stamp field defines DEFUALT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP, but canceled the limit MySQL 5.6 and MySQL 5.7 version;

  2. MySQL 5.6 default parameters explicit_defaults_for_timestamp version value of 1, the default value is 0 in the parameter explicit_defaults_for_timestamp MySQL 5.7 version;

  3. In MySQL 5.5 and MySQL 5.7 version timestamp type defaults to NOT NULL, in the MySQL 5.6 release timestamp type defaults to NULL;

  4. When the construction of the table statement is scheduled for c1 timestamp,

  • 在MySQL 5.5中等价于c1 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

  • In MySQL 5.6 is equivalent to a c1timestamp NULL DEFAULT NULL;

  • 在MySQL 5.7中等价于c1 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

  1. When the construction of the table statement c1 timestamp default 0, the
  • In MySQL 5.5 is equivalent to a c1timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ;
  • In MySQL 5.6 is equivalent to a c1timestamp NULL DEFAULT '0000-00-00 00:00:00';
  • In MySQL 5.7 equivalent to c1timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ;

PS1: MySQL 5.6 and MySQL 5.7 release versions of the main differences defaults parameters affecting explicit_defaults_for_timestamp subject.

PS2: When the default value of the timestamp column "0000-00-00 00:00:00", use the "time stamp is not within the range," the default value does not generate a warning.

Timestamp type of exception thrown

When MySQL parameters time_zone = system, time zone do the query timestamp fields will call system changeover, while the presence of zones global lock problem when the system, to access the multi-volume concurrent large data causes the thread context frequent switching, CPU usage skyrocketing, the system response set slow suspended animation.

Timestamp type and time type selection

In the section "Database guidance" document, would recommend using timestamp type instead of datetime field, on the grounds that timestamp type uses 4 bytes and datetime fields using 8 bytes, but with decreasing disk performance and memory cost, the actual production environment, use the timestamp type does not bring much performance boost, but may limit the scope and impact of the use of business due to the timestamp type definitions and values.

In MySQL 5.6.4 and later versions, you may be the type of stamp (timestamp) the most accurate data microseconds, also may be time types (datetime) data microsecond maximum accuracy, time types (datetime) can also obtain the same type of timestamp effect, as will be defined as the field dt1 dATETIME (3) NOT NULL DEFAULT NOW (3) ON UPDATE NOW (3); time types (datetime) access range '1000-01-0100: 00: 00.000000' to '9999 -12-3123: 59: 59.999999 ', better store data of each time slot.

Timestamp type recommendations

  1. In the case concerned only the last data update time, it recommended that the timestamp column is defined as TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

  2. In the case of interest and the creation time of the update time, the update time is recommended to set the timestamp field, defined as the time of creation or DAETIME TIMESTAMP DEFAULT '0000-00-00 00:00:00', and when a record is explicit Creating a specified time;

  3. Recommendation defines only a single timestamp column in the table, and to explicitly define and DEFAULT ON UPDATE attributes;

  4. Although the assignment of the timestamp field in MySQL or update, it is recommended only for the timestamp column explicitly inserts and updates, if necessary;

  5. The recommended time_zone parameter is set to a value outside of the system, such as a server in China is set to '+8: 00';

  6. It recommended that the next test version of MySQL production line and online versions consistent.

Datetime Timestamp and the similarities and differences

Reference Links: https://blog.csdn.net/z3278221/article/details/81000876

Same point:

  1. Can be automatically updated and initialized, the same default display format YYYY-MM-dd HH: mm: ss

Except that:
the time range 2. timestamp is: '1970-01-01 00:00:01' UTC to UTC , the time zone is automatically converted '2038-01-19 03:14:07', the actual storage milliseconds, word 4 section storage
time of 3. datetime: '1000-01-01 00:00:00' to '9999-12-31 23:59:59', it does not support zone, 8 bytes of storage

Set timestamp and date are automatically updated

When an update of data or to insert a new date and no data assignment mydate, date and mydate these two fields will automatically default to the current time
CREATE TABLE `mytime` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` timestamp(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `mydate` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

2038 problem

When the timestamp stored for longer than '2038-01-19 03:14:07' UTC, mysql will error, because this is the mysql own problems, that timestamp is capped, more than naturally given, specific The reason to see the official document: https://dev.mysql.com/doc/refman/8.0/en/datetime.html, part shots are as follows:

Here Insert Picture Description

solution

Although the timestamp has an upper limit, but it is stored timestamp, may not have to consider the time zone, if it is related to the need to deal with time zone needs to address the 2038 time limit, the proposed change will timestamp integer type used to save time stamp, in the program then converts (the program never implemented, merely recommends caution !!)

If not need to consider the time zone, can be directly replaced with a timestamp datatime type, a wide range of values ​​as much datatime may FIG fancy;

Alternative ideas:
1. Modify the name of the original field;

ALTER TABLE `student` CHANGE `entry_date` `temp_entry_date` timestamp NOT NULL default '0000-00-00 00:00:00';
  1. Datatime a new type of field (a new one, meant to replace);
ALTER TABLE `student` ADD `entry_date` DATETIME NOT NULL default '0000-00-00 00:00:00';
  1. The original copy of the data field to a new column in the Field column;
UPDATE `student` SET `entry_date` = `temp_entry_date`;
  1. Delete the original columns;
ALTER TABLE `student` DROP `temp_entry_date`;

Complete sql follows :( Note that the default value of the original timestamp, and this also needs to add)

ALTER TABLE `student` CHANGE `entry_date` `temp_entry_date` timestamp NOT NULL default '0000-00-00 00:00:00';
ALTER TABLE `student` ADD `entry_date` DATETIME NOT NULL default '0000-00-00 00:00:00';
UPDATE `student` SET `entry_date` = `temp_entry_date`;
ALTER TABLE `student` DROP `temp_entry_date`;

Guess you like

Origin blog.csdn.net/weter_drop/article/details/89924451