mysql修改字段防止锁表

步骤1:

修改一个大表的字段,add column或者drop column,操作后表会锁住,此时查询ok,insert和update会一直等待锁。如图。

解决方案:

1、基于要操作的表创建一个临时表,执行要修改的操作,比如add column或者drop column

2、把表内容导出到文件(注意不要用intsert into table_copy select * from table,因为这样也很慢,也会锁表)

登陆mysql服务器,使用下面命令,其实也会锁表,只是下面的导出会更快而已。

select * from cms_gift_code into outfile '/usr/local/mysql/data/cms_gift_code.txt' fields terminated by ',' line terminated by '
';

3、把文件导入到临时表

同上(最后括号里面的是字段名,可以不加,不加的前提是两张表结构一样)

load data infile '/usr/local/mysql/data/cms_gift_code.txt' into table cms_gift_code_copy fields terminated by ',' lines terminated by '
' (id,gift_id,code,type,status,created_at,updated_at,phone,openid,other,user_ip);

4、对换临时表和正式表的表名。

原文地址:https://www.cnblogs.com/alazalazalaz/p/13627509.html