步驟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 '\r\n';
3、把文件導入到臨時表
同上(最后括號里面的是字段名,可以不加,不加的前提是兩張表結構一樣)
load data infile '/usr/local/mysql/data/cms_gift_code.txt' into table cms_gift_code_copy fields terminated by ',' lines terminated by '\r\n' (id,gift_id,code,type,status,created_at,updated_at,phone,openid,other,user_ip);
4、對換臨時表和正式表的表名。