========================================================================

ALTER TABLE 和FLUSH TABLE导致的间接等待

场景:
1、会话A执行耗时较长的操作;
2、会话B执行ALTER TABLE 或FLUSH TABLES等操作时,会向其他会话(线程)发送表变更通知,要求其他会话关闭再重新打开相关表;
3、会话A执行过程中收到会话B的变更通知,在会话A执行结束前,会话A阻塞会话B执行;
4、会话C收到会话B的变更通知,等待会话B完成,形成等待链:会话C>>会话B>>会话A
5、会话B因为其他原因执行失败或被关闭,ALTER TABLE或FLSUH TABLE等操作被取消
6、会话C转为等待会话A执行完成。
官方文档:
The thread got a notification that the underlying structure for a table has changed and it needs to reopen the table to get the new structure. However, to reopen the table, it must wait until all other threads have closed the table in question.
This notification takes place if another thread has used FLUSH TABLES or one of the following statements on the table in question: FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, orOPTIMIZE TABLE.
 
========================================================================
ALTER TABLE过程报主键重复
在MySQL 5.6版本中引入Online DDL特性,很多ALTER TABLE操作可以联机修改,该特性使用ROW LOG来保存ALTER操作期间发生的数据变化,并回放到新表中,保证数据一致性。
 
如果在业务高峰期执行Online DDL操作,可能报下面错误:
ERROR 1062 (23000) at line 13: Duplicate entry 'xxx' for key 'PRIMARY'
 
解决办法:
1、将DDL操作移到业务低峰期执行,降低错误出现概率
2、如果业务允许阻塞,修改ALTER TABLE语句使用阻塞方式执行
如ALTER命令为:
ALTER TABLE TB001 ADD C2 INT;
修改为:
ALTER TABLE TB001 ADD C2 INT, ALGORITHM =COPY;
 
 
========================================================================
修改表名
修改表名有两种语法:
RENAME TABLE old_table TO new_table;

ALTER TABLE old_table RENAME new_table;
使用RENAME TABLE方式可以一次修改多个表的表名,MySQL保证RENAME操作的原子性。
When you execute RENAME TABLE, you cannot have any locked tables or active transactions. With that condition satisfied, the rename operation is done atomically; no other session can access any of the tables while the rename is in progress.
 
 

MySQL--Alter Table注意事项的更多相关文章

  1. MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

    ALTER COLUMN 语法: ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 作用: 设置或删除列的默认值.该操作会直接修 ...

  2. mysql ALTER TABLE语句 语法

    mysql ALTER TABLE语句 语法 作用:用于在已有的表中添加.修改或删除列.无铁芯直线电机 语法:添加列:ALTER TABLE table_name ADD column_name da ...

  3. 【待整理】MySQL alter table modify vs alter table add产生state不一样

    MySQL:5.6.35 OS:redhat5.8 今天更新数据库某些表字段,有如下两SQL: ①alter table xx modify xxxx;(表大概是77w) ②alter table s ...

  4. mysql Alter table设置default的问题,是bug么?

    不用不知道,用了没用? 昨天在线上创建了一个表,其中有两个列是timestamp类型的,创建语句假设是这样的: create table timetest(id int, createtime tim ...

  5. MySQL ALTER TABLE语法

    先看一下定义(密密麻麻) ALTER TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_optio ...

  6. MySQL alter table时执行innobackupex全备再看Seconds_Behind_Master

    1.场景描述 早上7:25 接到Report中心同学告警,昨天业务报表数据没有完整跑出来,缺少500位业务员的数据,并且很快定位到,缺少的是huabei_order库上的数据.Report中心的数据是 ...

  7. mysql alter table

    准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...

  8. mysql 添加索引,ALTER TABLE和CREATE INDEX的区别

    nvicat-->mysql表设计-->创建索引. (1)使用ALTER TABLE语句创建索引,其中包括普通索引.UNIQUE索引和PRIMARY KEY索引3种创建索引的格式: PRI ...

  9. MySQL ALTER命令

    当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本文教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...

  10. faster alter table add column

    Create a new table (using the structure of the current table) with the new column(s) included. execu ...

随机推荐

  1. 【异常】Application failed to start due to an exception org.springframework.beans.factory.BeanCurrentlyInCreationException

    一. 异常信息:   2018-05-17 18:03:22.224 -DEBUG [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   ...

  2. YOLO v2 损失函数源码分析

    损失函数的定义是在region_layer.c文件中,关于region层使用的参数在cfg文件的最后一个section中定义. 首先来看一看region_layer 都定义了那些属性值: layer ...

  3. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. bootstrap滑动开关插件使用

    下载和演示插件地址 需要引入的css和js <link rel="stylesheet" type="text/css" href="/css/ ...

  5. notepad++插件安装

    notepad安装目录的  plugins 下重启 notepad.exe程序即可 插件下载地址  : https://sourceforge.net/projects/npp-plugins/fil ...

  6. MySQL 中Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化

    一.ICP优化原理 Index Condition Pushdown (ICP),也称为索引条件下推,体现在执行计划的上是会出现Using index condition(Extra列,当然Extra ...

  7. Redis (一) 概念安装

    一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start  ...

  8. delphi中TQueue的使用问题

    TQueue里存放的是指针,所要存储的内容最好建立在堆上,在pop方法之后释放掉这个空间. 实例代码: MMSQueue:= TQueue.Create; MMSQueue.Push(StrNew(P ...

  9. 在电脑用Chrome运行安卓apk程序[ARC Welder]

    chrome好用,但要FQ. 为了某些特别有意思的功能,FQ也要实现它. 这个就是:在chrome中使用android APP 一:系统需求: chrome浏览器 梯子(就是FQ,因为我们要去goog ...

  10. oracle主从表主外键对应关系

    一.首先让我们来了解下什么是主外键? 1.主键:唯一标识数据表中的某一行 1) 一个表中只能有一个主键.如果在其他字段上建立主键,则原来的主键就会取消.在ACCESS中,虽然主键不是必需的,但最好为每 ...