1. 程式人生 > >mysql開啟innoDB並且批量修改儲存引擎

mysql開啟innoDB並且批量修改儲存引擎

查詢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)

修改表的儲存引擎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-------