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.
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:
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:
Types of Partitions in MySQL
Partitions can be broadly classified in two types:
- 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:
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.
4.5 (5,653 ratings)
View Course
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:
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:
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:
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 –