Mysql分区表的管理与维护

改变一个表的分区方案只需使用alter table 加 partition_options 子句就可以了。和创建分区表时的create table语句很像。

创建表

CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
PARTITION BY RANGE( YEAR(purchased) ) (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (2000),
PARTITION p3 VALUES LESS THAN (2005)
);

创建插入数据存储过程

delimiter $$
drop procedure if exists pr_trb3$$
create procedure pr_trb3(in begindate date,in enddate date,in tabname varchar(40))
begin
while begindate<enddate 1="" begindate="date_add(begindate,interval" delimiter="" do="" drop="" end="" execute="" from="" insert="" pre="" prepare="" s="concat_ws('" set="" stmt=""><p>调用存储过程插入数据</p><pre class="brush:sql;">call pr_trb3('1985-01-01','2004-12-31','trb3');</pre>
<p>查看数据分布</p>
<pre class="brush:sql;">select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.00 sec)</pre>
<p>改变分区方案</p>
<pre class="brush:sql;">mysql> ALTER TABLE trb3 PARTITION BY KEY(id) PARTITIONS 4;
Query OK, 7304 rows affected (0.07 sec)
Records: 7304 Duplicates: 0 Warnings: 0</pre>
<p>查看数据</p>
<pre class="brush:sql;">select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='trb3';
+------+------+-------+------------+
| part | expr | descr | table_rows |
+------+------+-------+------------+
| p0 | `id` | NULL | 7472 |
| p1 | `id` | NULL | 0 |
| p2 | `id` | NULL | 0 |
| p3 | `id` | NULL | 0 |
+------+------+-------+------------+
4 rows in set (0.00 sec)
mysql> select 1826*4;
+--------+
| 1826*4 |
+--------+
| 7304 |
+--------+
1 row in set (0.00 sec)</pre>
<p>count(*)行数一致,说明数据没出问题,但是information_schema.partitions查出来的不对<del>,这就不知道为什么了</del></p>
<blockquote>
<p>For partitioned InnoDB tables, the row count given in the TABLE_ROWS column of the INFORMATION_SCHEMA.PARTITIONS table is only an estimated value used in SQL optimization, and is not always exact.</p>
</blockquote>
<pre class="brush:sql;">mysql> select count(*) from trb3;
+----------+
| count(*) |
+----------+
| 7304 |
+----------+
但是count(*)还是7304,什么鬼</pre>
<p>再次改变分区方案</p>
<pre class="brush:sql;">ALTER TABLE trb3
PARTITION BY RANGE( YEAR(purchased) ) (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (2000),
PARTITION p3 VALUES LESS THAN (2005)
);
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 0 |
| p3 | YEAR(purchased) | 2005 | 0 |
+------+------------------+-------+------------+
4 rows in set (0.00 sec)</pre>
<p><del>丢数据了。。</del><br>
更正,实际没丢,这个information_shcema.partitions表有延迟,过一会再查就好了</p>
<pre class="brush:sql;">mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.00 sec)</pre>
<p>官方文档说:<br>
This has the same effect on the structure of the table as dropping the table and re-creating it using CREATE TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;<br>
就是说ALTER TABLE trb3 PARTITION BY与 drop table然后重新create table trb3 partition by key(id) partitions 2一样呢。</p>
<h3 id="改存储引擎和普通表没啥区别">改存储引擎,和普通表没啥区别</h3>
<pre class="brush:sql;">mysql> drop table trb3;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
-> PARTITION BY RANGE( YEAR(purchased) ) (
-> PARTITION p0 VALUES LESS THAN (1990),
-> PARTITION p1 VALUES LESS THAN (1995),
-> PARTITION p2 VALUES LESS THAN (2000),
-> PARTITION p3 VALUES LESS THAN (2005)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> call pr_trb3('1985-01-01','2004-12-31','trb3');
Query OK, 0 rows affected (1.69 sec)
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.01 sec)
mysql> alter table trb3 engine=myisam;
Query OK, 7304 rows affected (0.02 sec)
Records: 7304 Duplicates: 0 Warnings: 0
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.01 sec)
mysql> show create table trb3\G
*************************** 1. row ***************************
Table: trb3
Create Table: CREATE TABLE `trb3` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`purchased` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE ( YEAR(purchased))
(PARTITION p0 VALUES LESS THAN (1990) ENGINE = MyISAM,
PARTITION p1 VALUES LESS THAN (1995) ENGINE = MyISAM,
PARTITION p2 VALUES LESS THAN (2000) ENGINE = MyISAM,
PARTITION p3 VALUES LESS THAN (2005) ENGINE = MyISAM) */
1 row in set (0.00 sec)</pre>
<h3 id="将表由分区表改为非分区表">将表由分区表改为非分区表</h3>
<pre class="brush:sql;">mysql> alter table trb3 remove partitioning;
Query OK, 7304 rows affected (0.01 sec)
Records: 7304 Duplicates: 0 Warnings: 0
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------+-------+------------+
| part | expr | descr | table_rows |
+------+------+-------+------------+
| NULL | NULL | NULL | 7304 |
+------+------+-------+------------+
1 row in set (0.00 sec)
mysql> show create table trb3\G
*************************** 1. row ***************************
Table: trb3
Create Table: CREATE TABLE `trb3` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`purchased` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)</pre>
<h3 id="range-list分区管理">Range List分区管理</h3>
<pre class="brush:sql;">mysql> drop table trb3;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
-> PARTITION BY RANGE( YEAR(purchased) ) (
-> PARTITION p0 VALUES LESS THAN (1990),
-> PARTITION p1 VALUES LESS THAN (1995),
-> PARTITION p2 VALUES LESS THAN (2000),
-> PARTITION p3 VALUES LESS THAN (2005)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> call pr_trb3('1985-01-01','2004-12-31','trb3');
Query OK, 0 rows affected (1.75 sec)
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.00 sec)</pre>
<h4 id="增加分区">增加分区</h4>
<pre class="brush:sql;">mysql> alter table trb3 add partition (partition p5 values less than(2010));
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0</pre>
<h4 id="合并分区">合并分区</h4>
<pre class="brush:sql;">mysql> alter table trb3 reorganize partition p3,p5 into(partition p5 values less than(2010));
Query OK, 1826 rows affected (0.03 sec)
Records: 1826 Duplicates: 0 Warnings: 0
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p5 | YEAR(purchased) | 2010 | 1826 |
+------+------------------+-------+------------+
4 rows in set (0.00 sec)</pre>
<h4 id="分裂分区">分裂分区</h4>
<pre class="brush:sql;">mysql> ALTER TABLE trb3 REORGANIZE PARTITION p5 INTO (
-> PARTITION p3 VALUES LESS THAN (2005),
-> PARTITION p4 VALUES LESS THAN (2010)
-> );
Query OK, 1826 rows affected (0.04 sec)
Records: 1826 Duplicates: 0 Warnings: 0
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | 1990 | 1826 |
| p1 | YEAR(purchased) | 1995 | 1826 |
| p2 | YEAR(purchased) | 2000 | 1826 |
| p3 | YEAR(purchased) | 2005 | 1826 |
| p4 | YEAR(purchased) | 2010 | 0 |
+------+------------------+-------+------------+
5 rows in set (0.00 sec)</pre>
<h3 id="hash-key分区">HASH KEY分区</h3>
<pre class="brush:sql;">CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
PARTITION BY hash( YEAR(purchased) )
partitions 12;
mysql>call pr_trb3('1985-01-01','2004-12-31','trb3');
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | NULL | 731 |
| p1 | YEAR(purchased) | NULL | 365 |
| p2 | YEAR(purchased) | NULL | 365 |
| p3 | YEAR(purchased) | NULL | 365 |
| p4 | YEAR(purchased) | NULL | 366 |
| p5 | YEAR(purchased) | NULL | 730 |
| p6 | YEAR(purchased) | NULL | 730 |
| p7 | YEAR(purchased) | NULL | 730 |
| p8 | YEAR(purchased) | NULL | 732 |
| p9 | YEAR(purchased) | NULL | 730 |
| p10 | YEAR(purchased) | NULL | 730 |
| p11 | YEAR(purchased) | NULL | 730 |
+------+------------------+-------+------------+
12 rows in set (0.00 sec)</pre>
<h4 id="缩建分区从12个到8个">缩建分区从12个到8个</h4>
<pre class="brush:sql;">mysql> ALTER TABLE trb3 COALESCE PARTITION 4;
Query OK, 7304 rows affected (0.13 sec)
Records: 7304 Duplicates: 0 Warnings: 0
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='trb3';
+------+------------------+-------+------------+
| part | expr | descr | table_rows |
+------+------------------+-------+------------+
| p0 | YEAR(purchased) | NULL | 732 |
| p1 | YEAR(purchased) | NULL | 1095 |
| p2 | YEAR(purchased) | NULL | 1095 |
| p3 | YEAR(purchased) | NULL | 1095 |
| p4 | YEAR(purchased) | NULL | 1097 |
| p5 | YEAR(purchased) | NULL | 730 |
| p6 | YEAR(purchased) | NULL | 730 |
| p7 | YEAR(purchased) | NULL | 730 |
+------+------------------+-------+------------+
8 rows in set (0.00 sec)
mysql> select count(*) from trb3;
+----------+
| count(*) |
+----------+
| 7304 |
+----------+
1 row in set (0.00 sec)</pre>
<p>没丢数据</p>
<p>收缩前2004年在P0</p>
<pre class="brush:sql;">mysql> select mod(2004,12);
+--------------+
| mod(2004,12) |
+--------------+
| 0 |
+--------------+</pre>
<p>收缩后2004年在P4</p>
<pre class="brush:sql;">mysql> select mod(2004,8);
+-------------+
| mod(2004,8) |
+-------------+
| 4 |
+-------------+</pre>
<h3 id="exchanging-partitions-and-subpartitions-with-tables">Exchanging Partitions and Subpartitions with Tables</h3>
<h3 id="分区子分区交换">分区(子分区)交换</h3>
<pre class="brush:sql;"> ALTER TABLE pt EXCHANGE PARTITION p WITH TABLE nt</pre>
<p>pt是一个分区表,p是pt的分区或子分区,而nt是一个非分区表</p>
<h4 id="限制条件">限制条件:</h4>
<p>1.表nt不是分区表<br>
2.表nt不是临时表<br>
3.表pt和nt结构在其他方面是相同的<br>
4.表n没有外键约束,也没有其他表引用它的列为外键<br>
5.表nt的所有行都包含在表p的分区范围内(比如p range分区最大values less than 10,那么表nt不能有大于等于10的值)</p>
<h4 id="权限">权限:</h4>
<p>除了 ALTER, INSERT, and CREATE 权限外,你还要有DROP权限才能执行ALTER TABLE … EXCHANGE PARTITION.</p>
<h4 id="其他注意事项">其他注意事项:</h4>
<p>1.执行ALTER TABLE … EXCHANGE PARTITION 不会调用任何在nt表和p表上的触发器<br>
2.在交换表中的任何AUTO_INCREMENT列会被reset<br>
3.IGNORE关键字在执行ALTER TABLE … EXCHANGE PARTITION时会失效</p>
<h4 id="完整实例语句如下">完整实例语句如下:</h4>
<pre class="brush:sql;">ALTER TABLE pt
EXCHANGE PARTITION p
WITH TABLE nt;</pre>
<p>在一次ALTER TABLE EXCHANGE PARTITION 中,只能有一个分区和一个非分区表被交换<br>
想交换多个,就执行多次ALTER TABLE EXCHANGE PARTITION<br>
任何MySQL支持的分区类型都可以进行交换</p>
<h4 id="交换实例">交换实例</h4>
<pre class="brush:sql;">CREATE TABLE e (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30)
)
PARTITION BY RANGE (id) (
PARTITION p0 VALUES LESS THAN (50),
PARTITION p1 VALUES LESS THAN (100),
PARTITION p2 VALUES LESS THAN (150),
PARTITION p3 VALUES LESS THAN (MAXVALUE)
);
INSERT INTO e VALUES
(1669, "Jim", "Smith"),
(337, "Mary", "Jones"),
(16, "Frank", "White"),
(2005, "Linda", "Black");</pre>
<p>创建一个与e结构一样的非分区表e2</p>
<pre class="brush:sql;">mysql> create table e2 like e;
Query OK, 0 rows affected (0.01 sec)
mysql> show create table e2\G
*************************** 1. row ***************************
Table: e2
Create Table: CREATE TABLE `e2` (
`id` int(11) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (50) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (100) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (150) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */
1 row in set (0.00 sec)
mysql> alter table e2 remove partitioning;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table e2\G
*************************** 1. row ***************************
Table: e2
Create Table: CREATE TABLE `e2` (
`id` int(11) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)</pre>
<p>查看数据在e表中的分布:</p>
<pre class="brush:sql;">select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='e'
+------+------+----------+------------+
| part | expr | descr | table_rows |
+------+------+----------+------------+
| p0 | id | 50 | 1 |
| p1 | id | 100 | 0 |
| p2 | id | 150 | 0 |
| p3 | id | MAXVALUE | 3 |
+------+------+----------+------------+
4 rows in set (0.00 sec)</pre>
<p>将分区p0与e2表进行交换:</p>
<pre class="brush:sql;">mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
Query OK, 0 rows affected (0.01 sec)
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='e';
+------+------+----------+------------+
| part | expr | descr | table_rows |
+------+------+----------+------------+
| p0 | id | 50 | 0 |
| p1 | id | 100 | 0 |
| p2 | id | 150 | 0 |
| p3 | id | MAXVALUE | 3 |
+------+------+----------+------------+
4 rows in set (0.01 sec)
mysql> select * from e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.00 sec) </pre>
<p>重做实验,这次在交换前在表e2中插入一些数据</p>
<pre class="brush:sql;">mysql> insert into e2 values(16,'FAN','BOSHI');
Query OK, 1 row affected (0.00 sec)
mysql> insert into e2 values(51,'DU','YALAN');
Query OK, 1 row affected (0.00 sec)
mysql> select * from e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | FAN | BOSHI |
| 51 | DU | YALAN |
+----+-------+-------+
2 rows in set (0.00 sec)
mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
ERROR 1737 (HY000): Found a row that does not match the partition</pre>
<p>报错了,因为51超出了p0的范围。<br>
如之前所说,此时使用IGNORE也无济于事</p>
<pre class="brush:sql;">mysql> ALTER IGNORE TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
ERROR 1737 (HY000): Found a row that does not match the partition</pre>
<p>修改id为49,这样就属于p0的范围了</p>
<pre class="brush:sql;">mysql> update e2 set id=49 where id=51;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
Query OK, 0 rows affected (0.01 sec)
mysql> select
-> partition_name part,
-> partition_expression expr,
-> partition_description descr,
-> table_rows
-> from information_schema.partitions where
-> table_schema = schema()
-> and table_name='e';
+------+------+----------+------------+
| part | expr | descr | table_rows |
+------+------+----------+------------+
| p0 | id | 50 | 2 |
| p1 | id | 100 | 0 |
| p2 | id | 150 | 0 |
| p3 | id | MAXVALUE | 3 |
+------+------+----------+------------+
4 rows in set (0.00 sec)
e2的数据被交换到了p0中
mysql> select * from e partition(p0);
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | FAN | BOSHI |
| 49 | DU | YALAN |
+----+-------+-------+
2 rows in set (0.00 sec)
e的p0分区中的数据被交换到了e2中
mysql> select * from e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.01 sec)</pre>
<h4 id="交换subpartition">交换subpartition</h4>
<pre class="brush:sql;">CREATE TABLE es (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30)
)
PARTITION BY RANGE (id)
SUBPARTITION BY KEY (lname)
SUBPARTITIONS 2 (
PARTITION p0 VALUES LESS THAN (50),
PARTITION p1 VALUES LESS THAN (100),
PARTITION p2 VALUES LESS THAN (150),
PARTITION p3 VALUES LESS THAN (MAXVALUE)
);
INSERT INTO es VALUES
(1669, "Jim", "Smith"),
(337, "Mary", "Jones"),
(16, "Frank", "White"),
(2005, "Linda", "Black");
CREATE TABLE es2 LIKE es;
ALTER TABLE es2 REMOVE PARTITIONING;</pre>
<p>尽管我们没有显示的指定每个子分区的名字,我们仍可以通过information_schema.partitions表获取到子分区的名字</p>
<pre class="brush:sql;">select
partition_name part,
subpartition_name,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name='es';
+------+-------------------+------+----------+------------+
| part | subpartition_name | expr | descr | table_rows |
+------+-------------------+------+----------+------------+
| p0 | p0sp0 | id | 50 | 1 |
| p0 | p0sp1 | id | 50 | 0 |
| p1 | p1sp0 | id | 100 | 0 |
| p1 | p1sp1 | id | 100 | 0 |
| p2 | p2sp0 | id | 150 | 0 |
| p2 | p2sp1 | id | 150 | 0 |
| p3 | p3sp0 | id | MAXVALUE | 3 |
| p3 | p3sp1 | id | MAXVALUE | 0 |
+------+-------------------+------+----------+------------+</pre>
<p>接下来,开始将p3sp0和es进行交换</p>
<pre class="brush:sql;">mysql> select * from es partition(p3sp0);
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
3 rows in set (0.00 sec)
mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from es partition(p3sp0);
Empty set (0.00 sec)
mysql> select * from es2;
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
3 rows in set (0.00 sec)</pre>
<p>如果一个分区表有子分区,那么你只能以子分区为粒度进行交换,而不能直接交换子分区的父分区</p>
<pre class="brush:sql;">mysql> ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2;
ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition</pre>
<p>EXCHANGE PARTITION有着严格的要求<br>
两个将要交换的表的 列名,列的创建顺序,列的数量,以及索引都要严格一致。当然存储引擎也要一致</p>
<pre class="brush:sql;">mysql> desc es2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| fname | varchar(30) | YES | | NULL | |
| lname | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> create index id_name on es2(id,fname);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
ERROR 1736 (HY000): Tables have different definitions</pre>
<p>改变es2的存储引擎</p>
<pre class="brush:sql;">mysql> drop index id_name on es2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table es2 engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL</pre>
<h3 id="分区表的维护">分区表的维护</h3>
<p>CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, and REPAIR TABLE可以被用于维护分区表</p>
<p>Rebuilding partitions.相当于将分区中的数据drop掉再插入回来,对于避免磁盘碎片很有效<br>
Example:</p>
<pre class="brush:sql;">ALTER TABLE t1 REBUILD PARTITION p0, p1;</pre>
<p>Optimizing partitions.如果你的表增加删除了大量数据,或者进行了大量的边长列的更新操作( VARCHAR, BLOB, or TEXT columns)。那么optimize partition将回收未使用的空间,并整理分区数据文件。<br>
Example:</p>
<pre class="brush:sql;">ALTER TABLE t1 OPTIMIZE PARTITION p0, p1;</pre>
<p>运行OPTIMIZE PARTITION 相当于做了 CHECK PARTITION, ANALYZE PARTITION, and REPAIR PARTITION</p>
<blockquote>
<p>Some MySQL storage engines, including InnoDB, do not support per-partition optimization; in these cases, ALTER TABLE … OPTIMIZE PARTITION rebuilds the entire table. In MySQL 5.6.9 and later, running this statement on such a table causes the entire table to rebuilt and analyzed, and an appropriate warning to be issued. (Bug #11751825, Bug #42822) Use ALTER TABLE … REBUILD PARTITION and ALTER TABLE … ANALYZE PARTITION instead, to avoid this issue.</p>
</blockquote>
<p>Analyzing partitions.读取并保存分区的键分布<br>
Example:</p>
<pre class="brush:sql;">ALTER TABLE t1 ANALYZE PARTITION p3;</pre>
<p>Repairing partitions.修补被破坏的分区<br>
Example:</p>
<pre class="brush:sql;">ALTER TABLE t1 REPAIR PARTITION p0,p1;</pre>
<p>Checking partitions.可以使用几乎与对非分区表使用CHECK TABLE 相同的方式检查分区。<br>
Example:</p>
<pre class="brush:sql;">ALTER TABLE trb3 CHECK PARTITION p1;</pre>
<p>这个命令可以告诉你表trb3的分区p1中的数据或索引是否已经被破坏。如果发生了这种情况,使用“ALTER TABLE … REPAIR PARTITION”来修补该分区。</p>
<h4 id="以上每个命令都支持将分区换成all">以上每个命令都支持将分区换成ALL</h4>
<blockquote>
<p>The use of mysqlcheck and myisamchk is not supported with partitioned tables.</p>
</blockquote>
<p>mysqlcheck和myisamchk不支持分区表</p>
<p>你可以使用 ALTER TABLE … TRUNCATE PARTITION. 来删除一个或多个分区中的数据<br>
如:ALTER TABLE … TRUNCATE PARTITION ALL删除所有数据</p>
<p>ANALYZE, CHECK, OPTIMIZE, REBUILD, REPAIR, and TRUNCATE 操作不支持 subpartitions.</p>
</enddate>

以上所述是小编给大家介绍的Mysql分区表的管理与维护,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • MySQL分区表的局限和限制详解

    禁止构建 分区表达式不支持以下几种构建: 存储过程,存储函数,UDFS或者插件 声明变量或者用户变量 可以参考分区不支持的SQL函数 算术和逻辑运算符 分区表达式支持+,-,*算术运算,但是不支持DIV和/运算(还存在,可以查看Bug #30188, Bug #33182).但是,结果必须是整形或者NULL(线性分区键除外,想了解更多信息,可以查看分区类型). 分区表达式不支持位运算:|,&,^,<<,>>,~ . HANDLER语句 在MySQL 5.7.1之前的分区表不

  • MySQL优化之分区表

    当数据库数据量涨到一定数量时,性能就成为我们不能不关注的问题,如何优化呢? 常用的方式不外乎那么几种: 1.分表,即把一个很大的表达数据分到几个表中,这样每个表数据都不多. 优点:提高并发量,减小锁的粒度 缺点:代码维护成本高,相关sql都需要改动 2.分区,所有的数据还在一个表中,但物理存储数据根据一定的规则存放在不同的文件中,文件也可以放到另外磁盘上 优点:代码维护量小,基本不用改动,提高IO吞吐量 缺点:表的并发程度没有增加 3.拆分业务,这个本质还是分表. 优点:长期支持更好 缺点:代码

  • mysql使用教程之分区表的使用方法(删除分区表)

    MySQL使用分区表的好处: 1,可以把一些归类的数据放在一个分区中,可以减少服务器检查数据的数量加快查询.2,方便维护,通过删除分区来删除老的数据.3,分区数据可以被分布到不同的物理位置,可以做分布式有效利用多个硬盘驱动器. MySQL可以建立四种分区类型的分区: RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区. LIST 分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择.  www.jb51.net HASH分区:基于用户

  • Rails中使用MySQL分区表一个提升性能的方法

    MySQL 的分区表是一种简单有效的处理极大数据表的特性,通过它可以使应用程序几乎很少改动就能达成对极大数据表的高效处理,但由于 Rails ActiveRecord 设计上一些惯例,可能导致一些数据处理不能利用分区表特性,反而变得很慢,在使用分区表过程中一定要多加注意. 下面以一个例子来说明.在 light 系统中,有一张数据表是 diet_items, 主要字段是 id, schedule_id, meal_order food_id, weight, calory 等等,它的每一条记录表示

  • Mysql分区表的管理与维护

    改变一个表的分区方案只需使用alter table 加 partition_options 子句就可以了.和创建分区表时的create table语句很像. 创建表 CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) PARTITION BY RANGE( YEAR(purchased) ) ( PARTITION p0 VALUES LESS THAN (1990), PARTITION p1 VALUES LESS THA

  • MySQL分区表管理命令汇总

    目录 一.ANALYZE和CHECK PARTITION 分析和检查分区 二.REPAIR 修复分区 三.OPTIMIZE 分区 四.REBUILD分区 五.新增和删除分区 前言: 分区是一种表的设计模式,正确的分区可以极大地提升数据库的查询效率,完成更高质量的SQL编程.但是如果错误地使用分区,那么分区可能带来毁灭性的的结果. 分区功能并不是在存储引擎层完成的,因此不只有InnoDB存储引擎支持分区,常见的存储引擎MyISAM.NDB等都支持分区.但是并不是所有的存储引擎都支持,如CSV.FE

  • MySQL 分区表中分区键为什么必须是主键的一部分

    目录 水平拆分VS垂直拆分 分区表 MySQL8.0中分区表的变化 为什么分区键必须是主键的一部分? 本地分区索引VS全局索引 总结 前言: 分区是一种表的设计模式,通俗地讲表分区是将一大表,根据条件分割成若干个小表.但是对于应用程序来讲,分区的表和没有分区的表是一样的.换句话来讲,分区对于应用是透明的,只是数据库对于数据的重新整理 随着业务的不断发展,数据库中的数据会越来越多,相应地,单表的数据量也会越到越大,大到一个临界值,单表的查询性能就会下降. 这个临界值,并不能一概而论,它与硬件能力.

  • MySQL分区表的正确使用方法

    MySQL分区表概述 我们经常遇到一张表里面保存了上亿甚至过十亿的记录,这些表里面保存了大量的历史记录. 对于这些历史数据的清理是一个非常头疼事情,由于所有的数据都一个普通的表里.所以只能是启用一个或多个带where条件的delete语句去删除(一般where条件是时间). 这对数据库的造成了很大压力.即使我们把这些删除了,但底层的数据文件并没有变小.面对这类问题,最有效的方法就是在使用分区表.最常见的分区方法就是按照时间进行分区. 分区一个最大的优点就是可以非常高效的进行历史数据的清理. 1.

  • MySQL分区表的最佳实践指南

    前言: 分区是一种表的设计模式,通俗地讲表分区是将一大表,根据条件分割成若干个小表.但是对于应用程序来讲,分区的表和没有分区的表是一样的.换句话来讲,分区对于应用是透明的,只是数据库对于数据的重新整理.本篇文章给大家带来的内容是关于MySQL中分区表的介绍及使用场景,有需要的朋友可以参考一下,希望对你有所帮助. 1.分区的目的及分区类型 MySQL在创建表的时候可以通过使用PARTITION BY子句定义每个分区存放的数据.在执行查询的时候,优化器根据分区定义过滤那些没有我们需要的数据的分区,这

  • 详解MySQL分区表

    前言: 分区是一种表的设计模式,通俗地讲表分区是将一大表,根据条件分割成若干个小表.但是对于应用程序来讲,分区的表和没有分区的表是一样的.换句话来讲,分区对于应用是透明的,只是数据库对于数据的重新整理.本篇文章给大家带来的内容是关于MySQL中分区表的介绍及使用场景,有需要的朋友可以参考一下,希望对你有所帮助. 1.分区的目的及分区类型 MySQL在创建表的时候可以通过使用PARTITION BY子句定义每个分区存放的数据.在执行查询的时候,优化器根据分区定义过滤那些没有我们需要的数据的分区,这

  • MySQL数据操作管理示例详解

    目录 一.查看表和查看表的定义 二.删除表 三.创建表 四.表类型 五.修改表 六.完整性 1.数据完整性 2.使用约束实现数据完整性 3.在数据表上添加约束 4.删除约束 七.添加数据 八.修改数据 九.删除数据 十.SQL语句中的运算符 算数运算符 比较运算符 逻辑运算符 一.查看表和查看表的定义 SHOW TABLE 表名 DESC 表名; DESCRIBE 表名 二.删除表 DROP TABLE [IF EXISTS ] 表名; 如果表USER存在,删除表USER DROP TABLE

  • MySQL图形化管理工具的使用及说明

    目录 MySQL 图形化管理工具的介绍 1.MySQL Workbench 2.Navicat 3.SQLyog 4.DBeaver 5.DataGrip 总结 MySQL 图形化管理工具的介绍 MySQL 图形化管理工具极大地方便了数据库的操作与管理,常用的图形化管理工具有: MysQL Workbench phpMyAdmin Navicat Preminum MySQLDumper SQLyog dbeaver MysQL ODBc Connector DataGrip 1.MySQL W

  • Navicat for MySQL(mysql图形化管理工具)是什么?

    MySQL现已经成为大多数中小企业及个人站长建站的首选数据库,其自带了简单web图形管理phpmyadmin工具,但是管理.操作能力有限,这就使得人们常需要寻找一个更为专业,管理功能更为强大的管理工具,以方便于我们更好的应用和管理MySQL数据库. 这里就给大家介绍一个常用的MySQL数据库管理工具:Navicat for MySQL.首先我们介绍一下: Navicat for MySQL是什么? Navicat for MySQL是一款强大的MySQL数据库管理和开发工具,它为专业开发者提供了

  • 管理与维护宽带路由器的注意事项

    企业在组网过程中一般都使用路由器,所以随着企业对网络的依赖越来越强,路由器便成为企业网络的命脉,如果平时没有及时对企业路由器进行相关的管理和维护,时间长了,慢慢地就会引发多种莫名其妙的故障,最后导致企业路由器不能正常工作而造成企业网络中断,这时候就会对企业用户造成诸多的不便. 而且,很多的企业路由器故障的隐性都很高,不留心的话很难察觉到,很让人头痛,要是等 到企业路由器的性能下降.网络中断等现象出现,才急急忙忙地动用大量的人力物力来补救,那就很费劲了.另外要是遇上雷电等突发的情况,但却没有预先为

随机推荐