Exchanging Partitions and Subpartitions with Tables--官方文档
2015-08-04 16:51
441 查看
原文地址:https://dev.mysql.com/doc/refman/5.6/en/partitioning-management-exchange.html
In MySQL 5.6, it is possible to exchange a table partition or subpartition with a table using
Table
Table
The structures of tables
Table
There are no rows in
In addition to the
You should also be aware of the following effects of
Executing
Any
The
The complete syntax of the
EXCHANGE PARTITION
WITH TABLE
[/code]
One and only one partition or subpartition may be exchanged with one and only one nonpartitioned table in a single
Now we create a nonpartitioned copy of
Query OK, 0 rows affected (1.34 sec)
mysql>
Query OK, 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
[/code]
You can see which partitions in table
->
->
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
Note
For partitioned
To exchange partition
Query OK, 0 rows affected (0.28 sec)
[/code]
More precisely, the statement just issued causes any rows found in the partition to be swapped with those found in the table. You can observe how this has happened by querying the
->
->
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 0 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
If you query table
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.00 sec)
[/code]
The table to be exchanged with the partition does not necessarily have to be empty. To demonstrate this, we first insert a new row into table
Query OK, 1 row affected (0.05 sec)
mysql>
->
->
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
Now we once again exchange partition
Query OK, 0 rows affected (0.28 sec)
[/code]
The output of the following queries shows that the table row that was stored in partition
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 16 | Frank | White |
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
4 rows in set (0.00 sec)
mysql>
->
->
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
mysql>
+----+---------+-------+
| id | fname | lname |
+----+---------+-------+
| 41 | Michael | Green |
+----+---------+-------+
1 row in set (0.00 sec)
[/code]
Query OK, 1 row affected (0.08 sec)
mysql>
ERROR 1707 (HY000): Found row that does not match the partition
[/code]
The
ERROR 1707 (HY000): Found row that does not match the partition
[/code]
->
->
->
->
->
->
->
->
->
->
->
->
Query OK, 0 rows affected (2.76 sec)
mysql>
->
->
->
->
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql>
Query OK, 0 rows affected (1.27 sec)
mysql>
Query OK, 0 rows affected (0.70 sec)
Records: 0 Duplicates: 0 Warnings: 0
[/code]
Although we did not explicitly name any of the subpartitions when creating table
->
->
+----------------+-------------------+------------+
| PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
+----------------+-------------------+------------+
| p0 | p0sp0 | 1 |
| p0 | p0sp1 | 0 |
| p1 | p1sp0 | 0 |
| p1 | p1sp1 | 0 |
| p2 | p2sp0 | 0 |
| p2 | p2sp1 | 0 |
| p3 | p3sp0 | 3 |
| p3 | p3sp1 | 0 |
+----------------+-------------------+------------+
8 rows in set (0.00 sec)
[/code]
The following
Query OK, 0 rows affected (0.29 sec)
[/code]
You can verify that the rows were exchanged by issuing the following queries:
->
->
+----------------+-------------------+------------+
| PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
+----------------+-------------------+------------+
| p0 | p0sp0 | 1 |
| p0 | p0sp1 | 0 |
| p1 | p1sp0 | 0 |
| p1 | p1sp1 | 0 |
| p2 | p2sp0 | 0 |
| p2 | p2sp1 | 0 |
| p3 | p3sp0 | 0 |
| p3 | p3sp1 | 0 |
+----------------+-------------------+------------+
8 rows in set (0.00 sec)
mysql>
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
3 rows in set (0.00 sec)
[/code]
If a table is subpartitioned, you can exchange only a subpartition of the table—not an entire partition—with an unpartitioned table, as shown here:
ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition
[/code]
The comparison of table structures used by MySQL is very strict. The number, order, names, and types of columns and indexes of the partitioned table and the nonpartitioned table must match exactly. In addition, both tables must use the same storage engine:
Query OK, 0 rows affected (1.31 sec)
mysql>
Query OK, 0 rows affected (0.53 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
*************************** 1. row ***************************
Table: es3
Create Table: CREATE TABLE `es3` (
`id` int(11) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL[/code]
In MySQL 5.6, it is possible to exchange a table partition or subpartition with a table using
ALTER TABLEWITH TABLEptEXCHANGE PARTITION [code]p
nt, where
ptis the partitioned table and
pis the partition or subpartition of
ptto be exchanged with unpartitioned table
nt, provided that the following statements are true:
Table
ntis not itself partitioned.
Table
ntis not a temporary table.
The structures of tables
ptand
ntare otherwise identical.
Table
ntcontains no foreign key references, and no other table has any foreign keys that refer to
nt.
There are no rows in
ntthat lie outside the boundaries of the partition definition for
p.
In addition to the
ALTER,
INSERT, and
CREATEprivileges usually required for
ALTER TABLEstatements, you must have the
DROPprivilege to perform
ALTER TABLE ... EXCHANGE PARTITION.
You should also be aware of the following effects of
ALTER TABLE ... EXCHANGE PARTITION:
Executing
ALTER TABLE ... EXCHANGE PARTITIONdoes not invoke any triggers on either the partitioned table or the table to be exchanged.
Any
AUTO_INCREMENTcolumns in the exchanged table are reset.
The
IGNOREkeyword has no effect when used with
ALTER TABLE ... EXCHANGE PARTITION.
The complete syntax of the
ALTER TABLE ... EXCHANGE PARTITIONstatement is shown here, where
ptis the partitioned table,
pis the partition or subpartition to be exchanged, and
ntis the nonpartitioned table to be exchanged with
p:
ALTER TABLE [code]pt
EXCHANGE PARTITION
p
WITH TABLE
nt;
[/code]
One and only one partition or subpartition may be exchanged with one and only one nonpartitioned table in a single
ALTER TABLE EXCHANGE PARTITIONstatement. To exchange multiple partitions or subpartitions, use multiple
ALTER TABLE EXCHANGE PARTITIONstatements.
EXCHANGE PARTITIONmay not be combined with other
ALTER TABLEoptions. The partitioning and (if applicable) subpartitioning used by the partitioned table may be of any type or types supported in MySQL 5.6.
Exchanging a Partition with a Nonpartitioned Table
Suppose that a partitioned tableehas been created and populated using the following SQL statements:
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");
Now we create a nonpartitioned copy of
enamed
e2. This can be done using the mysql client as shown here:
mysql> [code]CREATE TABLE e2 LIKE e;
Query OK, 0 rows affected (1.34 sec)
mysql>
ALTER TABLE e2 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
[/code]
You can see which partitions in table
econtain rows by querying the
INFORMATION_SCHEMA.PARTITIONStable, like this:
mysql> [code]SELECT PARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
Note
For partitioned
InnoDBtables, the row count given in the
TABLE_ROWScolumn of the
INFORMATION_SCHEMA.PARTITIONStable is only an estimated value used in SQL optimization, and is not always exact.
To exchange partition
p0in table
ewith table
e2, you can use the
ALTER TABLEstatement shown here:
mysql> [code]ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
Query OK, 0 rows affected (0.28 sec)
[/code]
More precisely, the statement just issued causes any rows found in the partition to be swapped with those found in the table. You can observe how this has happened by querying the
INFORMATION_SCHEMA.PARTITIONStable, as before. The table row that was previously found in partition
p0is no longer present:
mysql> [code]SELECT PARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 0 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
If you query table
e2, you can see that the “missing” row can now be found there:
mysql> [code]SELECT * FROM e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.00 sec)
[/code]
The table to be exchanged with the partition does not necessarily have to be empty. To demonstrate this, we first insert a new row into table
e, making sure that this row is stored in partition
p0by choosing an
idcolumn value that is less than 50, and verifying this afterwards by querying the
PARTITIONStable:
mysql> [code]INSERT INTO e VALUES (41, "Michael", "Green");
Query OK, 1 row affected (0.05 sec)
mysql>
SELECT PARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
[/code]
Now we once again exchange partition
p0with table
e2using the same
ALTER TABLEstatement as previously:
mysql> [code]ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
Query OK, 0 rows affected (0.28 sec)
[/code]
The output of the following queries shows that the table row that was stored in partition
p0and the table row that was stored in table
e2, prior to issuing the
ALTER TABLEstatement, have now switched places:
mysql> [code]SELECT * FROM e;
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 16 | Frank | White |
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
4 rows in set (0.00 sec)
mysql>
SELECT PARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
mysql>
SELECT * FROM e2;
+----+---------+-------+
| id | fname | lname |
+----+---------+-------+
| 41 | Michael | Green |
+----+---------+-------+
1 row in set (0.00 sec)
[/code]
Non-Matching Rows
You should keep in mind that any rows found in the nonpartitioned table prior to issuing theALTER TABLE ... EXCHANGE PARTITIONstatement must meet the conditions required for them to be stored in the target partition; otherwise, the statement fails. To see how this occurs, first insert a row into
e2that is outside the boundaries of the partition definition for partition
p0of table
e. For example, insert a row with an
idcolumn value that is too large; then, try to exchange the table with the partition again:
mysql> [code]INSERT INTO e2 VALUES (51, "Ellen", "McDonald");
Query OK, 1 row affected (0.08 sec)
mysql>
ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
ERROR 1707 (HY000): Found row that does not match the partition
[/code]
The
IGNOREkeyword is accepted, but has no effect when used with
EXCHANGE PARTITION, as shown here:
mysql> [code]ALTER IGNORE TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
ERROR 1707 (HY000): Found row that does not match the partition
[/code]
Exchanging a Subpartition with a Nonpartitioned Table
You can also exchange a subpartition of a subpartitioned table (see Section 19.2.6, “Subpartitioning”) with a nonpartitioned table using anALTER TABLE ... EXCHANGE PARTITIONstatement. In the following example, we first create a table
esthat is partitioned by
RANGEand subpartitioned by
KEY, populate this table as we did table
e, and then create an empty, nonpartitioned copy
es2of the table, as shown here:
mysql> [code]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)
->
);
Query OK, 0 rows affected (2.76 sec)
mysql>
INSERT INTO es VALUES
->
(1669, "Jim", "Smith"),
->
(337, "Mary", "Jones"),
->
(16, "Frank", "White"),
->
(2005, "Linda", "Black");
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql>
CREATE TABLE es2 LIKE es;
Query OK, 0 rows affected (1.27 sec)
mysql>
ALTER TABLE es2 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.70 sec)
Records: 0 Duplicates: 0 Warnings: 0
[/code]
Although we did not explicitly name any of the subpartitions when creating table
es, we can obtain generated names for these by including the
SUBPARTITION_NAMEof the
PARTITIONStable from
INFORMATION_SCHEMAwhen selecting from that table, as shown here:
mysql> [code]SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'es';
+----------------+-------------------+------------+
| PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
+----------------+-------------------+------------+
| p0 | p0sp0 | 1 |
| p0 | p0sp1 | 0 |
| p1 | p1sp0 | 0 |
| p1 | p1sp1 | 0 |
| p2 | p2sp0 | 0 |
| p2 | p2sp1 | 0 |
| p3 | p3sp0 | 3 |
| p3 | p3sp1 | 0 |
+----------------+-------------------+------------+
8 rows in set (0.00 sec)
[/code]
The following
ALTER TABLEstatement exchanges subpartition
p3sp0table
eswith the nonpartitioned table
es2:
mysql> [code]ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
Query OK, 0 rows affected (0.29 sec)
[/code]
You can verify that the rows were exchanged by issuing the following queries:
mysql> [code]SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
->
FROM INFORMATION_SCHEMA.PARTITIONS
->
WHERE TABLE_NAME = 'es';
+----------------+-------------------+------------+
| PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
+----------------+-------------------+------------+
| p0 | p0sp0 | 1 |
| p0 | p0sp1 | 0 |
| p1 | p1sp0 | 0 |
| p1 | p1sp1 | 0 |
| p2 | p2sp0 | 0 |
| p2 | p2sp1 | 0 |
| p3 | p3sp0 | 0 |
| p3 | p3sp1 | 0 |
+----------------+-------------------+------------+
8 rows in 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)
[/code]
If a table is subpartitioned, you can exchange only a subpartition of the table—not an entire partition—with an unpartitioned table, as shown here:
mysql> [code]ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2;
ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition
[/code]
The comparison of table structures used by MySQL is very strict. The number, order, names, and types of columns and indexes of the partitioned table and the nonpartitioned table must match exactly. In addition, both tables must use the same storage engine:
mysql> [code]CREATE TABLE es3 LIKE e;
Query OK, 0 rows affected (1.31 sec)
mysql>
ALTER TABLE es3 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.53 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
SHOW CREATE TABLE es3\G
*************************** 1. row ***************************
Table: es3
Create Table: CREATE TABLE `es3` (
`id` int(11) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
ALTER TABLE es3 ENGINE = MyISAM;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es3;
ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL[/code]
相关文章推荐
- Algorithm Description
- Linq 模糊查询。Countains StartsWith EndsWith Indexof截取字符串,都可以用在Linq查询语句里
- 博文搬家
- Spark官方Blog:SequoiaDB与Spark深度整合
- Valid Anagram
- cocos2d-x 音效
- Berkeley's SICP in python3(五)
- sdk6下启用spidev
- SAP CO模块权限控制
- 运维必须掌握的Linux面试题
- C++ primer plus 练习10
- javaEE
- Zorka和Zico在Mac OS X上部署应注意的事项
- SSH 案例学习总结(一)
- acm hdu p2185 确定比赛名次
- [LeetCode]Container With Most Water
- 【前端学习笔记】深入学习Javascript:EVENT总结
- SAP 供应商寄售业务的标准流程
- DIV_CSS布局问题:3个水平对齐布局的DIV,左右固定宽,中间宽度自动填充
- 获取IPhone相册中图片的方法(包括获取所有图片)