mysql alter table 速度慢,MySQL ALTER TABLE在小表中占用很长时间

I have two tables in my scenario

table1, which has about 20 tuples

table2, which has about 3 million tuples

table2 has a foreign key referencing table1 "ID" column.

When I try to execute the following query:

ALTER TABLE table1 MODIFY vccolumn VARCHAR(1000);

It takes forever. Why is it taking that long? I have read that it should not, because it only has 20 tuples.

Is there any way to speed it up without having server downtime? Because the query is locking the table, also.

解决方案

I would guess the ALTER TABLE is waiting on a metadata lock, and it has not actually starting altering anything.

What is a metadata lock?

When you run any query like SELECT/INSERT/UPDATE/DELETE against a table, it must acquire a metadata lock. Those queries do not block each other. Any number of queries of that type can have a metadata lock.

But a DDL statement like CREATE/ALTER/DROP/TRUNCATE/RENAME or event CREATE TRIGGER or LOCK TABLES, must acquire an exclusive metadata lock. If any transaction still holds a metadata lock, the DDL statement waits.

You can demonstrate this. Open two terminal windows and open the mysql client in each window.

Window 1: CREATE TABLE foo ( id int primary key );

Window 1: START TRANSACTION;

Window 1: SELECT * FROM foo; -- it doesn't matter that the table has no data

Window 2: DROP TABLE foo; -- notice it waits

Window 1: SHOW PROCESSLIST;

+-----+------+-----------+------+---------+------+---------------------------------+------------------+-----------+---------------+

| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |

+-----+------+-----------+------+---------+------+---------------------------------+------------------+-----------+---------------+

| 679 | root | localhost | test | Query | 0 | starting | show processlist | 0 | 0 |

| 680 | root | localhost | test | Query | 4 | Waiting for table metadata lock | drop table foo | 0 | 0 |

+-----+------+-----------+------+---------+------+---------------------------------+------------------+-----------+---------------+

You can see the drop table waiting for the table metadata lock. Just waiting. How long will it wait? Until the transaction in window 1 completes. Eventually it will time out after lock_wait_timeout seconds (by default, this is set to 1 year).

Window 1: COMMIT;

Window 2: Notice it stops waiting, and it immediately drops the

table.

So what can you do? Make sure there are no long-running transactions blocking your ALTER TABLE. Even a transaction that ran a quick SELECT against your table earlier will hold its metadata lock until the transaction commits.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值