Convert MyISAM tables to InnoDB

Did you know that InnoDB replaces MyISAM as the default storage engine since MySQL 5.5.5? For this reason you may want to convert most (if not all) of your current MyISAM tables to InnoDB.

WordPress does for example make use of the default storage engine. That means old WordPress installations will make use of MyISAM if you don’t update them to use InnoDB instead.

SQL to get the job done

This is the SQL to convert a single table:

ALTER TABLE `databasename`.`tablename` ENGINE=InnoDB;

You can generate this SQL for all tables in a certain set of databases by using the following query:

SELECT CONCAT('ALTER TABLE `', table_schema, '`.`', TABLE_NAME, '` ENGINE=InnoDB;') AS query
FROM information_schema.tables
WHERE table_schema IN ('db1', 'db2');

For a list of the tables that use MyISAM run the following query:

SELECT table_schema, TABLE_NAME
FROM information_schema.tables
WHERE engine = 'MyISAM';

The above query will show you tables in the databases “information_schema” and “mysql” and you may not care to see those since they are system databases that should not be tampered with. This query excludes them from the results:

SELECT table_schema, TABLE_NAME
FROM information_schema.tables
WHERE engine = 'MyISAM' AND table_schema NOT IN('information_schema', 'mysql');

And why not combine it with the query generator while we are at it:

SELECT table_schema, TABLE_NAME, CONCAT('ALTER TABLE `', table_schema, '`.`', TABLE_NAME, '` ENGINE=InnoDB;') AS query
FROM information_schema.tables
WHERE engine = 'MyISAM' AND table_schema NOT IN('information_schema', 'mysql');
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *