17.2.2. LIST
Partitioning
List partitioning in MySQL is similar to range partitioning in
many ways. As in partitioning by RANGE
, each
partition must be explicitly defined. The chief difference is
that, in list partitioning, each partition is defined and
selected based on the membership of a column value in one of a
set of value lists, rather than in one of a set of contiguous
ranges of values. This is done by using PARTITION BY
LIST(expr
)
where
expr
is a column value or an
expression based on a column value and returning an integer
value, and then defining each partition by means of a
VALUES IN
(value_list
)
, where
value_list
is a comma-separated list
of integers.
Note: In MySQL
5.1, it is possible to match against only a list of
integers when partitioning by LIST
.
Unlike the case with partitions defined by range, list
partitions do not need to be declared in any particular order.
For more detailed syntactical information, see
Section 13.1.5, “CREATE TABLE
Syntax”.
For the examples that follow, we assume that the basic
definition of the table to be partitioned is provided by the
CREATE TABLE
statement shown here:
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
);
(This is the same table used as a basis for the examples in
Section 17.2.1, “RANGE
Partitioning”.)
Suppose that there are 20 video stores distributed among 4
franchises as shown in the following table:
To partition this table in such a way that rows for stores
belonging to the same region are stored in the same partition,
you could use the CREATE TABLE
statement
shown here:
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY LIST(store_id) (
PARTITION pNorth VALUES IN (3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,10,11,19,20),
PARTITION pWest VALUES IN (4,12,13,14,18),
PARTITION pCentral VALUES IN (7,8,15,16)
);
This makes it easy to add or drop employee records relating to
specific regions to or from the table. For instance, suppose
that all stores in the West region are sold to another company.
All rows relating to employees working at stores in that region
can be deleted with the query ALTER TABLE employees
DROP PARTITION pWest;
, which can be executed much more
efficiently than the equivalent DELETE
query
DELETE FROM employees WHERE store_id IN
(4,12,13,14,18);
.
Important: If you try to insert
a row such that the column value (or the partitioning
expression's return value) is not found in any of the
partitioning value lists, the INSERT
query
will fail with an error. For example, given the
LIST
partitioning scheme just outlined, this
query will fail:
INSERT INTO employees VALUES
(224, 'Linus', 'Torvalds', '2002-05-01', '2004-10-12', 42, 21);
Failure occurs because the store_id
column
value 21
is not found in any of the value
lists used to define partitions pNorth
,
pEast
, pWest
, or
pCentral
. It is important to note that there
is no “catch-all” definition for list partitions
analogous to VALUES LESS THAN MAXVALUE
which
accommodates values not found in any of the value lists. In
other words, any value which is to be matched must be
found in one of the value lists.
As with RANGE
partitioning, it is possible to
combine LIST
partitioning with partitioning
by hash or key to produce a composite partitioning
(subpartitioning). See
Section 17.2.5, “Subpartitioning”.