以下是已知的 MERGE 表问题:The following are known problems with MERGE
tables:
MERGE子表通过父表被锁定。如果父表是临时表,它不会被锁定,因此子表也不会被锁定;这意味着并行使用底层的MyISAM表会导致它们损坏。MERGEchild tables are locked through the parent table. If the parent is a temporary table, it is not locked, and thus the child tables are also not locked; this means that parallel use of the underlyingMyISAMtables corrupts them.如果你使用
ALTER TABLE将一个MERGE表更改为另一种存储引擎,则与底层表的映射将丢失。相反,底层MyISAM表中的行会被复制到修改后的表中,然后该表使用指定的存储引擎。If you useALTER TABLEto change aMERGEtable to another storage engine, the mapping to the underlying tables is lost. Instead, the rows from the underlyingMyISAMtables are copied into the altered table, which then uses the specified storage engine.MERGE表的INSERT_METHOD表选项指示用于向MERGE表插入数据的底层MyISAM表。然而,在至少向该MyISAM表直接插入一行之前,为该MyISAM表使用的AUTO_INCREMENT表选项对向MERGE表的插入没有影响。TheINSERT_METHODtable option for aMERGEtable indicates which underlyingMyISAMtable to use for inserts into theMERGEtable. However, use of theAUTO_INCREMENTtable option for thatMyISAMtable has no effect for inserts into theMERGEtable until at least one row has been inserted directly into theMyISAMtable.MERGE表无法在整个表上维护唯一性约束。当你执行INSERT操作时,数据会进入第一个或最后一个MyISAM表(由INSERT_METHOD选项决定)。MySQL 确保唯一键值在该MyISAM表内保持唯一,但在集合中的所有底层表中并不保证唯一。AMERGEtable cannot maintain uniqueness constraints over the entire table. When you perform anINSERT, the data goes into the first or lastMyISAMtable (as determined by theINSERT_METHODoption). MySQL ensures that unique key values remain unique within thatMyISAMtable, but not over all the underlying tables in the collection.由于
MERGE引擎无法对底层表集合强制唯一性约束,REPLACE不能按预期工作。两个关键事实是:Because theMERGEengine cannot enforce uniqueness over the set of underlying tables,REPLACEdoes not work as expected. The two key facts are:REPLACE只能检测其将要写入的基础表中的唯一键冲突(由INSERT_METHOD选项决定)。这与MERGE表本身中的冲突不同。REPLACEcan detect unique key violations only in the underlying table to which it is going to write (which is determined by theINSERT_METHODoption). This differs from violations in theMERGEtable itself.如果
REPLACE检测到唯一键冲突,它只会更改它正在写入的基础表中的相应行;也就是说,依据INSERT_METHOD选项确定的第一个或最后一个表。IfREPLACEdetects a unique key violation, it changes only the corresponding row in the underlying table it is writing to; that is, the first or last table, as determined by theINSERT_METHODoption.
类似的考虑也适用于
INSERT ... ON DUPLICATE KEY UPDATE。Similar considerations apply forINSERT ... ON DUPLICATE KEY UPDATE.MERGE表不支持分区。也就是说,你不能对MERGE表进行分区,也不能对MERGE表的任何底层MyISAM表进行分区。MERGEtables do not support partitioning. That is, you cannot partition aMERGEtable, nor can any of aMERGEtable's underlyingMyISAMtables be partitioned.你不应该对任何映射到开放的
MERGE表的表使用ANALYZE TABLE、REPAIR TABLE、OPTIMIZE TABLE、ALTER TABLE、DROP TABLE、DELETE(没有WHERE子句),或TRUNCATE TABLE。如果这样做,MERGE表可能仍然引用原始表,并产生意外结果。为了解决这个问题,请确保在执行上述操作之前,通过执行FLUSH TABLES语句关闭所有MERGE表。You should not useANALYZE TABLE,REPAIR TABLE,OPTIMIZE TABLE,ALTER TABLE,DROP TABLE,DELETEwithout aWHEREclause, orTRUNCATE TABLEon any of the tables that are mapped into an openMERGEtable. If you do so, theMERGEtable may still refer to the original table and yield unexpected results. To work around this problem, ensure that noMERGEtables remain open by issuing aFLUSH TABLESstatement prior to performing any of the named operations.意外的结果包括在
MERGE表上操作时可能报告表损坏。如果在对基础MyISAM表执行命名操作之后发生这种情况,损坏消息是虚假的。为了解决这个问题,在修改MyISAM表之后发出FLUSH TABLES语句。The unexpected results include the possibility that the operation on theMERGEtable reports table corruption. If this occurs after one of the named operations on the underlyingMyISAMtables, the corruption message is spurious. To deal with this, issue aFLUSH TABLESstatement after modifying theMyISAMtables.DROP TABLE对正在被MERGE表使用的表在 Windows 上无法工作,因为MERGE存储引擎的表映射对 MySQL 的上层是隐藏的。Windows 不允许删除打开的文件,因此您必须先刷新所有MERGE表(使用FLUSH TABLES)或在删除表之前删除MERGE表。DROP TABLEon a table that is in use by aMERGEtable does not work on Windows because theMERGEstorage engine's table mapping is hidden from the upper layer of MySQL. Windows does not permit open files to be deleted, so you first must flush allMERGEtables (withFLUSH TABLES) or drop theMERGEtable before dropping the table.在访问表时(例如,作为
SELECT或INSERT语句的一部分),会检查MyISAM表和MERGE表的定义。这些检查通过比较列顺序、类型、大小以及相关索引,确保表的定义与父MERGE表的定义匹配。如果表之间存在差异,将返回错误并导致语句失败。由于这些检查在打开表时进行,对单个表的任何定义更改,包括列更改、列顺序调整和存储引擎修改,都将导致语句失败。The definition of theMyISAMtables and theMERGEtable are checked when the tables are accessed (for example, as part of aSELECTorINSERTstatement). The checks ensure that the definitions of the tables and the parentMERGEtable definition match by comparing column order, types, sizes and associated indexes. If there is a difference between the tables, an error is returned and the statement fails. Because these checks take place when the tables are opened, any changes to the definition of a single table, including column changes, column ordering, and engine alterations cause the statement to fail.MERGE表及其底层表中索引的顺序应当一致。如果您使用ALTER TABLE向用于MERGE表的表添加UNIQUE索引,然后再使用ALTER TABLE向MERGE表添加非唯一索引,如果底层表中已有非唯一索引,则表的索引顺序将不同。(这是因为ALTER TABLE会将UNIQUE索引放在非唯一索引前,以便快速检测重复键。)因此,对具有此类索引的表执行查询可能返回意外结果。The order of indexes in theMERGEtable and its underlying tables should be the same. If you useALTER TABLEto add aUNIQUEindex to a table used in aMERGEtable, and then useALTER TABLEto add a nonunique index on theMERGEtable, the index ordering is different for the tables if there was already a nonunique index in the underlying table. (This happens becauseALTER TABLEputsUNIQUEindexes before nonunique indexes to facilitate rapid detection of duplicate keys.) Consequently, queries on tables with such indexes may return unexpected results.如果你遇到类似于 ERROR 1017 (HY000):找不到文件:'
tbl_name.MRG' (errno: 2) 的错误信息,这通常表示某些底层表没有使用MyISAM存储引擎。请确认所有这些表都是MyISAM。If you encounter an error message similar to ERROR 1017 (HY000): Can't find file: 'tbl_name.MRG' (errno: 2), it generally indicates that some of the underlying tables do not use theMyISAMstorage engine. Confirm that all of these tables areMyISAM.MERGE表的最大行数是 264(约 1.844E19;与MyISAM表相同)。无法将多个MyISAM表合并成一个行数超过此数量的MERGE表。The maximum number of rows in aMERGEtable is 264 (~1.844E+19; the same as for aMyISAMtable). It is not possible to merge multipleMyISAMtables into a singleMERGEtable that would have more than this number of rows.当前已知使用具有不同行格式的底层
MyISAM表与父MERGE表时会失败。请参见 Bug #32364。Use of underlyingMyISAMtables of differing row formats with a parentMERGEtable is currently known to fail. See Bug #32364.当
MERGE生效时,你不能更改非临时LOCK TABLES表的并集列表。以下操作 不起作用:You cannot change the union list of a nontemporaryMERGEtable whenLOCK TABLESis in effect. The following does not work:CREATE TABLE m1 ... ENGINE=MRG_MYISAM ...; LOCK TABLES t1 WRITE, t2 WRITE, m1 WRITE; ALTER TABLE m1 ... UNION=(t1,t2) ...;
但是,你可以在临时
MERGE表上执行此操作。However, you can do this with a temporaryMERGEtable.你不能使用
CREATE ... SELECT创建MERGE表,无论是临时MERGE表还是非临时MERGE表。例如:You cannot create aMERGEtable withCREATE ... SELECT, neither as a temporaryMERGEtable, nor as a nontemporaryMERGEtable. For example:CREATE TABLE m1 ... ENGINE=MRG_MYISAM ... SELECT ...;
尝试这样做会导致错误:
tbl_name不是BASE TABLE。Attempts to do this result in an error:tbl_nameis notBASE TABLE.在某些情况下,如果底层表包含
CHAR或BINARY列,MERGE表与底层表之间PACK_KEYS表选项值不同,可能会导致意外结果。作为解决方法,请使用ALTER TABLE确保所有相关表具有相同的PACK_KEYS值。(错误 #50646)In some cases, differingPACK_KEYStable option values among theMERGEand underlying tables cause unexpected results if the underlying tables containCHARorBINARYcolumns. As a workaround, useALTER TABLEto ensure that all involved tables have the samePACK_KEYSvalue. (Bug #50646)