Upload
ram-n-sangwan
View
45
Download
2
Embed Size (px)
Citation preview
MySQL DBA Training Session 22. Partitioned Tables in MySQLRAM N SANGWAN
WWW.RNSANGWAN.COM
YOUTUBE CHANNEL HTTP://YOUTUBE.COM/USER/THESKILLPEDIA
WANT TO LEARN OR TEACH JOIN WWW.THESKILLPEDIA.COM
WWW.RNSANGWAN.COM 1
Who am I?
• Corporate Trainer
• More than 25 Years of Experience.
• More than 50 Technologies
• Managing Alliance Softech Pvt Ltd as Managing Director
• Running More than 300 Live Websites
• Major Technologies
◦ RDBMS : MySQL, Sybase (Now SAP ASE), DB2, Oracle, SQL Server, SAP HANA
◦ Linux : Virtualization, High Availability, Deployment
◦ PHP, Perl, Python
◦ Storage Technologies
◦ Many More…
WWW.RNSANGWAN.COM 2
Agenda for the Session
• MySQL Partitioning concepts
• Horizontal partitioning
• Vertical partitioning
• Choosing a Partitioning Method
• MySQL Partitioning Range partition
• MySQL Partitioning List Partition
• MySQL Partitioning Hash Partition
• MySQL Partitioning - Key Partition
• Partition Management Commands
WWW.RNSANGWAN.COM 3
MySQL Partitioning concepts
• Partitioning is dividing up data in the database into distinct independent elements.
• Partitioning serves three main purposes
◦ data manageability
◦ performance
◦ availability
• Two ways to partition database tables
◦ Horizontal partitioning
◦ Vertical partitioning
WWW.RNSANGWAN.COM 4
Advantages
• It allows you to split a table across multiple files, through the usage of a special partitioningrule called partitioning function.
• Partitioning can only take place in engines that support partitioning, engines like MyISAM orInnoDB and not like CSV or BlackHole.
• The big performance boost in database partition is that partitions that do not satisfy a certainrule are not scanned, this is called partition pruning.
WWW.RNSANGWAN.COM 5
Horizontal partitioning
• Different rows are stored in different tables:
◦ customers with ID's less than 100,000 are stored in customers1 table and customers with ID's greaterare stored in customers2 table, etc.
• Table schemas are exactly the same (i.e.customer1 and customer2 tables have the sameschema), as if the table was cut into parts using a horizontal line.
• Archiving older data is commonly used example of horizontal partitioning, another techniqueusing horizontal partitioning is sharding which involves using separate servers to host similartypes of data:
◦ customer1 table would be held on server1and customer2 table would be held on server2.
• Horizontal partitioning is achieved manually using merge tables, as merge tables do notautomatically assign data to different underlying tables.
• Internal partitioning assigns records to a partition based on certain criteria.
WWW.RNSANGWAN.COM 6
Vertical partitioning
• Different fields are stored in different tables:
◦ Customer names and email addresses might be stored in Customers and Addresses tables. Oftennormalization involves vertical partitioning, which means splitting up otherwise normalized data.
• In vertical partitioning the tables are split up vertically which means that the schemas will bedifferent but they contain similar records.
• This type of partitioning is also know as row splitting.
• Different physical storage could be used as well, you could even store pictures and largedocuments on the file system instead of in the database.
• Another option is to split up data in dynamic and static data tables, a static data table couldthen use query cache to improve performance.
WWW.RNSANGWAN.COM 7
Choosing a Partitioning Method
• Range Partitioning• Data usually accessed by date
• Limited number of (primary) partitions needed
• Ordered Intelligent keys
• Supports Sub Partitions
• List Partitioning• Grouping data in partitions out of order (1,5,7 in partition x)
• Limited number of (primary) partitions needed
• Intelligent keys
• Supports Sub Partitions
WWW.RNSANGWAN.COM 8
Choosing a Partitioning Method Contd..
• Hash Partitioning• Low maintenance
• Works with limited or large number of partitions
• Non-intelligent keys (can work with some cases of intelligent keys)
• Key Partitioning• Non-integer based partitioned keys (md5 hash)
• Low maintenance
WWW.RNSANGWAN.COM 9
MySQL Range partition Example
CREATE TABLE t1 (
r_name VARCHAR(50) NOT NULL,
region_code TINYINT UNSIGNED NOT NULL
)
PARTITION BY RANGE( region_code ) (
PARTITION p0 VALUES LESS THAN (64),
PARTITION p1 VALUES LESS THAN (128),
PARTITION p3 VALUES LESS THAN MAXVALUE
);
WWW.RNSANGWAN.COM 10
MySQL List Partition Example
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT )
PARTITION BY LIST(store_id) (
PARTITION pNorth VALUES IN (16,15,3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,8,10,11,19,20),
PARTITION pWest VALUES IN (7, 4,12,13,14,18) );
WWW.RNSANGWAN.COM 11
MySQL Partitioning Hash Partition:
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT )
PARTITION BY HASH(store_id)
PARTITIONS 5;
WWW.RNSANGWAN.COM 12
MySQL Key Partition Example
CREATE TABLE k1 (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20))
PARTITION BY KEY()
PARTITIONS 4;
CREATE TABLE k1 (
id INT NOT NULL,
name VARCHAR(20),
UNIQUE KEY (id) )
PARTITION BY KEY()
PARTITIONS 4;
WWW.RNSANGWAN.COM 13
Partition Management Commands
• Add partitionalter table employees_by_region add partition (partition england values in (1,2),partition scotland values in (3,4)
);
• drop partitionalter table employees_by_region drop partition scotland;
• coalesce partitionalter table employees_hash coalesce partition 4;
WWW.RNSANGWAN.COM 14
Partition Management Commands Contd..
• Reorganize partitionalter table employees repair partition p0, p1;
• Analyze partitionalter table employees analyze partition 3;
• Check partitionalter table employees check partition 3;
• Optimize partitionalter table employees optimize partition p0, p1;
• Rebuild partitionalter table employees rebuild partition p0, p3;
WWW.RNSANGWAN.COM 15
Thank You
WWW.RNSANGWAN.COM 16