一、shrink segment两个好处
You use online segment shrink to reclaim fragmented free space below the high water mark in an Oracle Database segment. The benefits of segment shrink are these:
l Compaction of data leads to better cache utilization, which in turn leads to better online transaction processing (OLTP) performance.
l The compacted data requires fewer blocks to be scanned in full table scans, which in turns leads to better decision support system (DSS) performance.
二、shrink操作两个阶段
segment shrink分为两个阶段:
1、 数据重组(compact):通过一系列insert、delete操作,将数据尽量排列在段的前面。在这个过程中需要在表上加Row Exclusive(RX)锁,即只在需要移动的行上加锁。由于涉及到rowid的改变,需要enable row movement。同时要disable基于rowid的trigger。这一过程对业务影响较小。
2、 HWM调整:第二阶段是调整HWM位置,释放空闲数据块。此过程需要在表上加X锁,会造成表上的所有DML语句阻塞。在业务特别繁忙的系统可能造成比较大的影响。
shrink space语句两个阶段都执行;
shrink space compact只执行第一个阶段。
如果系统业务比较繁忙,可以先执行shrink space compact重组数据,然后在业务不忙得时候再执行shrink space降低HWM释放空闲数据块。
三、shrink的操作前提条件
1、Shrink operations can be performed only on segments in locally managed tablespaces with automatic segment space management (ASSM).
2、 shrink必须开启行迁移功能!
Segment shrink requires that rows be moved to new locations. Therefore, you must first enable row movement in the object you want to shrink and disable any rowid-based triggers defined on the object.
alter table table_name enable row movement;
注意:此操作会造成引用表的对象(如存储过程、包、视图等)变为无效。执行完shrink后,最好执行一下utlrp.sql来编译无效的对象。
四、shrink语法:
alter table
compact:重组表,只执行第一阶段,相当于把块中数据打结实了,但会保持high water mark。
cascade:收缩表,降低high water mark,并且相关索引也要收缩一下。
五、哪些segment可以被shrink
Within an ASSM tablespace, all segment types are eligible for online segment shrink except these:
l IOT mapping tables
l Tables with rowid based materialized views
l Tables with function-based indexes