EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MySQL Partition

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » MySQL Tutorial » MySQL Partition

MySQL Partition

Introduction to MySQL Partition

The following article provides an outline for MySQL Partition. Partitions are the dividers or separators that can be created in the tables of the database that store a huge number of records. Partitioning leads to scalability and an increase in the performance of the database access and retrieval of data with very few additional resources. Implementing partitions in your tables helps in faster retrieval of data when proper queries and partitions are created.

The column or parameter on which the partition is being created in the table must always be present in the where clause of the insert, update, delete and select queries that are being constructed on that table. If not done so, it will lead to the scanning of whole table records instead of a single partition where that record will belong which will prove to be very time and resource consuming.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Checking Support for Partitions in MySQL

We can check whether MySQL is supporting the partitions by checking the contents of plugins table located inside the information_schema schema using the following query.

Code:

SELECT PLUGIN_STATUS,PLUGIN_NAME,PLUGIN_VERSION FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_TYPE='STORAGE ENGINE' and PLUGIN_NAME = "partition";

Output:

Gives the following output if your MySQL partition support is active:

MySQL Partition 1

Alternatively, you can fire the following command to check whether partition plugin is active and supported by your version of MySQL.

Code:

SHOW PLUGINS;

Output:

Gives the following output that will contain a plugin named partition and its status along with all other plugins of database:

MySQL Partition 2

Types of Partitions in MySQL

Partitions can be broadly classified in two types:

Types of Partitions in MySQL

  • Horizontal Partitioning
  • Vertical Partitioning

1. Horizontal Partitioning

In horizontal partitioning, the data is split and partitioned by dividing the number of rows of the table.

For example, suppose that a table consists of 150000 rows then we can create any number of partitions say 10 partitions on that table, so that each partition will contain 15000 rows. Horizontal partitioning reduces the number of rows to be managed while performing any database operation on that table. Horizontal partitioning is further classified depending on the type of key used for partitioning.

2. Vertical Partitioning

Vertical partitioning deals with the separation of the columns instead of rows. Hence the resulting partitions will contain the same number of rows as they were previously in the original table but the number of columns will be reduced. This works similarly to normalization but vertical partitioning implements certain concepts that are even more advanced than normalization.

Techniques of Partitioning

Given below are the techniques of partitioning:

1. Range Partitioning

In this type of partition we create the partitions based on the range of the values. This range needs to be consecutive and at the same time none of the range should overlap the other one. All the records whose column value will fall into the particular range value will be added in that partition. For creating the partitions based on range VALUES LESS THAN clause is used to define the partition ranges while creation.

For example, let us create a table that will store the cities in the country and create 5 partitions based on pin codes.

Code:

CREATE TABLE cities (
id INT NOT NULL,
name VARCHAR(30),
population VARCHAR(30),
pincode INT NOT NULL)
PARTITION BY RANGE (pincode) (
PARTITION p0 VALUES LESS THAN (215000),
PARTITION p1 VALUES LESS THAN (415000),
PARTITION p2 VALUES LESS THAN (615000),
PARTITION p3 VALUES LESS THAN (815000),
PARTITION p4 VALUES LESS THAN (999999));

Output:

Range

2. List Partitioning

List partitioning is quite similar in working as of range partitioning. The only difference between both of partitioning techniques is that in list partitioning you need to mention the set of the values in the comma-separated format for each partition instead of defining the whole range of values. Note that the values that are mentioned for partitioning should be discrete. PARTITION BY LIST (expression) clause is used to mention the expression which can be any column having integer datatype or any other expression on columns that will result in an integer value. Further, VALUES IN (list of values) is used to specify the set of discrete integer values that will fall into that partition.

Popular Course in this category
MS SQL Training (13 Courses, 11+ Projects)13 Online Courses | 11 Hands-on Projects | 62+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,653 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)

For example, we will create a table named writers that will contain columns name, id, rate, and joining_date and we will create four partitions based on rates. Consider that the fixed values of rates are 500, 550, 600, 650,….1000. So, here we will create 4 partitions depending on the rate values.

Code:

CREATE TABLE `writers` (
`id` int(11) NOT NULL,
`name` varchar(10) COLLATE latin1_danish_ci NOT NULL,
`rate` integer DEFAULT NULL,
`joining_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_danish_ci
PARTITION BY LIST(rate) (
PARTITION low VALUES IN (500,550),
PARTITION medium VALUES IN (600,650,700),
PARTITION high VALUES IN (750,800,850),
PARTITION best VALUES IN (900,950,1000)
);

Output:

List

3. Hash Partitioning

In hash partitioning, we do not need to specify the values that will be considered for the partitioning and which value will fall into which partition is decided by the MySQL itself. We just need to mention the expression on which hashing will be done and the number of partitions to be created. We can mention that we want to perform hash partition by using PARTITION BY HASH (expression) where expression should always return integer value and further use PARTITIONS to mention the number of partitions to create.

For example, we will create articles table and 4 partitions on technology_id column using hash partitioning technique.

Code:

CREATE TABLE articles (
id INT NOT NULL,
name VARCHAR(30),
status VARCHAR(30),
submit_date DATE NOT NULL DEFAULT '1970-01-01',
technology_id INT
)
PARTITION BY HASH(technology_id)
PARTITIONS 4;

Output:

Hash

4. Key Partitioning

This technique is similar to hash partitioning except that the hashing of the columns is done internally by MySQL and we don’t need to mention the hash expression. Instead just mention the PARTITION BY KEY(). It is not necessary for these columns of keys to be of integer type.

We can use key partitioning as shown in the following example where two partitions are created for table testers on their key using key partitioning technique.

Code:

CREATE TABLE testers (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20)
)
PARTITION BY KEY()
PARTITIONS 2;

Output:

Key

Conclusion

We can implement partitioning in the database when there is too much of data residing in the tables. Various techniques can be used for partitioning. Which one to choose depends upon the use case and requirement. Partition leads to great performance and scalability in the MySQL database.

Recommended Articles

This is a guide to MySQL Partition. Here we discuss the introduction to MySQL Partition, checking support for partitions, types of partitions and techniques. You may also have a look at the following articles to learn more –

  1. MySQL Database Repair
  2. MySQL Timestamp
  3. MySQL IN Operator
  4. MySQL Constraints

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MySQL Tutorial
  • Advanced
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL UUID
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL Transaction
    • MySQL sort_buffer_size
    • MySQL Sync
    • MySQL Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
  • Basic
    • Introduction to MySQL
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • How To Install MySQL
    • MySQL OpenSource
    • MySQL Commands
    • Views in MySQL
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL count()
    • MIN() in MySQL
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • TRUNCATE TABLE MySQL
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL 
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT 
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL COALESCE
    • MySQL Cluster
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - MS SQL Certification Courses Learn More