Saturday, April 18, 2009

Convert MyISAM tables to InnoDB

If you want to convert a MyISAM table to InnoDB, the process is fairly easy, but you can do something extra to speed things up. Before converting the table, adjust its order so that the primary key column is in order:

ALTER TABLE tablename ORDER BY 'primary_key_column';

This will pre-arrange the table so that it can be converted quickly without a lot of re-arranging required in MySQL. Then, simply change the table engine:

ALTER TABLE tablename ENGINE = INNODB;

If your table is large, then it may take a while to convert it over. There will probably be a fair amount of CPU usage and disk I/O in the process.

These statements are also safe in replicated environments. When you issue this statement to the master, it will begin the conversion process. Once it is complete on the master, the statement will roll down to the slaves, and they will begin the conversion as well. Keep in mind, however, that this can greatly reduce the performance of your configuration in the process.

Special thanks to Matthew Montgomery for the ORDER BY recommendation.

3 Responses

lokimona
October 24th, 2007 at 8:37 pm
will the same trick for me to convert innodb to Myisam just by changing the ENGINE = INNODB to ENGINE = MYISAM .Do i have to make changes


thornibr
November 19th, 2007 at 9:22 am
MySIAM data files are stored arbitrary - its only the ibdata files that are stored sorted to the primary key. So this is pointless to do when converting from innodb to MyIASM.


papsy
December 15th, 2008 at 6:28 pm
In some cases, this statement does not work:

ALTER TABLE tablename ORDER BY ‘primary_key_column’;

Instead, use:

ALTER TABLE tablename ORDER BY primary_key_column;

(Note the removal of the single quotes)

No comments: