首先我建议你在对MySQL表做DDL操作时:

  1 执行 show processlist 查看,要操作的表(数据库对象)是否处于锁状态

  if("未锁定")
{
执行DDL语句
}else
{
三思后行
}

  作为一个程序猿,随着开发的进行,我们要面临需求的变更。

  随之而来的有可能就是表结构的变化--字段的增加,字段数据类型的更新。

此时此刻,我就在Alter Table面前跪了。

My Sql 中 Waiting for table metadata lock,主要发生在你在Alter 一个表时,在这个表上有未完成的查询或者操作,其独占了metalock状态下,alter 操作就会等待这个锁释放,接下来是一段漫长的block之旅

  以下是mysql 官方对于Metadata Locking概念的解释:

  MySQL 5.5.3 and up uses metadata locking to manage concurrent access to database objects and to ensure data consistency. Metadata locking applies not just to tables, but also to schemas and stored programs (procedures, functions, triggers, and scheduled events).

  Metadata locking does involve some overhead, which increases as query volume increases. Metadata contention increases the more  that multiple queries attempt to access the same objects.

  Metadata locking is not a replacement for the table definition cache, and its mutexes and locks differ from the LOCK_open mutex. The following discussion provides some information about how metadata locking works.

  To ensure transaction serializability, the server must not permit one session to perform a data definition language (DDL) statement on a table that is used in an uncompleted explicitly or implicitly started transaction in another session. The server achieves this by acquiring metadata locks on tables used within a transaction and deferring release of those locks until the transaction ends. A metadata lock on a table prevents changes to the table's structure. This locking approach has the implication that a table that is being used by a transaction within one session cannot be used in DDL statements by other sessions until the transaction ends.

  This principle applies not only to transactional tables, but also to nontransactional tables. Suppose that a session begins a  transaction that uses transactional table t and nontransactional table nt as follows:

START TRANSACTION;
SELECT * FROM t;
SELECT * FROM nt;

  The server holds metadata locks on both t and nt until the transaction ends. If another session attempts a DDL or write lock operation on either table, it blocks until metadata lock release at transaction end. For example, a second session blocks if it attempts any of these operations:

DROP TABLE t;
ALTER TABLE t ...;
DROP TABLE nt;
ALTER TABLE nt ...;
LOCK TABLE t ... WRITE;

  If the server acquires metadata locks for a statement that is syntactically valid but fails during execution, it does not release the locks early. Lock release is still deferred to the end of the transaction because the failed statement is written  to the binary log and the locks protect log consistency.

  In autocommit mode, each statement is in effect a complete  transaction, so metadata locks acquired for the statement are  held only to the end of the statement.

  Metadata locks acquired during a PREPARE statement are released once the statement has been prepared, even if preparation occurs within a multiple-statement transaction.

  Before MySQL 5.5.3, when a transaction acquired the equivalent of a metadata lock for a table used within a statement, it released the lock at the end of the statement. This approach had the disadvantage that if a DDL statement occurred for a table that was being used by another session in an active transaction, statements could be written to the binary log in the wrong order.

  此时此刻,我唯一想说的就是,假如我在犯了这个错误之前就知道这个真相是多么幸福的事。。。

原文传送门

My Sql 中要Alter Table的同学请注意!!!的更多相关文章

  1. 【转】SQL 语句:Alter table

    转载自:http://www.w3school.com.cn/sql/sql_alter.asp ALTER TABLE 语句 ALTER TABLE 语句用于在已有的表中添加.修改或删除列. SQL ...

  2. sql语句-ALTER TABLE

    在工作中常遇到要维护数据库表的列,这里主要介绍sql语句中的ALTER TABLE 语句.ALTER TABLE 语句用于在已有的表中添加.修改或删除列. 如需在表中添加列,请使用下列语法: ALTE ...

  3. SQL ALTER TABLE 语句

    ALTER TABLE 语句 ALTER TABLE 语句用于在已有的表中添加.修改或删除列. SQL ALTER TABLE 语法 如需在表中添加列,请使用下列语法: ALTER TABLE tab ...

  4. SQL-W3School-高级:SQL ALTER TABLE 语句

    ylbtech-SQL-W3School-高级:SQL ALTER TABLE 语句 1.返回顶部 1. ALTER TABLE 语句 ALTER TABLE 语句用于在已有的表中添加.修改或删除列. ...

  5. PL/SQL中如何执行DDL、SCL?

    PL/SQL程序中不能直接执行DDL语句.为什么? 假设我们在pl/sql程序中有这样的一条DDL语句—— drop table emp:在第一次解析pl/sql中的“drop table emp;” ...

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

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

  7. MySQL5.6 ALTER TABLE 分析和测试

    在MySQL5.5和之前版本,在运行的生产环境对大表(超过数百万纪录)执行Alter操作是一件很困难的事情.因为将重建表和锁表,影响用户者的使用.因此知道Alter操作何时结束对我们是非常重要的.甚至 ...

  8. SQL ALTER TABLE 语句在项目中的使用

    1.在实际的项目开发过程中,之前已经创建好的实体类可能需要增加/删除字段,亦或是更改已有字段的属性,比如主键的增长策略从自增型改为UUID型,那么就会涉及到 SQL 中 alter table 语句的 ...

  9. ORACLE中通过SQL语句(alter table)来增加、删除、修改字段

    1.添加字段: alter table  表名  add (字段  字段类型)  [ default  '输入默认值']  [null/not null]  ; 2.添加备注: comment on ...

随机推荐

  1. zip命令的常用选项

    zip命令的常用选项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大家都知道,在linux上一切皆文件,在实际生产环境中,如果我们需要部署一些系统的服务,我们会将一些软件包提前下 ...

  2. HTML5的Server-Sent Events介绍////////////////zzz

    HTML5有一个Server-Sent Events(SSE)功能,允许服务端推送数据到客户端.(通常叫数据推送).我们来看下,传统的WEB应用程序通信时的简单时序图: 现在Web App中,大都有A ...

  3. js 控制浏览器窗口大小

    //打开一个新窗口并设置其大小window.open('index.html','','width=450,height=750,location=no,menubar=no,status=no,to ...

  4. c# 利用动态库DllImport("kernel32")读写ini文件(提供Dmo下载)

    c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...

  5. linux 连接到阿里云服务器

    当Windows拥有xshell软件可以连接到你的远程服务器时,Linux其实自己带有的ssh就可以连接: 具体命令是: ssh root@60.2.5.201.81然后输入你服务器的密码:××××× ...

  6. QuickRun-快速运行助手

    大家平时需要打开软件的时候都是怎么操作的?一般来说有三种方式: 切换到桌面再点击软件的快捷方式(会将当前正在操作的软件隐藏,再切换回之前的状态比较麻烦,特别是桌面被密密麻麻的图标铺满的时候,找到一个快 ...

  7. Entity Framework7 入门之全功能.NET版本下使用EF7(含源码)另附数据迁移常见错误处理

    Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7 昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Fr ...

  8. ResultSet用法集锦

    ResultSet用法集锦 结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数 ...

  9. windows2008 c盘清理

    可以删除 C:\inetpub\logs\LogFiles 里面的日志文件

  10. java综合实训第二次