Recently, some netizens have mentioned the problem of shrinking Oracle data files, which is a common issue that DBAs often encounter. Usually we need to shrink the corresponding data files to reduce the pressure from disk space and improve the overall performance of the database. But this is not applicable in all cases, especially in production environments. Because the production environment data cleaning is relatively small, so the space waste is also smaller, and once the contraction will re-automatically expand the data file, wasting system resources. For the UAT,DEV environment, multi-DB, disk space pressure is very high, it is necessary to shrink a bit. Tightening your belts and days is a common thing, haha. In summary, shrinking the data file will allow disk space to be freed and speed up data migration, Rman backup, and so on. This article shares Tom Master's shrink script and gives links to undo, temp table space, and table-segment contractions.
Several cases of shrinkage:
Shrink table segment (shrink space)
Shrinking temporary table spaces
Shrink Undo Table Space
1. Presentation of shrinking data files
[SQL]View PlainCopyprint?
- [Email protected]:~/dba_scripts/custom/sql> sql
- Sql*plus:release 10.2.0.3.0-production on Wed Oct-15:05:18
- Copyright (c) 1982, 2006, Oracle. All rights Reserved.
- Connected to :
- Oracle Database 10g Release 10.2.0.3.0-64bit Production
- Goe[email protected]> @shrink_data_files;
- VALUE
- --------------------
- 8192
- Smallest
- Size current Poss.
- file_name Poss. Size Savings
- -------------------------------------------------- -------- -------- --------
- /u02/database/usbotst/oradata/sysusbotst.dbf 605 650
- /u02/Database/usbotst/oradata/usbotst_archive_idx. 725 1,871 1,146
- Dbf
- /u02/database/usbotst/oradata/usbotst_his_idx.dbf 1
- /u02/database/usbotst/oradata/usbotst_ipo_idx.dbf 7 3
- /u02/database/usbotst/oradata/usbotst_account_tbl. 6,293 6,293 0
- Dbf
- /u02/database/usbotst/oradata/usbotst_rpt_tbl.dbf 373 352
- /u02/database/usbotst/oradata/usbotst_audit_tbl.db 938 966
- F
- /u02/database/usbotst/oradata/tbs_rman01.dbf
- /u02/database/usbotst/undo/undotbsusbotst.dbf 358 7,350 6,992
- /u02/database/usbotst/oradata/usbotst_archive_tbl. 760 1,950 1,190
- Dbf
- /u02/database/usbotst/oradata/usbotst_rpt_idx.dbf 359 349
- /u02/database/usbotst/oradata/usbotst_vou_tbl.dbf 4 145 141
- /u02/database/usbotst/oradata/usbotst_stock_l_tbl. 4
- Dbf
- /u02/database/usbotst/oradata/usbotst_ca_idx.dbf 1
- /u02/database/usbotst/oradata/usbotst_his_tbl.dbf 1 959 958
- /u02/database/usbotst/oradata/usbotst_vou_idx.dbf 2
- /u02/database/usbotst/oradata/sysauxusbotst.dbf 697 103
- /u02/database/usbotst/oradata/spot_data.dbf Bayi
- /u02/DATABASE/USBOTST/ORADATA/USBOTST_TX_TBL.DBF 103
- /u02/DATABASE/USBOTST/ORADATA/USBOTST_TX_HIS_TBL.D 878 790
- Bf
- /u02/database/usbotst/oradata/usbotst_ca_tbl.dbf 1
- /u02/database/usbotst/oradata/usbotst_imp_exp_tbl. 108
- Dbf
- .........................................................................
- --------
- Sum 29,686 -the total space that can be freed
- The rows selected.
- Database altered.
- Database altered.
- Database altered.
- Database altered.
- Alter database datafile '/u02/database/usbotst/oradata/usbotst_ipo_idx.dbf '
- *
- ERROR at line 1:
- Ora-03297:file contains used data beyond requested RESIZE value
- ---> Author:leshami--->blog:http://blog.csdn.net/leshami
- ...........................................
- -There may be cases where individual files cannot be shrunk, suggesting that the minimum size is exceeded.
2. Shrink Script
[SQL]View PlainCopyprint?
- --This script can be used for Oracle 10g,11g
- [Email protected]:~/dba_scripts/custom/sql> more Shrink_data_files.sql
- Set verify off
- Col Value Format A20
- Column file_name format A50 word_wrapped
- Column smallest format 999,990 heading "smallest| Size|poss. "
- Column currsize format 999,990 heading "current| Size "
- Column savings format 999,990 heading "poss.| Savings "
- Break on the report
- Compute sum of savings on report
- Column Value New_val blksize
- Select value from v$parameter where name = ' db_block_size '
- /
- Select file_name,
- Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) smallest,
- Ceil (blocks*&&blksize/1024/1024) currsize,
- Ceil (blocks*&&blksize/1024/1024)-
- Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) savings
- From Dba_data_files A,
- ( select file_id, max (block_id+blocks-1) HWM
- From dba_extents
- GROUP by file_id) b
- where a.file_id = b.file_id (+)
- /
- Column cmd format A75 word_wrapped
- Set heading off feedback off termout off
- Spool/tmp/tmp_shrink_data_files.sql
- Select ' ALTER DATABASE datafile '| | file_name| | "' resize ' | |
- Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) | | ' m; ' cmd
- From Dba_data_files A,
- ( select file_id, max (block_id+blocks-1) HWM
- From dba_extents
- GROUP by file_id) b
- where a.file_id = b.file_id (+)
- and Ceil (blocks*&&blksize/1024/1024)-
- Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) > 0
- /
- Spool off;
- Set heading on feedback in termout on
- @/tmp/tmp_shrink_data_files.sql
Ext.: http://blog.csdn.net/leshami/article/details/13628463
Shrinking Oracle Data files