表在同一庫中不同表空間上轉移[轉]

47328983發表於2011-04-07
 今天開發說有一個表空間有300G,想要把這個表空間上的物件清理下,等表空間使用降低時,遷移到本庫的另外一個小的表空間上。然後刪除舊的表空間和其資料檔案,釋放物理磁碟空間。

        因為是在同一個庫上,而且遷移的兩個表空間名字不同,所以不能使用oracle的exp/imp來完成(因為exp/imp傳輸表空間,要求表空間名字相同),也不能使用rman copy datafiles到其他地方,因為這樣不能縮小datafiles的大小。所以採用以下方法處理:表遷移,從一個表空間遷移到另外一個表空間:

一、普通表和索引:

1、轉移表

alter table table_name t move tablespace tablespace_name;

2、轉移索引

alter index index_name rebuild tablespace tablespace_name;

二、含有lob欄位的表和索引:

1、轉移表

alter table table_name t move tablespace tablespace_name;

2、轉移索引

alter index index_name rebuild tablespace tablespace_name;

3、轉移lob欄位

alter table table_name move lob (column_name_01) store as (tablespace tablespace_name); .

.......................................

alter table table_name move lob (column_name_0n) store as (tablespace tablespace_name);

注:表中有多個欄位的要逐個轉移。

三、分割槽表和索引:

1、分割槽表的分割槽要一個分割槽一個分割槽的轉移

alter table table_name move partition partition01_name tablespace tablespace_name;

.......................................

alter table table_name move partition partition0n_name tablespace tablespace_name;

2、分割槽表的本地索引,要一個分割槽一個分割槽的轉移

alter index index_name rebuild partition partition01_name tablespace tablespace_name;

 ........................................

alter index index_name rebuild partition partition0n_name tablespace tablespace_name;

注:分割槽表的本地索引在各個分割槽上的索引名字相同。

3、分割槽表全域性索引

alter index index_name rebuild tablespace tablespace_name;

四、測試過程

建立測試表省略!!!

SQL> col segment_name format a30;

SQL> col tablespace_name format a20;

1、測試普通表

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME TABLESPACE

------------------------------ ----------

 T1                TEST01

IN_T1           TEST01

2 rows selected.

SQL> alter table t1 move tablespace test02;

Table altered.

SQL> alter index in_t1 rebuild tablespace test02;

Index altered.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NA TABLESPACE

 ----------                  ----------

T1                          TEST02

IN_T1                    TEST02

2、測試含lob欄位的表

SQL> create table t2(a integer,b blob) tablespace test01;

Table created.

SQL> create index in_t2 on t2(a) tablespace test01;

Index created.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                                   TABLESPACE

------------------------------                             ----------

T1                                                                  TEST02

IN_T1                                                            TEST02

T2                                                                  TEST01

SYS_IL0000052527C00002$$               TEST01

SYS_LOB0000052527C00002$$           TEST01

IN_T2 TEST01 6 rows selected.

SQL> alter table t2 move tablespace test02;

Table altered.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                                  TABLESPACE

 ------------------------------                           ----------

T1                                                               TEST02

IN_T1                                                         TEST02

T2                                                               TEST02

SYS_IL0000052527C00002$$            TEST01

SYS_LOB0000052527C00002$$        TEST01

IN_T2                                                           TEST01

6 rows selected.

SQL> alter table t2 move lob (b) store as (tablespace test02);

Table altered.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                        TABLESPACE

------------------------------                 ----------

T1                                                     TEST02

IN_T1                                                 TEST02

T2                                                      TEST02

SYS_IL0000052527C00002$$   TEST02

SYS_LOB0000052527C00002$$ TEST02

IN_T2                                                   TEST01

6 rows selected.

SQL> alter index in_t2 rebuild tablespace test02;

Index altered.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                        TABLESPACE

 ------------------------------                  ----------

T1                                                      TEST02

IN_T1                                                TEST02

T2                                                       TEST02

SYS_IL0000052527C00002$$    TEST02

SYS_LOB0000052527C00002$$  TEST02

IN_T2                                                     TEST02

6 rows selected.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                TABLESPACE

 ------------------------------         ----------

T1                                              TEST02

IN_T1                                         TEST02

T2                                               TEST02

SYS_IL0000052527C00002$$ TEST02

SYS_LOB0000052527C00002$$ TEST02

IN_T2                                           TEST02

DBOBJS                                      TEST02

DBOBJS                                      TEST02

DBOBJS_IDX TEST02

DBOBJS_IDX TEST02

DBOBJS_IDX02 TEST02

11 rows selected.

3、測試分割槽表

SQL> CREATE TABLE dbobjs (OBJECT_ID NUMBER NOT NULL, OBJECT_NAME varchar2(128), CREATED DATE NOT NULL) PARTITION BY RANGE (CREATED) (PARTITION dbobjs_06 VALUES LESS THAN (TO_DATE('01/01/2007', 'DD/MM/YYYY')), PARTITION dbobjs_07 VALUES LESS THAN (TO_DATE('01/01/2008', 'DD/MM/YYYY')));

SQL> CREATE INDEX dbobjs_idx ON dbobjs (created) LOCAL (PARTITION dbobjs_06 TABLESPACE test01, PARTITION dbobjs_07 TABLESPACE test01);

SQL> create index dbobjs_idx02 on dbobjs(OBJECT_NAME) tablespace test01;

SQL> alter table dbobjs move partition dbobjs_06 tablespace test02;

Index altered.

SQL> alter table dbobjs move partition dbobjs_07 tablespace test02;

Index altered.

SQL> alter index dbobjs_idx rebuild partition dbobjs_06 tablespace test02;

Index altered.

SQL> alter index dbobjs_idx rebuild partition dbobjs_07 tablespace test02;

Index altered.

SQL> alter index dbobjs_idx02 rebuild tablespace test02;

Index altered.

SQL> select segment_name,tablespace_name from dba_segments where wner='TEST';

SEGMENT_NAME                             TABLESPACE

------------------------------                       ----------

T1                                                         TEST02

IN_T1                                                   TEST02

T2                                                          TEST02

SYS_IL0000052527C00002$$       TEST02

SYS_LOB0000052527C00002$$   TEST02

IN_T2                                                     TEST02

DBOBJS                                                 TEST02

DBOBJS                                                 TEST02

DBOBJS_IDX                                          TEST02

DBOBJS_IDX                                          TEST02

DBOBJS_IDX02                                       TEST02

11 rows selected.

至此,所有的資料物件都可以遷移成功。


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

相關文章