Shrinking Oracle Data files

Source: Internet
Author: User

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?
  1. [Email protected]:~/dba_scripts/custom/sql> sql
  2. Sql*plus:release 10.2.0.3.0-production on Wed Oct-15:05:18
  3. Copyright (c) 1982, 2006, Oracle.  All rights Reserved.
  4. Connected to :
  5. Oracle Database 10g Release 10.2.0.3.0-64bit Production
  6. Goe[email protected]> @shrink_data_files;
  7. VALUE
  8. --------------------
  9. 8192
  10. Smallest
  11. Size current Poss.
  12. file_name Poss. Size Savings
  13. -------------------------------------------------- -------- -------- --------
  14. /u02/database/usbotst/oradata/sysusbotst.dbf 605 650
  15. /u02/Database/usbotst/oradata/usbotst_archive_idx. 725 1,871 1,146
  16. Dbf
  17. /u02/database/usbotst/oradata/usbotst_his_idx.dbf 1
  18. /u02/database/usbotst/oradata/usbotst_ipo_idx.dbf 7 3
  19. /u02/database/usbotst/oradata/usbotst_account_tbl. 6,293 6,293 0
  20. Dbf
  21. /u02/database/usbotst/oradata/usbotst_rpt_tbl.dbf 373 352
  22. /u02/database/usbotst/oradata/usbotst_audit_tbl.db 938 966
  23. F
  24. /u02/database/usbotst/oradata/tbs_rman01.dbf
  25. /u02/database/usbotst/undo/undotbsusbotst.dbf 358 7,350 6,992
  26. /u02/database/usbotst/oradata/usbotst_archive_tbl. 760 1,950 1,190
  27. Dbf
  28. /u02/database/usbotst/oradata/usbotst_rpt_idx.dbf 359 349
  29. /u02/database/usbotst/oradata/usbotst_vou_tbl.dbf 4 145 141
  30. /u02/database/usbotst/oradata/usbotst_stock_l_tbl. 4
  31. Dbf
  32. /u02/database/usbotst/oradata/usbotst_ca_idx.dbf 1
  33. /u02/database/usbotst/oradata/usbotst_his_tbl.dbf 1 959 958
  34. /u02/database/usbotst/oradata/usbotst_vou_idx.dbf 2
  35. /u02/database/usbotst/oradata/sysauxusbotst.dbf 697 103
  36. /u02/database/usbotst/oradata/spot_data.dbf Bayi
  37. /u02/DATABASE/USBOTST/ORADATA/USBOTST_TX_TBL.DBF 103
  38. /u02/DATABASE/USBOTST/ORADATA/USBOTST_TX_HIS_TBL.D 878 790
  39. Bf
  40. /u02/database/usbotst/oradata/usbotst_ca_tbl.dbf 1
  41. /u02/database/usbotst/oradata/usbotst_imp_exp_tbl. 108
  42. Dbf
  43. .........................................................................
  44. --------  
  45. Sum 29,686 -the total space that can be freed
  46. The rows selected.
  47. Database altered.
  48. Database altered.
  49. Database altered.
  50. Database altered.
  51. Alter database datafile '/u02/database/usbotst/oradata/usbotst_ipo_idx.dbf '
  52. *
  53. ERROR at line 1:
  54. Ora-03297:file contains used data beyond requested RESIZE value
  55. ---> Author:leshami--->blog:http://blog.csdn.net/leshami
  56. ...........................................
  57. -There may be cases where individual files cannot be shrunk, suggesting that the minimum size is exceeded.

2. Shrink Script

[SQL]View PlainCopyprint?
  1. --This script can be used for Oracle 10g,11g
  2. [Email protected]:~/dba_scripts/custom/sql> more Shrink_data_files.sql
  3. Set verify off
  4. Col Value Format A20
  5. Column file_name format A50 word_wrapped
  6. Column smallest format 999,990 heading "smallest| Size|poss. "
  7. Column currsize format 999,990 heading "current| Size "
  8. Column savings format 999,990 heading "poss.| Savings "
  9. Break on the report
  10. Compute sum of savings on report
  11. Column Value New_val blksize
  12. Select value from v$parameter where name = ' db_block_size '
  13. /
  14. Select file_name,
  15. Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) smallest,
  16. Ceil (blocks*&&blksize/1024/1024) currsize,
  17. Ceil (blocks*&&blksize/1024/1024)-
  18. Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) savings
  19. From Dba_data_files A,
  20. ( select file_id, max (block_id+blocks-1) HWM
  21. From dba_extents
  22. GROUP by file_id) b
  23. where a.file_id = b.file_id (+)
  24. /
  25. Column cmd format A75 word_wrapped
  26. Set heading off feedback off termout off
  27. Spool/tmp/tmp_shrink_data_files.sql
  28. Select ' ALTER DATABASE datafile '| | file_name| |  "' resize ' | |
  29. Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) | | ' m; ' cmd
  30. From Dba_data_files A,
  31. ( select file_id, max (block_id+blocks-1) HWM
  32. From dba_extents
  33. GROUP by file_id) b
  34. where a.file_id = b.file_id (+)
  35. and Ceil (blocks*&&blksize/1024/1024)-
  36. Ceil ((NVL (hwm,1) *&&blksize)/1024/1024) > 0
  37. /
  38. Spool off;
  39. Set heading on feedback in termout on
  40. @/tmp/tmp_shrink_data_files.sql
Ext.: http://blog.csdn.net/leshami/article/details/13628463

Shrinking Oracle Data files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.