在表之间交换/移动分区

时间:2016-12-09 12:29:37

标签: oracle partitioning

我将此表作为表(100M行):

create table prova_log(
       id_dispositive       number,
       type                 number,
       date_verification    date,
       status               number
)
partition by range (date_verification) interval (NUMTODSINTERVAL(3,'DAY'))
subpartition by list (type)  
subpartition TEMPLATE (
    SUBPARTITION type1 VALUES (1),
    SUBPARTITION type2 VALUES (2),
    SUBPARTITION type3 VALUES (3),
    SUBPARTITION type4 VALUES (4)
)                          
(
   partition p0816 values less than (to_date('01/09/2016','dd/mm/yyyy'))
);

我想用旧值进行某种备份,所以我创建了这个(0行):

create table prova_log_old (
       id_dispositive       number,
       type                 number,
       date_verification    date,
       status               number
)
partition by range (date_verification) interval (NUMTODSINTERVAL(3,'DAY'))
subpartition by list (type)  
subpartition TEMPLATE (
    SUBPARTITION type1 VALUES (1),
    SUBPARTITION type2 VALUES (2),
    SUBPARTITION type3 VALUES (3),
    SUBPARTITION type4 VALUES (4)
)                          
(
   partition p_old values less than (to_date('01/09/2016','dd/mm/yyyy'))
);

所以我想将旧分区(15天以上)移动/复制/交换(15天以上)转移到 prova_log_old

为此,我创造了这份工作:

PROCEDURE move_data_from_huge_table
   IS
      -- This will move all data after 'vcountdaystokeepdata' days
      vcountdaystokeepdata        NUMBER := 15;
      vcountdatainsidepartition   NUMBER := 0;
   BEGIN
      FOR item IN
         (SELECT *
            FROM (SELECT partition_name,
                         TO_DATE
                            (TRIM
                                ('''' FROM REGEXP_SUBSTR
                                             (EXTRACTVALUE
                                                 (DBMS_XMLGEN.getxmltype
                                                     (   'select high_value from all_tab_partitions where table_name='''
                                                      || table_name
                                                      || ''' and table_owner = '''
                                                      || table_owner
                                                      || ''' and partition_name = '''
                                                      || partition_name
                                                      || ''''
                                                     ),
                                                  '//text()'
                                                 ),
                                              '''.*?'''
                                             )
                                ),
                             'syyyy-mm-dd hh24:mi:ss'
                            ) high_value
                    FROM all_tab_partitions
                   WHERE table_name = 'PROVA_LOG')
           WHERE high_value < SYSDATE - vcountdaystokeepdata)
      LOOP                 
         EXECUTE IMMEDIATE    'alter table PROVA_LOG EXCHANGE PARTITION '
                           || item.partition_name
                           || ' with table PROVA_LOG_OLD';

         EXECUTE IMMEDIATE    'select count(*) from PROVA_LOG partition ('
                           || item.partition_name
                           || ')'
                      INTO vcountdatainsidepartition;

         IF vcountdatainsidepartition = 0
         THEN
            EXECUTE IMMEDIATE    'ALTER TABLE PROVA_LOG DROP PARTITION '
                              || item.partition_name
                              || '';
         END IF;

      END LOOP;
   END;

但是当我运行程序时,我得到了

  

ORA-14292分区类型的表必须与复合分区的子分区类型匹配

我认为我的备份表中必须有一个与我的主分区表同名的分区吗?

我该如何做到这一点?

我尝试add分区到我的备份表但没有成功。重要的是要提到所有分区的名称都是随机的(oracle生成它)。

2 个答案:

答案 0 :(得分:2)

I still don't understand why you want to move the partitions, anyway I have a solution.

First of all, you can address a partition either like

SELECT COUNT(*) FROM PROVA_LOG PARTITION (SYS_P7138);

or you can do it as

SELECT COUNT(*) FROM PROVA_LOG PARTITION FOR (TO_DATE('2016-10-01', 'YYYY-MM-DD'));

or if you prefer DATE literals

SELECT COUNT(*) FROM PROVA_LOG PARTITION FOR (DATE '2016-10-01');

An automatic solution for you problem could be this one:

DECLARE

    CURSOR TabPartitions IS
    SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE
    FROM USER_TAB_PARTITIONS 
    WHERE TABLE_NAME = 'PROVA_LOG'
    ORDER BY 1,2;

    ts DATE;

BEGIN
    FOR aPart IN TabPartitions LOOP
        EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT ts;
        IF ts <> DATE '2016-09-10' AND ts < SYSDATE - 15 THEN
            --EXECUTE IMMEDIATE 'INSERT INTO PROVA_LOG_OLD SELECT * FROM PROVA_LOG PARTITION FOR (DATE '''||TO_CHAR(ts, 'yyyy-mm-dd')||''')'; 
            --EXECUTE IMMEDIATE 'ALTER TABLE PROVA_LOG DROP PARTITION FOR (DATE '''||TO_CHAR(ts, 'yyyy-mm-dd')||''') UPDATE GLOBAL INDEXES';
            EXECUTE IMMEDIATE 'ALTER TABLE PROVA_LOG EXCHANGE PARTITION FOR (DATE '''||TO_CHAR(ts, 'yyyy-mm-dd')||''') WITH TABLE PROVA_LOG_OLD INCLUDING INDEXES';
        END IF;
    END LOOP;

END;

Your backup table must be like this:

CREATE TABLE prova_log_old (
       id_dispositive       NUMBER,
       TYPE                 NUMBER,
       date_verification    DATE,
       status               NUMBER
)
PARTITION BY LIST (TYPE) 
(
    PARTITION type1 VALUES (1),
    PARTITION type2 VALUES (2),
    PARTITION type3 VALUES (3),
    PARTITION type4 VALUES (4)
);

or no partitioning at all

CREATE TABLE prova_log_old (
       id_dispositive       NUMBER,
       TYPE                 NUMBER,
       date_verification    DATE,
       status               NUMBER
);

答案 1 :(得分:1)

You are doing it wrong. You exchange partition with all partitioned table not with it partition, just look one more at your code

 EXECUTE IMMEDIATE    'alter table PROVA_LOG EXCHANGE PARTITION '
                           || item.partition_name
                           || ' with table PROVA_LOG_OLD';

In case of exchange partition you should do as follows

  1. Create empty table without partition with same structure like PROVA_LOG but not partitioned.

  2. Exchange partition in production table with new_table

  3. Exchange partition in hist table with new_table