NOTE:
We have many options to reclaim the space for LOB Segment . But we need to identify the Application code about the Command what they are using exactly. If Code uses Delete Command then DB level we cannot see any improvement for Block or Segment level free space.
So check what is the command used by Application code and confirm with this Steps.
Refer:
How to Shrink a Securefile LOB Using Online Redefinition (DBMS_REDEFINITION)? (Doc ID 1394613.1)
How To Reclaim Wasted Space on The Segment (Table, Index and LOB) and Tablespace Levels (Doc ID 1682748.1)
Why is no space released after an ALTER TABLE ... SHRINK? (Doc ID 820043.1)
Primary Note:Overview of Online Redefinition of Tables (DBMS_REDEFINITION) (Doc ID 1357825.1)
#########sample 2
SEGMENT SHRINK and Details. (Doc ID 242090.1) To BottomTo Bottom
SEGMENT SHRINK
SEGMENT SHRINK and Details. (Doc ID 242090.1) To BottomTo Bottom
* ***
PURPOSE
========
In Oracle10g and onwards, we have the option to shrink a segment, which will help DBAs to manage the space in better way.
This feature also help for better performance for query.
SCOPE & APPLICATION
===================
Information on new Oracle10g feature.
Oracle 10g Segment shrink
=========================
Mandatory
=========
Init.ora parameter 'Compatible' must be >=10.0
Shrink operations can be performed only on segments in locally managed
tablespaces with automatic segment space management (ASSM).
How it works
============
1. Enable row movement for the table.
SQL> ALTER TABLE user_name.table_name ENABLE ROW MOVEMENT;
2. Shrink table but don't want to shrink HWM (High Water Mark).
SQL> ALTER TABLE user_name.table_name SHRINK SPACE COMPACT;
3. Shrink table and HWM too.
SQL> ALTER TABLE user_name.table_name SHRINK SPACE;
4. Shrink table and all dependent index too.
SQL> ALTER TABLE user_name.table_name SHRINK SPACE CASCADE;
5. Shrink table under MView.
SQL> ALTER TABLE <table name> SHRINK SPACE;
6. Shrink Index only.
SQL> ALTER INDEX <index nam> SHRINK SPACE;
7.. alter table TABLE_NAME disable ROW MOVEMENT;
Restrictions on the shrink_clause, 10gR1
========================================
1. You cannot specify this clause for a cluster, a clustered table, or any
object with a LONG column.
2. Segment shrink is not supported for LOB segments even if CASCADE is
specified.
3. Segment shrink is not supported for tables with function-based indexes.
4. This clause does not shrink mapping tables or overflow segments of
index-organized tables, even if you specify CASCADE.
5. You cannot shrink a table that is the primary table of an ON COMMIT
materialized view. Rowid materialized views must be rebuilt after the
shrink operation.
6. Table with a domain index is not supported.
Restrictions on the shrink_clause, 10gR2
========================================
1. You cannot specify this clause for a cluster, a clustered table, or any
object with a LONG column.
2. Segment shrink is not supported for tables with function-based indexes or
bitmap join indexes.
3. This clause does not shrink mapping tables of index-organized tables,
even if you specify CASCADE.
4. You cannot specify this clause for a compressed table.
5. You cannot shrink a table that is the primary table of an ON COMMIT
materialized view. Rowid materialized views must be rebuilt after the
shrink operation.
6. Table with a domain index is not supported.
Restrictions on the shrink_clause, 11gR1
========================================
1. You cannot combine this clause with any other clauses in the same ALTER TABLE
statement.
2. You cannot specify this clause for a cluster, a clustered table, or any
object with a LONG column.
3. Segment shrink is not supported for tables with function-based indexes or
bitmap join indexes.
4. This clause does not shrink mapping tables of index-organized tables, even if
you specify CASCADE.
5. You cannot specify this clause for a compressed table.
6. You cannot shrink a table that is the primary table of an ON COMMIT
materialized view. Rowid materialized views must be rebuilt after the shrink
operation.
Restrictions on the shrink_clause, 11gR2
========================================
1. You cannot combine this clause with any other clauses in the same ALTER TABLE statement.
2. You cannot specify this clause for a cluster, a clustered table, or any object with a LONG column.
3. Segment shrink is not supported for tables with function-based indexes, domain indexes, or bitmap join indexes.
4. This clause does not shrink mapping tables of index-organized tables, even if you specify CASCADE.
5. You cannot specify this clause for a compressed table.
6. You cannot shrink a table that is the primary table of an ON COMMIT materialized view. Rowid materialized views must be rebuilt after the shrink operation.
Restrictions on the shrink_clause, 12cR1
========================================
1. You cannot combine this clause with any other clauses in the same ALTER TABLE statement.
2. You cannot specify this clause for a cluster, a clustered table, or any object with a LONG column.
3. Segment shrink is not supported for tables with function-based indexes, domain indexes, or bitmap join indexes.
4. This clause does not shrink mapping tables of index-organized tables, even if you specify CASCADE.
5. You can specify this clause for a table with Advanced Row Compression enabled (ROW STORE COMPRESS ADVANCED). You cannot specify this clause for a table with any other type of table compression enabled.
6. You cannot shrink a table that is the primary table of an ON COMMIT materialized view. Rowid materialized views must be rebuilt after the shrink operation.
Restrictions on the shrink_clause, 12cR2
========================================
1. You cannot combine this clause with any other clauses in the same ALTER TABLE statement.
2. You cannot specify this clause for a cluster, a clustered table, or any object with a LONG column.
3. Segment shrink is not supported for tables with function-based indexes, domain indexes, or bitmap join indexes.
4. This clause does not shrink mapping tables of index-organized tables, even if you specify CASCADE.
5. You can specify this clause for a table with Advanced Row Compression enabled (ROW STORE COMPRESS ADVANCED). You cannot specify this clause for a table with any other type of table compression enabled.
6. You cannot shrink a table that is the primary table of an ON COMMIT materialized view. Rowid materialized views must be rebuilt after the shrink operation.
Query/DML Concurrency
=======================
The online phase of segment shrink is done with DML-compatible locks. Hence DMLs
can coexist during this phase. During the space-release/HWM adjustment phase,
incompatible locks will be acquired on the table, hence, DMLs will block on
shrink.
There are no user visible errors that shrink will cause on DMLs.
Queries cache the segment HWM. Oracle guarantees that the HWM always moves forward,
hence CR (consistent read) is not required on segment header and extent map blocks. The only operations
that cause the segment HWM to move backward are drop and truncate.
We allow queries to coexist with drop/truncate DDLs since queries do not acquire locks.
If after the drop/truncate, the space gets reused in some other segment, then the
queries get "8103 - object does not exist" external error message.
During segment shrink, the segmentâs incarnation number is changed in the bitmap
blocks and segment header when the segment HWM is adjusted. Subsequent data block
changes happen at this newer incarnation number.
Queries that span this phase can die with an external error "10632 - invalid rowid"
if
1) They read the updated bitmap blocks (that have new inc#). Note that this failure
happens if the space is not reused
2) The space got reused for some other object or the same object.
Limitations on Online Segment Shrink, 10gR2:
============================================
Within an ASSM tablespace, all segment types are eligible for online segment shrink except these:
- IOT mapping tables
- Tables with rowid based materialized views
- Tables with function-based indexes
Dependency Maintenance and Restrictions on Shrink
=================================================
The only dependency that will be taken care of during segment shrink is the index.
The indexes will not be in an unusable state after shrink.
The compaction phase of segment shrink will be done as insert/delete pairs. The
DML triggers will not be fired during data movement phase. Since the data does
not change, it is not required to fire the triggers.
ROWID based triggers should be disabled before issuing a shrink since it will not
fire during shrink.
Segment shrink cannot be done on objects with on-commit materialized views.
Materialized views that are based on primary key need not be refreshed or rebuilt
after shrink.
However, it is the DBAâs responsibility to refresh/rebuild the materialized views
that are rowid based.
Availability
============
Segment shrink is done online, thereby it increases the availability of the object.
While conventional DML operations can coexist with segment shrink, parallel DMLs
cannot.
During segment shrink, data will be moved as part of the compaction phase. During
compaction locks will be held on individual rows and/or blocks containing the data.
This will cause the concurrent DMLs like updates and deletes to serialize on the
locks. The compaction will be done in units of smaller transactions, so the availability
of the object will not be impacted significantly.
However during certain phases of segment shrink (when the HWM is adjusted), the segment
will have to be locked in exclusive mode.
This phase is for a very short duration and should impact the availability of the
object less significantly.
Security
========
The privileges required to execute segment shrink on an object will be the same
as that for ALTER object.
Detail Example
===============
SQL> set serveroutput on
SQL> declare
2 v_unformatted_blocks number;
3 v_unformatted_bytes number;
4 v_fs1_blocks number;
5 v_fs1_bytes number;
6 v_fs2_blocks number;
7 v_fs2_bytes number;
8 v_fs3_blocks number;
9 v_fs3_bytes number;
10 v_fs4_blocks number;
11 v_fs4_bytes number;
12 v_full_blocks number;
13 v_full_bytes number;
14 begin
15 dbms_space.space_usage ('SYSTEM', 'T_SHRINK', 'TABLE', v_unformatted_blocks,
16 v_unformatted_bytes, v_fs1_blocks, v_fs1_bytes, v_fs2_blocks, v_fs2_bytes,
17 v_fs3_blocks, v_fs3_bytes, v_fs4_blocks, v_fs4_bytes, v_full_blocks, v_full_bytes);
18 dbms_output.put_line('Unformatted Blocks = '||v_unformatted_blocks);
19 dbms_output.put_line('FS1 Blocks = '||v_fs1_blocks);
20 dbms_output.put_line('FS2 Blocks = '||v_fs2_blocks);
21 dbms_output.put_line('FS3 Blocks = '||v_fs3_blocks);
22 dbms_output.put_line('FS4 Blocks = '||v_fs4_blocks);
23 dbms_output.put_line('Full Blocks = '||v_full_blocks);
24 end;
25 /
Unformatted Blocks = 0
FS1 Blocks = 0
FS2 Blocks = 1
FS3 Blocks = 1
FS4 Blocks = 3
Full Blocks = 0
PL/SQL procedure successfully completed.
SQL> alter table t_shrink shrink space compact;
alter table t_shrink shrink space compact
*
ERROR at line 1:
ORA-10636: ROW MOVEMENT is not enabled
SQL> ALTER TABLE t_shrink ENABLE ROW MOVEMENT;
Table altered.
SQL> ALTER TABLE t_shrink SHRINK SPACE COMPACT;
Table altered.
SQL> set serveroutput on
SQL> declare
2 v_unformatted_blocks number;
3 v_unformatted_bytes number;
4 v_fs1_blocks number;
5 v_fs1_bytes number;
6 v_fs2_blocks number;
7 v_fs2_bytes number;
8 v_fs3_blocks number;
9 v_fs3_bytes number;
10 v_fs4_blocks number;
11 v_fs4_bytes number;
12 v_full_blocks number;
13 v_full_bytes number;
14 begin
15 dbms_space.space_usage ('SYSTEM', 'T_SHRINK', 'TABLE', v_unformatted_blocks,
16 v_unformatted_bytes, v_fs1_blocks, v_fs1_bytes, v_fs2_blocks, v_fs2_bytes,
17 v_fs3_blocks, v_fs3_bytes, v_fs4_blocks, v_fs4_bytes, v_full_blocks, v_full_bytes);
18 dbms_output.put_line('Unformatted Blocks = '||v_unformatted_blocks);
19 dbms_output.put_line('FS1 Blocks = '||v_fs1_blocks);
20 dbms_output.put_line('FS2 Blocks = '||v_fs2_blocks);
21 dbms_output.put_line('FS3 Blocks = '||v_fs3_blocks);
22 dbms_output.put_line('FS4 Blocks = '||v_fs4_blocks);
23 dbms_output.put_line('Full Blocks = '||v_full_blocks);
24 end;
25 /
Unformatted Blocks = 0
FS1 Blocks = 0
FS2 Blocks = 0
FS3 Blocks = 0
FS4 Blocks = 2
Full Blocks = 1
PL/SQL procedure successfully completed.
SQL> ALTER TABLE t_shrink SHRINK SPACE;
Table altered.
SQL> declare
2 v_unformatted_blocks number;
3 v_unformatted_bytes number;
4 v_fs1_blocks number;
5 v_fs1_bytes number;
6 v_fs2_blocks number;
7 v_fs2_bytes number;
8 v_fs3_blocks number;
9 v_fs3_bytes number;
10 v_fs4_blocks number;
11 v_fs4_bytes number;
12 v_full_blocks number;
13 v_full_bytes number;
14 begin
15 dbms_space.space_usage ('SYSTEM', 'T_SHRINK', 'TABLE', v_unformatted_blocks,
16 v_unformatted_bytes, v_fs1_blocks, v_fs1_bytes, v_fs2_blocks, v_fs2_bytes,
17 v_fs3_blocks, v_fs3_bytes, v_fs4_blocks, v_fs4_bytes, v_full_blocks, v_full_bytes);
18 dbms_output.put_line('Unformatted Blocks = '||v_unformatted_blocks);
19 dbms_output.put_line('FS1 Blocks = '||v_fs1_blocks);
20 dbms_output.put_line('FS2 Blocks = '||v_fs2_blocks);
21 dbms_output.put_line('FS3 Blocks = '||v_fs3_blocks);
22 dbms_output.put_line('FS4 Blocks = '||v_fs4_blocks);
23 dbms_output.put_line('Full Blocks = '||v_full_blocks);
24 end;
25 /
Unformatted Blocks = 0
FS1 Blocks = 0
FS2 Blocks = 0
FS3 Blocks = 0
FS4 Blocks = 1
Full Blocks = 1
PL/SQL procedure successfully completed.
SQL>
========
To monitor the progress of a long running SHRINK operation you may execute the pl/sql block in this note. You should see a change in the number of blocks. And it is not possible to verify how much time it still need to take for now.
For specific details on the output of the pl/sql blocks in this Note please reference the following documentation:
For 9i:
http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96612/d_space2.htm#1002701
For 10gR2:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_space.htm#sthref6026
For 11gR1:
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_space.htm#sthref7301
For 11gR2:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_space.htm#ARPLS68113
For 12cR1:
http://docs.oracle.com/database/121/ARPLS/d_space.htm#ARPLS68113
For 12cR2:
http://docs.oracle.com/database/122/ARPLS/DBMS_SPACE.htm#ARPLS68113
Can I cancel shrink operation? :
====================
Shrinking space of a large segment can take a lot of time, e.g. tens of hours, and can generate lots of redo.
Therefore, it is not advised to interrupt a shrink statement.
Best approach would be:
1. Initially, only use:
SQL> alter table shrink space compact;
This also takes long but should have little impact because of only short dml locks
2. Only when convenient, issue
SQL> alter table shrink space;
Before running in production, it is advised to test for possible impact in your test environment.
RELATED DOCUMENTS
-----------------
SQL Reference Manual, 10.1 and 10.2
Oracle® Database Administrator's Guide10g Release 1 and 2
Note:149516.1 BMB versus Freelist Segment: DBMS_SPACE.UNUSED_SPACE and DBA_TABLES.EMPTY_BLOCKS
##sample 3
Oracle Streams enables the sharing of data and events in a data stream, either within a database or from one database to another. This article is intended to provide information regarding the management of LOGMNR_RESTART_CKPT$ table.
SOLUTION
Periodically, the mining process checkpoints itself for quicker restart.
These checkpoint information is maintained in the SYSAUX tablespace by default.
From Oracle 10.2 onwards, the purging of logmnr_restart_ckpt$ is done automatically
by Oracle. There is a capture parameter checkpoint_retention_time that determines how
frequently the purge occurs.
CHECKPOINT_RETENTION_TIME, controls the amount of checkpoint data that is retained by
moving the FIRST_SCN of the capture process forward. When the checkpoint_retention_time
is exceeded (default = 60 days), the FIRST_SCN is moved and the Streams metadata tables
previous to this scn(FIRST_SCN) can be purged. Space in the SYSAUX tablespace should be
reclaimed at this time.
You can alter checkpoint_retention_time to lesser value to purge the metatdata
tables more frequently using the following syntax :
exec dbms_capture_adm.alter_capture(capture_name =>'<name> ',CHECKPOINT_RETENTION_TIME=><days>);
For example :-
exec dbms_capture_adm.alter_capture(capture_name =>'STRMADMIN_CAPTURE ',CHECKPOINT_RETENTION_TIME=>7);
Here we are setting the retention time to 7 days. Any data in table logmnr_restart_ckpt$
older than 7 days is purged automatically.
You can then use the shrink command to manually free unused space both above and
below the high water mark of the table.
alter table system.LOGMNR_RESTART_CKPT$ enable row movement;
alter table system.LOGMNR_RESTART_CKPT$ shrink space ;
alter table system.LOGMNR_RESTART_CKPT$ modify lob (CKPT_INFO) (shrink space);
alter table system.LOGMNR_RESTART_CKPT$ disable row movement;
The index associated with the table can be shrunk using
alter index <index name> shrink space;
##########sample 3 实际操作
一、查询删除前表空间
select A.tablespace_name,total,free,(total-free),round((total-free)/total,4)*100
from (select tablespace_name,sum(bytes) free from dba_free_space group by tablespace_name) A,
(select tablespace_name,sum(bytes) total from dba_data_files group by tablespace_name) B where
A.tablespace_name = B.tablespace_name and A.tablespace_name = 'IGFS_DATA';
SELECT a.tablespace_name ,b.maxbytes/1024/1024/1024 "maxbyes_GB",total/1024/1024/1024 "bytes_GB",free/1024/1024/1024 "free_GB",(total-free) /1024/1024/1024 "use_GB",
ROUND((total-free)/total,4)*100 "use_%",ROUND((total-free)/b.maxbytes,4)*100 "maxuse_%"
FROM
(SELECT tablespace_name,SUM(bytes) free FROM DBA_FREE_SPACE
GROUP BY tablespace_name
) a,
(SELECT tablespace_name,sum(case autoextensible when 'YES' then maxbytes else bytes end) maxbytes,SUM(bytes) total FROM DBA_DATA_FILES
GROUP BY tablespace_name
) b
WHERE a.tablespace_name=b.tablespace_name
order by "maxuse_%" desc;
二、删除表数据 delete 时候只花了7-8分钟不到的时间.。
delete from user.ab_LOG where trade_date>='20140101' and trade_date<='20141231' ;
Commit;
delete from user.ab_LOG where trade_date>='20150101' and trade_date<='20151231' ;
Commit;
三、整理表空间
开启行移动
ALTER TABLE user.ab_LOG ENABLE ROW MOVEMENT;
收缩表(会阻塞DML操作并回收对应的索引),shrink space 一共花了2个小时多一点。释放了总共30G 数据空间。
ALTER TABLE user.ab_LOG SHRINK SPACE CASCADE;
关闭行移动
ALTER table user.ab_LOG disable ROW MOVEMENT;
->开始时间 :21:55
TABLESPACE_NAME maxbyes_GB bytes_GB free_GB use_GB use_% maxuse_%
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
tabspace 297.999863 302.820267 13.5998535 289.220413 95.51 97.05
->观察时间:22:42
TABLESPACE_NAME maxbyes_GB bytes_GB free_GB use_GB use_% maxuse_%
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
tabspace 297.999863 302.820267 13.7063599 289.113907 95.47 97.02
->结束时间23:49
TABLESPACE_NAME maxbyes_GB bytes_GB free_GB use_GB use_% maxuse_%
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
tabspace 297.999863 302.820267 40.4465332 262.373734 86.64 88.04
##实际操作的过程如下,发现有如下问题。
问题1,敲下shrink space 时候,其他都是黑盒操作,也不知道完成进度。尝试使用如下sql 观察,发现该sql 只能观察dml, 不能观察ddl, 也就是shrink space
这种ddl 观察不到记录。
#########方法1 观察sql , 无效
SELECT A.USERNAME,
A.SID,
A.SERIAL#,
A.OPNAME,
A.TARGET,
A.START_TIME,
A.LAST_UPDATE_TIME,
C.OSUSER,
C.MACHINE,
C.PROGRAM,
ROUND(A.SOFAR * 100 / TOTALWORK, 0) || '%' AS JINDU,
A.TIME_REMAINING,
A.ELAPSED_SECONDS,
B.SQL_TEXT
FROM GV$SESSION_LONGOPS A, GV$SQL B, GV$SESSION C
WHERE A.SQL_ADDRESS = B.ADDRESS
AND A.SQL_HASH_VALUE = B.HASH_VALUE
AND A.SID = C.SID
AND C.STATUS = 'ACTIVE'
####
##方法2 有效
咨询测试人员,当时的恢复环境,同样的数据做数据清理,花了多久时间。得到的答复是1个半小时。实际上生产环境消耗的时间超过1个半小时,
经过分析,如果想评估shrink space 的时间,时间主要消耗临时表空间征用,估计跟重建索引有关系。
可以观察redo 产生的日志量,如果测试产生的日志量 达到了50G, 那么生产环境也应该消耗了50G 左右
##经过分析,测试环境的单个redo在2G 左右,生产单个redo 在200M 左右,所以生产redo 切换太频繁,也可能导致生产实际运行时间超过测试环境。
redo size
测试2G
生产200M;
清理60万shrinks space AB_LOG 时间,造成了时间消耗从测试环境只要 1个半小时变到 生产环境 2个小时.
######
问题2:
由于linux 空间超时时间为300秒,echo $TMOUT
如果直接在前台sqlplus 执行 SHRINK SPACE
可以会超时退出,这个时候,观察后台sql 仍然在跑,理论上不影响该条sql 执行。但是后继这种情况可以考虑如下2种
方法。
方法1: oracle 用户下, 未经验证
执行直接 TMOUT=0
方法2 ,在pl/sql devoloper 端执行
这个时候。应该没有超时限制
############ 查看lob 对应的segment name
select owner,table_name,column_name,segment_name,index_name from dba_lobs;
#####