交换分区
SWAP_PARTITIONS_BETWEEN_TABLES 将 DROP_PARTITIONS 和 MOVE_PARTITIONS_TO_TABLE 的操作合并为单个事务。如果定期将已分区数据从一个表加载到另一个表,并且需要刷新第二个表中的分区,则 SWAP_PARTITIONS_BETWEEN_TABLES 很有用。
例如,您可能具有一个按日期分区的收入表,而且经常将数据从临时表移动到该表中。临时表有时包含目标表中已有日期的数据。在这种情况下,首先必须从目标表中删除这些日期的分区,然后将其替换为临时表中的相应分区。可以通过调用一次 SWAP_PARTITIONS_BETWEEN_TABLES 来完成这两个任务。
通过将删除和移动操作包装在单个事务中,SWAP_PARTITIONS_BETWEEN_TABLES 可保持交换数据的完整性。如果交换操作中的任何任务失败,整个操作则会失败并回退。
示例
以下示例创建两个已分区表,然后在两者之间交换某些分区。
这两个表的定义相同,且具有不同 year
值的分区。交换 year
= 2008 和 year
= 2009 的分区。这两个表都至少有两行要交换。
-
创建
customer_info
表:=> CREATE TABLE customer_info ( customer_id INT NOT NULL, first_name VARCHAR(25), last_name VARCHAR(35), city VARCHAR(25), year INT NOT NULL) ORDER BY last_name PARTITION BY year;
-
向
customer_info
表中插入数据:INSERT INTO customer_info VALUES (1,'Joe','Smith','Denver',2008), (2,'Bob','Jones','Boston',2008), (3,'Silke','Muller','Frankfurt',2007), (4,'Simone','Bernard','Paris',2014), (5,'Vijay','Kumar','New Delhi',2010); OUTPUT -------- 5 (1 row) => COMMIT;
-
查看表数据:
=> SELECT * FROM customer_info ORDER BY year DESC; customer_id | first_name | last_name | city | year -------------+------------+-----------+-----------+------ 4 | Simone | Bernard | Paris | 2014 5 | Vijay | Kumar | New Delhi | 2010 2 | Bob | Jones | Boston | 2008 1 | Joe | Smith | Denver | 2008 3 | Silke | Muller | Frankfurt | 2007 (5 rows)
-
创建第二个表
member_info
,其具有与customer_info
相同的定义:=> CREATE TABLE member_info LIKE customer_info INCLUDING PROJECTIONS; CREATE TABLE
-
向
member_info
表中插入数据:=> INSERT INTO member_info VALUES (1,'Jane','Doe','Miami',2001), (2,'Mike','Brown','Chicago',2014), (3,'Patrick','OMalley','Dublin',2008), (4,'Ana','Lopez','Madrid',2009), (5,'Mike','Green','New York',2008); OUTPUT -------- 5 (1 row) => COMMIT; COMMIT
-
查看
member_info
表中的数据:=> SELECT * FROM member_info ORDER BY year DESC; customer_id | first_name | last_name | city | year -------------+------------+-----------+----------+------ 2 | Mike | Brown | Chicago | 2014 4 | Ana | Lopez | Madrid | 2009 5 | Mike | Green | New York | 2008 3 | Patrick | OMalley | Dublin | 2008 1 | Jane | Doe | Miami | 2001 (5 rows)
-
要交换分区,请运行 SWAP_PARTITIONS_BETWEEN_TABLES 函数:
=> SELECT SWAP_PARTITIONS_BETWEEN_TABLES('customer_info', 2008, 2009, 'member_info'); SWAP_PARTITIONS_BETWEEN_TABLES ---------------------------------------------------------------------------------------------- 1 partition values from table customer_info and 2 partition values from table member_info are swapped at epoch 1045. (1 row)
-
查询这两个表以确认它们交换了各自的 2008 年和 2009 年记录:
=> SELECT * FROM customer_info ORDER BY year DESC; customer_id | first_name | last_name | city | year -------------+------------+-----------+-----------+------ 4 | Simone | Bernard | Paris | 2014 5 | Vijay | Kumar | New Delhi | 2010 4 | Ana | Lopez | Madrid | 2009 3 | Patrick | OMalley | Dublin | 2008 5 | Mike | Green | New York | 2008 3 | Silke | Muller | Frankfurt | 2007 (6 rows)
=> SELECT * FROM member_info ORDER BY year DESC; customer_id | first_name | last_name | city | year -------------+------------+-----------+---------+------ 2 | Mike | Brown | Chicago | 2014 2 | Bob | Jones | Boston | 2008 1 | Joe | Smith | Denver | 2008 1 | Jane | Doe | Miami | 2001 (4 rows)