在資料表空間之間移動資料對象
一、基礎指令碼
在Oracle資料庫的資料表空間之間移動對象會涉及多種類型的資料對象
移動表:
- alter table t_a move tablespace tbs_a;
移動索引:
- alter index i_a rebuild tablespace tbs_a;
移動LOBSEGMENT:
- alter table t_a move lob(colume_a) store as (tablespace tbs_a);
根據實際檢驗LOBINDEX會隨著相應的LOBSEGMENT自動移動
移動表的同時移動LOBSEGMENT:
- alter table t_a move tablespace tbs_a lob(colume_a) store as (tablespace tbs_a);
如果表中有欄位為LONG類型的,那麼該表不能按照上面的方法移動,只能使用EXP/IMP來移動了
二、批量產生移動指令碼
如果有大量的對象需要移動,一行一行寫指令碼就很不現實,可以使用下面的指令碼來產生移動的指令碼
--產生LOBSEGMENT的移動語句,目的是把使用者USR_A的LOBSEGMENT對象從資料表空間TBS_OLD移動到TBS_NEW,所產生的語句執行後,相應的LOBINDEX對象也會跟著移動到TBS_NEW
- select 'alter table ' || owner || '.' || table_name || ' move LOB(' || column_name || ') store as (tablespace TBS_NEW);' from dba_lobs where owner = 'USR_A' and tablespace_name='TBS_OLD';
--如要把LOB段所在的表也隨同LOB段移動到新的資料表空間,可使用以下語句來產生指令碼
- select 'alter table ' || owner || '.' || table_name || ' move tablespace TBS_NEW LOB(' || column_name || ') store as (tablespace TBS_NEW);' from dba_lobs where owner = 'USR_A' and tablespace_name='TBS_OLD';
--產生移動USR_A的表和索引的指令碼,排序是為了移動表的指令碼在前,移動索引的指令碼在後,按序執行即可;否則先移索引後移表會導致索引失效
- select 'alter ' || segment_type || ' USR_A.' || segment_name || ' ' || decode(segment_type,'TABLE','move','INDEX','rebuild') || ' ' || 'tablespace TBS_NEW;' from dba_segments where owner = 'USR_A' and tablespace_name='TBS_OLD' and segment_type in ('TABLE','INDEX') order by segment_type desc;
--可以用以下語句檢查dba_indexes資料字典表中的失效的索引資訊
- select * from dba_indexes where status = 'UNUSABLE';
三、一些可能會用到的查詢語句
另外還有一些可能會用到的查詢資料表空間中資料對象資訊的語句
--統計使用者USR_A的資料對象數
- select count(*) from dba_segments where owner='USR_A';
--查看使用者USR_A的資料對象所分布的資料表空間名
- select distinct tablespace_name from dba_segments where owner='USR_A';
--查看使用者USR_A的LOB段和LOB索引對象
- select * from dba_segments where owner='USR_A' and segment_type in ('LOBSEGMENT','LOBINDEX');
--分資料表空間、段類型統計使用者USR_A的資料對象數
- select tablespace_name,segment_type,count(*) from dba_segments where owner='USR_A';
- group by tablespace_name,segment_type;
- order by tablespace_name,segment_type;