PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5
This section discusses the types of partitioning which are available in MySQL 5.7. These include the types listed here:
RANGE partitioning. This type of partitioning assigns rows to partitions based on column values falling within a given range. SeeSection 22.2.1, “RANGE Partitioning”. For information about an extension to this type,
RANGE COLUMNS, seeSection 22.2.3.1, “RANGE COLUMNS partitioning”.LIST partitioning. Similar to partitioning by
RANGE, except that the partition is selected based on columns matching one of a set of discrete values. SeeSection 22.2.2, “LIST Partitioning”. For information about an extension to this type,LIST COLUMNS, seeSection 22.2.3.2, “LIST COLUMNS partitioning”.HASH partitioning. With this type of partitioning, a partition is selected based on the value returned by a user-defined expression that operates on column values in rows to be inserted into the table. The function may consist of any expression valid in MySQL that yields an integer value. SeeSection 22.2.4, “HASH Partitioning”.
An extension to this type,
LINEAR HASH, is also available, seeSection 22.2.4.1, “LINEAR HASH Partitioning”.KEY partitioning. This type of partitioning is similar to partitioning by
HASH, except that only one or more columns to be evaluated are supplied, and the MySQL server provides its own hashing function. These columns can contain other than integer values, since the hashing function supplied by MySQL guarantees an integer result regardless of the column data type. An extension to this type,LINEAR KEY, is also available. SeeSection 22.2.5, “KEY Partitioning”.
A very common use of database partitioning is to segregate data by date. Some database systems support explicit date partitioning, which MySQL does not implement in 5.7. However, it is not difficult in MySQL to create partitioning schemes based onDATE,TIME, orDATETIME columns, or based on expressions making use of such columns.
When partitioning byKEY orLINEAR KEY, you can use aDATE,TIME, orDATETIME column as the partitioning column without performing any modification of the column value. For example, this table creation statement is perfectly valid in MySQL:
CREATE TABLE members ( firstname VARCHAR(25) NOT NULL, lastname VARCHAR(25) NOT NULL, username VARCHAR(16) NOT NULL, email VARCHAR(35), joined DATE NOT NULL)PARTITION BY KEY(joined)PARTITIONS 6; In MySQL 5.7, it is also possible to use aDATE orDATETIME column as the partitioning column usingRANGE COLUMNS andLIST COLUMNS partitioning.
MySQL's other partitioning types, however, require a partitioning expression that yields an integer value orNULL. If you wish to use date-based partitioning byRANGE,LIST,HASH, orLINEAR HASH, you can simply employ a function that operates on aDATE,TIME, orDATETIME column and returns such a value, as shown here:
CREATE TABLE members ( firstname VARCHAR(25) NOT NULL, lastname VARCHAR(25) NOT NULL, username VARCHAR(16) NOT NULL, email VARCHAR(35), joined DATE NOT NULL)PARTITION BY RANGE( YEAR(joined) ) ( PARTITION p0 VALUES LESS THAN (1960), PARTITION p1 VALUES LESS THAN (1970), PARTITION p2 VALUES LESS THAN (1980), PARTITION p3 VALUES LESS THAN (1990), PARTITION p4 VALUES LESS THAN MAXVALUE);Additional examples of partitioning using dates may be found in the following sections of this chapter:
For more complex examples of date-based partitioning, see the following sections:
MySQL partitioning is optimized for use with theTO_DAYS(),YEAR(), andTO_SECONDS() functions. However, you can use other date and time functions that return an integer orNULL, such asWEEKDAY(),DAYOFYEAR(), orMONTH(). SeeSection 12.7, “Date and Time Functions”, for more information about such functions.
It is important to remember—regardless of the type of partitioning that you use—that partitions are always numbered automatically and in sequence when created, starting with0. When a new row is inserted into a partitioned table, it is these partition numbers that are used in identifying the correct partition. For example, if your table uses 4 partitions, these partitions are numbered0,1,2, and3. For theRANGE andLIST partitioning types, it is necessary to ensure that there is a partition defined for each partition number. ForHASH partitioning, the user-supplied expression must evaluate to an integer value. ForKEY partitioning, this issue is taken care of automatically by the hashing function which the MySQL server employs internally.
Names of partitions generally follow the rules governing other MySQL identifiers, such as those for tables and databases. However, you should note that partition names are not case-sensitive. For example, the followingCREATE TABLE statement fails as shown:
mysql> CREATE TABLE t2 (val INT) -> PARTITION BY LIST(val)( -> PARTITION mypart VALUES IN (1,3,5), -> PARTITION MyPart VALUES IN (2,4,6) -> );ERROR 1488 (HY000): Duplicate partition name mypart Failure occurs because MySQL sees no difference between the partition namesmypart andMyPart.
When you specify the number of partitions for the table, this must be expressed as a positive, nonzero integer literal with no leading zeros, and may not be an expression such as0.8E+01 or6-2, even if it evaluates to an integer value. Decimal fractions are not permitted.
In the sections that follow, we do not necessarily provide all possible forms for the syntax that can be used for creating each partition type; this information may be found inSection 13.1.18, “CREATE TABLE Statement”.
PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5