简体   繁体   English

有关ALTER TABLE ADD COLUMN的性能问题

[英]Performance concerns regarding ALTER TABLE ADD COLUMN

I have a big MySQL InnoDB table having 5 million rows. 我有一个拥有500万行的MySQL InnoDB大表。 I need to add a column to the table which will have a default int value. 我需要在表中添加一列,该列将具有默认的int值。

What is the best way to do it? 最好的方法是什么? The normal alter table command appears to take a lot of time. 正常的alter table命令似乎要花费很多时间。 Is there any better way to do it? 有什么更好的方法吗? Basically I want to know if there is any faster way or efficient way of doing it. 基本上,我想知道是否有更快的方法或有效的方法。

And if the table has foreign key references, is there any way other than alter table to do this? 并且,如果表具有外键引用,除了alter table之外,还有其他方法可以做到这一点吗?

Any help appreciated. 任何帮助表示赞赏。

I would not say this is a better way, but ... You could create a separate table for the new data and set it up as foreign key relationship to the existing table. 我不会说这是一种更好的方法,但是...您可以为新数据创建一个单独的表,并将其设置为与现有表的外键关系。 That would be "fast", but if the data really belongs in the main table and every (or most) existing records will have a value, then you should just alter the table and add it. 那将是“快速的”,但是如果数据确实属于主表,并且每个(或大多数)现有记录都将有一个值,那么您应该更改表并将其添加。

Suppose the table looked like this: 假设表如下所示:

CREATE TABLE mytable
(
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(25),
    PRIMARY KEY (id),
    KEY name (name)
);

and you want to add an age column with 并且您想添加一个年龄列

ALTER TABLE mytable ADD COLUMN age INT NOT NULL DEFAULT 0;

You could perform the ALTER TABLE in stages as follows: 您可以分阶段执行ALTER TABLE ,如下所示:

CREATE TABLE mytablenew LIKE mytable;
ALTER TABLE mytablenew ADD COLUMN age INT NOT NULL DEFAULT 0;
INSERT INTO mytablenew SELECT id,name FROM mytable;
ALTER TABLE mytable RENAME mytableold;
ALTER TABLE mytablenew RENAME mytable;
DROP TABLE mytableold;

If mytable uses the MyISAM Storage Engine and has nonunique indexes, add two more lines 如果mytable使用MyISAM存储引擎并具有非唯一索引,请再添加两行

CREATE TABLE mytablenew LIKE mytable;
ALTER TABLE mytablenew ADD COLUMN address VARCHAR(50);
ALTER TABLE mytablenew DISABLE KEYS;
INSERT INTO mytablenew SELECT id,name FROM mytable;
ALTER TABLE mytable RENAME mytableold;
ALTER TABLE mytablenew RENAME mytable;
DROP TABLE mytableold;
ALTER TABLE mytable ENABLE KEYS;

This will let you see how many seconds each stage takes. 这将使您看到每个阶段花费多少秒。 From here, you can decide whether or not a straightforward ALTER TABLE is better. 从这里,您可以决定一个简单的ALTER TABLE是否更好。

This technique gets a little gory if there are foreign key references. 如果有外键引用,则此技术会有点麻烦。

Your steps would be 您的步骤将是

  • SET UNIQUE_CHECKS = 0;
  • SET FOREIGN_KEY_CHECKS = 0;
  • Drop the foreign key references in mytable . 将外键引用放在mytable
  • Perform the ALTER TABLE in Stages 分阶段执行ALTER TABLE
  • Create the foreign key references in mytable . mytable创建外键引用。
  • SET UNIQUE_CHECKS = 1;
  • SET FOREIGN_KEY_CHECKS = 1;

Give it a Try !!! 试试看 !!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM