你的位置:首页 > 软件开发 > 数据库 > MySQL 之 Metadata Locking 研究

MySQL 之 Metadata Locking 研究

发布时间:2015-10-19 01:00:08
MySQL 在 5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(数据库对象)的。包括表结构、schema、存储过程、函数、触发器、mysql的调度事件(events). 要 ...

MySQL 之 Metadata Locking 研究

MySQL 在 5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(数据库对象)的。包括表结构、schema、存储过程、函数、触发器、mysql的调度事件(events). 要理解 metadata lock 最重要的一点就是:将 metadata lock放到数据库事务的语义中来理解。metadata lock 的作用就是当一个事务在执行时,事务涉及到的所有元数据(metadata,也就是 database objects),必须是安全的。比如你在一个事物中select一个table,必须保证该table在你的事物完成之前,她不会被删除了,或者不会被修改了。

相关文档:http://dev.mysql.com/doc/refman/5.6/en/metadata-locking.html

1. metadata lock 的作用

MySQL 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 lock管理对database objects的并发访问,保证数据一致性。

2.metadata lock 会导致性能损耗和锁争用

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 lock 的引入导致一定的性能损耗。对同一个database object的访问越多,就会越导致该对象上的metadata lock的争用。

3.

Metadata locking is not a replacement for the table definition cache, and its mutexes and locks differ from the LOCK_open mutex.

metadata lock 并不是 为了替代 表定义缓存。其mutex和lock和 LOCK_open mutex不一样。

4.

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.

正在运行中的事务,必须要在事务开始时获得它要访问的所有的database objects上的 metadata lock, 然后在事务结束时释放那些database objects上的metadata lock. 事务和metadata lock的关系是极其紧密的:有事务必然就必然有metadata lock,事物结束就释放。metadata lock防止事物中的database objects 被修改,比如阻止事物中的table的结构被修改。所以事物中的database objects上执行DDL会被阻塞,直到事务结束。

5.

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;

metadata lock不仅仅涉及到事务引擎中的table,同样也适用于非事务引擎中的table. metadata lock不仅仅阻塞DDL,同时也阻塞 lock table table_name write 语句

6.

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.

如果一个sql语句语法正确,但是却执行失败了,其上的metadata lock并不会马上释放,而是要在事务结束之后才释放。这是为了保证日志的一致性。

7.

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.

自动提交模式(mysql命令行工具默认是自动提交模式),语句一执行完马上就释放metadata lock,因为他是单语句事务。

8.

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

事务中的metadata lock直到事务结束才释放,但是有一个特例:事务中的prepare(一般用在存储过程中的动态语句)语句一执行完马上释放对应的metadata lock.

9.

Before MySQL 5.5, 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.

MySQL 5.5 引入了metadata lock,取代了之前版本中的等价物。

但是metadata lock和她之前的等价物有一个区别:metadata lock直到事务结束才释放,而她的等价物是语句执行完就马上释放。metadata lock这样做的目的是为了保证 binary log 顺序的正确。

10. 实验1(lock table xxx write 语句; 实验环境Centos下的mysql5.6.27)

首先在终端A执行:

mysql> lock table cats write;Query OK, 0 rows affected (0.01 sec)

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:MySQL 之 Metadata Locking 研究

关键词:MYSQL

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