您的位置:首页 > 数据库 > MySQL

mysql开启innoDB并且批量修改存储引擎

2015-10-09 14:34 761 查看
查找mysql plugins lib目录
mysql> show variables like 'plugin_dir';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| plugin_dir    | /www/wdlinux/mysql-5.1.69/lib/mysql/plugin |
+---------------+--------------------------------------------+

查找是否已经存在innodb的so从步骤二中查看是否已经存在innodb的so如果不存在 则从mysql的安装结构中将innodb so拷贝到plugins_dir目录中

查看现有的引擎

mysql> show engines;
+------------+---------+-----------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                   | Transactions | XA   | Savepoints |
+------------+---------+-----------------------------------------------------------+--------------+------+------------+
| CSV        | YES     | CSV storage engine                                        | NO           | NO   | NO         |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                     | NO           | NO   | NO         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance    | NO           | NO   | NO         |
+------------+---------+-----------------------------------------------------------+--------------+------+------------+

安装innodb

mysql> install plugin InnoDB soname 'ha_innodb.so';

生成批量修改数据库引擎SQL

mysql> SELECT CONCAT('ALTER TABLE ',table_name,' ENGINE=InnoDB;') FROM information_schema.tables WHERE table_schema='weboa' AND ENGINE='myisam';

查看数据表使用引擎

mysql> select concat(table_schema,'.',table_name) as table_name ,engine from information_schema.tables where table_schema = 'weboa';

mysql修改表的存储引擎(myisam<=>innodb)
http://blog.csdn.net/wyzxg/article/details/7412969

修改表的存储引擎myisam<=>innodb

 

查看表的存储引擎
mysql> show create table tt7;

+-------+-------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                            |

+-------+-------------------------------------------------------------------------------------------------------------------------+

| tt7   | CREATE TABLE `tt7` (

  `id` int(10) default NULL,

  `name` char(10) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 | 

+-------+-------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

查看表的数据量
mysql> select count(1) from tt7;

+----------+

| count(1) |

+----------+

| 16777216 | 

+----------+

1 row in set (0.00 sec)

 

方法一:

直接更改存储引擎
mysql> alter table tt7 engine=innodb;

Query OK, 16777216 rows affected (2 min 39.80 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

方法二:

 

把方法一中的存储引擎改回myisam
mysql> alter table tt7 engine=myisam;

Query OK, 16777216 rows affected (27.09 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

从这里也可以看出myisam表要比innodb表快很多

 

创建个和tt7同样表结构的表
mysql> create table tt7_tmp like tt7;

Query OK, 0 rows affected (0.02 sec)

 

tt7_tmp作为中间结果集
mysql> insert into tt7_tmp select * from tt7;

Query OK, 16777216 rows affected (27.20 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

 

删除原表的数据
mysql> truncate table tt7;

Query OK, 16777725 rows affected (0.18 sec)

 

这回更改原表的存储引擎
mysql> alter table tt7 engine=innodb;

Query OK, 0 rows affected (0.06 sec)

Records: 0  Duplicates: 0  Warnings: 0

速度很快就完成了

 

再把中间结果集的数据导回原表中
mysql> insert into tt7 select * from tt7_tmp;

Query OK, 16777216 rows affected (2 min 0.95 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

 

删除中间表
mysql> drop table tt7_tmp;

测试结果:

方法二比较快一点,但是数据量要是比较大的话,方法二就要采用化整为零的分批操作的方式,否则insert操作将会具耗时,并产生大量的undo日志。

如果是小表的话(500M以内,根据自己系统的硬件环境),采用方法一就可以

如果是大表的话,那就采用方法二+批量的方式

如果是批量更改表的存储引擎

用于生成变更的SQL语句:
SELECT CONCAT('ALTER TABLE ',table_name,' ENGINE=InnoDB;') FROM information_schema.tables WHERE table_schema='db_name' AND ENGINE='myisam';

用于生成检查表的SQL语句:
SELECT CONCAT('CHECK TABLE ',table_name) FROM information_schema.tables WHERE table_schema='db_name';

根据自己系统配置修改如下参数,以加快变更速度(记得以前的值,一会还得改回来)
SET GLOBAL sort_buffer_size=64*1024*1024;

SET GLOBAL tmp_table_size=64*1024*1024;

SET GLOBAL read_buffer_size=32*1024*1024;

SET GLOBAL read_rnd_buffer_size=32*1024*1024;

------end-------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: