Change mysql field name in a huge table

隐身守侯 提交于 2019-12-10 14:12:10

问题


I have a table with 21 million row.I have to change one of the row name.When I try with the query "alter table company change id new_id int(11)"; query never ends.

Is there a simple way to change big mysql table's field name?


回答1:


First, before anything, backup the table:

mysqldump -uroot -p db big_table > /tmp/big_table.backup.sql

One thing to keep in mind when doing an ALTER command is to absolutely make sure that you have the same column details for the alter that you would like to not change.

So for example:

ALTER TABLE big_table MODIFY COLUMN id INT(11)

Would skip anything else like AUTO_INCREMENT and NOT NULL. So best to include that also into the alter statement.

ALTER TABLE big_table MODIFY COLUMN id INT(11) NOT NULL AUTO_INCREMENT;

Basically just copy over the create statement area for the ID column and replace what you want and include that into the modify statement.




回答2:


Use pt-online-schema-change to make long-running ALTER TABLE changes, without locking yourself out of the table.

http://www.percona.com/doc/percona-toolkit/pt-online-schema-change.html

It usually takes longer to complete the change, but you can still read from and write to the table while it's doing its work.

You can also listen to a webinar from the author, talking about how it works and some of its caveats:

http://www.percona.com/webinars/2012-05-02-zero-downtime-schema-changes-in-mysql




回答3:


You could simply do the following:

MyISAM table

CREATE TABLE big_table_new LIKE big_table;
ALTER TABLE big_table_new MODIFY COLUMN id INT(11);
ALTER TABLE big_table_new DISABLE KEYS;
INSERT INTO big_table_new SELECT * FROM big_table;
ALTER TABLE big_table_new ENABLE KEYS;
ALTER TABLE big_table RENAME big_table_bak;
ALTER TABLE big_table_new RENAME big_table;

InnoDB table

CREATE TABLE big_table_new LIKE big_table;
ALTER TABLE big_table_new MODIFY COLUMN id INT(11);
INSERT INTO big_table_new SELECT * FROM big_table;
ALTER TABLE big_table RENAME big_table_bak;
ALTER TABLE big_table_new RENAME big_table;

When you are done, big_table_bak is your backup.

If you are satisfied with the new big_table you can run

DROP TABLE big_table_bak;

If you want to revert back you can run

DROP TABLE big_table;
ALTER TABLE big_table_bak RENAME big_table;

Give it a Try !!!




回答4:


The less time consuming method to change name of a field in a huge Mysql table is to :

CREATE TABLE new_big_table SELECT id as new_id,field1,field2,field3 FROM big_table.




回答5:


What do You think about:

  1. export all data and truncate table
  2. alter table
  3. change name of row in exported file
  4. import file

It's just an fast idea, but it won't be fast (exporting/importing).

Or Just try to do ALTER during night few hour maintenance? :)



来源:https://stackoverflow.com/questions/13771304/change-mysql-field-name-in-a-huge-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!