MySQL Partition Table partition: Modify the partition field online. Learn more about partition (1)

Source: Internet
Author: User
Tags decimal to binary failover natural logarithm

The company is using partition online. The partition field of a table is incorrect and needs to be rebuilt. It turns out that there is no way to directly fix it using an SQL statement like modifying the primary key field or index field. Instead, you need to create a temporary table with a down time. So I carefully read the document and studied the details of partition.

When your company is online, the business is off-peak at a.m.. Execute:

Create temporary table

Create Table tbname_tmp (
Shard_id int not null,
...

Xxx_date datetime not null,
Primary Key (xxx_date, shard_id)
) Engine = InnoDB default charset = utf8 collate = utf8_bin
Partition by list (month (xxx_date ))(
Partition M1 values in (1 ),
Partition m2 values in (2 ),
Partition m3 values in (3 ),
Partition M4 values in (4 ),
Partition M5 values in (5 ),
Partition M6 values in (6 ),
Partition M7 values in (7 ),
Partition M8 values in (8 ),
Partition M9 values in (9 ),
Partition M10 values in (10 ),
Partition M11 values in (11 ),
Partition M12 values in (12)
);

Switch the table name and modify the table structure.

Rename table xxx to xxx_deleted, xxx_tmp to XXX;

Import raw data

Insert into XXX select * From xxx_deletedxxx_deleted;

OK, everything is done. The entire process is 50 minutes. After the mmm failover is switched, the outline operation table structure changes and data import. The actual downtime does not include the time for modifying the table structure partition field, only the Failover switching time is 30 seconds.

MySQL partition: the official English documents. The translation level is limited. If some documents are not translated into Chinese, they are directly posted in English.
1 List Partition Table
Mysql> Create Table 'eh '(
-> 'Id' int (11) not null,
-> 'Entitlement _ hist_id 'bigint (20) not null,
-> 'Entitlement _ id' bigint (20) not null,
-> 'User _ id' bigint (20) not null,
-> 'Date _ created 'datetime not null,
-> 'Status' smallint (6) Not null,
-> 'Created _ by 'varchar (32) Collate utf8_bin default null,
-> 'Modified _ by 'varchar (32) Collate utf8_bin default null,
-> 'Date _ modified' datetime not null,
-> Primary Key ('date _ modified', 'id ')
->) Engine = InnoDB default charset = utf8 collate = utf8_bin
-> /*! 50100 partition by list (month (date_modified ))
-> (Partition M1 values in (1) engine = InnoDB,
-> Partition m2 values in (2) engine = InnoDB,
-> Partition m3 values in (3) engine = InnoDB,
-> Partition M4 values in (4) engine = InnoDB,
-> Partition M5 values in (5) engine = InnoDB,
-> Partition M6 values in (6) engine = InnoDB,
-> Partition M7 values in (7) engine = InnoDB,
-> Partition M8 values in (8) engine = InnoDB,
-> Partition M9 values in (9) engine = InnoDB,
-> Partition M10 values in (10) engine = InnoDB,
-> Partition M11 values in (11) engine = InnoDB,
-> Partition M12 values in (12) engine = InnoDB )*/;
Query OK, 0 rows affected (0.10 Sec)

2 rang Partition Table
Mysql> Create Table rcX (
-> A int,
-> B INT,
-> C char (3 ),
-> D int
->)
-> Partition by range columns (A, D, C )(
-> Partition P0 values less than (5, 10, 'ggg '),
-> Partition P1 values less than (10, 20, 'mmmm '),
-> Partition P2 values less than (15,30, 'ss '),
-> Partition P3 values less than (maxvalue, maxvalue, maxvalue)
-> );
Query OK, 0 rows affected (0.15 Sec)

 

3 create range use less character
Create Table employees_by_lname (
Id int not null,
Fname varchar (30 ),
Lname varchar (30 ),
Hired date not null default '2017-01-01 ',
Separated date not null default '2017-12-31 ',
Job_code int not null,
Store_id int not null
)
Partition by range columns (lname )(
Partition P0 values less than ('G '),
Partition P1 values less than ('M '),
Partition P2 values less than ('T '),
Partition P3 values less than (maxvalue)
);

Alter table structure, add a new Partition Block
Alter table employees_by_lname partition by range columns (lname )(
Partition P0 values less than ('G '),
Partition P1 values less than ('M '),
Partition P2 values less than ('T '),
Partition P3 values less than ('U '),
Partition P4 values less than (maxvalue)
);

4 List columns partitioning
Character Column
Create Table customers_1 (
First_name varchar (25 ),
Last_name varchar (25 ),
Street_1 varchar (30 ),
Street_2 varchar (30 ),
City varchar (15 ),
Renewal date
)
Partition by list columns (City )(
Partition pregion_1 values in ('osskarshamn', 'H? Gsby ',' m? Nster? S '),
Partition pregion_2 values in ('vimmerby', 'hultsfred', 'v? Stervik '),
Partition pregion_3 values in ('n? Ssj? ', 'Eksj? ', 'Vetlanda '),
Partition pregion_4 values in ('uppvidinge', 'alivesta ', 'v? Xjo ')
);

Date Column
Create Table customers_2 (
First_name varchar (25 ),
Last_name varchar (25 ),
Street_1 varchar (30 ),
Street_2 varchar (30 ),
City varchar (15 ),
Renewal date
)
Partition by list columns (Renewal )(
Partition pweek_1 values in ('2017-02-01 ', '2017-02-02', '2017-02-03 ',
'2017-02-04 ', '2017-02-05', '2017-02-06 ', '2017-02-07 '),
Partition pweek_2 values in ('2017-02-08 ', '2017-02-09', '2017-02-10 ',
'2017-02-11 ', '2017-02-12', '2017-02-13 ', '2017-02-14 '),
Partition pweek_3 values in ('2017-02-15 ', '2017-02-16', '2017-02-17 ',
'2017-02-18 ', '2017-02-19', '2017-02-20 ', '2017-02-21 '),
Partition pweek_4 values in ('2017-02-22 ', '2017-02-23', '2017-02-24 ',
'2017-02-25 ', '2017-02-26', '2017-02-27 ', '2017-02-28 ')
);

5 hash partitioning
Int column, it can use digital Function
Create Table employeesint (
Id int not null,
Fname varchar (30 ),
Lname varchar (30 ),
Hired date not null default '2017-01-01 ',
Separated date not null default '2017-12-31 ',
Job_code int,
Store_id int
)
Partition by hash (mod (store_id, 4 ))
Partitions 4;

If you do not include a partitions clause, the number of partitions defaults to 1. as below:
Create Table employeestest (
Id int not null,
Fname varchar (30 ),
Lname varchar (30 ),
Hired date not null default '2017-01-01 ',
Separated date not null default '2017-12-31 ',
Job_code int,
Store_id int
)
Partition by hash (store_id );

Date Colum
Create Table employees2 (
Id int not null,
Fname varchar (30 ),
Lname varchar (30 ),
Hired date not null default '2017-01-01 ',
Separated date not null default '2017-12-31 ',
Job_code int,
Store_id int
)
Partition by hash (Year (hired ))
Partitions 4;

Truncate all data rows: alter table rcX truncate partition;

 

6 linear hash partitioning
Create Table employees_linear (
Id int not null,
Fname varchar (30 ),
Lname varchar (30 ),
Hired date not null default '2017-01-01 ',
Separated date not null default '2017-12-31 ',
Job_code int,
Store_id int
)
Partition by linear Hash (Year (hired ))
Partitions 4;

Given an expression expr, the partition in which the record is stored when linear hashing is used is Partition Number N from among num partitions, where n is derived according to the following algorithm:
(1) Find the next power of 2 greater than num. We call this value V; it can be calculated:
V = power (2, ceiling (log (2, num )))
(Suppose that num is 13. Then log (2, 13) is 3.7004397181411. Ceiling (3.7004397181411) is 4, and V = power (2, 4), which is 16 .)
(2) set n = f (column_list) & (V-1 ).
(3) While n> = num:
Set v = Ceil (V/2)
Set n = N & (V-1)

[Note] & the computing principles in SQL are as follows:
Convert decimal to binary.

Http://zh.wikipedia.org/wiki/%E4%BA%8C%E8%BF%9B%E5%88%B6

First, right-aligned. For example, to 0011 and 1000, it is determined by the number of each digit. If both are 1, the corresponding position of the result is 1; otherwise, it is 0.
If it is 1011 and 1000, the result is 1000.
If it is 0110 and 1010, the result is 0010.
But 3 is 1000, and 8 is, so the result of 3 & 8 is 0.

Ceiling (x) Ceil (x): returns the smallest integer value not less than X.
Log (x) log (B, X): If a parameter is called, this function returns the natural logarithm of X.
Power (x, y): returns the result value of the Y multiplication of X.

Calculation Method of Data Distribution in which area:
Suppose that the table T1, using linear hash partitioning and having 6 partitions, is created using this statement:

Create Table T1 (col1 int, col2 char (5), col3 date)
Partition by linear Hash (Year (col3 ))
Partitions 6;

Now assume that you want to insert two records into T1 having the col3 column values '2017-04-14 'and '2017-10-19 '. the partition number for the first of these is determined as follows:
V = power (2, ceiling (log (2, 6) = 8
N = year ('1970-04-14 ') & (8-1)
= 2003 & 7
= 3
(3> = 6 is false: Record stored in partition #3)

The number of the partition where the second record is stored is calculated as shown here:
V = 8
N = year ('1970-10-19 ') & (8-1)
= 1998 & 7
= 6

(6> = 6 is true: additional step required)

N = 6 & ceiling (8/2)
= 6 & 3
= 2

(2> = 6 is false: Record stored in partition #2)

The advantage in partitioning by linear hash is that the adding, dropping, merging, and splitting of partitions is made much faster, which can be beneficial when dealing with tables containing into large amounts (Batch) of data. the disadvantage is
That data is less likely to be evenly distributed between partitions as compared with the distribution obtained using regular hash partitioning.

Question 1: How does MySQL use an SQL statement and does not need a temporary table to delete partition fields? Change a partitioned table to a normal table?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.