星空网 > 软件开发 > 数据库

My Sql 中要Alter Table的同学请注意!!!

    

  首先我建议你在对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.

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

 

    

 

                                                                                                      原文传送门




原标题:My Sql 中要Alter Table的同学请注意!!!

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊旺季来临,如何迎接黑五网一?:https://www.kjdsnews.com/a/574731.html
被封超5万?亚马逊官方辟谣!只关闭中国卖家3000个账号:https://www.kjdsnews.com/a/574732.html
“天价”空运、港口拥堵恶化加剧黑五网一即将来临......:https://www.kjdsnews.com/a/574733.html
三十年“美东”,三十年“美西”:https://www.kjdsnews.com/a/574734.html
提前售罄?万圣节热门产品搜索量暴涨,但却暗藏侵权危机:https://www.kjdsnews.com/a/574735.html
怎么利用谷歌工具掌握核心关键字,提高产品曝光量:https://www.kjdsnews.com/a/574736.html
300万箱!ONE将投入250亿美元,用于扩大运力:https://www.kjdsnews.com/a/1836472.html
航运系统阻断,美港口无限期关闭!:https://www.kjdsnews.com/a/1836473.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流