重命名锁定的表 [英] renaming a locked table

查看:130
本文介绍了重命名锁定的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将表迁移到新架构时,我想确保使用复制和重命名过程将原子切换到新表.因此,我试图像这样重命名锁定的表:

When migrating a table to a new schema I want to make sure to have an atomic switch to the new table using the copy and rename procedure. Hence I am trying to rename a locked table like this:

CREATE TABLE foo_new (...)

-- copy data to new table, might take very long
INSERT INTO foo_new (id,created_at,modified_at)
  SELECT * FROM foo WHERE id <= 3;

LOCK TABLES foo WRITE, foo_new WRITE;

-- quickly copy the tiny rest over
INSERT INTO foo_new (id,created_at,modified_at)
  SELECT * FROM foo WHERE id > 3;

-- now switch to the new table
RENAME TABLE foo TO foo_old, foo_new TO foo;

UNLOCK TABLES;

不幸的是,结果为ERROR 1192 (HY000): Can't execute the given command because you have active locked tables or an active transaction.

这应该怎么做?

这是与mariadb:10.1一起使用的.

推荐答案

一般而言,Rick有权使用Percona工具(请参阅似乎不是这种情况.

While in general Rick is right to use the Percona Tools (see 1 and 2), the answer to the question really is to use ALTER TABLE. I thought RENAME was just an alias - but it seems like that's not the case.

测试似乎表明它可以正常工作:

Test seem to indicate that this works OK:

CREATE TABLE foo_new (...)

-- copy data to new table, might take very long
INSERT INTO foo_new (id,created_at,modified_at)
  SELECT * FROM foo WHERE id <= 3;

LOCK TABLES foo WRITE, foo_new WRITE;

-- quickly copy the tiny rest over
INSERT INTO foo_new (id,created_at,modified_at)
  SELECT * FROM foo WHERE id > 3;

-- now switch to the new table
ALTER TABLE foo RENAME TO foo_old;
ALTER TABLE foo_new RENAME TO foo;

UNLOCK TABLES;

这篇关于重命名锁定的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆