关于mysql的metadata lock
昨天晚上上线,却发现一个ddl语句长时间没有生效
查processlist, 发现包括ddl语句在内的众多查询提示 “Waiting for table metadata lock”
唯一没有该提示的查询为一个全表查询,并且Time项数值最大。
kill掉这个查询的线程,后面的ddl语句正常进行了
之前一直听说metadata lock,就是元数据锁,也叫字典锁或者表结构锁。但是没有遇到过。
后来又试了一下——只要在session1里有未完成的增删查改事务,如果在另一个session2中出现加表结构锁的语句时,session2都会等待session1的事务完成后才继续进行
在session2完成之前,所有的查询都会被阻塞。
所以在有ddl操作之前,最好还是先看一下有没有长时间没完成的查询,否则会影响数据的写入。
附1: ddl语句加锁策略与流程:
1)上MDL读锁
2)操作数据,最耗时,需copy data,简易流程如下:
a) 创建临时表A,重定义A为修改后的表结构
b) 从原表读取数据插入到A表
3)将MDL读锁升级为写锁
c) 删除原表,将A重命名为原表名
4)释放MDL写锁
附2:官网上的说明
8.10.4. Metadata Locking
MySQL 5.5.3 and up uses metadata locking to manage access to objects (tables, triggers, and so forth). Metadata locking is used to ensure data consistency but 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 case, and its mutxes 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 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;
Metadata locks are held on both t and nt until the transaction ends. If another session attempts a DDL 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 ...;
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