当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql分区表的管理与维护

Mysql分区表的管理与维护

2017年12月12日  | 移动技术网IT编程  | 我要评论

改变一个表的分区方案只需使用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分区表的管理与维护,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网