Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a column to a large MySql table while online

I need to add a new column to a table in MySQL DB (MyISAM table) that contains more than 20 Million rows.

The process of adding the column must be in run-time, I mean that the app will still be running and rows will still be inserted and selected while altering the table.

  • How will this affect the running app?
  • How long can it take to perform this change?

How can I do this process safely and without harming the running app?

like image 748
SimonW Avatar asked Dec 02 '12 09:12

SimonW


People also ask

How do I quickly add columns?

Insert a New Column (Keyboard Shortcut) Select a cell in the column to the left of which you want to add a new column. Use the keyboard shortcut Control Shift + In the Insert dialog box that opens, click the Entire Column option (or hit the C key) Click OK (or hit the Enter key).

How do I add a column to an existing table?

In Object Explorer, right-click the table to which you want to add columns and choose Design. Select the first blank cell in the Column Name column. Type the column name in the cell. The column name is a required value.


2 Answers

Table is locked while DDL queries are performed. This doesn't mean that the server doesn't accept queries on other sessions while locked but they are queued up and probably time out before your ALTER TABLE is done. Depending on factors like hardware, table structure and of course amount of rows (which you said is quite high) the alter will take a while.

On MySQL 5.5 (faster index creation, innodb), 8-core CPU, chip disks, altering a 5 mil row table with several indexes takes about 15-20 minutes in our case.

I suggest to create a copy, and alter the copy. You'll have to replay the data delta after you're done. Facebook had to deal with this on a way higher level, check this out

http://m.facebook.com/note.php?note_id=430801045932

Tho, I can't promise this would all work safely on the MyISAM Engine

EDIT:

Percona created a toolkit which apparently works on all storage engines:

http://www.mysqlperformanceblog.com/2012/04/05/percona-toolkit-2-1-with-new-online-schema-change-tool/

With this release we introduce a new version of pt-online-schema-change, a tool that enables you to ALTER large tables with no blocking or downtime. As you know, MySQL locks tables for most ALTER operations, but pt-online- schema-change performs the ALTER without any locking. Client applications can continue reading and writing the table with no interruption.

like image 197
Michel Feldheim Avatar answered Oct 21 '22 05:10

Michel Feldheim


While it's adding the new column, it will lock the table. The implication of this is that any application that tries to use the table will be blocked until it's done. No harm will be done to the data, but any applications that try to use the table will hang.

It's hard to say how long it will take, it depends on how big the database is already, since it will have to do lots of data copying, and the speed of your server. As someone said above, the way to find this out is to make a copy of the table and perform the change on the copy first.

like image 20
Barmar Avatar answered Oct 21 '22 06:10

Barmar