日期:2014-05-16  浏览次数:20811 次

mysqldump新增参数控制插入数据和建索引顺序

InnoDB有个特性“fast index creation”,创建和删除非聚簇索引时不需要重建表。

这句是转载的:This mechanism also means that you can generally speed the overall process of creating and loading an indexed table by creating the table with only the clustered index, and adding the secondary indexes after the data is loaded.

?

因此想到在mysqldump中,如果构造为先执行insert,再加索引,能够加速大的InnoDB表的重建过程。

?

1、mysqldump中的 “disable keys”

? 其实类似的思路在现在的mysqldump中已经有了,在insert之前有alter table xxx disable keys,插入数据之后alter table xxx enable keys. 但这个InnoDB 不支持。

?

2、实现

新增一个参数--keys-last,当使用--keys-last=1时,在create table之后把非聚簇索引删除,insert之后再创建。

?

3、执行效果

CREATE TABLE `tb` (

? `a` int(11) NOT NULL DEFAULT '0',

? `c` int(11) DEFAULT NULL,

? PRIMARY KEY (`a`),

? UNIQUE KEY `a` (`a`,`c`),

? KEY `b` (`c`,`a`),

? KEY `ccc` (`a`,`c`),

? CONSTRAINT `tb_ibfk_1` FOREIGN KEY (`a`) REFERENCES `ta` (`a`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert into tb values(1,1);