Exchanging Partitions and Subpartitions with Tables--官方文
原文地址: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? ?contains no foreign key references,and no other table has any foreign keys that refer to? 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.?columns in the exchanged table are reset.
?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.? Exchanging a Partition with a Nonpartitioned TableSuppose that a partitioned table? INSERT INTO e VALUES Now we create a nonpartitioned copy of? CREATE TABLE e2 LIKE e; Query OK,0 rows affected (1.34 sec) You can see which partitions in table? 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? To exchange partition? 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? 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? 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? INSERT INTO e VALUES (41,"Michael","Green"); Query OK,1 row affected (0.05 sec) Now we once again exchange partition? 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? 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) Non-Matching RowsYou 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? INSERT INTO e2 VALUES (51,"Ellen","McDonald"); Query OK,1 row affected (0.08 sec) The? ALTER IGNORE TABLE e EXCHANGE PARTITION p0 WITH TABLE e2; Exchanging a Subpartition with a Nonpartitioned TableYou 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? 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) Although we did not explicitly name any of the subpartitions when creating table? 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? 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) 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 – LOCATE vs INSTR
- PostgreSQL和GreenPlum数据库的区别
- Mysql实例Linux中更改转移mysql数据库目录的步骤
- 在MacOSX10.6上运行python服务器时出现MySQLdb错误
- Mysql入门MYSQL数据库中cmd命令操作详解
- Mysql应用使用percona-toolkit操作MySQL的实用命令小结
- MYSQL数据库SQL语句详解 MySQL update的正确用法
- Mysql必读MySQL中使用case when 语句实现多条件查询的方法
- Mysql入门在WIN命令提示符下mysql 用户新建、授权、删除,密
- MySql可以在多个表上回滚sql事务吗?