Search code examples
mysqlforeign-keysinnodbmyisamperformance

Restore InnoDB tables: very very slow?


I used to deal with Myisam tables, but for foreign connections I changed to Innodb tables (converted, and added foreign keys). I often download the database from the server, and fill the localhost with it. When it was myisam tables, it used to be fast as hell. But now it requires more than 30 minutes... I know it must do checks before insert records, but I disabled the foreign key checking. Some line of it:

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS t1;
CREATE TABLE `t1` (  `ID` int(10) unsigned NOT NULL auto_increment,  `NAME` varchar(255) NOT NULL default '',  PRIMARY KEY  (`ID`),  UNIQUE KEY `Index_2` (`NAME`)) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
TRUNCATE TABLE t1;

drop/create/truncate all tables. Then INSERT, INSERT... how to make it faster?


Solution

  • Assuming you use mysqldump, use --extended-insert. This will combine the inserts of multiple rows into one statement.

    Also try the --disable-keys option. Not only will this disable foreign key checks, it will also disable updating of indices until all rows have been inserted. It's typically much faster to update the index all at once.

    If you're not using mysqldump, you can execute ALTER TABLE table_name DISABLE KEYS; before the dump and ALTER TABLE table_name ENABLE KEYS; after the dump for each table.

    However, you'll likely find that a database-engine specific backup tool will be faster. Try XtraBackup.