Updated May 18, 2023
Introduction of MySQL Partitioning
MySQL Partitioning is used to improve performance and reduce the cost of storing large data. By partitioning, we are splitting the tables, indexes, and index-organized tables into smaller pieces by which queries can run faster. The partitioning can be done in two major forms:
- Horizontal Partitioning
- Vertical Partitioning.
Horizontal partitioning is a partition that will divide the table rows into multiple partitions. It doesn’t have any attribute missing in the partitions. Vertical partitioning is to partition a table row into various partitions.
In vertical partitioning, the most referenced columns in one table and the rest of the columns that are not frequently referenced will be stored in another partition. Partition support has to be provided to MySQL. To enable, we do it by option DWITH_PARTITION_STORAGE_ENGINE.
Syntax of MySQL Partitioning
Below is the syntax for the partition declaration: –
<Create table definition>
<table options>
<partition options>
The partition options are specified below: –
partition_options:
PARTITION BY
{ [LINEAR] HASH(expr)
| [LINEAR] KEY(column_list)
| RANGE(expr)
| LIST(expr) }
[PARTITIONS num]
[SUBPARTITION BY
{ [LINEAR] HASH(expr)
| [LINEAR] KEY(column_list) }
[SUBPARTITIONS num]
]
Types of Partitioning
Different Partition types are mentioned below: –
- LIST Partitioning
- RANGE Partitioning
- COLUMNS Partitioning
- HASH Partitioning
- KEY Partitioning
- Sub partitioning
Now let us see in detail the partitions: –
1. LIST Partitioning
LIST Partitioning allows data partitioning based on the values defined at the table creation time.
Syntax:
PARTITON BY LIST( <expression> )
(
PARTITION <partition_name1> VALUES IN ( <Desired partition1 values>)
PARTITION <partition_name2> VALUES IN ( <Desired partition2 values>)
.
.
PARTITION <partition_nameN> VALUES IN ( <Desired partitionN values>)
)
2. RANGE Partitioning
RANGE Partitioning allows the partition of the data based on the RANGE. The specified RANGE should be contiguous.
Syntax:
PARTITON BY RANGE ( <expression> )
(
PARTITION <partition_name1> VALUES LESS THAN( <Desired partition1 Date>)
PARTITION <partition_name2> VALUES LESS THAN ( <Desired partition2 Date>)
.
.
PARTITION <partition_nameN> VALUES LESS THAN ( <Desired partitionNDate>)
)
3. COLUMN Partitioning
COLUMN Partitioning can be done based on multiple columns. Therefore, we have two types of COLUMN Partitioning.
- RANGE COLUMNS Partitioning and
- LIST COLUMNS Partitioning.
Range Columns
It is similar to the RANGE partition. Here RANGE COLUMNS accepts a list of one or more columns as the partition key.
Syntax:
PARTITON BY RANGE COLUMNS( <column list> )
(
PARTITION <partition_name1> VALUES LESS THAN ( <Desired value list >)
PARTITION <partition_name2> VALUES LESS THAN ( <Desired value list >)
.
.
PARTITION <partition_nameN> VALUES LESS THAN ( <Desired value list >)
)
List Columns
It is similar to the LIST partition. Here LIST COLUMNS accepts a list of one or more columns as the partition key.
Syntax:
PARTITON BY LIST COLUMNS ( <column list> )
(
PARTITION <partition_name1> VALUES LESS THAN ( <Desired value list >)
PARTITION <partition_name2> VALUES LESS THAN ( <Desired value list >)
.
.
PARTITION <partition_nameN> VALUES LESS THAN ( <Desired value list >)
)
4. HASH Partitioning
Here in HASH, Partitioning the partition is done based on the column value and the number of partitions.
Syntax:
PARTITION BY HASH ( <expression> )
PARTITIONS <NUM Clause>;
5. KEY Partitioning
The KEY Partition is similar to the HASH partition. The MySQL server will do the Hashing function for the key partition.
Syntax:
PARTITION BY KEY( <expression> )
PARTITIONS <NUM Clause>;
6. SUB Partitioning
Sub-partition is to partition the partition table further.
Syntax:
PARTITON BY LIST <partition type1>( <expression> )
SUBPARTITION BY <partition type2>( <expression> )
SUBPARTITION <num clause>
How Does MySQL Partitioning Work?
Now let us see how the partition works in the tables: –
1. RANGE Partitioning
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE MONTHLY_SALES
(
SALES_NO INT,
SALES_DATE timestamp,
CUST_CODE INT,
TOTAL_AMOUNT int
)
PARTITION BY RANGE( UNIX_TIMESTAMP(SALES_DATE))
(
PARTITION PT1 VALUES LESS THAN (UNIX_TIMESTAMP('2020-04-01')),
PARTITION PT2 VALUES LESS THAN (UNIX_TIMESTAMP('2020-05-01')),
PARTITION PT3 VALUES LESS THAN (UNIX_TIMESTAMP('2020-06-01'))
);
2. LIST Partitioning
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE MONTHLY_SALES_LIST
(
SALES_NO INT,
SALES_DATE timestamp,
CUST_CODE INT,
TOTAL_AMOUNT int
)
PARTITION BY LIST ( CUST_CODE )
(
PARTITION PT1 VALUES IN( 1,2,3,4 ),
PARTITION PT2 VALUES IN ( 5,6,7,8 ),
PARTITION PT3 VALUES IN ( 9,10, 11, 12)
);
3. RANGE COLUMNS Partitioning
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE SALES_RANGE_COLUMNS
(
SALES_NO INT,
CUST_CODE INT,
SALES_NAME VARCHAR(20)
)
PARTITION BY RANGE COLUMNS ( SALES_NO, CUST_CODE, SALES_NAME )
(
PARTITION PT1 VALUES LESS THAN ( 1, 2, 'LUX' ),
PARTITION PT2 VALUES LESS THAN ( 2, 4, 'Paper' ),
PARTITION PT3 VALUES LESS THAN ( 3, 5, 'Pen' )
);
4. LIST COLUMNS Partitioning
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE SALES_LIST _COLUMNS
(
SALES_NO INT,
CUST_CODE INT,
SALES_NAME VARCHAR(20)
)
PARTITION BY LIST COLUMNS ( SALES_NO )
(
PARTITION PT1 VALUES IN ( 1, 2, 3 ),
PARTITION PT2 VALUES IN ( 4, 5, 6 ),
PARTITION PT3 VALUES IN ( 7, 8, 9 )
);
5. Key Partition
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE KEY_PARTITION
(
ID INT,
NAME VARCHAR(10),
LOCATION VARCHAR(20)
)
PARTITION BY KEY( ID)
PARTITIONS 2;
6. HASH Partition
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE MONTHLY_SALES_HASH
(
SALES_NO INT,
SALES_DATE timestamp,
CUST_CODE INT,
TOTAL_AMOUNT int
)
PARTITION BY HASH ( SALES_NO)
PARTITIONS 3;
7. SUB Partitioning
Now let us create the table and see how partition works on it:
Code:
CREATE TABLE MONTHLY_SALES_SUB_PARTITION
(
sale_NO INT,
sale_date DATE,
cust_codeVARCHAR(15),
AMOUNT int
)
PARTITION BY RANGE(YEAR(sale_date) )
SUBPARTITION BY HASH(TO_DAYS(sale_date))
SUBPARTITIONS 4 (
PARTITION pt0 VALUES LESS THAN (1990),
PARTITION pt1 VALUES LESS THAN (2000),
PARTITION pt2 VALUES LESS THAN (2010),
PARTITION pt3 VALUES LESS THAN MAXVALUE
);
Recommended Articles
We hope that this EDUCBA information on “MySQL Partitioning” was beneficial to you. You can view EDUCBA’s recommended articles for more information.