前言:

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 删除process_MySQL中Alter table 你不知道的性能问题相关推荐

  1. mysql锁表_MySQL中Alter table 你不知道的性能问题

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

  2. MySQL中alter table range partition

    最近在用MySQL开发新功能时,使用到了alter table range partition的功能,在此总结下mysql innodb支持的alter table range partition相关 ...

  3. 在线打mysql代码_mysql 在线alter table要留神_mysql

    mysql 在线alter table要小心 mysql 5.6之前, alter table操作对可用性有巨大的冲击(除了纯改表名.不影响任何数据的alter table).它的原理是, 0. al ...

  4. MySql删除表中重复数据

    有一表中存在大量重复数据 在此记录下我删除表内重复数据的方法 -- 新增测试表 create table basic_farmer ( id INT(11), user_name VARCHAR(25 ...

  5. MySQL删除表中的数据

    Mysql删除表中的数据有三种方法,分别是delete ,drop,truncate. 一.delete删除表中的数据 delete好from结合使用,格式一般为:delete from 表名 whe ...

  6. mysql根据id删除数据库,MYSQL删除表中的指定ID数据

    MYSQL删除表中的指定ID数据 删除A表中的ID 中的开头以B* 的数据库. 复制代码 代码如下: delete FROM A WHERE id like 'B%' 单独删除 A 表中的ID B 复 ...

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

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

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

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

  9. Hive中alter table 对分区表的操作:添加列,修改列,删除历史分区

    alter table 用于在已有的hive表中添加.修改删除列 在分区表中添加新的一列: alter table table_name add columns(columns_name double ...

最新文章

  1. 块级格式化上下文(Block Formatting Context)
  2. ajax(jquery)前后台传数组(Springmvc后台)
  3. 微信二次开发SDK使用教程--手机检测到有人点赞/取消点赞通知服务端
  4. LevelDB源码解读
  5. env-cmd is not recognized as an internal or external command
  6. 03:数据结构 栈、队列、链表与数组
  7. 计算机专业线性代数教学大纲,《线性代数》课程教学大纲
  8. ajax php接收不到数据库,PHP更新MySQL数据库与AJAX调用没有做任何事情
  9. python 预测 位置_Python:核岭回归预测,KRR
  10. 国内外优秀的源码网站
  11. 启动web项目卡在Initializing Spring root WebApplicationContext不动
  12. 大智慧c语言dll,调用大智慧dll,简单支持大智慧公式dll接口
  13. easyPOI 模板导出Excel
  14. centos安装MySQL怎么换端口_centos安装mysql后3306端口不通
  15. 深度哲学:王阳明心学精髓
  16. html中如何写if判断,HTML中的if判断用法
  17. matlab图注的上标和下标,关于matlab中特殊字符, 上标和下标
  18. [英语阅读]代金券成热门圣诞礼物
  19. intel驱动包下载
  20. idea本地项目部署到远程windows服务器

热门文章

  1. oracle pl/sql 游标,Oracle PL/SQL 关于游标的介绍
  2. 玩转 SpringBoot 2 快速整合 | Thymeleaf 篇
  3. Cannot add or update a child row: a foreign key constraint
  4. MySQL索引类型及创建
  5. Java Swing Mysql学生选课系统
  6. 基于JAVA+SpringMVC+Mybatis+MYSQL的线上电器商城
  7. Unity3D-Rigidbody
  8. Eclipse闪退解决方案
  9. 在SQL Server查询分析器里查询Excel文件数据
  10. 找到replication相关的作业和sql 语句