[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
MERGE
Tables
MERGE
tables were introduced in MySQL Version 3.23.25. The code
is now reasonably stable.
A MERGE
table (also known as a MRG_MyISAM
table) is a
collection of identical MyISAM
tables that can be used as one.
You can only SELECT
, DELETE
, and UPDATE
from the
collection of tables. If you DROP
the MERGE
table, you
are only dropping the MERGE
specification.
Note that DELETE FROM merge_table
used without a WHERE
will clear only the mapping for the table, not delete everything in the
mapped tables. (We plan to fix this in 4.1).
With identical tables we mean that all tables are created with identical
column and key information. You can't merge tables in which the
columns are packed differently, doesn't have exactly the same columns,
or have the keys in different order. However, some of the tables can be
compressed with myisampack
. See section myisampack
.
When you create a MERGE
table, you will get a `.frm' table
definition file and a `.MRG' table list file. The `.MRG' just
contains a list of the index files (`.MYI' files) that should
be used as one. Before 4.1.1 all used tables had to be in the same
database as the MERGE
table itself.
For the moment, you need to have SELECT
, UPDATE
, and
DELETE
privileges on the tables you map to a MERGE
table.
MERGE
tables can help you solve the following problems:
myisampack
, and then create a MERGE
to use these as one.
MERGE
table on this could be much faster than using
the big table. (You can also use a RAID to get the same
kind of benefits.)
MERGE
table for others. You can even have many
different MERGE
tables active, with possible overlapping files.
MERGE
file than trying to repair a really big file.
MERGE
table uses the
index of the individual tables. It doesn't need to maintain an index of
its one. This makes MERGE
table collections VERY fast to make or
remap. Note that you must specify the key definitions when you create
a MERGE
table!.
MERGE
table on them on demand.
This is much faster and will save a lot of disk space.
MERGE
over one table. There shouldn't be any really notable performance
impacts of doing this (only a couple of indirect calls and memcpy()
calls for each read).
The disadvantages with MERGE
tables are:
MyISAM
tables for a MERGE
table.
REPLACE
doesn't work.
MERGE
tables uses more file descriptors. If you are using a
MERGE
table that maps over 10 tables and 10 users are using this, you
are using 10*10 + 10 file descriptors. (10 datafiles for 10 users
and 10 shared index files.)
MERGE
storage engine will need to issue a read on all underlying tables to check
which one most closely matches the given key. If you then do a "read-next"
then the MERGE
storage engine will need to search the read buffers
to find the next key. Only when one key buffer is used up, the storage engine
will need to read the next key block. This makes MERGE
keys much slower
on eq_ref
searches, but not much slower on ref
searches.
See section 7.2.1 EXPLAIN
Syntax (Get Information About a SELECT
).
DROP TABLE
,
ALTER TABLE
,
DELETE FROM table_name
without a WHERE
clause,
REPAIR TABLE
,
TRUNCATE TABLE
,
OPTIMIZE TABLE
, or
ANALYZE TABLE
on any of the table that is
mapped by a MERGE
table that is "open". If you do this, the
MERGE
table may still refer to the original table and you will
get unexpected results. The easiest way to get around this deficiency
is to issue the FLUSH TABLES
command, ensuring no MERGE
tables remain "open".
When you create a MERGE
table you have to specify with
UNION=(list-of-tables)
which tables you want to use as
one. Optionally you can specify with INSERT_METHOD
if you want
insert for the MERGE
table to happen in the first or last table
in the UNION
list. If you don't specify INSERT_METHOD
or
specify NO
, then all INSERT
commands on the MERGE
table will return an error.
The following example shows you how to use MERGE
tables:
CREATE TABLE t1 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20)); CREATE TABLE t2 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20)); INSERT INTO t1 (message) VALUES ("Testing"),("table"),("t1"); INSERT INTO t2 (message) VALUES ("Testing"),("table"),("t2"); CREATE TABLE total (a INT NOT NULL AUTO_INCREMENT, message CHAR(20), KEY(a)) TYPE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST; SELECT * FROM total; |
Note that we didn't create a UNIQUE
or PRIMARY KEY
in the
total
table as the key isn't going to be unique in the total
table.
Note that you can also manipulate the `.MRG' file directly from the outside of the MySQL server:
shell> cd /mysql-data-directory/current-database shell> ls -1 t1.MYI t2.MYI > total.MRG shell> mysqladmin flush-tables |
Now you can do things like:
mysql> SELECT * FROM total; +---+---------+ | a | message | +---+---------+ | 1 | Testing | | 2 | table | | 3 | t1 | | 1 | Testing | | 2 | table | | 3 | t2 | +---+---------+ |
Note that the a
column, though declared as PRIMARY KEY
,
is not really unique, as MERGE
table cannot enforce uniqueness
over a set of underlying MyISAM
tables.
To remap a MERGE
table you can do one of the following:
DROP
the table and re-create it
ALTER TABLE table_name UNION=(...)
FLUSH TABLE
on the
MERGE
table and all underlying tables to force the storage engine to
read the new definition file.
If you use ALTER TABLE
to change a MERGE
table to another
table type, the mapping to the underlying tables is lost. Instead, the rows
from the underlying MyISAM
tables are copied into the altered table,
which then is assigned the new type.
14.2.1 MERGE Table Problems |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |