15.7 The MERGE Storage Engine

The 'MERGE' storage engine, also known as the 'MRG_MyISAM' engine, is a collection of identical 'MyISAM' tables that can be used as one. 'Identical' means that all tables have identical column data types and index information. You cannot merge 'MyISAM' tables in which the columns are listed in a different order, do not have exactly the same data types in corresponding columns, or have the indexes in different order. However, any or all of the 'MyISAM' tables can be compressed with note 'myisampack': myisampack. See note myisampack::. Differences between tables such as these do not matter:

An alternative to a 'MERGE' table is a partitioned table, which stores partitions of a single table in separate files. Partitioning enables some operations to be performed more efficiently and is not limited to the 'MyISAM' storage engine. For more information, see *note partitioning::.

When you create a 'MERGE' table, MySQL creates two files on disk. The files have names that begin with the table name and have an extension to indicate the file type. An '.frm' file stores the table format, and an '.MRG' file contains the names of the underlying 'MyISAM' tables that should be used as one. The tables do not have to be in the same database as the 'MERGE' table.

You can use note 'SELECT': select, note 'DELETE': delete, note 'UPDATE': update, and note 'INSERT': insert. on 'MERGE' tables. You must have 'SELECT', 'DELETE', and 'UPDATE' privileges on the 'MyISAM' tables that you map to a 'MERGE' table.

Note:

The use of 'MERGE' tables entails the following security issue: If a user has access to 'MyISAM' table T, that user can create a 'MERGE' table M that accesses T. However, if the user's privileges on T are subsequently revoked, the user can continue to access T by doing so through M.

Use of *note 'DROP TABLE': drop-table. with a 'MERGE' table drops only the 'MERGE' specification. The underlying tables are not affected.

To create a 'MERGE' table, you must specify a 'UNION=(LIST-OF-TABLES)' option that indicates which 'MyISAM' tables to use. You can optionally specify an 'INSERT_METHOD' option to control how inserts into the 'MERGE' table take place. Use a value of 'FIRST' or 'LAST' to cause inserts to be made in the first or last underlying table, respectively. If you specify no 'INSERT_METHOD' option or if you specify it with a value of 'NO', inserts into the 'MERGE' table are not permitted and attempts to do so result in an error.

The following example shows how to create a 'MERGE' table:

 mysql> CREATE TABLE t1 (
     ->    a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     ->    message CHAR(20)) ENGINE=MyISAM;
 mysql> CREATE TABLE t2 (
     ->    a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     ->    message CHAR(20)) ENGINE=MyISAM;
 mysql> INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
 mysql> INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
 mysql> CREATE TABLE total (
     ->    a INT NOT NULL AUTO_INCREMENT,
     ->    message CHAR(20), INDEX(a))
     ->    ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;

Column 'a' is indexed as a 'PRIMARY KEY' in the underlying 'MyISAM' tables, but not in the 'MERGE' table. There it is indexed but not as a 'PRIMARY KEY' because a 'MERGE' table cannot enforce uniqueness over the set of underlying tables. (Similarly, a column with a 'UNIQUE' index in the underlying tables should be indexed in the 'MERGE' table but not as a 'UNIQUE' index.)

After creating the 'MERGE' table, you can use it to issue queries that operate on the group of tables as a whole:

 mysql> SELECT * FROM total;
 +---+---------+
 | a | message |
 +---+---------+
 | 1 | Testing |
 | 2 | table   |
 | 3 | t1      |
 | 1 | Testing |
 | 2 | table   |
 | 3 | t2      |
 +---+---------+

To remap a 'MERGE' table to a different collection of 'MyISAM' tables, you can use one of the following methods:

The underlying table definitions and indexes must conform closely to the definition of the 'MERGE' table. Conformance is checked when a table that is part of a 'MERGE' table is opened, not when the 'MERGE' table is created. If any table fails the conformance checks, the operation that triggered the opening of the table fails. This means that changes to the definitions of tables within a 'MERGE' may cause a failure when the 'MERGE' table is accessed. The conformance checks applied to each table are:

If a 'MERGE' table cannot be opened or used because of a problem with an underlying table, *note 'CHECK TABLE': check-table. displays information about which table caused the problem.

Additional Resources

 File: manual.info.tmp, Node: merge-table-advantages, Next: merge-table-problems, Prev: merge-storage-engine, Up: merge-storage-engine

15.7.1 MERGE Table Advantages and Disadvantages

'MERGE' tables can help you solve the following problems:

The disadvantages of 'MERGE' tables are:

 File: manual.info.tmp, Node: merge-table-problems, Prev: merge-table-advantages, Up: merge-storage-engine

15.7.2 MERGE Table Problems

The following are known problems with 'MERGE' tables:

 File: manual.info.tmp, Node: federated-storage-engine, Next: example-storage-engine, Prev: merge-storage-engine, Up: storage-engines