8

I have a MySQL database that is up to about 17 GB in size and has 38 million entries. At the moment, I need to both increase the size of one column (varchar 40 to varchar 80) and add more columns.

Many of the fields are indexed including the one that I need to change. It is part of a unique pair that is necessary for the applications to work. In attempting to just make the change yesterday, the query ran for almost four hours without finishing, when I decided to cut our outage and just bring the service back up.

What is the most efficient way to make changes to something of this size?

Many of these entries are also old and if there is a good way to sort of shard off entries but still have them available that might help with this problem by making the table a much more manageable size.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cris Favero
  • 632
  • 8
  • 16

3 Answers3

8

You have some choices.

In any case you should take a backup before you do this stuff.

One possibility is to take your service offline and do it in place, as you have tried. If you do that you should disable key checks and constraints.

ALTER TABLE bigtable DISABLE KEYS;
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE (whatever);
ALTER TABLE (whatever else);
...
SET FOREIGN_KEY_CHECKS=1;
ALTER TABLE bigtable ENABLE KEYS;

This will allow the ALTER TABLE operation to go faster. It will regenerate the indexes all at once when you do ENABLE KEYS.

Another possibility is to create a new table with the new schema you want, then disable the keys on the new table, then do as @Bader suggested and insert the contents of the old table.

After your new table is built you will re-enable the keys on it, then rename the old table to some name like "old_bigtable" then rename the new table to "bigtable".

It's possible that you can keep your service online while you're populating the new table. But that might work poorly.

A third possibility is to dump your giant table (to a flat file) and then load it to a new table with the new layout. That is pretty much like the second possibility except that you get a table backup for free. You can make this go pretty fast with SELECT DATA INTO OUTFILE and LOAD DATA INFILE. You'll need to have access to your server machine's file system to do this.

In all cases, disable, then re-enable, the constraints and keys to get things to go fast.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 1
    the first one sounds like what i want I think i will attempt spinning up a few hour old back of the amzaon rds instance and attempt this one before trying another outage. – Cris Favero Oct 19 '12 at 17:11
  • 2
    After having time to work on this over the weekend the problem with this method is simply that innodb does not let you disable keys. – Cris Favero Oct 24 '12 at 14:33
6

Create a new table with the new structure you want with a different name for example NewTable.

Then insert data into this new table from the old table using the following query:

INSERT INTO NewTable (field1, field2, etc...) SELECT field1, field2, ... FROM OldTable

After this is done, you can drop the old table and rename the new table to the original name

DROP TABLE `OldTable`;
RENAME TABLE `NewTable` TO `OldTable` ;

I have tried this approach on a very large table and it's much much faster than altering the table.

PyQL
  • 1,830
  • 3
  • 18
  • 22
3

With MySQL 5.1 and again with 5.5 certain alter statements were enhanced to just modify the structure without rewriting the entire table ( http://dev.mysql.com/doc/refman/5.5/en/alter-table.html - search for in-place). The availability of this though varies by the type of change you are making and the engine in use, the most value comes from InnoDB Plugin. In the case of your specific changes though the entire table would be rewritten.

When we encounter these issues, we typically try to leverage replica databases. As long as you are adding and not removing you can run your DDL against the replica first and then schedule a brief outage for promoting the replica to the master role. If you happen to be on RDS this is even one of their suggested uses for their replica instances http://aws.amazon.com/about-aws/whats-new/2012/10/11/amazon-rds-mysql-rr-promotion/.

Some other alternatives include:

  • Selecting out a subset of records into a new table with the desired structure (use INTO OUTFILE to avoid a table lock). Once complete you can schedule a maintenance window and REPLACE INTO or UPDATE any records that have changed in the origin table since the initial data copy. Once the update is complete a RENAME TABLE... of both tables wraps the changes up.
  • Using a tool like Percona's pt-online-schema-change: http://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html. This tool works with triggers so if you already have triggers on the tables you want to change this may not fit your needs.
tpol
  • 301
  • 1
  • 6