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;
#####