MySQL batch replacement character set

original
2022/03/22 16:04
Reading 201

background

When optimizing the system performance, we found that a simple select left join statement was slow. Through explain, we found that it was because there was no index. After checking the structure of the following table, we found that it was because the character set was not unified. When querying tables, one used utf8, and the other used utf8mb4, When MySQL performs association queries, if the character sets are not unified, the index will become invalid. After the character set is uniformly modified to utf8mb4, the query becomes faster.

Due to the different levels of developers, the early stage of the project was not well standardized, which led to the emergence of a large number of non-standard character sets in the database. In order to ensure the quality of the project, it is necessary to modify the character sets in batches to ensure the database specification.

Character set replacement method

Modify the default character set in the database configuration

When we create a MySQL database, if we do not specify a character set, MySQL will initialize according to the character set configuration in the default configuration file. The default character set is configured in the my.ini file:

 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci

Modify the character set of the database

For some databases that have been created, if the default character set is incorrect, you also need to modify it:

 ALTER DATABASE ` database name ` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'

Modify Datasheet Character Set

For some database tables that have been created, we may need to modify them in batches:
First, execute the sql statement to get the sql that needs to be modified in batch:

 SELECT TABLE_SCHEMA 'database', TABLE_NAME 'table', TABLE_COLLATION 'Original collation', CONCAT ('ALTER TABLE ', TABLE_SCHEMA,'. ', TABLE_NAME,' DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ')' Modify SQL ' FROM information_schema.`TABLES`  WHERE (TABLE_COLLATION='utf8 'or TABLE_COLLATION like'% general% ') AND TABLE_SCHEMA=' [database name] ';

take Fix SQL Copy the data in that column and execute in batches

Modify the character set of database table fields

Some database table fields have character sets specified when they are created. These fields also need to be modified in batches, which is also the same as generating execution statements through scripts:
(Considering that the default value and annotation information should be retained, some attributes of the field should be judged when splicing.)

 SELECT c. TABLE_SCHEMA 'database', c. TABLE_NAME 'table', c. COLUMN_NAME 'field', c. COLUMN_DEFAULT 'Default value', c. IS_NULLABLE 'Is it empty', c. DATA_TYPE 'Field type', c. Character_set_name 'Original character set', c. Collation_name 'Original collation', CONCAT( 'ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' MODIFY COLUMN ', COLUMN_NAME, ' ', COLUMN_TYPE, ' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ', CASE WHEN c.is_nullable = 'NO' THEN 'NOT NULL' ELSE 'NULL'  END, CASE WHEN c.COLUMN_DEFAULT = '' THEN ' DEFAULT '''''  WHEN c.COLUMN_KEY = 'PRI' THEN ''  WHEN c.COLUMN_DEFAULT IS NULL THEN ( CASE WHEN c.is_nullable = 'NO' THEN '' ELSE ' DEFAULT NULL'  END ) ELSE concat( ' DEFAULT ', '''',  c.COLUMN_DEFAULT, '''' )  END, ' comment ', '''', c.COLUMN_COMMENT, '''', ';'  )'Fix SQL' FROM information_schema.`COLUMNS` c WHERE (CHARACTER_SET_NAME='utf8 'or COLLATION_NAME like'% general% ') AND TABLE_SCHEMA=' [database name] '
Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top