简体   繁体   English

如何使用正在使用的表来制作ALTER TABLE

[英]How to do an ALTER TABLE with a table in-use

I have a script that runs 24-7 on a table to perform necessary functions on it. 我有一个在表上运行24-7的脚本,以对其执行必要的功能。 However, when it is running, it is almost impossible to do an ALTER TABLE ADD INDEX statement, as it seems like it just hangs indefinitely. 但是,当它运行时,几乎不可能执行ALTER TABLE ADD INDEX语句,因为它似乎无限期地挂起。 Is there any way around this? 有没有办法解决? How should I go about adding this index? 我应该如何添加该索引?

The Alter table statement is getting a metadata lockout. Alter表语句正在获取元数据锁定。 You cannot perform your alter statement while another transaction is in process on the same table. 在同一张表上正在进行另一笔交易时,您无法执行alter语句。 Since your script runs 24-7, it is not possible to do what you are asking. 由于您的脚本运行24-7,因此您无法执行所要求的操作。

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. 为了确保事务可序列化,服务器不得允许一个会话对在另一会话中未完成的显式或隐式启动的事务中使用的表执行数据定义语言(DDL)语句。 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. 这种锁定方法的含义是,一个会话中一个事务正在使用的表在事务结束之前不能被其他会话在DDL语句中使用。

You can read more about this Here at dev.mysql. 你可以阅读更多关于此这里在dev.mysql。

Version 5.6 has ALTER TABLE ... ALGORITHM=INLACE ... to do ADD INDEX and several other ALTERs without blocking everything. 5.6版具有ALTER TABLE ... ALGORITHM=INLACE ...来执行ADD INDEX和其他几个ALTER的功能,而不会阻塞所有内容。

pt-online-table-alter (from Percona.com) can do it in older versions of MySQL. pt-online-table-alter(来自Percona.com)可以在旧版本的MySQL中做到这一点。 It uses a TRIGGER. 它使用触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM