按键分区类似于按哈希分区,只不过哈希分区使用用户定义的表达式,而键分区的哈希函数由 MySQL 服务器提供。NDB Cluster 使用 MD5() 来实现这个目的;对于使用其他存储引擎的表,服务器使用其自身的内部哈希函数。Partitioning by key is similar to partitioning by hash, except
that where hash partitioning employs a user-defined expression,
the hashing function for key partitioning is supplied by the
MySQL server. NDB Cluster uses
MD5() for this purpose; for
tables using other storage engines, the server employs its own
internal hashing function.
CREATE TABLE ... PARTITION BY
KEY 的语法规则与创建按哈希分区的表类似。主要区别如下:The syntax rules for CREATE TABLE ... PARTITION BY
KEY are similar to those for creating a table that is
partitioned by hash. The major differences are listed here:
使用
KEY而不是HASH。KEYis used rather thanHASH.KEY只接受零个或多个列名。任何用作分区键的列必须包含表的主键的全部或部分,如果表有主键的话。如果没有指定列名作为分区键,则使用表的主键(如果存在)。例如,以下CREATE TABLE语句在 MySQL 8.4 中是有效的:KEYtakes only a list of zero or more column names. Any columns used as the partitioning key must comprise part or all of the table's primary key, if the table has one. Where no column name is specified as the partitioning key, the table's primary key is used, if there is one. For example, the followingCREATE TABLEstatement is valid in MySQL 8.4:CREATE TABLE k1 ( id INT NOT NULL PRIMARY KEY, name VARCHAR(20) ) PARTITION BY KEY() PARTITIONS 2;如果没有主键但有唯一键,则使用唯一键作为分区键:If there is no primary key but there is a unique key, then the unique key is used for the partitioning key:
CREATE TABLE k1 ( id INT NOT NULL, name VARCHAR(20), UNIQUE KEY (id) ) PARTITION BY KEY() PARTITIONS 2;但是,如果唯一键列未定义为
NOT NULL,则前述语句将会失败。However, if the unique key column were not defined asNOT NULL, then the previous statement would fail.在这两种情况下,分区键都是
id列,尽管它未在SHOW CREATE TABLE的输出中显示,也未在信息模式PARTITIONS表的PARTITION_EXPRESSION列中显示。In both of these cases, the partitioning key is theidcolumn, even though it is not shown in the output ofSHOW CREATE TABLEor in thePARTITION_EXPRESSIONcolumn of the Information SchemaPARTITIONStable.与其他分区类型不同,用于
KEY分区的列不限制为整数或NULL值。例如,以下CREATE TABLE语句是有效的:Unlike the case with other partitioning types, columns used for partitioning byKEYare not restricted to integer orNULLvalues. For example, the followingCREATE TABLEstatement is valid:CREATE TABLE tm1 ( s1 CHAR(32) PRIMARY KEY ) PARTITION BY KEY(s1) PARTITIONS 10;如果指定了不同的分区类型,上述语句将无效。(在这种情况下,简单地使用
PARTITION BY KEY()也将有效,并且与PARTITION BY KEY(s1)具有相同的效果,因为s1是表的主键。)The preceding statement would not be valid, were a different partitioning type to be specified. (In this case, simply usingPARTITION BY KEY()would also be valid and have the same effect asPARTITION BY KEY(s1), sinces1is the table's primary key.)有关此问题的更多信息,请参见第 26.6 节,“分区的限制和限制”。For additional information about this issue, see Section 26.6, “Restrictions and Limitations on Partitioning”.
Columns with index prefixes are not supported in partitioning keys. This means that
CHAR,VARCHAR,BINARY,以及VARBINARYcolumns can be used in a partitioning key, as long as they do not employ prefixes; because a prefix must be specified forBLOB以及TEXTcolumns in index definitions, it is not possible to use columns of these two types in partitioning keys. In older versions of MySQL, columns using prefixes were permitted when creating, altering, or upgrading a partitioned table, even though they were not included in the table's partitioning key; in MySQL 8.4, this is no longer allowed deprecated, and the server rejects anyCREATE TABLE或者ALTER TABLEstatement affecting a partitioned table in which one or more columns using prefixes occur with an error. See Column index prefixes not supported for key partitioning.Columns with index prefixes are not supported in partitioning keys. This means thatCHAR,VARCHAR,BINARY, andVARBINARYcolumns can be used in a partitioning key, as long as they do not employ prefixes; because a prefix must be specified forBLOBandTEXTcolumns in index definitions, it is not possible to use columns of these two types in partitioning keys. In older versions of MySQL, columns using prefixes were permitted when creating, altering, or upgrading a partitioned table, even though they were not included in the table's partitioning key; in MySQL 8.4, this is no longer allowed deprecated, and the server rejects anyCREATE TABLEorALTER TABLEstatement affecting a partitioned table in which one or more columns using prefixes occur with an error. See Column index prefixes not supported for key partitioning.Note使用
NDB存储引擎的表会根据KEY隐式分区,使用表的主键作为分区键(与其他 MySQL 存储引擎相同)。如果 NDB Cluster 表没有显式主键,则NDB存储引擎为每个 NDB Cluster 表生成的 “隐藏” 主键将用作分区键。Tables using theNDBstorage engine are implicitly partitioned byKEY, using the table's primary key as the partitioning key (as with other MySQL storage engines). In the event that the NDB Cluster table has no explicit primary key, the “hidden” primary key generated by theNDBstorage engine for each NDB Cluster table is used as the partitioning key.If you define an explicit partitioning scheme for an
NDBtable, the table must have an explicit primary key, and any columns used in the partitioning expression must be part of this key. However, if the table uses an “空” partitioning expression—that is,PARTITION BY KEY()with no column references—then no explicit primary key is required.If you define an explicit partitioning scheme for anNDBtable, the table must have an explicit primary key, and any columns used in the partitioning expression must be part of this key. However, if the table uses an “empty” partitioning expression—that is,PARTITION BY KEY()with no column references—then no explicit primary key is required.您可以使用 ndb_desc 工具(带
-p选项)来观察此分区。You can observe this partitioning using the ndb_desc utility (with the-poption).ImportantFor a key-partitioned table, you cannot execute an
ALTER TABLE DROP PRIMARY KEY, as doing so generates the error ERROR 1466 (HY000): Field in list of fields for partition function not found in table. This is not an issue for NDB Cluster tables which are partitioned byKEY; in such cases, the table is reorganized using the “hidden” primary key as the table's new partitioning key. See Chapter 25, MySQL NDB Cluster 8.4.
也可以按线性键对表进行分区。下面是一个简单的示例:It is also possible to partition a table by linear key. Here is a simple example:
CREATE TABLE tk (
col1 INT NOT NULL,
col2 CHAR(5),
col3 DATE
)
PARTITION BY LINEAR KEY (col1)
PARTITIONS 3;
LINEAR 关键字对 KEY 分区的效果与对 HASH 分区的效果相同,只是分区编号是使用二的幂算法而不是取模运算得出的。有关该算法及其影响的描述,请参见 第 26.2.4.1 节,“LINEAR HASH 分区”。The LINEAR keyword has the same effect on
KEY partitioning as it does on
HASH partitioning, with the partition number
being derived using a powers-of-two algorithm rather than modulo
arithmetic. See Section 26.2.4.1, “LINEAR HASH Partitioning”, for
a description of this algorithm and its implications.