move 与shrink区别

shrink和move表空间的区别
alter table xxx shrink space 和 alter table xxx move

move命令是将数据从原来的段移到目标段的命令。

[move命令]


SQL> alter table emp move;

ORA-01652: unable to extend temp segment by 8 in tablespace AUTOSEG_TST

AUTOSEG_TST表空间没有足够的空闲空间。

SQL> select f.tablespace_name,d.file_name,f.bytes/1024 KBytes,f.blocks,d.autoextensible
from dba_free_space f,dba_data_files d,dba_tables t
where f.tablespace_name = d.tablespace_name
and f.tablespace_name = t.tablespace_name
and t.owner='SCOTT' and t.table_name='EMP';

TABLESPACE_NAME FILE_NAME KBYTES BLOCKS AUTOEXT
--------------- ---------------------------------------- ------- ------- -------
AUTOSEG_TST /export/home/ora10g/oradata/AUTOSEG1.DBF 64 8 NO



[shrink命令]
即使对象所在表空间几乎没有空闲空间,shrink命令也能执行。

SQL> alter table emp shrink space;

Table altered.



# 差异点4. 不需要重建index

[move命令]
table具有主键index的时候,如果使用move命令就必须重建index。


SQL> alter table dept move;

Table altered.


SQL> select owner,index_name,status from dba_indexes where table_owner='SCOTT'
and table_name='DEPT';

OWNER INDEX_NAME STATUS
----- ---------- --------
SCOTT PK_DEPT UNUSABLE ←(不能使用index)


SQL> select /*+ index(dept pk_dept) */ * from dept where rownum=1;

ORA-01502: index 'SCOTT.PK_DEPT' or partition of such index is in unusable state



执行move命令之后无法使用index,所以无法利用index查找。要解决这个问题让index恢复可以使用的状态,必须对index进行rebuild。


SQL> alter index pk_dept rebuild;

Index altered.


SQL> select /*+ index(dept pk_dept) */ * from dept where rownum=1;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK



[shirnk命令]
执行shrink命令的时候就不需要rebuild index。


SQL> alter table dept shrink space;

Table altered.


SQL> select /*+ index(dept pk_dept) */ * from dept where rownum=1;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK


SQL> select owner,index_name,status from dba_indexes where table_owner='SCOTT'
and table_name='DEPT';

OWNER INDEX_NAME STATUS
----- ---------- --------
SCOTT PK_DEPT VALID ←(index可以使用)



# 差异点5. cascade选项
前面用shrink命令让dept表缩小,cascade命令会让相关的pk_dept索引也同时缩小。

[shrink命令 -没有选项-]

--dept表shrink之前
SQL> select owner,segment_name,bytes,blocks,extents from dba_segments
where segment_name = 'PK_DEPT';

OWNER SEGMENT_NAME BYTES BLOCKS EXTENTS
----- ------------ ---------- ---------- ----------
SCOTT PK_DEPT 18874368 2304 33


SQL> alter table dept shrink space;

Table altered.



--dept表shrink之后
SQL> select owner,segment_name,bytes,blocks,extents from dba_segments
where segment_name = 'PK_DEPT';

OWNER SEGMENT_NAME BYTES BLOCKS EXTENTS
----- ------------ ---------- ---------- ----------
SCOTT PK_DEPT 18874368 2304 33



大家可以看到相关的pk_dept索引缩小了。

结论

我们已经针对Oracle 10g新功能shrink命令进行两次检验,主要都是介绍shrink命令的优点,下面追加相关限制事项。
1.无法解除行迁移
2.必须是local管理的自动段管理
3.不可以是下面的段:
-集群(cluster)、集群化表
-包含long列的物件
-LOB段
-包含函数索引(function index)的表

在我们的检验环境下,move命令的执行时间很短。所以,还是根据情况选择不同命令使用会比较好。这次就介绍到这里。


oracle 10g 关于收缩shrink
1、在10G 以前收缩表空间是用:

  1.exp/imp
  2.alter table xxx move 

 

2、10G后用shrink表空间

表空间中支持自动段空间管理 (ASSM), 否则会报 ORA-10635: Invalid segment or tablespace type

适用表,索引,大对象,IOT,物化视图



必须开启行迁移功能。

alter table tbname enable row movement shrink
保持HWM
alter table tbname shrink space compact;
回缩表与HWM
alter table tbname shrink space;
回缩表与相关索引
alter table tbname shrink space cascade;
回缩索引
alter index idxname shrink space;
  
相关限制:
1)、cluster中的表。
2)、有long类型的表。
3)、有on_commit物化视图的表。
4)、有基于rowid物化视图的表。
5)、大对象(LOB)索引。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章