Optimizing MySQL for ALTER TABLE of InnoDB

schuilr picture schuilr · Mar 17, 2009 · Viewed 23.4k times · Source

Sometime soon we will need to make schema changes to our production database. We need to minimize downtime for this effort, however, the ALTER TABLE statements are going to run for quite a while. Our largest tables have 150 million records, largest table file is 50G. All tables are InnoDB, and it was set up as one big data file (instead of a file-per-table). We're running MySQL 5.0.46 on an 8 core machine, 16G memory and a RAID10 config.

I have some experience with MySQL tuning, but this usually focusses on reads or writes from multiple clients. There is lots of info to be found on the Internet on this subject, however, there seems to be very little information available on best practices for (temporarily) tuning your MySQL server to speed up ALTER TABLE on InnoDB tables, or for INSERT INTO .. SELECT FROM (we will probably use this instead of ALTER TABLE to have some more opportunities to speed things up a bit).

The schema changes we are planning to do is adding a integer column to all tables and make it the primary key, instead of the current primary key. We need to keep the 'old' column as well so overwriting the existing values is not an option.

What would be the ideal settings to get this task done as quick as possible?

Answer

Tadas Sasnauskas picture Tadas Sasnauskas · Jul 3, 2012

You might want to look at pt-online-schema-change from Percona toolkit. Essentially what it does is:

  • Copies original table structure, runs ALTER.
  • Copies rows from old table to newly created one.
  • Uses triggers to track and sync changes while copying.
  • When everything is finished it swaps tables by renaming both.

Works very well for single instance databases, but might be quite tricky if you use replication and you can't afford stopping slaves and rebuilding them later.

There's also a nice webinar about this here.

PS: I know it's an old question, just answering in case someone hits this via search engine.