SQL使用EXCHANGE PARTITION创建备份表

时间:2016-10-04 12:33:39

标签: sql oracle backup partitioning partition

我想使用a1_crm_query对表格EXCHANGE PARTITION进行备份。此表包含具有不同状态的行,例如' ERROR',' NEW'或' DONE'如果有另一个表格({{ 1}})状态' ERROR'和' DONE'但是我的第一张桌子(a1_crm_query_LOG)只有' NEW'。

首先,我创建了我的表:

a1_crm_query

然后我用分区创建第二个表。

 CREATE TABLE ma_user.a1_crm_query (
       ID NUMBER PRIMARY KEY,
       DATA VARCHAR2(200) 

    );

然后将值插入CREATE TABLE ma_user.a1_crm_query_LOG ( ID NUMBER PRIMARY KEY, DATA VARCHAR2(200) ) PARTITION BY LIST (DATA) ( PARTITION DONE_STATUS VALUES ('DONE'), PARTITION ERROR_STATUS VALUES ('ERROR') ) ;

a1_crm_query

现在我想创建一个日常流程,该流程应该使用' DONE'和'错误'在表格INSERT INTO ma_user.a1_crm_query SELECT 1 , CAST('NEW' AS VARCHAR2(200)) FROM dual; INSERT INTO ma_user.a1_crm_query SELECT 2 , CAST('DONE' AS VARCHAR2(200)) FROM dual; INSERT INTO ma_user.a1_crm_query SELECT 3, CAST('ERROR' AS VARCHAR2(200)) FROM dual; 中,a1_crm_query_LOG应该只有' NEW'。

我尝试使用a1_crm_query

exchange partition

但在此 ALTER TABLE ma_user.a1_crm_query_LOG EXCHANGE PARTITION ERROR_STATUS WITH TABLE ma_user.a1_crm_query WITHOUT VALIDATION; ALTER TABLE ma_user.a1_crm_query_LOG EXCHANGE PARTITION DONE_STATUS WITH TABLE ma_user.a1_crm_query WITHOUT VALIDATION; 分区包含具有所有状态的所有行之后。

1 个答案:

答案 0 :(得分:1)

您可能希望创建两个临时表(temp_error,temp_done),其中包含从a1_crm_query表中复制的ERROR和DONE数据,然后使用表temp_error和temp_done交换分区。

您遇到的问题是WITHOUT VALIDATION。从本质上讲,您告诉Oracle我已经验证了正在交换的数据,因此Oracle不会为您验证它。

请参阅this link

更新:方法1 所以这就是我要做的。

CREATE TABLE TEMP_ERROR
(       ID NUMBER PRIMARY KEY,
        DATA VARCHAR2(200) );

CREATE TABLE TEMP_DONE 
(
  ID NUMBER PRIMARY KEY,
  DATA VARCHAR2(200) );

insert into TEMP_ERROR
select * from a1_crm_query
where data = 'ERROR';

insert into TEMP_DONE
select * from a1_crm_query
where data = 'DONE';

ALTER TABLE a1_crm_query_LOG EXCHANGE PARTITION ERROR_STATUS WITH TABLE TEMP_ERROR WITHOUT VALIDATION;

ALTER TABLE a1_crm_query_LOG EXCHANGE PARTITION DONE_STATUS WITH TABLE TEMP_DONE WITHOUT VALIDATION;

truncate table temp_error;
truncate table temp_done

更新2:方法2 如果您也可以对a1_crm_query进行分区,那么这种方法可能最适合您。您将需要一个临时表。此方法不需要删除或截断。

CREATE TABLE a1_crm_query (
   ID NUMBER PRIMARY KEY,
   DATA VARCHAR2(200) 

)
    PARTITION BY LIST (DATA) (
   PARTITION DONE_STATUS VALUES ('DONE'),       
   PARTITION ERROR_STATUS VALUES ('ERROR'),
   PARTITION OTHER_STATUS VALUES (DEFAULT)
   ) ;

CREATE TABLE a1_crm_query_LOG (
   ID NUMBER PRIMARY KEY,
   DATA VARCHAR2(200)
)
PARTITION BY LIST (DATA) (
   PARTITION DONE_STATUS VALUES ('DONE'),       
   PARTITION ERROR_STATUS VALUES ('ERROR')
   ) ;


INSERT INTO a1_crm_query SELECT 1 , 'NEW' FROM dual;
INSERT INTO a1_crm_query SELECT 2 , 'DONE'  FROM dual;
INSERT INTO a1_crm_query SELECT 3, 'ERROR' FROM dual; 
commit;       

CREATE TABLE interim
(       ID NUMBER PRIMARY KEY,
       DATA VARCHAR2(200) );



ALTER TABLE a1_crm_query EXCHANGE PARTITION ERROR_STATUS WITH TABLE INTERIM WITHOUT VALIDATION;
ALTER TABLE a1_crm_query_LOG EXCHANGE PARTITION ERROR_STATUS WITH TABLE INTERIM  WITHOUT VALIDATION;

ALTER TABLE a1_crm_query EXCHANGE PARTITION DONE_STATUS WITH TABLE INTERIM WITHOUT VALIDATION;
ALTER TABLE a1_crm_query_LOG EXCHANGE PARTITION DONE_STATUS WITH TABLE INTERIM WITHOUT VALIDATION;

然后必须在a1_crm_query

上重建索引
ALTER INDEX <index name> REBUILD;

select * from a1_crm_query;
select * from interim;
select * from a1_crm_query_LOG partition(ERROR_STATUS);
select * from a1_crm_query_LOG partition(done_STATUS)

另见this link