Performance Tuning - Partitioning
Performance Tuning - Partitioning
Advantages of partitioning
During the scan operation, MySQL optimizer accesses those partitions that will satisfy a
particular query. For example, a whole year sale records table may be broken up into 4
partitions (i.e. sale data from of Apr-Jun (partition p0), Jul-Sep (partition p1) , Oct-Dec
(partition p2), Jam-Mar (partition p0)) . If a query is issued that contains sale data
between Jul-Sep quarter, then it scans the partition p1 only instead of total table records
and the query will complete much sooner.
Partitioning allows you to have more control over how data is managed inside the
database. For example, you can drop specific partitions in a partitioned table where data
loses its usefulness. The process of adding new data, in some cases, be greatly facilitated
by adding one or more new partitions for storing that data using ALTER TABLE
command.
In partitioning, it is possible to store more data in one table than can be held on a single
disk or file system partition.
MySQL 5.6 supports explicit partition selection for queries. For example, SELECT *
FROM table1 PARTITION (p0,p1) WHERE col1< 10 selects only those rows in
partitions p0 and p1 that match the WHERE condition, this can greatly speed up queries
Partition selection also supports the data modification statements DELETE, INSERT,
REPLACE, UPDATE, and LOAD DATA, LOAD XML.
■ LIST— A partition is assigned based on a list of integer values. If the partition key has
one of these values, the partition is chosen.
For example, all rows where the field city_id
contains integers that reference New Delhi, Noida, Gandhi Nagar, or
gurugram could form one partition for Florida cities, and another partition could contain
city_id values for cities in Massachusetts such as Boston, Cambridge, Amherst, and
Northampton.
■ HASH —The value of a hash function determines membership in a partition. If there are
10 partitions, the hash function returns a value from 0 to 9.
■ KEY—An internal algorithm is used by mysqld to try to evenly distribute the data across
the partitions. A field, called the key column, is used for the determination of data
distribution.
RANGE and LIST partitions can be subpartitioned using HASH or KEY partitioning. This is
known as composite partitioning.
Each of these partitioning algorithms utilizes a partition key for determining in which partition
the data is stored. This partition key is defined when creating the partition and must be an
integer.
Additionally, if the table has a primary key, the primary key must include all the fields in
the partition key. If the table has no primary key but has a unique key, the unique key must
include all the fields in the partition key. However, a table can use partitioning if it has no
primary and unique keys specified.
No matter what the partition algorithm is, the advantage of using partitioning is partition
pruning.
If the field used in the partition key is part of a join or a filter (i.e., in the JOIN or WHERE
clauses), the query optimizer will know which partition(s) it needs to search. Thus, the list of
partitions to search is pruned, and query execution is faster. Horizontal partitioning makes data
more manageable, and partition pruning can make queries on partitioned data much faster.
RANGE partitioning
A table that is partitioned by range is partitioned in such a way that each partition contains rows
for which the partitioning expression value lies within a given range. Ranges should be
contiguous but not overlapping, and are defined using the VALUES LESS THAN operator.
Below table can be partitioned by range in a number of ways, depending on your needs. One way
would be to use the store_id column. For instance, you might decide to partition the table in
number of ways by adding a PARTITION BY RANGE clause as shown here:
alter table employees add partition ( partition p4 values less than maxvalue );
Again this …
Insert into employees values (104,'Sush','ogiri',501,22,'DW HOUSE DEPT','china');
This table can be partitioned by range in various of ways, depending on your requirement. Here
we have used the age column and adding a PARTITION BY RANGE clause. In these partitions
the range of the people table age column (age) are as of follow :
This employee table can be partitioned by range in various of ways, depending on your
requirement. Here we have used the age column and adding a PARTITION BY RANGE clause
with max value. In these partitions the range of the employee age Column (age) with MAX
Value are as of follow :
One more example with Insurance table example on date with year , on renewal column created
with Range Partition.
CREATE TABLE Insurance (
first_name VARCHAR(25),
last_name VARCHAR(25),
street VARCHAR(30),
city VARCHAR(15),
renewal DATE
)
PARTITION BY RANGE COLUMNS(renewal) (
PARTITION pWeek_1 VALUES LESS THAN('2010-02-09'),
PARTITION pWeek_2 VALUES LESS THAN('2010-02-15'),
PARTITION pWeek_3 VALUES LESS THAN('2010-02-22'),
PARTITION pWeek_4 VALUES LESS THAN('2010-03-01')
);
LIST Partitioning
---Normal Table
CREATE TABLE Example (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
job_code INT,
store_id INT
);
One more example with cities, city column created with List Partition
CREATE TABLE customers (
name VARCHAR(25),
street VARCHAR(30),
city VARCHAR(15)
)
PARTITION BY LIST COLUMNS(city) (
PARTITION pRegion_1 VALUES IN('Delhi', 'Agra', 'Noida'),
PARTITION pRegion_2 VALUES IN('Mumbai', 'Pune', 'Gandhinagar'),
PARTITION pRegion_3 VALUES IN('Hyderabad', 'Visakhapatnam', 'Tirupati'),
PARTITION pRegion_4 VALUES IN('Chennai', 'Bangalore', 'Cochin')
);
HASH Partitioning
HASH partitions evenly distribute data across a predetermined number of partition tables. HASH
partitioning uses the modulus operator (%) to distribute the data across these partitions.
Unlike RANGE and LIST partitioning, partition names and matching criteria for the partition
key are not user-defined. To create a table for employees partitioned into 10 partitions.
KEY Partitioning
MySQL KEY partition is a special form of HASH partition, where the hashing function for key
partitioning is supplied by the MySQL server. The server employs its own internal hashing
function which is based on the same algorithm as PASSWORD(). This is done by using
PARTITION BY KEY, adding in CREATE TABLE STATEMENT. In KEY partitioning KEY
takes only a list of zero or more column names. Any columns used as the partitioning key must
comprise part or all of the table's primary key if the table has one. If there is a primary key in a
table, it is used as partitioning key when no column is specified as the partitioning key. Here is
an example :
ALTER TABLE members ADD PARTITION (PARTITION p3 VALUES LESS THAN (2010));
ALTER TABLE members ADD PARTITION (PARTITION p4 VALUES LESS THAN (2020));
ALTER TABLE members ADD PARTITION (PARTITION p5 VALUES LESS THAN (1970));
ERROR 1493 (HY000): VALUES LESS THAN value must be strictly increasing for
each partition
ALTER TABLE members REORGANIZE PARTITION split1 INTO ( PARTITION splitA VALUES
LESS THAN (1960),PARTITION splitB VALUES LESS THAN (1970) );
ALTER TABLE members REORGANIZE PARTITION split0 INTO ( PARTITION s01 VALUES
LESS THAN (1960),PARTITION s02 VALUES LESS THAN (1970) );
MySQL Sub-Partitioning
If you feel some data are useless in a partitioned table you can drop one or more partition(s). To
delete all rows from partition p0 of sale_mast, you can use the following statement :
Rebuild partitions:
mysql> ALTER TABLE employee REBUILD PARTITION p0, p1;
Rebuild the partitions, can be a method to remove fragmentation as an example.
reclaim any unused space and to defragment the partition data file
mysql> ALTER TABLE employee OPTIMIZE PARTITION p0,p1;
You can check partitions for errors in much the same way that you can use CHECK TABLE
with nonpartitioned tables.
mysql> ALTER TABLE employee CHECK PARTITION p0;
Range partitioning
We will be creating some partitions on the MySQL table to understand how to create the partitions.
In below query the table employee will be partitioned into 10 even size hash partitions
The above query will create 7 even size partition of the table employee on the basis of id.
ALTER table employee PARTITION BY KEY(id) PARTITIONS 7;
SYSTEM VIEWS
SELECT TABLE_SCHEMA,TABLE_NAME,PARTITION_NAME,TABLE_ROWS FROM
INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME='employee';