Database Partitioning With MySQL
Database Partitioning With MySQL
#157
Get More Refcardz! Visit refcardz.com
CONTENTS INCLUDE:
WHAT IS DATABASE PARTITIONING? Since the variable have_partitioning has a value of YES, it indicates
MySQL server supports partitioning.
Partitioning is a database design technique with details as follows Similarly, you can also verify the partitioning support by issuing the SHOW
PLUGINS statement at the mysql prompt, as shown below.
Major Abilities of Partitioning
mysql> SHOW PLUGINS;
1. Splits large database into smaller and more manageable ones. +------------+----------+----------------+---------+---------+
2. Simplifies the overall data management and operations. | Name | Status | Type | Library | License |
+------------+----------+----------------+---------+---------+
3. Enhances the overall performance of the database.
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
4. Assists the MySQL Optimizer. | partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ndbcluster | DISABLED | STORAGE ENGINE | NULL | GPL |
Database Partitioning with MySQL
+------------+----------+----------------+---------+---------+
11 rows in set (0.00 sec)
Technique Description
Horizontal Partitions a big table based on the number of rows.
Vertical Partitions a table based on the different columns
If the status is being displayed as ACTIVE against the partition The following example illustrates the range-partitioning
plugin, it indicates the partitioning feature is enabled and technique. First, we create an employees table, as shown below:
available.
CREATE TABLE employees (
id INT NOT NULL,
To disable the partitioning support option, start the MySQL server
Hot with the –skip – partition option. The value of have_partitioning is now fname VARCHAR(30),
Tip DISABLED. lname VARCHAR(30),
hired DATE NOT NULL DEFAULT ‘1970-01-01’,
separated DATE NOT NULL DEFAULT ‘9999-12-31’,
job_code INT NOT NULL,
MYSQL DATABASE ENGINE – A PREREQUISITE FOR store_id INT NOT NULL
PARTITIONING );
Features
If there is no primary key available with the table but there is a unique
3. Each partition should be explicitly defined. Hot key, then the unique key can be used as a partitioning key, as shown
4. Partitions do not need to be declared in any order.
Tip below.
Managing Hash and Key Partitions – a Quick Glance Now, consider the case where you wish to obtain results from a
query such as:
1. When it comes to making changes to partitioning setup, it is
similar with hash and key partitions. SELECT fname, lname, region_code, dob FROM t1 WHERE
2. You cannot drop hash or key partitions. region_code > 125 AND region_code < 130;
3. You can merge hash or key partitions by using an ALTER It is easy to see that none of the rows which ought to be returned
TABLE statement with the coalesce partition clause, as shown will be in either of the partitions p0 or p3; that is, we need to
below: ALTER TABLE table_name COALESCE PARTITION 4; search only in partitions p1 and p2 to find matching rows. By
4. The number following the coalesce partition clause refers to doing so, we can save time and effort and find matching rows
the number of partitions to remove from the table instead of having to scan all partitions in the table. This “cutting
away” of unneeded partitions is known as “pruning.”
5. Attempting to remove more partitions than the table has will
lead to an error. The query optimizer can perform pruning whenever a WHERE
condition can be reduced to either of the following two cases:
• partition_column = constant
PARTITIONS MAINTENANCE
• partition_column IN (constant1, constant2, constant3,....,
constantN)
You may also carry out a number of table and associated partition
maintenance tasks by issuing a few SQL statements using the In the first case, the optimizer simply evaluates the partitioning
following options: expression for the value given. Then it determines and scans only
the partition containing that value. In many cases, the equal sign
SQL Options Description Equivalent SQL can be replaced with other arithmetic comparison operators,
Statement including <, >, <= , >= , and <>.
REBUILDING Used for rebuild ing ALTER TABLE table_
PARTITIONS a partition and for name REBUILD
Queries using BETWEEN in the WHERE clause can also take advantage
defragmentation. PARTITION p0, p1; Hot of partition pruning.
OPTIMIZING Used to reclaim any ALTER TABLE Tip
PARTITIONS unused space and table_name
defragment partition OPTIMIZE
data files. PARTITION p0, p1; In the second case, the optimizer evaluates the partitioning
ANALYZING Used to store key ALTER TABLE expression for each value in the list, creates a list of matching
PARTITIONS distributions about table_name partitions, and then scans only the partitions in that list. Further
partitions. ANALYZE partition pruning can be applied to short ranges, which the optimizer can
p3; convert into equivalent lists of values.
IMPLEMENTING PARTITION PRUNING TECHNIQUE Pruning can be used only on integer columns of tables partitioned
Hot by HASH or KEY. But, for a table that is partitioned by KEY, has a
composite primary key, and uses a composite partitioning key, it is
Partition pruning can be implemented for all partition types. Tip possible to perform pruning for queries meeting the following two
Though pruning for RANGE partitioning is already explained criteria:
in the above section, this section illustrates how this technique
can be applied to other types of partitions as well. For example, • The query must have a WHERE clause of the form
consider a table t3 that is partitioned by LIST, as shown below: pkcol1 = c1 AND pkcol2 = c2 AND ... pkcolN = cN,
where pkcol1..... pkcolN are the partitioning key
CREATE TABLE t3
columns and c1.....cN are constant values.
(
fname VARCHAR(50) NOT NULL, • All columns of the partitioning key must be
lname VARCHAR(50) NOT NULL, referenced in the WHERE clause.
region_code TINYINT UNSIGNED NOT NULL,
dob DATE NOT NULL
) ABOUT ‘EXPLAIN PARTITION’ STATEMENT
PARTITION BY LIST(region_code)
(
PARTITION r0 VALUES IN (1, 3),
1. Generally, EXPLAIN statement is used to obtain information
PARTITION r1 VALUES IN (2, 5, 8),
about how MySQL executes a statement.
PARTITION r2 VALUES IN (4, 9),
PARTITION r3 VALUES IN (6, 7, 10) 2. EXPLAIN PARTITION is highly helpful for examining queries
); involving partitions.
Table t3 shows that the region_code column is limited to values 3. A SELECT statement that is preceded by an EXPLAIN
between 1 and 10 (inclusive). So, when a select query is issued, statement displays information from the Optimizer about the
such as the one below— query execution plan.
SELECT * FROM t3 WHERE region_code BETWEEN 1 AND 3; 4. For Example: EXPLAIN PARTITIONS SELECT select_options
Importantly, pruning can also be employed for short ranges, 7. Different SQL modes on master and slave can cause different
because the optimizer can turn such conditions into IN relations. distributions of data among partitions.
For example, consider the next two queries with reference to the 8. Partitioned tables do not support FULLTEXT indexes or
previously defined table t4: searches. This includes MyISAM storage engine.
SELECT * FROM t4 WHERE region_code > 2 AND region_code < 6; 9. Columns with spatial data types, such as POINT or
GEOMETRY, cannot be used in partitioned tables.
10. Temporary tables cannot be partitioned.
SELECT * FROM t4 WHERE region_code BETWEEN 3 AND 5;
11. A partitioning key must be either an integer column or
an expression that resolves to an integer. The column or
In both of these cases, the WHERE clause is transformed by the expression value may also be NULL.
optimizer into WHERE region_code IN (3, 4, 5). This optimization 12. Partitioned tables do not support foreign keys.
is used only if the range size is smaller than the number of
partitions.
Narayana Maruvada is a computer science and This book provides a solid framework for both database
engineering graduate, currently working as novices and experienced DBA’s transitioning to MySQL.
a QA engineer at Benefitfocus, the country’s It provides essential coverage of the fundamentals of
leading provider of benefits technology. He is MySQL database management, including MySQL’s
part of a top notch technology organization that unique approach to basic database features and
focuses on rapid, high quality, and superiorly functions as well as coverage of SQL queries, data
architected solutions. Narayana has over 6 years and index types, stored-procedure, functions, triggers,
of experience developing and testing web-based applications. views, transactions, etc. It also offers comprehensive
In particular, Narayana has worked extensively with MySQL and coverage of advanced topics such as MySQL server tuning, managing
Oracle databases, tuning queries and configurations for maximum storage engines, caching, backup and recovery, managing users, index
performance and reliability. tuning, database and performance monitoring, security, and more.
#82
CONTENTS INCLUDE:
■
■
About Cloud Computing
Usage Scenarios Getting Started with
Aldon Cloud#64Computing
■
Underlying Concepts
Cost
by...
■
Upcoming Refcardz
youTechnologies ®
■
Data
t toTier
brough Comply.
borate.
Platform Management and more...
■
Chan
ge. Colla By Daniel Rubio
on:
dz. com
grati s
also minimizes the need to make design changes to support
CON
ABOUT CLOUD COMPUTING one time events. TEN TS
INC
s Inte -Patternvall
■
HTML LUD E:
Basics
Automated growthHTM
ref car
ContiPatterns an
■
However, the demands and technology used on such servers faced by web applications.Structur Tools
Core
Key ■
Structur e Elements
E: has changed substantially in recent years, especially with al Elem
INC LUD gration the entrance of service providers like Amazon, Google and Large scale growth scenarios involvingents
specialized
NTS and mor equipment
rdz !
HTML
CO NTE
HBase
Microsoft. es e... away by
(e.g. load balancers and clusters) are all but abstracted
Continu at Every e chang
About ns to isolat
relying on a cloud computing platform’s technology.
Software i-patter
■
n space
Re fca
e Work
Build
riptio
and Ant These companies Desc have a Privat
are in long deployed trol repos
itory
webmana applications
ge HTM
L BAS
■
Build
re
Opa
a system
Build as thescalethe By An
sitory of work prog foundati
Ge t Mo
Repo
This Refcard active
will introduce are within
to you to cloud riente computing, with an
ION d units etc. Some platforms ram support large grapRDBMS deployments.
■
The src
dy Ha
softw
EGRAT e ine loping task-o it and Java s written in hical on of
all attribute
softwar emphasis onDeve es by
Mainl
OUS
INT these ines providers, chang so youComm can better understand
also rece JavaScri user interfac web develop and the desc rris
Vis i
codel
ding e code Task Level as the
www.dzone.com
Data Warehousing
ous com to a USAGE SCENARIOS ate
Autom configurat
cies t
ely-defi thei tech L can is disp
ization, cannot be (and freq
Continu ry change
nden ymen layed
tion ned lang r visual eng nologies
Re fca
CI can ticular con used tter can rity all depe that all
targe
simp s will L t. HTM l, but r. Tags
es i-pa tend but to most phone services:
y Integ alize plans with late alloted resources,
file
ts with an and XHT lify all help or XHTML, Reg ardless L VS XH <a><
in a par hes som
etim s. Ant , they ctices,
Binar Centr temp your you prov TML b></
proces in the end bad pra enting
nt nmen
incurred cost
geme whether e a single based on
Creatsuchareresources were consumed
t enviro orthenot. Virtualization
muchallows MLaare physical othe
piece ofr hardware to be understand of HTML
approac ed with the cial, but, rily implem nden
cy Mana rties nt targe es to of the actually web cod ide a solid ing has bee
essa d to Depe prope into differe chang
utilized by multiple operating
function systems.simplerThis allows ing.resources foun job
efi nec itting n arou
associat to be ben
er
pare dation adm
Ge t
te builds commo
are not Fortuna
late Verifi e comm than
ality be allocated nd for
n com Cloud computing asRun it’sremo
known etoday has changed this. expecte irably, that
befor
They they
etc.
(e.g. bandwidth, elem CPU) tohas
nmemory, exclusively totely
Temp
job has some time
Build
lts whe
ually,
appear effects. rm a
Privat y, contin nt team Every mov
entsinstances. ed to CSS
used
to be, HTML d. Earl
ed resu gration
The various resourcesPerfo consumed by webperio applications
dicall (e.g.
opme page system
individual operating bec Brow y exp . Whi
adverse unintend d Builds sitory Build r to devel common (HTM . ause ser HTM anded le it
ous Inte web dev manufacture L had very far mor has done
Stage Repo
e bandwidth, memory, CPU) areIntegtallied
ration on a per-unit CI serve basis L or XHT
produc Continu Refcard
e Build rm an ack from extensio .) All are
e.com
the not
DZone, Inc.
s on
expand
tutorials, cheat sheets, blogs, feature articles, source code and more.
refcardz@dzone.com
“DZone is a developer’s dream,” says PC Magazine.
Sponsorship Opportunities
Copyright © 2012 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a
retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, sales@dzone.com Version 1.0
without prior written permission of the publisher.