2006-07-25

convert MyISAM to InnoDB

MySQL的文档说InnoDB类型的表在处理数据比较大时,效率要比MyISAM类型好。urts52上有个字典数据库的数据为6.4个GB,不算小了。于是想把这个数据库的所有表从MyISAM 类型转换成 InnoDB类型,常见的办法是。
1. Dump the database with mysqldump
2. Change TYPE=ISAM to TYPE=INNODB
3. Add entries to /etc/my.cnf and restart MySQL
4. Load the database with mysql

不过如果MySQL server已经支持InnoDB了,可以用更简单的办法,如转换单个表的类型:

alter table mytable type=InnoDB;如果要转换一个数据库所有表,可以用:

for t in $(mysql --batch --column-names=false -e "show tables" mydbname);
do
mysql -e "alter table $t type=InnoDB" mydbname;
done


如果要转换一个数据库带有某个特定前缀的表或忽略某些表,可以搭配grep来使用:

for t in $(mysql --batch --column-names=false -e "show tables" mydbname |grep "include_this");
do
mysql -e "alter table $t type=InnoDB" mydbname;
done
for t in $(mysql --batch --column-names=false -e "show tables" mydbname | grep -v "exclude_this");
do
mysql -e "alter table $t type=InnoDB" mydbname;
done


或者直接输入多个表的名称: for t in table_1 table_2 table_n;
do
mysql -e "alter table $t type=InnoDB" mydbname;
done

没有评论:

发表评论