ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL修改排序规则是否一定重建表

2022-01-08 12:09:21  阅读:153  来源: 互联网

标签:NULL name MySQL t1 sec mysql 排序 重建


官方文档:

alter table:

https://dev.mysql.com/doc/refman/5.7/en/alter-table.html

 

online ddl:

https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-operations.html

 

其中都没有讲明修改整个表排序规则,或某个字段排序规则是否需要重建表。

根据 innodb 行存储格式判断,innodb 底层存储时并没有排序规则的概念,但是在创建索引时,确实会受到排序规则影响。

理论上来说,只要要修改排序规则的表里,不涉及到索引的排序规则,重新排列,即不用重建表。

 

测试如下:

生成批量测试数据,

create table t1(id int primary key auto_increment,name varchar(200));

mysql> insert into t1 select null,repeat('a',200);
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
.....
填充大量数据
mysql> insert into t1 select null,repeat('a',200) from t1;
Query OK, 1048576 rows affected (6.80 sec)
Records: 1048576  Duplicates: 0  Warnings: 0

 

查看列排序规则

mysql> show full columns from t1;
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type         | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id    | int          | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| name  | varchar(200) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references |         |
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
2 rows in set (0.00 sec)

 

修改表排序规则实验:

可以看到确实重建表了,我感觉不用重建表也是可以的,id-int 类型,并不受排序规则影响,name 列并没有索引,底层数据存储也不会受排序规则影响。

mysql> alter table t1 convert to character set  utf8mb4 collate utf8mb4_bin;
Query OK, 0 rows affected (3.61 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> show processlist;
+-----+-----------------+-----------------+-------+---------+---------+------------------------+-----------------------------------------------------------------------------+
| Id  | User            | Host            | db    | Command | Time    | State                  | Info                                                                        |
+-----+-----------------+-----------------+-------+---------+---------+------------------------+-----------------------------------------------------------------------------+
|   5 | event_scheduler | localhost       | NULL  | Daemon  | 1012398 | Waiting on empty queue | NULL                                                                        |
|  18 | root            | 127.0.0.1:50427 | NULL  | Sleep   |     437 |                        | NULL                                                                        |
|  19 | root            | 127.0.0.1:50428 | NULL  | Sleep   |     437 |                        | NULL                                                                        |
| 119 | root            | 127.0.0.1:55559 | ceshi | Query   |       3 | altering table         | alter table t1 convert to character set  utf8mb4 collate utf8mb4_bin        |
| 121 | root            | 127.0.0.1:55821 | NULL  | Query   |       0 | init                   | show processlist                                                            |
+-----+-----------------+-----------------+-------+---------+---------+------------------------+-----------------------------------------------------------------------------+
5 rows in set (0.00 sec)

 

修改列排序规则实验:

现在 name 列是没有索引的,可以看到修改排序规则是马上修改,并没有重建表,应该是只修改数据字典。

mysql> alter table t1 modify name varchar(200) collate utf8mb4_bin;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table t1 modify name varchar(200) collate utf8mb4_general_ci;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

为 name 列加上索引,再修改排序规则:

从结果看,应该是重建了表。

mysql> alter table t1 add index idx_name(name);
Query OK, 0 rows affected (27.90 sec)
Records: 0 Duplicates: 0 Warnings: 0


mysql> show full columns from t1; +-------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +-------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ | id | int | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | | | name | varchar(200) | utf8mb4_general_ci | YES | MUL | NULL | | select,insert,update,references | | +-------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ 2 rows in set (0.00 sec) mysql> alter table t1 modify name varchar(200) collate utf8mb4_bin; Query OK, 2097152 rows affected (12.57 sec) Records: 2097152 Duplicates: 0 Warnings: 0

 

标签:NULL,name,MySQL,t1,sec,mysql,排序,重建
来源: https://www.cnblogs.com/nanxiang/p/15777892.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有