原文地址: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 ALTER TABLE 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:

  1. Table nt is not itself partitioned.

  2. Table nt is not a temporary table.

  3. The structures of tables pt and nt are otherwise identical.

  4. Table nt contains no foreign key references, and no other table has any foreign keys that refer to nt.

  5. There are no rows in nt that lie outside the boundaries of the partition definition for p.

In addition to the ALTERINSERT, and CREATE privileges usually required for ALTER TABLE statements, you must have the DROP privilege to perform ALTER TABLE ... EXCHANGE PARTITION.

You should also be aware of the following effects of ALTER TABLE ... EXCHANGE PARTITION:

  • Executing ALTER TABLE ... EXCHANGE PARTITION does not invoke any triggers on either the partitioned table or the table to be exchanged.

  • Any AUTO_INCREMENT columns in the exchanged table are reset.

  • The IGNORE keyword has no effect when used with ALTER TABLE ... EXCHANGE PARTITION.

The complete syntax of the ALTER TABLE ... EXCHANGE PARTITION 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:

ALTER TABLE 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 ALTER TABLE EXCHANGE PARTITION statement. To exchange multiple partitions or subpartitions, use multiple ALTER TABLE EXCHANGE PARTITION statements. EXCHANGE PARTITION may not be combined with other ALTER TABLE options. 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 table e has 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 e named e2. This can be done using the mysql client as shown here:

mysql> 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

You can see which partitions in table e contain rows by querying the INFORMATION_SCHEMA.PARTITIONS table, like this:

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)
Note

For partitioned InnoDB tables, the row count given in the TABLE_ROWS column of theINFORMATION_SCHEMA.PARTITIONS table is only an estimated value used in SQL optimization, and is not always exact.

To exchange partition p0 in table e with table e2, you can use the ALTER TABLE statement shown here:

mysql> 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 INFORMATION_SCHEMA.PARTITIONS table, as before. The table row that was previously found in partition p0 is no longer present:

mysql> 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 e2, you can see that the “missing” row can now be found there:

mysql> 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 e, making sure that this row is stored in partition p0 by choosing an id column value that is less than 50, and verifying this afterwards by querying the PARTITIONS table:

mysql> 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)

Now we once again exchange partition p0 with table e2 using the same ALTER TABLE statement as previously:

mysql> 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 p0 and the table row that was stored in table e2, prior to issuing the ALTER TABLE statement, have now switched places:

mysql> 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)

Non-Matching Rows

You should keep in mind that any rows found in the nonpartitioned table prior to issuing the ALTER TABLE ... EXCHANGE PARTITION 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 e2 that is outside the boundaries of the partition definition for partition p0 of table e. For example, insert a row with an id column value that is too large; then, try to exchange the table with the partition again:

mysql> 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

The IGNORE keyword is accepted, but has no effect when used with EXCHANGE PARTITION, as shown here:

mysql> ALTER IGNORE TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
ERROR 1707 (HY000): Found row that does not match the partition

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 an ALTER TABLE ... EXCHANGE PARTITION statement. In the following example, we first create a table es that is partitioned by RANGE and subpartitioned by KEY, populate this table as we did table e, and then create an empty, nonpartitioned copy es2 of the table, as shown here:

mysql> 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: 0mysql> 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

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_NAME of the PARTITIONS table from INFORMATION_SCHEMAwhen selecting from that table, as shown here:

mysql> 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 ALTER TABLE statement exchanges subpartition p3sp0 table es with the nonpartitioned table es2:

mysql> 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:

mysql> 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)

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> ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2;
ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition

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> 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: 0mysql> 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: 0mysql> 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

转载于:https://www.cnblogs.com/davidwang456/p/4702475.html

Exchanging Partitions and Subpartitions with Tables--官方文档相关推荐

  1. c oracle帮助文档下载,Oracle 19c 官方文档——Concepts

    Oracle 19c 官方文档--Concepts 免费 专栏简介 19c 作为 Oracle 12c 的最终版以及稳定版,在正式的 Linux 版本出来之前,先熟悉其各个部分的特性是必须的,以此为目 ...

  2. kafka官方文档学习笔记2--QuickStart

    下载kafka https://www.apache.org/dyn/closer.cgi?path=/kafka/1.0.0/kafka_2.11-1.0.0.tgz 解压安装包 > tar ...

  3. Cassandra 3.x官方文档(1)---关于Cassandra

    写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...

  4. mysql 标识符规则_MySQL 标识符到底区分大小写么——官方文档告诉你

    最近在阿里云服务器上部署一个自己写的小 demo 时遇到一点问题,查看 Tomcat 日志后定位到问题出现在与数据库服务器交互的地方,执行 SQL 语句时会返回 指定列.指定名 不存在的错误.多方查证 ...

  5. spark之4:基础指南(源自官方文档)

    spark之4:基础指南(源自官方文档) @(SPARK)[spark, 大数据] spark之4基础指南源自官方文档 一简介 二接入Spark 三初始化Spark 一使用Shell 四弹性分布式数据 ...

  6. Gora官方文档之二:Gora对Map-Reduce的支持

    参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...

  7. clickhouse官方文档_clickhouse分析:结合grafana和metabase完成监控和数据分析

    点击上方蓝字关注我们 " 关于clickhouse的监控和可视化界面,想必刚接触到ch的人是一头雾水,大厂往往会给ch集群定制各种监控和可视化分析,普通用户就需要我们自己寻求现成的开源工具, ...

  8. clickhouse官方文档_clickhouse分析:zookeeper减压概述

    点击上方蓝字关注我们 " 使用复制表之后,随着数据量的增加,zookeeper是瓶颈?这个问题估计任何一个对ch关注的人都会看到,当然解决这个问题是需要花费较大精力的.本次我主要想分享ch官 ...

  9. kafka官方文档学习笔记3--配置简述

    Kafka使用key-value键值对格式的配置,这些配置即可以在进程启动时,根据指定的properties文件加载,也可以通过编程的方式,在程序中动态指定:根据集群中角色的不同分为6种配置: bro ...

  10. [HBase进阶]--rowkey设计要点(官方文档介绍)

    官方文档说明 http://hbase.apache.org/book.html#rowkey.design 一.Hotspotting(热点效应) 1.hbase是字典排序,这是一种优化扫描的方式, ...

最新文章

  1. 在家搭建大数据分布式计算环境!
  2. 懒人的懒方法之-回车跳转大法
  3. 维护服务器技术员流程,技术员岗位工作的流程表.doc
  4. Python基础教程:赋值、深拷贝与浅拷贝(内存地址)
  5. 基于HiKariCP组件,分析连接池原理
  6. mysql 主主切换_mysql 主-主配置中进行切换
  7. React 之受控组件和非受控组件
  8. 德勤咨询:阿里云是跨国企业上云优选
  9. 51nod 1134最长递增子序列
  10. CentOS6.9+Hadoop2.7.3+Hive1.2.1+Hbase1.3.1+Spark2.1.1
  11. 单片机百位计数c语言,单片机c语言版数管动态显示实验报告.doc
  12. Nooploop空循环 TOFSense激光测距传感器 模块 红外测距测高
  13. 【艾特淘】淘宝改sku名字有影响吗?淘宝sku怎么修改不降权
  14. Linux 服务器CPU占用率100%,使用率高解决方案
  15. go import导入包详解
  16. 谁说大象不能跳舞读后感
  17. python 文件操作OS总结
  18. 利用Winhex,OllyDbg和W32Dasm破解USB监控器
  19. 手机与计算机之间的文件传输,电脑与手机如何快速传输文件
  20. P1500 丘比特的烦恼

热门文章

  1. 微盘 计算机英语,高中英语,微盘.doc
  2. oracle dbcontrol界面,oracle enterprise manager配置简介
  3. java -cp 引用多个包_javac编译单文件、多文件引入jar包、-cp解决无法加载主类问题...
  4. oracle union详解,Oracle中的union和join
  5. java什么叫内部对象,java – 函数对象的内部类中的变量/对象会发生什么?
  6. mysql range用法_MySQL的常用函数
  7. php 图像 处理,PHP 处理图像步骤解析
  8. 城市列表简称JSON数据
  9. 结构体指针struct stu *p;和结构体变量struct stu p;结构体为什么要用指针引用而不用变量引用
  10. Maven如何用Eclipse创建一个Maven项目【笔记自用】