About MySQL metadata lock

Source: Internet
Author: User
Tags table definition

Last night on-line, but found a DDL statement has not been effective for a long time

Check processlist, found numerous query hints including DDL statements "waiting for table metadata lock"

The only query that does not have the hint is a full-table query, and the time item has the largest value.

Kill the thread for the query, and the subsequent DDL statements are normal.

I have always heard that metadata lock is a metadata lock, also called a dictionary lock or a table structure lock. But I haven't met you.

Then tried again-as long as there are incomplete additions and deletions in the session1 transaction, if there is a table structure lock in another Session2 statement, Session2 will wait for the completion of the Session1 transaction before continuing

All queries will be blocked until the Session2 is complete.

So before you have DDL operations, it's best to look at the queries that have not been completed for a long time, otherwise it will affect the writing of the data.

Attached 1:DDL statement locking policy and process:

1) on MDL Read lock
2) Operation data, the most time-consuming, the need to copy data, simple process as follows:
A) Create a temporary table A, redefine a as the modified table structure
b) Read data from the original table into table A
3) Upgrade the MDL read lock to write lock
c) Delete the original table and rename a to the original table name
4) Release the MDL write lock

Attached 2: Instructions on the official website

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 Increa Ses. Metadata contention increases the more this 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 mu Tex. 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, used in a uncompleted transaction in another session. The server achieves the acquiring metadata locks on tables used within a transaction and deferring release of those lo Cks until the transaction ends. A metadata lock on a table prevents changes to the table ' s structure. This locking approach have the implication that a table that's being used by a transaction within one session cannot be us Ed in DDL statements by other sessions until the transaction ends.


This principle applies isn't only to transactional tables and also to nontransactional tables. Suppose 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 is 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 the these operations:


DROP TABLE t;
ALTER TABLE t ...;
DROP TABLE NT;
ALTER TABLE nt ...;
If the server acquires metadata locks for a statement that's 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 are written to the binary log and The locks protect log consistency.


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


Metadata Locks acquired during a PREPARE statement is 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, if a DDL statement occurred for a table is being used by another session in An active transaction, statements could is written to the binary log in the wrong order

About MySQL metadata lock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.