时间:2021-07-01 10:21:17 帮助过:14人阅读
DATETIME
,TIME
,TIMESTAMP
data types to support microseconds:Incompatible change: For TIME, DATETIME, and TIMESTAMP columns, the storage required for tables created before MySQL 5.6.4 differs from storage required for tables created in 5.6.4 and later. This is due to a change in 5.6.4 that permits these temporal types to have a fractional part. After upgrading from MySQL 5.5 to MySQL 5.6.4 or later, it is recommended that you also upgrade from MySQL 5.5 to MySQL 5.6 TIME, DATETIME, and TIMESTAMP types…
The problem is that the manual doesn’t tell youhowto “upgrade from MySQL 5.5 to MySQL 5.6 TIME, DATETIME, and TIMESTAMP types”. I figured simply rebuilding the relevant tables would probably do the trick, and I found some confirmation in ablog postfrom the MySQL server team. Quoting that post:
The ALTER TABLE requests ADD/CHANGE/MODIFY COLUMN, ADD INDEX or FORCE operation upon detecting old temporal data types upgrades them to the new format. Also a ‘NOTE’ is reported to indicate the user that an upgrade of the old temporal columns to the new format has been performed as well.
So I figured I would useALTER TABLE ... FORCE
to rebuild my tables. But which tables need to be rebuilt? I could simply rebuild every table that hasDATETIME
,TIME
, and/orTIMESTAMP
columns, but I’d rather be able to tell which storage format the table is using so don’t end up rebuilding it if I don’t need to. For InnoDB tables I can identify the columns using the old temporal types by checking theMTYPE
value in theINFORMATION_SCHEMA.INNODB_SYS_COLUMNS
table, since that value will be6 (INT)
for the old storage format and3 (FIXBINARY)
for the new storage format. Since almost all of my tables are InnoDB that approach works well for me. For other storage engines I’ll just rebuild all tables withDATETIME
,TIME
, and/orTIMESTAMP
columns.
Here’s a query to list all of the relevant columns suspected of using the old format:
|
And here’s a quick way to dump theALTER TABLE
commands to a script and then execute that script:
|
If I re-run the above it will not rebuild the InnoDB tables that were already rebuilt, but it will rebuild the MyISAM tables that were already rebuilt because I don’t know how to tell if they are using the old format or not.
Since I enabled warnings I get this friendly note every time I convert a table:
Note (Code 1880): TIME/TIMESTAMP/DATETIME columns of old format have been upgraded to the new format.
As an added bonus all of myDATETIME
columns shrink from 8 bytes to 5 bytes (assuming I’m not using fractional seconds)!