MySQL Partitioning
MySQL Partitioning
MySQL Partitioning
1
and onwards
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 1
Why Partitioning?
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 2
Partitioned Tables
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 3
Partition Management
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 4
MySQL Partitioning Types
• Range Partitioning
• List Partitioning
• Hash Partitioning
• Composite Partitioning
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 5
Storage Engines
• Partition applies to all storage engines
– MyISAM
– InnoDB
– Archive
– NDB Cluster
– Falcon
• Exceptions
– Merge
• Don’t mix storage engines in one table for now (5.1 limitation)
– Example:
– Archive Engine for really old data (> 10 years)
– MyISAM for medium old data (> 1 year)
– InnoDB for current data
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 6
Key Partitioning
• Definition
– Same as Hash partitioning except that MySQL
decides the hash function using the given fields
• Benefits
– Very good hash function
– Tight integration with MySQL Cluster partitioning
• Drawbacks
– Same as for Hash partitioning
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 7
LINEAR HASH/KEY partitioning
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 8
Partition Function
• PARTITION BY RANGE(f(f1,f2,f3))
• Partition function must always deliver integer result
(5.1 limitation)
• Partition function can contain any number of fields, but
must contain at least one field
• If primary key is defined, no fields outside of primary
key is allowed in partition function
• Partition function can contain a large variety of
character functions, date functions and mathematical
functions
• If unique key is defined on a table, no fields outside of
unique key is allowed in partition function (this
limitation does not exist for tables in MySQL Cluster)
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 9
Partition Options
MAX_ROWS
MIN_ROWS
NODEGROUP (only MySQL Cluster)
DATA DIRECTORY (only MyISAM)
INDEX DIRECTORY (only MyISAM)
COMMENT
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 10
INFORMATION SCHEMA for
Partitioning
• Provides information of:
– Partition names
– Where partitions are stored
– Information about partition types
– Information about partition functions
– Number of rows per partition
– Average row length in partitions
– Other attributes of partitions (timestamps, …)
• Support SHOW CREATE TABLE
• Support SHOW TABLE STATUS
– Will be displayed as Create option PARTITIONED
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 11
EXPLAIN for queries using
Partitioned Tables
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 12
Partition Management
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 14
Add RANGE/LIST Partition
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 15
ADD HASH/KEY partition(s)
ALTER TABLE t1 ADD PARTITION (PARTITION p4);
Old Partitions
p1 p2 p3
p1 p2 p3 p4
New Partitions
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 16
ADD LINEAR HASH/KEY
partition(s)
ALTER TABLE t1 ADD PARTITION (PARTITION p4);
Old Partitions
p1 p2 p3
p2 p4 New Partitions
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 17
COALESCE HASH/KEY partition(s)
ALTER TABLE t1 COALESCE PARTITION 1;
Old Partitions
p1 p2 p3 p4
p1 p2 p3 New Partitions
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 18
COALESCE LINEAR HASH/KEY
partition(s)
Old Partitions
p1 p2 p3 p4
p2 New Partitions
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 19
Using REORGANIZE partition to
SPLIT a partition
ALTER TABLE t1 REORGANIZE PARTITION 2006_2007 INTO
(PARTITION 2006 VALUES LESS THAN (2007),
PARTITION 2007 VALUES LESS THAN (2008));
2006 2007
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 20
Using REORGANIZE partition to
MERGE partitions
ALTER TABLE t1 REORGANIZE PARTITION 2006,2007 INTO
(PARTITION 2006_2007 VALUES LESS THAN (2008));
2006_2007
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 21
Using REORGANIZE partition to
balance partitions
ALTER TABLE t1 REORGANIZE PARTITION 2006_Q1,2006_Q2 INTO
(PARTITION 2006_M1_4 VALUES LESS THAN (DATE(’2006-05-01’),
PARTITION 2006_M_5_8 VALUES LESS THAN (DATE(’2006-09-01’));
2006_M_1_4 2006_M_5_8
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 22
Using REORGANIZE partition to move
partitions to new disk device
ALTER TABLE t1 REORGANIZE PARTITION 2006 INTO
(PARTITION 2006 DATA DIRECTORY ’/home/user/2006/data_file’
INDEX DIRECTORY ’/home/user/2006/index_file’ VALUES LESS THAN (2007));
2006
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 23
Using REBUILD partition to recreate
partition
ALTER TABLE t1 REBUILD PARTITION 2006;
2006
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 24
Partition Pruning
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 25
Partition Pruning
SELECT * FROM Cars Where PRICE > 9000
Results
Price
>9000
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 26
Partition Pruning
Select *FROM Cars Where PRICE >9000
and COLOR= Red
8000-TSU564-1988 7000-SAG293-2004 12000-FBI007-2004
1000-YUK333-1981
11000-YRG213-2005 21100-GRO293-1956
34000-SIE568-2004
13000-KAR365-2001 8500-KHO297-2004
Results
Price
>9000
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 27
Dynamic Partitioning Pruning
SELECT * FROM t1, t2 WHERE t2.a = t1.a;
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 28
Partitioning and NULL values
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 29
Partititioning Implementation
MyISAM/
InnoDB/
Federated/
…..
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 30
index_read Algorithm
Handler output
Merge Sort
Part
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 31
Insert in Partitioning Table
Insert
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 32
Updating Partition
Delete Insert
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 33
Partitioning for MySQL Cluster
• Default for MySQL Cluster:
– All tables in MySQL Cluster are partitioned
– Default for an ENGINE=NDB is PARTITION BY KEY()
• User Defined Partitioning for MySQL Cluster
– Supports same partitioning types as rest of MySQL (Beta using –new)
– PARTITION BY KEY is fully integrated with NDB kernel
– Partitions defined in MySQL mapped to partitions in NDB storage
engine, thus no extra overhead for many partitions
– Partitioning makes manual placement of partitions possible
• NOTE:
– No support for DROP PARTITION
– Full table copy is always employed for Partition Management
• Locks entire table (=> not on-line)
• More memory resources used
• More processing resources
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 34
Cluster: Partitioning Controls
Physical Distribution
• Accessing relevant partitions only (partition pruning) for a query
optimizes communication costs
• A table no longer has to be distributed on all nodes
• Altering the table partitioning can change the physical
distribution
– Altering table partitioning is currently done as a copying
ALTER TABLE
• Node groups can be populated on-line
– ALTER TABLE account
ADD PARTITION (PARTITION P2 NODEGROUP 2)
– With future support for adding nodes (node groups) on-
line, tables can be re-partitioned to populate the added
nodes
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 35
Cluster: Partition by Key
CREATE TABLE service(user int unsigned,
service varchar,
parameter
int)
PRIMARY KEY (user, service)
PARTITION BY KEY (user)
ENGINE=NDBCLUSTER;
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 36
Cluster: Default Tables
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 37
Backup and Restore for partitioned
tables in MySQL Cluster
SCENARIO DESCRIPTION:
• Backup taken in cluster with 4 node groups
• Restore performed in cluster with 2 node groups
• Assume table manually partitioned to be in node group 0
and 3
• What can be done for the partition in node group 3?
• For mysqldump and Cluster Replication the table must
be created by the user before applying the user
records/binlog
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 38
Nodegroup maps for ndb_restore
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 41
More information on Partitioning
• Documentation
– http://dev.mysql.com/doc/refman/5.1/en/partitioni
ng.html
• Blog
– http://mikaelronstrom.blogspot.com
• Forums
– http://forums.mysql.com/list.php?106
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 42
Thank You!
mikael@mysql.com
Copyright 2005 MySQL AB The World’s Most Popular Open Source Database 43