MERGE存储引擎是一组Myisam表的组合,这些Myisam表必须结构完全相同,MERGE表本身并没有数据,
对MERGE类型的表可以进行查询、更新、删除操作,这些操作实际上是对内部的Myisam表进行的。对于
MERGE类型表的插入操作,是通过INSERT_METHOD子句定义插入的表,可以有3个不同的值,使用FIRST或
LAST值使得插入操作被相应地作用在第一或最后一个表上,不定义这个子句或者定义为NO,表示不能对这个
MEGRE表执行插入操作。
可以对MERGE表进行DROP操作,这个操作只是删除MERGE的定义,对内部的表没有任何的影响。
MERGE在磁盘上保留两个文件,文件名以表的名字开始,一个.frm文件存储表定义,另一个.MRG文件
包含组合表的信息,包括MERGE表由哪些表组成、插入新的数据时的依据。
可以通过修改.MRG文件来修改MERGE表,但是修改后要通过FLUSH TABLES 刷新。
mysql> create table payment_2006(
-> country_id smallint,
-> payment_date datetime,
-> amount decimal(15,2),
-> key idx_fk_country_id(country_id))engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> create table payment_2007(
-> country_id smallint,
-> payment_date datetime,
-> amount decimal(15,2),
-> key idx_fk_country_id(country_id)
-> )engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> create table payment_all(
-> country_id smallint,
-> payment_date datetime,
-> amount decimal(15,2),
-> index(country_id)
-> )engine=merge union=(payment_2006,payment_2007) insert_method=last;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into payment_2006 values (1,'2006-05-01',100000),(2,'2006-08-15',150000);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into payment_2007 values (1,'2007-02-20',350000),(2,'2007-07-15',220000);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from payment_2006;
+------------+---------------------+-----------+
| country_id | payment_date | amount |
+------------+---------------------+-----------+
| 1 | 2006-05-01 00:00:00 | 100000.00 |
| 2 | 2006-08-15 00:00:00 | 150000.00 |
+------------+---------------------+-----------+
2 rows in set (0.00 sec)
mysql> select * from payment_2007;
+------------+---------------------+-----------+
| country_id | payment_date | amount |
+------------+---------------------+-----------+
| 1 | 2007-02-20 00:00:00 | 350000.00 |
| 2 | 2007-07-15 00:00:00 | 220000.00 |
+------------+---------------------+-----------+
2 rows in set (0.00 sec)
mysql> select * from payment_all;
+------------+---------------------+-----------+
| country_id | payment_date | amount |
+------------+---------------------+-----------+
| 1 | 2006-05-01 00:00:00 | 100000.00 |
| 2 | 2006-08-15 00:00:00 | 150000.00 |
| 1 | 2007-02-20 00:00:00 | 350000.00 |
| 2 | 2007-07-15 00:00:00 | 220000.00 |
+------------+---------------------+-----------+
4 rows in set (0.00 sec)
#可以发现,payment_all 表中的数据payment_2006 和payment_2007表的记录合并后的结果集。
在payment_all表插入一条数据后,由于MERGE表的定义是insert_method=last,会向表中最后一个插入记录,
虽然数据插入的是2006年的,但2007表中仍然会写到。
mysql> insert into payment_all values(3,'2006-03-31',112200);
Query OK, 1 row affected (0.00 sec)
mysql> select * from payment_all;
+------------+---------------------+-----------+
| country_id | payment_date | amount |
+------------+---------------------+-----------+
| 1 | 2006-05-01 00:00:00 | 100000.00 |
| 2 | 2006-08-15 00:00:00 | 150000.00 |
| 1 | 2007-02-20 00:00:00 | 350000.00 |
| 2 | 2007-07-15 00:00:00 | 220000.00 |
| 3 | 2006-03-31 00:00:00 | 112200.00 |
+------------+---------------------+-----------+
5 rows in set (0.00 sec)
mysql> select * from payment_2007;
+------------+---------------------+-----------+
| country_id | payment_date | amount |
+------------+---------------------+-----------+
| 1 | 2007-02-20 00:00:00 | 350000.00 |
| 2 | 2007-07-15 00:00:00 | 220000.00 |
| 3 | 2006-03-31 00:00:00 | 112200.00 |
+------------+---------------------+-----------+
3 rows in set (0.00 sec)
#这也是MERGE表和分区表的区别,MERGE表并不能智能地将记录写到对应的表中,而分区表示可以的
(分区功能在5.1中推出)通常我们使用MERGE表来透明的对多个表进行查询和更新操作,而对这种按照时间记录的操作日志表
则可以透明的进行插入操作。