前言:

MySQL 的大表运维总是令人头疼的一件事,特别是大表表结构的修改尤为困难。

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

其次,大多数的alter table操作都会涉及 lock --- copy to new table --- rename --- unlock的过程,锁表时间会很长。

本文不是讨论如何进行大表表结构变更, 而是汇总一些不涉及copy to new table这一步的alter table情况。

这些情况下,mysql会直接修改frm文件,而lock的时间也仅是秒级的。

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.

mysql锁表_MySQL中Alter table 你不知道的性能问题相关推荐

  1. mysql 删除process_MySQL中Alter table 你不知道的性能问题

    前言: MySQL 的大表运维总是令人头疼的一件事,特别是大表表结构的修改尤为困难. 首先,alter table 的process不可被kill , 一旦执行就不可回退. 其次,大多数的alter ...

  2. mysql锁表_MYSQL锁表问题的解决方法

    本文实例讲述了MYSQL锁表问题的解决方法.分享给大家供大家参考,具体如下: 很多时候!一不小心就锁表!这里讲解决锁表终极方法! 案例一 mysql>show processlist; 参看sq ...

  3. mysql 锁24小时_MySQL中Alter table 不长时间锁表的情况汇总。

    查看原文:http://www.tanbo.name/html/99669.html 前言: MySQL 的大表运维总是令人头疼的一件事,特别是大表表结构的修改尤为困难. 首先,alter table ...

  4. mysql 并发 锁表_MySQL中的锁(表锁、行锁) 并发控制锁

    https://github.com/MrLining/mysql/wiki/MySQL%E4%B8%AD%E7%9A%84%E9%94%81%EF%BC%88%E8%A1%A8%E9%94%81%E ...

  5. mysql alter table 速度慢_MySQL中ALTER TABLE时的性能问题

    当对于一个大表进行ALTER TABLE的时候,性能问题就产生了.MySQL大部分改动的步骤如下:根据新的表结构创建一个空表,从旧表中把数据取出来插入到新表中,在删除旧表.这是个非常漫长的过程.许多人 ...

  6. 阿里云mysql不让锁表_MySQL中InnoDB锁不住表的原因

    MySQL中InnoDB锁不住表是因为如下两个参数的设置: mysql> show variables like '%timeout%'; +-------------------------- ...

  7. 易语言mysql锁表_MySQL的3种锁定机制

    MySQL各存储引擎使用了3种类型的锁定机制: 1. 行级锁定(row-level) --- MyISAM.Memory.CSV *锁定对象颗粒度最小,发生资源争用的概率最小,在并发处理能力上有较大优 ...

  8. mysql optimize 用法_mysql中OPTIMIZE TABLE的作用

    在使用mysql的时候有时候,可能会发现尽管一张表删除了许多数据,但是这张表表的数据文件和索引文件却奇怪的没有变小.这是因为mysql在删除数据(特别是有Text和BLOB)的时候,会留下许多的数据空 ...

  9. laravel mysql 锁表_Laravel中MySQL的乐观锁与悲观锁

    MySQL/InnoDB的加锁,是一个老生常谈的话题.在数据库高并发请求下,如何兼顾数据完整性与用户体验的敏捷性是一代又一代程序员一直在思考的问题. 乐观锁 乐观锁之所以叫乐观,是因为这个模式不会对数 ...

最新文章

  1. Git 简介1-常用术语
  2. intent的startActivityForResult()方法
  3. thinkphp隐藏后台地址
  4. django3.2.8使用simpleui注意事项
  5. boost::mp11::mp_max_element相关用法的测试程序
  6. CString string 转换
  7. “同一天生日”网络募捐被疑造假,为什么却成了程序员的锅?
  8. 在Linux中查找用户帐户信息和登录详细信息的11种方法
  9. 第十一节:Springboot整合log4j2日志
  10. POJ 3071 概率DP
  11. 微信网页开发 /JS-SDK说明文档
  12. NorthWind 数据库整体关系
  13. js转换金额,元,万元
  14. MFC 显示对话框内鼠标单击点的坐标值
  15. 求原谅---好久没更新了
  16. 企业风险管理与内部控制常见问题与思考
  17. 实战 —— 生存唯一随机数
  18. 淘宝客如何通过小红书引流?如何找出你的目标人群有哪些?
  19. 【MyBatis】搭建MyBatis
  20. Concat和Add操作

热门文章

  1. 简单理解CAP-BASE
  2. 腾讯招.NET,居然要求精通MySQL,而不是SQLServer!
  3. 一个static和面试官扯了一个小时,舌战加强版
  4. 15个基本的C#面试问题
  5. 自动给 Asp.Net Core Api 增加 ApiVersionNeutral
  6. Web Template Studio 发布!在 VS Code 中轻松创建 Web 应用
  7. ASP.NET Core 沉思录 - 环境的思考
  8. .NET/C# 获取一个正在运行的进程的命令行参数
  9. dotnetcore-officeaddin-toolbox : Office 365 Add-in开发人员的工具箱
  10. Xamarin中国技术社区及BXUG官网上线啦