回收段空间

经过一段时间,update和delete操作会产生一些空闲的空间,但是又不足以重用,这种的空闲空间就是碎片空闲空间。消除碎片的最好的一个方法就是使用online segment shrink。这个过程收缩高水位线以下的碎片空闲空间。在收缩完后,高水位线移动,可以使用segment advisor来看段是否会在这种收缩中获益,只有assm的本地管理表空间才能做这个操作。
segment advisor 产生下面的建议:
1如果检测到对象有大量的空闲空间,他建议在线shrink。如果是没有使用assm,那么建议使用在线重定义
2如果检测到一个表有很多的行链接超过了一定的比例,那么它会记录下来。

automatic segment advisor
这个东西有scheduler job来启动,默认的维护窗口在scheduler中指定。默认的定义如下:
周一到周五的晚上,在10点到6点
周六的12点到周一的12点。
automatic segmetn advisor不会分析每一个数据库对象,他是检查数据库统计信息,采样段数据,然后选择下面对象来分析:
1超过阀值的表空间
2有最多活动的段
3有最高增长率的段
查看segment advisor结果
会有几个类型的结果:1建议2发现3动作4对象,可以查看dba_advisor_*视图,调用dbms_space.asa_recommendations
在查看之前,线却东任务是完成的
select task_name, status from dba_advisor_tasks where owner = 'STEVE' and advisor_name = 'Segment Advisor';
使用下面的查询来看结果
select af.task_name, ao.attr2 segname, ao.attr3 partition, ao.type, af.message 
  from dba_advisor_findings af, dba_advisor_objects ao
  where ao.task_id = af.task_id
  and ao.object_id = af.object_id
  and ao.owner = 'STEVE';

Shrinking Database Segments Online
dml操作或是查询可能会受影响。并发的dml操作在shrink操作的最后被阻塞。索引在这个期间被维护,在操作之后仍然是可用的。segment shrink要求行被移动到新的位置,因此,你必须要启用行移动并且禁用rowid相关的触发器。

You can shrink space in a table, index-organized table, index, partition, subpartition, materialized view, or materialized view log. You do this using ALTER TABLE, ALTER INDEX, ALTER MATERIALIZED VIEW, or ALTER MATERIALIZED VIEW LOG statement with the SHRINK SPACE clause

有两个参数可以控制shrink操作是怎么进行的:
The COMPACT clause lets you divide the shrink segment operation into two phases. When you specify COMPACT, Oracle Database defragments the segment space and compacts the table rows but postpones the resetting of the high water mark and the deallocation of the space until a future time. This option is useful if you have long-running queries that might span the operation and attempt to read from blocks that have been reclaimed. The defragmentation and compaction results are saved to disk, so the data movement does not have to be redone during the second phase. You can reissue the SHRINK SPACE clause without the COMPACT clause during off-peak hours to complete the second phase.
 
The CASCADE clause extends the segment shrink operation to all dependent segments of the object. For example, if you specify CASCADE when shrinking a table segment, all indexes of the table will also be shrunk. (You need not specify CASCADE to shrink the partitions of a partitioned table.) To see a list of dependent segments of a given object, you can run the OBJECT_DEPENDENT_SEGMENTS procedure of the DBMS_SPACE package

查看对象依赖的段,可以用上面提供的方法来看了。
Examples
 
Shrink a table and all of its dependent segments (including LOB segments):
 ALTER TABLE employees SHRINK SPACE CASCADE;

Shrink a LOB segment only:
 ALTER TABLE employees MODIFY LOB (perf_review) (SHRINK SPACE);
 
Shrink a single partition of a partitioned table:
 ALTER TABLE customers MODIFY PARTITION cust_P1 SHRINK SPACE;
 
Shrink an IOT index segment and the overflow segment:
 ALTER TABLE cities SHRINK SPACE CASCADE;
 
Shrink an IOT overflow segment only:
 ALTER TABLE cities OVERFLOW SHRINK SPACE;

回收未使用的空间
在回收前,运行dbms_space包的unused_space存储过程,可以查看高水位线的位置和未使用的空间。
The following statements deallocate unused space in a segment (table, index or cluster):
 ALTER TABLE table DEALLOCATE UNUSED KEEP integer;
ALTER INDEX index DEALLOCATE UNUSED KEEP integer;
ALTER CLUSTER cluster DEALLOCATE UNUSED KEEP integer;
keep语句是可选的,让你保留多少的空间。可以检查dba_free_space来看回收的空间释放情况。

你可能感兴趣的:(segment,shrink)