In MySQL, when you specify UTF-8 as the character set for a column, the utf8mb3 character set is used by default. This character set uses a maximum of 3 bytes to store a character. To store information such as emojis, you must change the column character set to utf8mb4. Typically, changing a column character set requires rebuilding the table. This process is time-consuming and can significantly affect your business. PolarDB for MySQL supports instant column character set changes. This feature lets you quickly change the character set with no impact on your business.
Prerequisites
Your cluster must be one of the following versions:
PolarDB for MySQL 8.0.1 with a revision version of 8.0.1.1.40 or later. You can use Query the version number to confirm the cluster version.
NoteYou must configure the
loose_innodb_support_instant_modify_charsetparameter before you can use this feature on PolarDB for MySQL 8.0.1 clusters.PolarDB for MySQL 8.0.2.
NotePolarDB for MySQL 8.0.2 clusters support the instant column character set change feature by default. You do not need to configure any parameters.
Limits
You can change the character set only for columns of the CHAR, VARCHAR, ENUM, and TEXT types.
You can change a column character set only from utf8mb3 to utf8mb4, or change any character set to the BINARY type.
No indexes can exist on the column whose character set you want to change.
Before and after the character set change, the column length must meet the following requirements:
The maximum storage length of the column must be either less than 256 bytes both before and after the change, or greater than 255 bytes both before and after the change. The storage length is the actual storage space consumed by the character set.
For example, consider a VARCHAR(85) column that uses the utf8mb3 character set. Because utf8mb3 uses a maximum of 3 bytes to store a character, the maximum storage length of this column is
85*3=255bytes. If you change the character set of this column to utf8mb4, its maximum storage length becomes85*4=340bytes. Because the maximum storage length after the change is greater than 256 bytes, the character set cannot be changed instantly.The maximum storage length of the column with the target character set cannot be less than its maximum storage length with the original character set.
For example, consider a CHAR(120) column that uses the utf8mb3 character set. The maximum storage length of the column is
120*3=360bytes. After you change the column to the BINARY type, the maximum storage length of the column becomes120*1=120bytes. Therefore, this column does not meet the requirements for an instant column character set change.
Procedure
Configure the parameter.
For PolarDB for MySQL 8.0.1, you must set the
loose_innodb_support_instant_modify_charsetparameter to ON to use the instant column character set change feature. For instructions about setting parameter values, see Set cluster parameters and node parameters.Parameter name
Parameter level
Description
loose_innodb_support_instant_modify_charset
Global
Controls the instant column character set change feature. Valid values:
OFF (default): Disables the instant column character set change feature.
ON: Enables the instant column character set change feature.
Change the column character set.
Specify ALGORITHM=INPLACE to force the use of the instant column character set change feature. Example:
ALTER TABLE tablename MODIFY COLUMN test_column varchar(60) CHARACTER SET utf8mb4, ALGORITHM = INPLACE;When you use the preceding statement, the following error indicates that the INPLACE algorithm cannot perform the current column character set change. Make sure that the
loose_innodb_support_instant_modify_charsetparameter is set to ON, and check that the conditions described in the Limits section are met.ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.If you do not specify ALGORITHM or specify ALGORITHM=DEFAULT, PolarDB automatically selects the fastest algorithm to complete the change. Examples:
ALTER TABLE tablename MODIFY COLUMN test_column varchar(60) CHARACTER SET utf8mb4, ALGORITHM = DEFAULT; ALTER TABLE tablename MODIFY COLUMN test_column varchar(60) CHARACTER SET utf8mb4;
Results
The following example uses tables t1 and t2 to compare the results of changing a column character set from utf8mb3 to utf8mb4 with the instant column character set change feature enabled and disabled. Each table contains 10 million records. The t1 and t2 tables have identical table schemas and data volumes.
When the instant column character set change feature is disabled, changing the column character set requires rebuilding the table. The operation takes about 1 minute. During the entire Data Definition Language (DDL) process, only query operations are allowed.
ALTER TABLE t1 MODIFY COLUMN c varchar(2) CHARACTER SET utf8mb4 NOT NULL DEFAULT '';The following result is returned:
Query OK, 10000000 rows affected (59.66 sec) Records: 10000000 Duplicates: 0 Warnings: 0After you enable the instant column character set change feature, the column character set change completes in seconds, regardless of the table's data volume.
ALTER TABLE t2 MODIFY COLUMN c varchar(2) CHARACTER SET utf8mb4 NOT NULL DEFAULT '',ALGORITHM = INPLACE;The following result is returned:
Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0
Contact us
If you have questions about DDL operations, Contact us.