Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing table type to InnoDB

Tags:

mysql

innodb

I have a myisam only dedicated 32 GB RAM mysql server that is running on default configuration. I want to change the engine type to InnoDB of one table in order to avoid table locks. It has 50 million records and size on disk is around 15 GB. I am using mysql version 5.5 I guess I will need to add the following options and restart mysql.

innodb_buffer_pool_size=1G
innodb_log_file_size=100M
innodb_file_per_table=1

What else is considered to be necessary while changing the engine type?

like image 786
shantanuo Avatar asked Apr 06 '12 04:04

shantanuo


People also ask

How can I convert tables from MyISAM to InnoDB?

SELECT CONCAT('ALTER TABLE ',TABLE_NAME,' ENGINE=InnoDB;') FROM INFORMATION_SCHEMA. TABLES WHERE ENGINE='MyISAM' AND table_schema = 'mydatabase'; Works like a charm. After running this you first need to execute the following query: USE databasename; Then you can use the queries that the above script gives.

Should I change MyISAM to InnoDB?

A lot of older sites are still using the MyISAM storage engine in their database. Over recent years, InnoDB has shown to perform better and be more reliable. A big reason to use InnoDB over MyISAM, is the lack of full table-level locking. This allows your queries to process faster.

How do I change my default engine to InnoDB in MySQL?

Set your default storage engine to InnoDB by adding default_storage_engine=InnoDB to the [mysqld] section of the system config file located at: /etc/my. cnf. Restarting the MySQL service is necessary for the server to detect changes to the file.


2 Answers

You'll actually be running a command to convert each table.

It goes faster to first sort the table:

ALTER TABLE tablename ORDER BY primary_key_column;

Then, run the alter command:

ALTER TABLE tablename ENGINE = INNODB;

It might take a while if the table is really large, and it will use a lot of your CPU....

like image 158
Jonathan Barlow Avatar answered Oct 03 '22 08:10

Jonathan Barlow


First of all check if your database supports InnoDB engine (I bet it is supported ;)):

SHOW ENGINES\G

If so, there is already default innodb related parameters in place, check them with:

SHOW VARIABLES LIKE '%innodb%'

and try to understand them and alter the to your specific needs. Even if you use the default params, you are now fine to play arround with InnoDB tables.

If you want to create only InnoDB tables, you can change your default storage engine, either for your current session with: SET storage_engine=INNODB; or in your config using default-storage-engine option.

By the way, the fastest way to convert a table to InnoDB is not the above described way. One can do the following to convert a table to InnoDB by simply inserting the data:

CREATE TABE new AS SELECT * FROM old WHERE 1<>1;
ALTER TABLE new ENGINE = INNODB;
INSERT INTO new SELECT * FROM old;

Of course you have to add the indexes you need manually, but its usually worth the time (and pain) you save compared to the ALTER TABLE ... on slightly bigger tables.

like image 22
Flo Doe Avatar answered Oct 03 '22 06:10

Flo Doe