12 March, 2019

Partitioning -- 13c : Merging Partitions

The reverse of SPLITting a Partition is to MERGE two adjacent partitions.

I reverse the SPLIT that I did in the previous blog post.

SQL> l
  1  select partition_name, tablespace_name, high_value
  2  from user_tab_partitions
  3  where table_name = 'SALES_DATA'
  4* order by partition_position
SQL> /

PARTITION_NAME                 TABLESPACE_NAME                HIGH_VALUE
------------------------------ ------------------------------ --------------------------
P_2016                         ARCHIVE_SALES_DATA             TO_DATE(' 2017-01-01 00:00
P_2017                         TBS_YEAR_2017                  TO_DATE(' 2018-01-01 00:00
P_2018                         TBS_YEAR_2018                  TO_DATE(' 2019-01-01 00:00
P_2019_H1                      TBS_YEAR_2019                  TO_DATE(' 2019-07-01 00:00
P_2019_H2                      TBS_YEAR_2019                  TO_DATE(' 2020-01-01 00:00
P_2020                         TBS_YEAR_2020                  TO_DATE(' 2021-01-01 00:00
P_MAXVALUE                     USERS                          MAXVALUE

7 rows selected.

SQL> 
SQL> alter table sales_data                                                  
  2  merge partitions P_2019_H1, P_2019_H2
  3  into partition P_2019
  4  update indexes
  5  /

Table altered.

SQL> 
SQL> select partition_name, tablespace_name, high_value
  2  from user_tab_partitions
  3  where table_name = 'SALES_DATA'
  4  order by partition_position
  5  /

PARTITION_NAME                 TABLESPACE_NAME                HIGH_VALUE
------------------------------ ------------------------------ --------------------------
P_2016                         ARCHIVE_SALES_DATA             TO_DATE(' 2017-01-01 00:00
P_2017                         TBS_YEAR_2017                  TO_DATE(' 2018-01-01 00:00
P_2018                         TBS_YEAR_2018                  TO_DATE(' 2019-01-01 00:00
P_2019                         HEMANT                         TO_DATE(' 2020-01-01 00:00
P_2020                         TBS_YEAR_2020                  TO_DATE(' 2021-01-01 00:00
P_MAXVALUE                     USERS                          MAXVALUE

6 rows selected.

SQL> 


But, we find that the new Partition was created in the default "HEMANT"  tablespace !  So, we have to be careful about specifying target tablespace(s).

Let me reverse the action and try again.

SQL> alter table sales_data
  2  split partition P_2019 at (to_date('01-JUL-2019','DD-MON-YYYY'))
  3  into (partition P_2019_H1 tablespace TBS_YEAR_2019, partition P_2019_H2 tablespace TBS_YEAR_2019)
  4  update indexes
  5  /

Table altered.

SQL> 
SQL> select partition_name, tablespace_name, high_value
  2  from user_tab_partitions
  3  where table_name = 'SALES_DATA'
  4  order by partition_position
  5  /

PARTITION_NAME                 TABLESPACE_NAME                HIGH_VALUE
------------------------------ ------------------------------ --------------------------
P_2016                         ARCHIVE_SALES_DATA             TO_DATE(' 2017-01-01 00:00
P_2017                         TBS_YEAR_2017                  TO_DATE(' 2018-01-01 00:00
P_2018                         TBS_YEAR_2018                  TO_DATE(' 2019-01-01 00:00
P_2019_H1                      TBS_YEAR_2019                  TO_DATE(' 2019-07-01 00:00
P_2019_H2                      TBS_YEAR_2019                  TO_DATE(' 2020-01-01 00:00
P_2020                         TBS_YEAR_2020                  TO_DATE(' 2021-01-01 00:00
P_MAXVALUE                     USERS                          MAXVALUE

7 rows selected.

SQL> alter table sales_data
  2  merge partitions P_2019_H1, P_2019_H2
  3  into partition P_2019 tablespace TBS_YEAR_2019
  4  update indexes
  5  /

Table altered.

SQL> select partition_name, tablespace_name, high_value
  2  from user_tab_partitions
  3  where table_name = 'SALES_DATA'
  4  order by partition_position
  5  /

PARTITION_NAME                 TABLESPACE_NAME                HIGH_VALUE
------------------------------ ------------------------------ --------------------------
P_2016                         ARCHIVE_SALES_DATA             TO_DATE(' 2017-01-01 00:00
P_2017                         TBS_YEAR_2017                  TO_DATE(' 2018-01-01 00:00
P_2018                         TBS_YEAR_2018                  TO_DATE(' 2019-01-01 00:00
P_2019                         TBS_YEAR_2019                  TO_DATE(' 2020-01-01 00:00
P_2020                         TBS_YEAR_2020                  TO_DATE(' 2021-01-01 00:00
P_MAXVALUE                     USERS                          MAXVALUE

6 rows selected.

SQL> 


So, when running Table Partition DDL, be careful about the intended and actual target Tablespace(s).  (What about Index Partitions ?  The UPDATE INDEXES clause can specify target tablespaces for each Index Partition of each Index as well ... something like : (this is from the documentation on the ALTER TABLE command)
UPDATE INDEXES (cost_ix (PARTITION c_p1 tablespace tbs_02, 
                           PARTITION c_p2 tablespace tbs_03))

No comments: