EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Partitioning
 

MySQL Partitioning

Updated May 18, 2023

MySQL Partitioning

 

 

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:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  • 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: –

  1. LIST Partitioning
  2. RANGE Partitioning
  3. COLUMNS Partitioning
  4. HASH Partitioning
  5. KEY Partitioning
  6. 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'))
);

Range-1.1

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)
);

MySQL Partitioning-1.2

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' )
);

MySQL Partitioning-1.3

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 )
);

MySQL Partitioning-1.4

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;

Output 1.5

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;

MySQL Partitioning-1.6

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
);

Output 1.7

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.

  1. MySQL IF Function
  2. Working with MySQL UUID
  3. MySQL Merge
  4. Toad for MySQL

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW