oracle 调整表空间大小 (resize)

﹏ヽ暗。殇╰゛Y 2022-04-05 06:52 1238阅读 0赞

有的时候为了解决磁盘空间不足等情况,需要把一些表空间大小resize一下,其实语句很简单,如下:

  1. SQL> ALTER DATABASE
  2. 2 TEMPFILE '/opt/database/ebs/db/data/temp12.dbf'
  3. 3 RESIZE 4G;
  4. ALTER DATABASE
  5. *
  6. ERROR at line 1:
  7. ORA-03297: file contains used data beyond requested RESIZE value

如上:ora-03297 报错估计大家在resize的时候会经常遇到,那么如何判定resize 大小准确的大小呢,下面给出一个脚本,利用该脚本来生成resize语句:

相关脚本连接:https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=254440300234769&id=130866.1&_afrWindowMode=0&_adf.ctrl-state=b3zfmsxmq_4

具体如下:

  1. REM Script is meant for Oracle version 9 and higher
  2. REM -----------------------------------------------
  3. set serveroutput on
  4. exec dbms_output.enable(1000000);
  5. declare
  6. cursor c_dbfile is
  7. select f.tablespace_name,f.file_name,f.file_id,f.blocks,t.block_size
  8. ,decode(t.allocation_type,'UNIFORM',t.initial_extent/t.block_size,0) uni_extent
  9. ,decode(t.allocation_type,'UNIFORM',(128+(t.initial_extent/t.block_size)),128) file_min_size
  10. from dba_data_files f,
  11. dba_tablespaces t
  12. where f.tablespace_name = t.tablespace_name
  13. and t.status = 'ONLINE'
  14. order by f.tablespace_name,f.file_id;
  15. cursor c_freespace(v_file_id in number) is
  16. select block_id, block_id+blocks max_block
  17. from dba_free_space
  18. where file_id = v_file_id
  19. order by block_id desc;
  20. /* variables to check settings/values */
  21. dummy number;
  22. checkval varchar2(10);
  23. block_correction1 number;
  24. block_correction2 number;
  25. /* running variable to show (possible) end-of-file */
  26. file_min_block number;
  27. /* variables to check if recycle_bin is on and if extent as checked is in ... */
  28. recycle_bin boolean:=false;
  29. extent_in_recycle_bin boolean;
  30. /* exception handler needed for non-existing tables note:344940.1 */
  31. sqlstr varchar2(100);
  32. table_does_not_exist exception;
  33. pragma exception_init(table_does_not_exist,-942);
  34. /* variable to spot space wastage in datafile of uniform tablespace */
  35. space_wastage number;
  36. begin
  37. /* recyclebin is present in Oracle 10.2 and higher and might contain extent as checked */
  38. begin
  39. select value into checkval from v$parameter where name = 'recyclebin';
  40. if checkval = 'on'
  41. then
  42. recycle_bin := true;
  43. end if;
  44. exception
  45. when no_data_found
  46. then
  47. recycle_bin := false;
  48. end;
  49. /* main loop */
  50. for c_file in c_dbfile
  51. loop
  52. /* initialization of loop variables */
  53. dummy :=0;
  54. extent_in_recycle_bin := false;
  55. file_min_block := c_file.blocks;
  56. begin
  57. space_wastage:=0; /* reset for every file check */
  58. <<check_free>>
  59. for c_free in c_freespace(c_file.file_id)
  60. loop
  61. /* if blocks is an uneven value there is a need to correct
  62. with -1 to compare with end-of-file which is even */
  63. block_correction1 := (0-mod(c_free.max_block,2));
  64. block_correction2 := (0-mod(c_file.blocks,2));
  65. if file_min_block+block_correction2 = c_free.max_block+block_correction1
  66. then
  67. /* free extent is at end so file can be resized */
  68. file_min_block := c_free.block_id;
  69. /* Uniform sized tablespace check if space at end of file
  70. is less then uniform extent size */
  71. elsif (c_file.uni_extent !=0) and ((c_file.blocks - c_free.max_block) < c_file.uni_extent)
  72. then
  73. /* uniform tablespace which has a wastage of space in datafile
  74. due to fact that space at end of file is smaller than uniform extent size */
  75. space_wastage:=c_file.blocks - c_free.max_block;
  76. file_min_block := c_free.block_id;
  77. else
  78. /* no more free extent at end of file, file cannot be further resized */
  79. exit check_free;
  80. end if;
  81. end loop;
  82. end;
  83. /* check if file can be resized, minimal size of file 128 {+ initial_extent} blocks */
  84. if (file_min_block = c_file.blocks) or (c_file.blocks <= c_file.file_min_size)
  85. then
  86. dbms_output.put_line('Tablespace: '||c_file.tablespace_name||' Datafile: '||c_file.file_name);
  87. dbms_output.put_line('cannot be resized no free extents found');
  88. dbms_output.put_line('Note: for some cases, dba_free_spaces data is not accurate, and this script does not work for such cases. You may want to manually check if the datafile is feasible to be resized');
  89. dbms_output.put_line('.');
  90. else
  91. /* file needs minimal no of blocks which does vary over versions,
  92. using safe value of 128 {+ initial_extent} */
  93. if file_min_block < c_file.file_min_size
  94. then
  95. file_min_block := c_file.file_min_size;
  96. end if;
  97. dbms_output.put_line('Tablespace: '||c_file.tablespace_name||' Datafile: '||c_file.file_name);
  98. dbms_output.put_line('current size: '||(c_file.blocks*c_file.block_size)/1024||'K'||' can be resized to: '||round((file_min_block*c_file.block_size)/1024)||'K (reduction of: '||round(((c_file.blocks-file_min_block)/c_file.blocks)*100,2)||' %)');
  99. /* below is only true if recyclebin is on */
  100. if recycle_bin
  101. then
  102. begin
  103. sqlstr:='select distinct 1 from recyclebin$ where file#='||c_file.file_id;
  104. execute immediate sqlstr into dummy;
  105. if dummy > 0
  106. then
  107. dbms_output.put_line('Extents found in recyclebin for above file/tablespace');
  108. dbms_output.put_line('Implying that purge of recyclebin might be needed in order to resize');
  109. dbms_output.put_line('SQL> purge tablespace '||c_file.tablespace_name||';');
  110. end if;
  111. exception
  112. when no_data_found
  113. then null;
  114. when table_does_not_exist
  115. then null;
  116. end;
  117. end if;
  118. dbms_output.put_line('SQL> alter database datafile '''||c_file.file_name||''' resize '||round((file_min_block*c_file.block_size)/1024)||'K;');
  119. if space_wastage!=0
  120. then
  121. dbms_output.put_line('Datafile belongs to uniform sized tablespace and is not optimally sized.');
  122. dbms_output.put_line('Size of datafile is not a multiple of NN*uniform_extent_size + overhead');
  123. dbms_output.put_line('Space that cannot be used (space wastage): '||round((space_wastage*c_file.block_size)/1024)||'K');
  124. dbms_output.put_line('For optimal usage of space in file either resize OR increase to: '||round(((c_file.blocks+(c_file.uni_extent-space_wastage))*c_file.block_size)/1024)||'K');
  125. end if;
  126. dbms_output.put_line('.');
  127. end if;
  128. end loop;
  129. end;
  130. /

将上面的脚本保存到oracle_check_resize.sql,然后我们运行一下:

  1. [orahec@db-hx-189-208 awr]$ sqlplus / as sysdba
  2. SQL*Plus: Release 11.2.0.4.0 Production on Tue Dec 11 11:26:15 2018
  3. Copyright (c) 1982, 2013, Oracle. All rights reserved.
  4. Connected to:
  5. Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options
  7. SQL> spool oracle_check_resize.txt
  8. SQL> @oracle_check_resize.sql
  9. PL/SQL procedure successfully completed.
  10. Tablespace: HEC_DATA Datafile: /opt/Oracle/hecdb/oradata/HEC/hecdata01.dbf
  11. cannot be resized no free extents found
  12. Note: for some cases, dba_free_spaces data is not accurate, and this script does
  13. not work for such cases. You may want to manually check if the datafile is
  14. feasible to be resized
  15. .
  16. Tablespace: HEC_DATA Datafile: /opt/Oracle/hecdb/oradata/HEC/hecdata02.dbf
  17. cannot be resized no free extents found
  18. Note: for some cases, dba_free_spaces data is not accurate, and this script does
  19. not work for such cases. You may want to manually check if the datafile is
  20. feasible to be resized
  21. .
  22. Tablespace: HEC_DATA Datafile: /opt/Oracle/hecdb/oradata/HEC/hecdata03.dbf
  23. current size: 16777216K can be resized to: 14888960K (reduction of: 11.25 %)
  24. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/hecdata03.dbf'
  25. resize 14888960K;
  26. .
  27. Tablespace: HEC_DATA Datafile: /opt/Oracle/hecdb/oradata/HEC/hecdata04.dbf
  28. cannot be resized no free extents found
  29. Note: for some cases, dba_free_spaces data is not accurate, and this script does
  30. not work for such cases. You may want to manually check if the datafile is
  31. feasible to be resized
  32. .
  33. Tablespace: SYSAUX Datafile: /opt/Oracle/hecdb/oradata/HEC/sysaux01.dbf
  34. cannot be resized no free extents found
  35. Note: for some cases, dba_free_spaces data is not accurate, and this script does
  36. not work for such cases. You may want to manually check if the datafile is
  37. feasible to be resized
  38. .
  39. Tablespace: SYSAUX Datafile: /opt/Oracle/hecdb/oradata/HEC/sysaux02.dbf
  40. current size: 5242880K can be resized to: 190464K (reduction of: 96.37 %)
  41. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/sysaux02.dbf' resize
  42. 190464K;
  43. .
  44. Tablespace: SYSTEM Datafile: /opt/Oracle/hecdb/oradata/HEC/system01.dbf
  45. cannot be resized no free extents found
  46. Note: for some cases, dba_free_spaces data is not accurate, and this script does
  47. not work for such cases. You may want to manually check if the datafile is
  48. feasible to be resized
  49. .
  50. Tablespace: SYSTEM Datafile: /opt/Oracle/hecdb/oradata/HEC/system02.dbf
  51. current size: 8007680K can be resized to: 8001536K (reduction of: .08 %)
  52. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/system02.dbf' resize
  53. 8001536K;
  54. .
  55. Tablespace: SYSTEM Datafile: /opt/Oracle/hecdb/oradata/HEC/system03.dbf
  56. current size: 16777216K can be resized to: 1378304K (reduction of: 91.78 %)
  57. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/system03.dbf' resize
  58. 1378304K;
  59. .
  60. Tablespace: UNDOTBS1 Datafile: /opt/Oracle/hecdb/oradata/HEC/undotbs01.dbf
  61. current size: 28385280K can be resized to: 24642624K (reduction of: 13.19 %)
  62. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/undotbs01.dbf'
  63. resize 24642624K;
  64. .
  65. Tablespace: USERS Datafile: /opt/Oracle/hecdb/oradata/HEC/users01.dbf
  66. current size: 5120K can be resized to: 1344K (reduction of: 73.75 %)
  67. SQL> alter database datafile '/opt/Oracle/hecdb/oradata/HEC/users01.dbf' resize
  68. 1344K;
  69. .
  70. PL/SQL procedure successfully completed.
  71. SQL> exit

发表评论

表情:
评论列表 (有 0 条评论,1238人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux下调整目录的空间

    一、目的 在使用CentOS6.5版本linux系统的时候,发现根目录(/)的空间不是很充足,而其他目录空间有很大的空闲,所以本文主要是针对现在已有的空间进行调整。首先,先来