Home >Database >Mysql Tutorial >merge存储引擎应用_MySQL

merge存储引擎应用_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:52:021203browse

merge存储引擎,也叫做MRG_MyISAM,可以将同构的表合在一起使用。文档上说的“同构”指的是表定义相同,表的索引相同,但根据测试,索引结构不一样也没有问题。甚至在某些情况下,必须不同才能取得正确的数据。

我们创建两个表m1,m2,并各插入几条数据。

 1 mysql> show create table m1/G<br> 2 *************************** 1. row ***************************<br> 3        Table: m1<br> 4 Create Table: CREATE TABLE `m1` (<br> 5   `a` int(11) NOT NULL,<br> 6   `b` int(11) DEFAULT NULL,<br> 7   PRIMARY KEY (`a`)<br> 8 ) ENGINE=MyISAM DEFAULT CHARSET=latin1<br> 9 1 row in set (0.00 sec)<br>10 <br>11 mysql> show create table m2/G<br>12 *************************** 1. row ***************************<br>13        Table: m2<br>14 Create Table: CREATE TABLE `m2` (<br>15   `a` int(11) NOT NULL,<br>16   `b` int(11) DEFAULT NULL,<br>17   PRIMARY KEY (`a`)<br>18 ) ENGINE=MyISAM DEFAULT CHARSET=latin1<br>19 1 row in set (0.00 sec)<br>20 <br>21 mysql> select * from m1;<br>22 +---+------+<br>23 | a | b    |<br>24 +---+------+<br>25 | 1 |    1 |<br>26 | 2 |    2 |<br>27 | 3 |    3 |<br>28 +---+------+<br>29 3 rows in set (0.00 sec)<br>30 <br>31 mysql> select * from m2;<br>32 +---+------+<br>33 | a | b    |<br>34 +---+------+<br>35 | 1 |    1 |<br>36 | 2 |    2 |<br>37 | 3 |    3 |<br>38 +---+------+<br>39 3 rows in set (0.00 sec)

接下来,我们创建一个merge表m,注意,我们没有为m指定任何的key,但是该merge表是可以使用的:

 1 mysql> show create table m/G<br> 2 *************************** 1. row ***************************<br> 3        Table: m<br> 4 Create Table: CREATE TABLE `m` (<br> 5   `a` int(11) NOT NULL,<br> 6   `b` int(11) DEFAULT NULL<br> 7 ) ENGINE=MRG_MyISAM DEFAULT CHARSET=latin1 UNION=(`m1`,`m2`)<br> 8 1 row in set (0.00 sec)<br> 9 <br>10 mysql> select * from m;<br>11 +---+------+<br>12 | a | b    |<br>13 +---+------+<br>14 | 1 |    1 |<br>15 | 2 |    2 |<br>16 | 3 |    3 |<br>17 | 1 |    1 |<br>18 | 2 |    2 |<br>19 | 3 |    3 |<br>20 +---+------+<br>21 6 rows in set (0.00 sec)

对m加上primary key(a)之后,毫无疑问,这也是可以使用的,但是因为key是primay key,所以只能检索出一条记录:

 1 mysql> show create table m/G<br> 2 *************************** 1. row ***************************<br> 3        Table: m<br> 4 Create Table: CREATE TABLE `m` (<br> 5   `a` int(11) NOT NULL,<br> 6   `b` int(11) DEFAULT NULL,<br> 7   PRIMARY KEY (`a`)<br> 8 ) ENGINE=MRG_MyISAM DEFAULT CHARSET=latin1 UNION=(`m1`,`m2`)<br> 9 1 row in set (0.00 sec)<br>10 <br>11 mysql> select * from m where a=1;<br>12 +---+------+<br>13 | a | b    |<br>14 +---+------+<br>15 | 1 |    1 |<br>16 +---+------+<br>17 1 row in set (0.00 sec)

将primary key改成普通的key之后:

 1 mysql> alter table m add index (a), drop primary key;<br> 2 Query OK, 0 rows affected (0.00 sec)<br> 3 Records: 0  Duplicates: 0  Warnings: 0<br> 4 <br> 5 mysql> select * from m where a=1;<br> 6 +---+------+<br> 7 | a | b    |<br> 8 +---+------+<br> 9 | 1 |    1 |<br>10 | 1 |    1 |<br>11 +---+------+<br>12 2 rows in set (0.00 sec)

merge表在很多情况下都可以给我们带来便利,特别是在分表的环境中尤其合适。

欢迎上网易彩票买彩票啊~
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn