mysql賦權,mysql 刪除process_MySQL中Alter table 你不知道的性能問題

 2023-10-05 阅读 26 评论 0

摘要:前言:mysql賦權、MySQL 的大表運維總是令人頭疼的一件事,特別是大表表結構的修改尤為困難。首先,alter table 的process不可被kill , 一旦執行就不可回退。其次,大多數的alter table操作都會涉及 lock --- copy to new table --- rename -

前言:

mysql賦權、MySQL 的大表運維總是令人頭疼的一件事,特別是大表表結構的修改尤為困難。

首先,alter table 的process不可被kill , 一旦執行就不可回退。

其次,大多數的alter table操作都會涉及 lock --- copy to new table --- rename --- unlock的過程,鎖表時間會很長。

本文不是討論如何進行大表表結構變更, 而是匯總一些不涉及copy to new table這一步的alter table情況。

這些情況下,mysql會直接修改frm文件,而lock的時間也僅是秒級的。

b2b9c46324e2cae0dc3df89b86bcd1c5.png

MySQL 5.0 系列 (即5.0.x 版本)

如果你是這個版本,很遺憾所有alter table 操作都會進行temp table copy。以下是摘自官方文檔的解釋:

If you use any option to ALTER TABLE other than RENAME, MySQL always creates a temporary table, even if the data wouldn't strictly need to be copied (such as when you change the name of a column).

MySQL 5.1 系列 (即5.1.x 版本)

以下操作不會有copy temp table操作,即鎖表時間較短。

1. ALTER TABLE tbl_name RENAME TO new_tbl_name

2. 不涉及數據修改的操作

2.1 列改名 (除了innodb)

2.2 修改默認值 (注意:必須使用 modify ,而不能使用change)

2.3 增加ENUM的枚舉定義 (注意:僅當新增枚舉在當前允許最大值內,例:1B 可存8個枚舉,2B可存128個枚舉)

3. 通過add partition 添加分區

4. 重命名索引

5. 添加刪除索引 (僅 innodb plugin支持)

詳見官方文檔說明:

For ALTER TABLE tbl_name RENAME TO new_tbl_name without any other options, MySQL simply renames any files that correspond to the table tbl_name without making a copy. (You can also use the RENAME TABLEstatement to rename tables. See Section 13.1.33, “RENAME TABLE Syntax”.) Any privileges granted specifically for the renamed table are not migrated to the new name. They must be changed manually.

Alterations that modify only table metadata and not table data can be made immediately by altering the table's.frm file and not touching table contents. The following changes are fast alterations that can be made this way:

Renaming a column, except for the InnoDB storage engine.

Changing the default value of a column (except for NDB tables; see Limitations of NDBCLUSTER online operations).

Changing the definition of an ENUM or SET column by adding new enumeration or set members to the end of the list of valid member values, as long as the storage side of the data type does not change. For example, adding a member to a SET column that has 8 members changes the required storage per value from 1 byte to 2 bytes; this will require a table copy. Adding members in the middle of the list causes renumbering of existing members, which requires a table copy.

ALTER TABLE ... ADD PARTITION creates no temporary table except when used with NDB tables. ADD or DROPoperations for RANGE or LIST partitions are immediate operations or nearly so. ADD or COALESCE operations forHASH or KEY partitions copy data between changed partitions; unless LINEAR HASH or LINEAR KEY was used, this is much the same as creating a new table (although the operation is done partition by partition). REORGANIZEoperations copy only changed partitions and do not touch unchanged ones.

Renaming an index, except for InnoDB.

Adding or dropping an index, for InnoDB (if InnoDB Plugin is used) and NDB.

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/113765.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息