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

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

1 if("未锁定")

2 {

3 执行DDL语句

4 }else

5 {

6 三思后行

7 }

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

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

此时此刻,我就在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.

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

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

TAG标签:同学

alter在MySQL中是什么意思_MySql中要AlterTable的同学请注意!!!_MySQL相关推荐

  1. mysql中int(16)_MySQL中int(M)和tinyint(M)数值类型中M值的意义

    在一开始接触MySQL数据库时,对于int(M)及tinyint(M)两者数值类型后面的M值理解是最多能够插入数据库中的值不能大于M: 后来工作后,也是一边学习一边使用,之后的理解是其中的M的意思是插 ...

  2. mysql的explain怎么看_mysql中explain用法详解

    如果在select语句前放上关键词explain,mysql将解释它如何处理select,提供有关表如何联接和联接的次序. explain的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 1 ...

  3. mysql两个字段相减_MySQL 中NULL和空值的区别?

    作为后台开发,在日常工作中如果要接触Mysql数据库,那么不可避免会遇到Mysql中的NULL和空值.那你知道它们有什么区别吗? 学不动了,也不想知道它们有什么区别.大兄弟,不行啊,要面试! 前些天我 ...

  4. mysql数据库中的int类型_MySQL中int(M)和tinyint(M)数值类型中M值的意义

    在一开始接触MySQL数据库时,对于int(M)及tinyint(M)两者数值类型后面的M值理解是最多能够插入数据库中的值不能大于M: 后来工作后,也是一边学习一边使用,之后的理解是其中的M的意思是插 ...

  5. mysql无关子查询_mysql中相关,无关子查询,表与表之间的关系以及编码和乱码的解决...

    ※MySQL中的字符编码(注意,utf8中没有'-',跟Java中不一样) SHOW VARIABLES; //查看系统变量 //查询字符编码相关的系统变量 SHOW VARIABLES WHERE ...

  6. mysql ddl log 源码_MySQL中ddl_log.log初步认识

    今天发现测试数据库环境里在凌晨00:00时产生的一个日志文件-ddl_log.log,打开内容几乎是杂乱无章的,只有零散的信息,感觉和表中分区有关系,然后推断可能是因为定时event在00:00时调用 ...

  7. mysql id自动增长_MySQL中的6种约束,你掌握了几种?

    -点击上方"爱数据学习社"关注我们吧!- 在MySQL里,"约束"指的是对表中数据的一种限制约束,它能够确保数据库中数据的准确性和有效性. 比如有的数据是必填项 ...

  8. mysql获取当月最后一天_mysql中获取本月第一天、本月最后一天、上月第一天、上月最后一天

    mysql获取当月最后一天_mysql中获取本月第一天.本月最后一天.上月第一天.上月最后一天等等 转自: https://blog.csdn.net/min996358312/article/det ...

  9. 修改mysql 视图字段类型_MySQL中的数据类型,列选项,视图

    列的数据类型 整型 类型 取值范围 tinyint -128 - 127 smallint -32768 - 32767(几万) mediumint -8388608 - 8388607(几百万) i ...

最新文章

  1. 理解同步异步、阻塞与非阻塞
  2. python: append、extend、insert、add
  3. 摸清全国农村集体家底-农业大健康:产权改革谋定清产核资
  4. Linux 文件查找命令
  5. 解密SVM系列(二):SVM的理论基础(转载)
  6. android设备报警推送,Firebase推送通知未送达所有android设备
  7. java and dsl_Groovy语法糖以及DSL
  8. hdu2844 Coins(普通的多重背包 + 二进制优化)
  9. weblogic 部署应用步骤
  10. 转载-谈谈我这些年的互联网赚钱经历
  11. 数据挖掘:数据清洗——缺失值处理
  12. 【Structure Light】reading notes(一)
  13. Android使用Activity用作弹出式对话框Dialog
  14. Win2003启用硬件加速(DirectX,声显卡启用)
  15. 使用Arcgis制作的专题地图
  16. WiFi配适器Linux,centos7 打开wifi适配器(wifiadapter)和安装centos7 需要注意的
  17. html5艺术字效果,WEB前端开发,网络特殊字体的制作工具-font-spider
  18. 基于MDB_ICP协议的纸币识别器与自动售货机通讯的研究
  19. 分享一下我的一些学习方法
  20. js获取当前日期及下一天日期

热门文章

  1. freemarker英文日期显示成中文问题
  2. win10系统快速进入bios的设置方法
  3. 多空线趋势策略-简易好用型
  4. 感染人数已经超过6千,何时达到峰值?数学统计建模分析告诉你
  5. FTP:vsftpd中的local_umask和anon_umask
  6. java输入十个,键盘输入十个数,输出最大数
  7. screen 使用命令
  8. vue开发微信公众号调用相机和相册(上传到自己的服务器)
  9. 华硕fl8000u主板怎么拆_华硕顽石4笔记本 FL5900U如何打开后盖
  10. HDUOJ 2059 龟兔赛跑——