HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to add column to big table in MySQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnbigmysqlhowtableadd

Problem

I'm a PHP developer so don't be strict. I have a big table ~5.5gb dump. Our PM decided to make new column in it to perform new feature. Table is InnoDB so what i tried:

-
Alter table in screen with table lock. Took ~30hours and nothing. So I just stopped it. First I made a mistake because I didn't end all transactions but the 2nd time was no multilock. Status was copy to tmp table.

-
Since I also need to apply partitioning for this table we decide to make dump, rename and make table with same name and new structure. But dump is making strict copy(at least I didn't found something else). So i added to dump a new column with sed and query it. But some strange errors began. I believe it was caused by charset. Table in utf-8 and file became us-ascii after sed. So I got errors(unknown command '\'') on 30% of data. So this is also a bad way.

What are other options to accomplish this and speed performance(I can do it with php script, but it will took ages). What will be performance of INSERT SELECT in this case.

Thanks for any advance.

Solution

alter table add column, algorithm=inplace, lock=none will alter a MySQL 5.6 table without copying the table and without locking impact.

Just tested this yesterday, mass inserted 70K rows into a 280K row 7 partition table, 10K rows into each partition, with 5 seconds sleep in between to allow other throughput.

Started the mass inserts, then in separate session started the online alter statement above in MySQL Workbench, the alter finished before the inserts, two new columns were added, and no rows resulted from the alter meaning MySQL did not copy any rows.

Context

StackExchange Database Administrators Q#44777, answer score: 19

Revisions (0)

No revisions yet.