KEY
Partitioning
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 MariaDB server. This internal hashing function is based on the same algorithm as PASSWORD()
.
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
is used rather thanHASH
.KEY
takes 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 TABLE
statement is valid in MariaDB 5.6:
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;
However, if the unique key column were not defined as
NOT NULL
, then the previous statement would fail.In both of these cases, the partitioning key is the
id
column, even though it is not shown in the output ofSHOW CREATE TABLE
or in thePARTITION_EXPRESSION
column of theINFORMATION_SCHEMA.PARTITIONS
table.Unlike the case with other partitioning types, columns used for partitioning by
KEY
are not restricted to integer orNULL
values. For example, the followingCREATE TABLE
statement is valid:CREATE TABLE tm1 ( s1 CHAR(32) PRIMARY KEY ) PARTITION BY KEY(s1) PARTITIONS 10;
The preceding statement would not be valid, were a different partitioning type to be specified. (In this case, simply using
PARTITION BY KEY()
would also be valid and have the same effect asPARTITION BY KEY(s1)
, sinces1
is the table's primary key.)For additional information about this issue, see , "Restrictions and Limitations on Partitioning".Important
For 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.
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;
Using LINEAR
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 , "LINEAR HASH
Partitioning", for a description of this algorithm and its implications.