
原文地址: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 ptEXCHANGE PARTITION p WITH table nt,where pt is the partitioned table and p is the partition or subpartition of pt to be exchanged with unpartitioned table nt,provIDed that the following statements are true:
contains no foreign key references,and no other table has any foreign keys that refer to .There are no rows in nt that lIE outsIDe the boundarIEs of the partition deFinition for p.In addition to the , ,and privileges usually required for statements,you must have the privilege to perform .
You should also be aware of the following effects of :
Executing does not invoke any triggers on either the partitioned table or the table to be exchanged.Any columns in the exchanged table are reset.The keyword has no effect when used with .The complete Syntax of the statement is shown here,where pt
is the partitioned table, p is the partition or subpartition to be exchanged,and nt is the nonpartitioned table to be exchanged with p:pt EXCHANGE PARTITION p WITH table nt;
One and only one partition or subpartition may be exchanged with one and only one nonpartitioned table in a single statement. To exchange multiple partitions or subpartitions,use multiple statements. may not be combined with other options. The partitioning and (if applicable) subpartitioning used by the partitioned table may be of any type or types supported in MysqL 5.6.
Suppose that a partitioned table has been created and populated using the following SQL statements:
INSERT INTO e VALUES
(1669,"Jim","Smith"),(337,"Mary","Jones"),(16,"Frank","White"),(2005,"Linda","Black");
Now we create a nonpartitioned copy of named . This can be done using the mysql clIEnt as shown here:
CREATE table e2 liKE e;query OK,0 rows affected (1.34 sec)MysqL> <strong >
ALTER table e2 REMOVE PARTITIONING;
query OK,0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
You can see which partitions in table contain rows by querying the table,like this:
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)
For partitioned tables,the row count given in the column of the table is only an estimated value used in sql optimization,and is not always exact.
To exchange partition in table with table ,you can use the statement shown here:
ALTER table e EXCHANGE PARTITION p0 WITH table e2;query OK,0 rows affected (0.28 sec)
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 table,as before. The table row that was prevIoUsly found in partition is no longer present:
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)
If you query table ,you can see that the
SELECT * FROM e2;+----+-------+-------+| ID | fname | lname |+----+-------+-------+| 16 | Frank | White |+----+-------+-------+1 row in set (0.00 sec)
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 ,making sure that this row is stored in partition by choosing an column value that is less than 50,and verifying this afterwards by querying the table:
INSERT INTO e VALUES (41,"Michael","Green"); query OK,1 row affected (0.05 sec)MysqL> <strong >
SELECT PARTITION_name,table_ROWS
-> <strong >FROM informatION_SCHEMA.PARTITIONS
-> <strong >WHERE table_name = 'e';
+----------------+------------+
| PARTITION_name | table_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)
Now we once again exchange partition with table using the same statement as prevIoUsly:
ALTER table e EXCHANGE PARTITION p0 WITH table e2;query OK,0 rows affected (0.28 sec)
The output of the following querIEs shows that the table row that was stored in partition and the table row that was stored in table ,prior to issuing the statement,have Now switched places:
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)name="IDm139741535630368">Non-Matching RowsMysqL> <strong >
SELECT PARTITION_name,table_ROWS
-> <strong >FROM informatION_SCHEMA.PARTITIONS
-> <strong >WHERE table_name = 'e';
+----------------+------------+
| PARTITION_name | table_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)MysqL> <strong >
SELECT * FROM e2;
+----+---------+-------+
| ID | fname | lname |
+----+---------+-------+
| 41 | Michael | Green |
+----+---------+-------+
1 row in set (0.00 sec)
You should keep in mind that any rows found in the nonpartitioned table prior to issuing the statement 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 that is outsIDe the boundarIEs of the partition deFinition for partition of table . For example,insert a row with an column value that is too large; then,try to exchange the table with the partition again:
INSERT INTO e2 VALUES (51,"Ellen","McDonald");query OK,1 row affected (0.08 sec)MysqL> <strong >
ALTER table e EXCHANGE PARTITION p0 WITH table e2;
<span >ERROR 1707 (HY000): Found row that does not match the partition
The keyword is accepted,but has no effect when used with ,as shown here:
ALTER IGnorE table e EXCHANGE PARTITION p0 WITH table e2;Exchanging a Subpartition with a Nonpartitioned Table
You can also exchange a subpartition of a subpartitioned table (see ) with a nonpartitioned table using an statement. In the following example,we first create a table that is partitioned by and subpartitioned by ,populate this table as we dID table ,and then create an empty,nonpartitioned copy of the table,as shown here:
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> <strong >
INSERT INTO es VALUES
-> <strong >(1669,
-> <strong >(337,
-> <strong >(16,
-> <strong >(2005,"Black");
query OK,4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0MysqL> <strong >
CREATE table es2 liKE es;
query OK,0 rows affected (1.27 sec)MysqL> <strong >
ALTER table es2 REMOVE PARTITIONING;
query OK,0 rows affected (0.70 sec)
Records: 0 Duplicates: 0 Warnings: 0
Although we dID not explicitly name any of the subpartitions when creating table ,we can obtain generated names for these by including the of the table from when selecting from that table,as shown here:
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)
The following statement exchanges subpartition table with the nonpartitioned table :
ALTER table es EXCHANGE PARTITION p3sp0 WITH table es2;query OK,0 rows affected (0.29 sec)
You can verify that the rows were exchanged by issuing the following querIEs:
SELECT PARTITION_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> <strong >
SELECT * FROM es2;
+------+-------+-------+
| ID | fname | lname |
+------+-------+-------+
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | linda | Black |
+------+-------+-------+
3 rows in set (0.00 sec)
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:
ALTER table es EXCHANGE PARTITION p3 WITH table es2;
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:
CREATE table es3 liKE e;query OK,0 rows affected (1.31 sec)总结MysqL> <strong >
ALTER table es3 REMOVE PARTITIONING;
query OK,0 rows affected (0.53 sec)
Records: 0 Duplicates: 0 Warnings: 0MysqL> <strong >
SHOW CREATE table es3\G
1. row
table: es3
Create table: CREATE tablees3(<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>int(11) NOT NulL,f<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>varchar(30) DEFAulT NulL,l<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>varchar(30) DEFAulT NulL
) ENGINE=InnoDB DEFAulT CHARSET=latin1
1 row in set (0.00 sec)MysqL> <strong >
ALTER table es3 ENGINE = MyISAM;
query OK,0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0MysqL> <strong >
ALTER table es EXCHANGE PARTITION p3sp0 WITH table es3;
<span >ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MysqL
以上是内存溢出为你收集整理的Exchanging Partitions and Subpartitions with Tables--官方文档全部内容,希望文章能够帮你解决Exchanging Partitions and Subpartitions with Tables--官方文档所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)