码迷,mamicode.com
首页 > 数据库 > 详细

MySQL如何妥善更改大表表结构(Alter table structure of a single column

时间:2015-01-13 12:17:32      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

MySQL如何妥善更改大表表结构(Alter table structure of a single column

http://blog.sina.com.cn/s/blog_445e807b0101egpf.html

在网上搜到的一段很有帮助的三段脚本,贴出来供参考,以飨读者,做个笔记:
#
#  Script 1
#  Alter table structure of a single column of a large table
#
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
You can perform this on all slaves. What about the master ??? How do you prevent this \
from replicating to the slaves. Simple: Don‘t send the SQL \into the master‘s binary logs.\
 Simply shut off binary logging in the session before doing the ALTER TABLE stuff:
#
#  Script 2
#  Alter table structure of a single column of a large table
#  while preventing it from replicating to slaves
#
SET SQL_LOG_BIN = 0;
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
But wait !!! What about any new data that comes in while processing these \
commands ??? Renaming the table in the beginning of the operation should do the trick. \
Let alter this code a little to prevent entering new data in that respect:
#
#  Script 3
#  Alter table structure of a single column of a large table
#  while preventing it from replicating to slaves
#  and preventing new data from entering into the old table
#
SET SQL_LOG_BIN = 0;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
CREATE TABLE WorkingTableNew LIKE WorkingTableOld;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
Script 1 can be executed on any slave that do not have binary logs enabled
Script 2 can be executed on any slave that does have binary logs enabled
Script 3 can be executed on a master or anywhere else

 

MySQL如何妥善更改大表表结构(Alter table structure of a single column

标签:

原文地址:http://www.cnblogs.com/MYSQLZOUQI/p/4220676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!