Dynamic table partitioning in Oracle

前端 未结 4 1163
灰色年华
灰色年华 2021-02-14 18:24

I\'m in the process of building a database storage for my app consisting on a single table with a huge data volume (hundreds of millions of records). I\'m planning on having an

4条回答
  •  独厮守ぢ
    2021-02-14 18:52

    Here is a sample of something I came up with for creating partitions using SYSDATE and an offset. I had to create replacement parameters with a string concatenated to the SYSDATE:

    COLUMN temp_var new_value partition_name_01;
    SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 3, 'YYYYMMDD') AS temp_var FROM dual;
    COLUMN temp_var new_value partition_date_01;
    SELECT TO_CHAR(SYSDATE - 3, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
    COLUMN temp_var new_value partition_name_02;
    SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 2, 'YYYYMMDD') AS temp_var FROM dual;
    COLUMN temp_var new_value partition_date_02;
    SELECT TO_CHAR(SYSDATE - 2, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
    COLUMN temp_var new_value partition_name_03;
    SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 1, 'YYYYMMDD') AS temp_var FROM dual;
    COLUMN temp_var new_value partition_date_03;
    SELECT TO_CHAR(SYSDATE - 1, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
    COLUMN temp_var new_value partition_name_04;
    SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE, 'YYYYMMDD') AS temp_var FROM dual;
    COLUMN temp_var new_value partition_date_04;
    SELECT TO_CHAR(SYSDATE, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
    CREATE TABLE TABLE_NAME
    (
       SEQ_NO                 NUMBER NOT NULL,
       INSERT_DATE            DATE NOT NULL,
       FIRST_NAME             VARCHAR2 (256 BYTE),
       LAST_NAME              VARCHAR2 (256 BYTE),
       ID_NUM                 NUMBER,
       ID_STATUS              NUMBER
    )
    
    PARTITION BY RANGE
       (INSERT_DATE)
       SUBPARTITION BY LIST
          (ID_STATUS)
          SUBPARTITION TEMPLATE (
             SUBPARTITION SP1 VALUES (0) TABLESPACE &tblspce,
             SUBPARTITION SP2 VALUES (1) TABLESPACE &tblspce,
             SUBPARTITION SP3 VALUES (2) TABLESPACE &tblspce)
    
       (
       PARTITION &partition_name_01
          VALUES LESS THAN
          (TO_DATE ('&partition_date_01',
                       'SYYYY-MM-DD HH24:MI:SS',
                       'NLS_CALENDAR=GREGORIAN')),
    
       PARTITION &partition_name_02
          VALUES LESS THAN
          (TO_DATE ('&partition_date_02',
                       'SYYYY-MM-DD HH24:MI:SS',
                       'NLS_CALENDAR=GREGORIAN')),
    
       PARTITION &partition_name_03
          VALUES LESS THAN
          (TO_DATE ('&partition_date_03',
                       'SYYYY-MM-DD HH24:MI:SS',
                       'NLS_CALENDAR=GREGORIAN')),
    sysdate
    
       PARTITION &partition_name_04
          VALUES LESS THAN
          (TO_DATE ('&partition_date_04',
                       'SYYYY-MM-DD HH24:MI:SS',
                       'NLS_CALENDAR=GREGORIAN')))
    
    ENABLE ROW MOVEMENT;
    

提交回复
热议问题