Oracle 12.2 聯機重定義多個分割槽並將其移動到不同的表空間中

eric0435發表於2017-07-18

下面的例子將演示如何聯機重定義多個分割槽並將基於範圍分割槽的表salestable的兩個分割槽移動到新表空間中。原始表jy.salestable的建立如下:

SQL> create table jy.salestable
  2  (s_productid number,
  3  s_saledate date,
  4  s_custid number,
  5  s_totalprice number)
  6  tablespace users
  7  partition by range(s_saledate)
  8  (partition sal10q1 values less than (to_date('01-apr-2010', 'dd-mon-yyyy')),
  9  partition sal10q2 values less than (to_date('01-jul-2010', 'dd-mon-yyyy')),
 10  partition sal10q3 values less than (to_date('01-oct-2010', 'dd-mon-yyyy')),
 11  partition sal10q4 values less than (to_date('01-jan-2011', 'dd-mon-yyyy')));

Table created.

這個例子會將分割槽sal10q1與sal10q2移動到example表空間中。sal10q3與sal10q4分割槽不會被移動。為了移動分割槽表空間example必須存在。這裡已經先建立好了表空間example。對原始表jy.salestable建立一個本地分割槽索引,操作如下:

SQL> create index jy.sales_index on jy.salestable (s_saledate, s_productid, s_custid) local;

Index created.

注意,在12.2中也可以執行alter table ... move partition ... online語句來將分割槽移動到其它表空間中。

聯機重定義操作如下:
1.用要執行聯機重定義操作的使用者登入資料庫

SQL> conn jy/jy@jypdb
Connected.

2.驗證原始表jy.salestable是否可以執行聯機重定義

SQL> begin
  2  dbms_redefinition.can_redef_table(
  3    uname => 'jy',
  4    tname => 'salestable',
  5    options_flag => DBMS_REDEFINITION.CONS_USE_ROWID,
  6    part_name => 'sal10q1, sal10q2');
  7  end;
  8  /

PL/SQL procedure successfully completed.

3.在新表空間example中建立中間表。因為這是對分割槽執行聯機重定義,因此中間表不能是分割槽表。

SQL> create table jy.int_salestb1
  2  (s_productid number,
  3  s_saledate date,
  4  s_custid number,
  5  s_totalprice number)
  6  tablespace example;

Table created.

SQL> create table jy.int_salestb2
  2  (s_productid number,
  3  s_saledate date,
  4  s_custid number,
  5  s_totalprice number)
  6  tablespace example;

Table created.

4.使用rowid方法來執行重定義操作

SQL> begin
  2  dbms_redefinition.start_redef_table(
  3    uname => 'jy',
  4    orig_table => 'salestable',
  5    int_table => 'int_salestb1, int_salestb2',
  6    col_mapping => NULL,
  7    options_flag => DBMS_REDEFINITION.CONS_USE_ROWID,
  8    part_name => 'sal10q1, sal10q2',
  9    continue_after_errors => TRUE);
 10  end;
 11  /

PL/SQL procedure successfully completed.

注意,part_name引數用來指定所有要重定義的分割槽,int_table引數用來指定每個分割槽所對應的中間表,continue_after_errors引數被設定為true,因此重定義操作即使當某個特定分割槽遇到錯誤也會繼續執行。

5.在中間表上建立任何本地索引

SQL> create index jy.int_sales1_index on jy.int_salestb1
  2  (s_saledate, s_productid, s_custid)
  3  tablespace example;

Index created.

SQL> create index jy.int_sales2_index on jy.int_salestb2
  2  (s_saledate, s_productid, s_custid)
  3  tablespace example;

Index created.

6.可選操作同步中間表

SQL> begin
  2  dbms_redefinition.sync_interim_table(
  3    uname => 'jy',
  4    orig_table => 'salestable',
  5    int_table => 'int_salestb1, int_salestb2',
  6    part_name => 'sal10q1, sal10q2',
  7    continue_after_errors => TRUE);
  8  end;
  9  /

PL/SQL procedure successfully completed.

7.完成重定義操作

SQL> begin
  2  dbms_redefinition.finish_redef_table(
  3    uname => 'jy',
  4    orig_table => 'salestable',
  5    int_table => 'int_salestb1, int_salestb2',
  6    part_name => 'sal10q1, sal10q2',
  7    continue_after_errors => TRUE);
  8  end;
  9  /

PL/SQL procedure successfully completed.

8.可選操作,查詢dba_redefinition_status檢視來確保對每個分割槽都重定義操作成功

SQL> select base_table_owner, base_table_name, operation, status from dba_redefinition_status;

no rows selected

如果有任何分割槽重定義失敗,檢視dba_redefinition_errors會顯示出錯誤原因,修正故障重新執行聯機重定義操作。

下面的查詢顯示了表jy.salestable有兩個分割槽已經移動到了新的表空間example中了

SQL> select partition_name, tablespace_name from dba_tab_partitions where table_name = 'SALESTABLE' and table_owner='JY';
PARTITION_NAME                                                                   TABLESPACE_NAME
-------------------------------------------------------------------------------- ------------------------------
SAL10Q1                                                                          EXAMPLE
SAL10Q2                                                                          EXAMPLE
SAL10Q3                                                                          USERS
SAL10Q4                                                                          USERS

到此聯機重定義操作完成

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-2142202/,如需轉載,請註明出處,否則將追究法律責任。

相關文章