Rumah pangkalan data tutorial mysql mysql出现Waiting for table metadata lock的原因及解决方案_MySQL

mysql出现Waiting for table metadata lock的原因及解决方案_MySQL

Jun 01, 2016 pm 01:31 PM
mysql penyelesaian

bitsCN.com

mysql出现Waiting for table metadata lock的原因及解决方案

 

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;
Salin selepas log masuk

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 ...;
Salin selepas log masuk

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

一个没提交的事务使用了A表, 另外一个session 对A表进行alter,出现waiting for table metadata lock

在insert into t select * from share 运行时, 同时执行alter table t add index(play_count),

alter table语句会Waiting for table metadata lock, 直到insert into … select 语句结束。

不是传说5.6支持online DDL么? 怎么还会Waiting for table metadata lock?

后来想想, online DDL应该是指在alter table进行的时候, 插入/修改/删除数据的sql语句不会Waiting for table metadata lock.

MySQL 5.6 enhances many other types OF ALTER TABLE operations TO avoid copying the TABLE.

Another enhancement allows SELECT queries AND INSERT, UPDATE, AND DELETE (DML) statements TO proceed while the TABLE IS being altered.

This combination OF features IS now known AS online DDL.

那么就让alter table wait去吧。

后来又发现另外一个神奇的事:

mysql [localhost] {msandbox} (spc) > SHOW processlist;+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+| Id | USER     | Host      | db   | Command | TIME | State                           | Info                                |+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+|  5 | msandbox | localhost | spc  | Query   |    1 | Waiting FOR TABLE metadata LOCK | ALTER TABLE t ADD INDEX(play_count) ||  8 | msandbox | localhost | spc  | Query   |    3 | USER sleep                      | SELECT sleep(100) FROM t            || 10 | msandbox | localhost | spc  | Query   |    0 | init                            | SHOW processlist                    |+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+
Salin selepas log masuk

重启后再试一次:

mysql [localhost] {msandbox} (spc) > SHOW processlist;+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+| Id | USER     | Host      | db   | Command | TIME | State                           | Info                                |+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+|  1 | msandbox | localhost | spc  | Query   |  129 | USER sleep                      | SELECT sleep(100) FROM t            ||  2 | msandbox | localhost | spc  | Query   |  102 | Waiting FOR TABLE metadata LOCK | ALTER TABLE t DROP INDEX play_count ||  3 | msandbox | localhost | spc  | Query   |    0 | init                            | SHOW processlist                    |+----+----------+-----------+------+---------+------+---------------------------------+-------------------------------------+
Salin selepas log masuk

 

 

 

这个sleep的时间。。。已经超过100秒了…

 

结论:

在准备alter table tbl 的时候,先观察一下,有没有正在运行的,且在短时间内无法结束的sql语句在操作tbl表

bitsCN.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Artikel Panas

R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Repo: Cara menghidupkan semula rakan sepasukan
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Artikel Panas

R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Repo: Cara menghidupkan semula rakan sepasukan
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Tag artikel panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Kemahiran pemprosesan struktur data besar PHP Kemahiran pemprosesan struktur data besar PHP May 08, 2024 am 10:24 AM

Kemahiran pemprosesan struktur data besar PHP

Melaksanakan Algoritma Pembelajaran Mesin dalam C++: Cabaran dan Penyelesaian Biasa Melaksanakan Algoritma Pembelajaran Mesin dalam C++: Cabaran dan Penyelesaian Biasa Jun 03, 2024 pm 01:25 PM

Melaksanakan Algoritma Pembelajaran Mesin dalam C++: Cabaran dan Penyelesaian Biasa

Bagaimana untuk mengoptimumkan prestasi pertanyaan MySQL dalam PHP? Bagaimana untuk mengoptimumkan prestasi pertanyaan MySQL dalam PHP? Jun 03, 2024 pm 08:11 PM

Bagaimana untuk mengoptimumkan prestasi pertanyaan MySQL dalam PHP?

Bagaimana untuk menggunakan sandaran dan pemulihan MySQL dalam PHP? Bagaimana untuk menggunakan sandaran dan pemulihan MySQL dalam PHP? Jun 03, 2024 pm 12:19 PM

Bagaimana untuk menggunakan sandaran dan pemulihan MySQL dalam PHP?

Bagaimana untuk memasukkan data ke dalam jadual MySQL menggunakan PHP? Bagaimana untuk memasukkan data ke dalam jadual MySQL menggunakan PHP? Jun 02, 2024 pm 02:26 PM

Bagaimana untuk memasukkan data ke dalam jadual MySQL menggunakan PHP?

Apakah senario aplikasi jenis penghitungan Java dalam pangkalan data? Apakah senario aplikasi jenis penghitungan Java dalam pangkalan data? May 05, 2024 am 09:06 AM

Apakah senario aplikasi jenis penghitungan Java dalam pangkalan data?

Bagaimana untuk membetulkan ralat mysql_native_password tidak dimuatkan pada MySQL 8.4 Bagaimana untuk membetulkan ralat mysql_native_password tidak dimuatkan pada MySQL 8.4 Dec 09, 2024 am 11:42 AM

Bagaimana untuk membetulkan ralat mysql_native_password tidak dimuatkan pada MySQL 8.4

Bagaimana untuk menggunakan prosedur tersimpan MySQL dalam PHP? Bagaimana untuk menggunakan prosedur tersimpan MySQL dalam PHP? Jun 02, 2024 pm 02:13 PM

Bagaimana untuk menggunakan prosedur tersimpan MySQL dalam PHP?

See all articles