1. 程式人生 > >Mysql的儲存引擎之:MERGE儲存引擎

Mysql的儲存引擎之:MERGE儲存引擎

Merge是基於MyISAM儲存引擎的表,所以Merge儲存引擎也叫做MGR_MyISAM儲存引擎,作用是將一組MyISAM儲存引擎的表聚合成一張表。

Merge中要聚合的MyISAM表的列和索引必須有相同的定義及順序。

建立MERGE儲存引擎的表

建立兩個基於MyISAM儲存引擎的表table_myisam1、tabl_myisam2:

mysql> create table table_myisam1(id intprimary key, name varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.04 sec)

mysql> create table table_myisam2(id intprimary key, name varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into table_myisam1 values(1,'myisam1');
Query OK, 1 rows affected (0.00 sec)
Records: 1 Duplicates: 0  Warnings: 0

mysql> insert into table_myisam2 values(1,' myisam2');
Query OK, 1 rows affected (0.00 sec)
Records: 1 Duplicates: 0  Warnings: 0

建立基於MERGE儲存引擎的表table_merge,聚合上面的table_myisam1、tabl_myisam2兩張表:

mysql> create table table_merge(id intprimary key, name varchar(20)) engine=merge union(table_myisam1,table_myisam2);
Query OK, 0 rows affected (0.05 sec)

查詢下table_merge表(就是聚合table_myisam1、tabl_myisam2表的資料):

mysql> select * from table_merge;
+----+---------+
| id | name    |
+----+---------+
|  1| myisam1 |
|  1| myisam2 |
+----+---------+
2 rows in set (0.00 sec)

其實這裡有個奇怪現象,就是Merge表中的ID欄位是主鍵,但現在重複了竟然沒事。。。

感興趣同學可以自己做個試驗研究一下

檢視下table_merge表的資料檔案:

注:. MRG不是儲存資料,而是指向資料來源地的檔案; .frm是表結構定義檔案

[[email protected] test]$ ll table_merge.*
-rw-rw---- 1 mysql mysql 8586 Sep  2 16:02 table_merge.frm
-rw-rw---- 1 mysql mysql   28 Sep 2 16:02 table_merge.MRG

檢視下 .MRG檔案內容(指向了聚合的兩張表):

[[email protected] test]$ more table_merge.MRG
table_myisam1
table_myisam2

注:使用MERGE儲存引擎的表不會儲存資料,只是彙總了MyISAM表的資料而已

MERGE表的插入操作

向Merge表插入資料有兩種操作方式:

1:向MyISAM子表插入資料

2:向Merge表插入資料

向MyISAM子表插入資料,然後在Merge表中體現

向table_myisam1表新增一條資料:

mysql> insert into table_myisam1 values(2,'new_myisam1');
Query OK, 1 row affected (0.00 sec)

檢視table_merge表資料:

mysql> select * from table_merge;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam1 |
|  1| myisam1     |
|  1| myisam2     |
+----+-------------+
3 rows in set (0.00 sec)

直接向MERGE表插入資料

直接向MERGE表插入資料的時候,需要加上插入的引數:insert_method

insert_method引數的可選項:

1:first 向第一個表插入資料

2:last 向最後一個表插入資料

3:no 不插入資料(預設值)

向MERGE表中聚合的最後一個表插入資料

mysql> alter table table_merge insert_method=last;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0  Warnings: 0

mysql> insert into table_merge values(2,'new_myisam2');
Query OK, 1 row affected (0.00 sec)

檢視下MERGE表table_merge:

mysql> select * from table_merge;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam1 |
|  1| myisam1     |
|  2| new_myisam2 |
|  1| myisam2     |
+----+-------------+
4 rows in set (0.00 sec)

檢視下MERGE表中聚合的最後一個MyISAM表table_myisam2:

mysql> select * from table_myisam2;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam2 |
|  1| myisam2     |
+----+-------------+
2 rows in set (0.00 sec)

MERGE表的DROP操作

對Merge表的Drop操作

Merge表是聚合了MyISAM表的操作,當drop一個Merge表時,只會drop掉Merge表本身,不會對聚合的MyISAM子表產生影響,同時各個MyISAM子表也不再有聯絡。

再建立一個用於測試drop的Merge表table_merge_drop:

mysql> create table table_merge_drop(id int primary key, name varchar(20)) engine=merge union(table_myisam1,table_myisam2);
Query OK, 0 rows affected (0.03 sec)

mysql> select * from table_merge_drop;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam1 |
|  1| myisam1     |
|  2| new_myisam2 |
|  1| myisam2     |
+----+-------------+
4 rows in set (0.00 sec)

Drop這個Merge表table_merge_drop:

mysql> drop table table_merge_drop;
Query OK, 0 rows affected (0.00 sec)

再次檢視MyISAM子表的資料,依然存在:

mysql> select * from table_myisam1;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam1 |
|  1| myisam1     |
+----+-------------+
2 rows in set (0.00 sec)

mysql> select * from table_myisam2;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam2 |
|  1| myisam2     |
+----+-------------+
2 rows in set (0.00 sec)

對Merge表中聚合的MyISAM子表的Drop操作

如果刪除了Merge表中某個MyISAM子表, Merge表會報錯,需要重新更改union聚合的表即可

刪除Merge表中MyISAM子表table_myisam2:

mysql> drop table table_myisam2;
Query OK, 0 rows affected (0.00 sec)

查詢Merge表會報錯:

mysql> select * from table_merge;
ERROR 1168 (HY000): Unable to openunderlying table which is differently defined or of non-MyISAM type or doesn'texist

更改Merger表的union表聚合,只保留table_myisam1這一個MyISAM子表

mysql> alter table table_merge engine=merge union(table_myisam1) ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0  Warnings: 0

再次查詢Merge表,正常:

mysql> select * from table_merge;
+----+-------------+
| id | name        |
+----+-------------+
|  2| new_myisam1 |
|  1| myisam1     |
+----+-------------+
2 rows in set (0.00 sec)