表空间统计报告 Tablespace growth Report

  1. SQL> select TS# from v$tablespace where name='ABC' ;
  2. TS#
  3. ----------
  4. 6
  5. set serverout on
  6. set verify off
  7. set lines 200
  8. set pages 2000
  9. DECLARE
  10. v_ts_id number;
  11. not_in_awr EXCEPTION;
  12. v_ts_name varchar2(200) := UPPER('&Tablespace_Name');
  13. v_ts_block_size number;
  14. v_begin_snap_id number;
  15. v_end_snap_id number;
  16. v_begin_snap_date date;
  17. v_end_snap_date date;
  18. v_numdays number;
  19. v_ts_begin_size number;
  20. v_ts_end_size number;
  21. v_ts_growth number;
  22. v_count number;
  23. v_ts_begin_allocated_space number;
  24. v_ts_end_allocated_space number;
  25. BEGIN
  26. SELECT ts# into v_ts_id FROM v$tablespace where name = v_ts_name;
  27. SELECT count(*) INTO v_count FROM dba_hist_tbspc_space_usage where tablespace_id=v_ts_id;
  28. IF v_count = 0 THEN
  29. RAISE not_in_awr;
  30. END IF ;
  31. SELECT block_size into v_ts_block_size FROM dba_tablespaces where tablespace_name = v_ts_name;
  32. SELECT min(snap_id), max(snap_id), min(trunc(to_date(rtime,'MM/DD/YYYY HH24:MI:SS'))), max(trunc(to_date(rtime,'MM/DD/YYYY HH24:MI:SS')))
  33. into v_begin_snap_id,v_end_snap_id, v_begin_snap_date, v_end_snap_date from dba_hist_tbspc_space_usage where tablespace_id=v_ts_id;
  34. v_numdays := v_end_snap_date - v_begin_snap_date;
  35. SELECT round(max(tablespace_size)*v_ts_block_size/1024/1024,2) into v_ts_begin_allocated_space from dba_hist_tbspc_space_usage where tablespace_id=v_ts_id and snap_id = v_begin_snap_id;
  36. SELECT round(max(tablespace_size)*v_ts_block_size/1024/1024,2) into v_ts_end_allocated_space from dba_hist_tbspc_space_usage where tablespace_id=v_ts_id and snap_id = v_end_snap_id;
  37. SELECT round(max(tablespace_usedsize)*v_ts_block_size/1024/1024,2) into v_ts_begin_size from dba_hist_tbspc_space_usage where tablespace_id=v_ts_id and snap_id = v_begin_snap_id;
  38. SELECT round(max(tablespace_usedsize)*v_ts_block_size/1024/1024,2) into v_ts_end_size from dba_hist_tbspc_space_usage where tablespace_id=v_ts_id and snap_id = v_end_snap_id;
  39. v_ts_growth := v_ts_end_size - v_ts_begin_size;
  40. DBMS_OUTPUT.PUT_LINE(CHR(10));
  41. DBMS_OUTPUT.PUT_LINE('Tablespace Block Size: '||v_ts_block_size);
  42. DBMS_OUTPUT.PUT_LINE('---------------------------');
  43. DBMS_OUTPUT.PUT_LINE(CHR(10));
  44. DBMS_OUTPUT.PUT_LINE('Summary');
  45. DBMS_OUTPUT.PUT_LINE('========');
  46. DBMS_OUTPUT.PUT_LINE('1) Allocated Space: '||v_ts_end_allocated_space||' MB'||' ('||round(v_ts_end_allocated_space/1024,2)||' GB)');
  47. DBMS_OUTPUT.PUT_LINE('2) Used Space: '||v_ts_end_size||' MB'||' ('||round(v_ts_end_size/1024,2)||' GB)');
  48. DBMS_OUTPUT.PUT_LINE('3) Used Space Percentage: '||round(v_ts_end_size/v_ts_end_allocated_space*100,2)||' %');
  49. DBMS_OUTPUT.PUT_LINE(CHR(10));
  50. DBMS_OUTPUT.PUT_LINE('History');
  51. DBMS_OUTPUT.PUT_LINE('========');
  52. DBMS_OUTPUT.PUT_LINE('1) Allocated Space on '||v_begin_snap_date||': '||v_ts_begin_allocated_space||' MB'||' ('||round(v_ts_begin_allocated_space/1024,2)||' GB)');
  53. DBMS_OUTPUT.PUT_LINE('2) Current Allocated Space on '||v_end_snap_date||': '||v_ts_end_allocated_space||' MB'||' ('||round(v_ts_end_allocated_space/1024,2)||' GB)');
  54. DBMS_OUTPUT.PUT_LINE('3) Used Space on '||v_begin_snap_date||': '||v_ts_begin_size||' MB'||' ('||round(v_ts_begin_size/1024,2)||' GB)' );
  55. DBMS_OUTPUT.PUT_LINE('4) Current Used Space on '||v_end_snap_date||': '||v_ts_end_size||' MB'||' ('||round(v_ts_end_size/1024,2)||' GB)' );
  56. DBMS_OUTPUT.PUT_LINE('5) Total growth during last '||v_numdays||' days between '||v_begin_snap_date||' and '||v_end_snap_date||': '||v_ts_growth||' MB'||' ('||round(v_ts_growth/1024,2)||' GB)');
  57. IF (v_ts_growth <= 0 OR v_numdays <= 0) THEN
  58. DBMS_OUTPUT.PUT_LINE(CHR(10));
  59. DBMS_OUTPUT.PUT_LINE('!!! NO DATA GROWTH WAS FOUND FOR TABLESPCE '||V_TS_NAME||' !!!');
  60. ELSE
  61. DBMS_OUTPUT.PUT_LINE('6) Per day growth during last '||v_numdays||' days: '||round(v_ts_growth/v_numdays,2)||' MB'||' ('||round((v_ts_growth/v_numdays)/1024,2)||' GB)');
  62. DBMS_OUTPUT.PUT_LINE(CHR(10));
  63. DBMS_OUTPUT.PUT_LINE('Expected Growth');
  64. DBMS_OUTPUT.PUT_LINE('===============');
  65. DBMS_OUTPUT.PUT_LINE('1) Expected growth for next 30 days: '|| round((v_ts_growth/v_numdays)*30,2)||' MB'||' ('||round(((v_ts_growth/v_numdays)*30)/1024,2)||' GB)');
  66. DBMS_OUTPUT.PUT_LINE('2) Expected growth for next 60 days: '|| round((v_ts_growth/v_numdays)*60,2)||' MB'||' ('||round(((v_ts_growth/v_numdays)*60)/1024,2)||' GB)');
  67. DBMS_OUTPUT.PUT_LINE('3) Expected growth for next 90 days: '|| round((v_ts_growth/v_numdays)*90,2)||' MB'||' ('||round(((v_ts_growth/v_numdays)*90)/1024,2)||' GB)');
  68. END IF;
  69. EXCEPTION
  70. WHEN NO_DATA_FOUND THEN
  71. DBMS_OUTPUT.PUT_LINE(CHR(10));
  72. DBMS_OUTPUT.PUT_LINE('!!! TABLESPACE DOES NOT EXIST !!!');
  73. WHEN NOT_IN_AWR THEN
  74. DBMS_OUTPUT.PUT_LINE(CHR(10));
  75. DBMS_OUTPUT.PUT_LINE('!!! TABLESPACE USAGE INFORMATION NOT FOUND IN AWR !!!');
  76. END;
  77. /

实际执行结果如下:





posted @ 2015-11-09 15:26  hao_xiaoyu  阅读(423)  评论(0编辑  收藏  举报