TOM大神編寫的show_space函式程式碼

xypincle發表於2017-03-26

  1. CREATE OR REPLACE PROCEDURE show_space(p_segname IN VARCHAR2,

  2.                                        p_owner IN VARCHAR2 DEFAULT USER,
  3.                                        p_type IN VARCHAR2 DEFAULT 'TABLE',
  4.                                        p_partition IN VARCHAR2 DEFAULT NULL) AS
  5.   l_free_blks NUMBER;
  6.   l_total_blocks NUMBER;
  7.   l_total_bytes NUMBER;
  8.   l_unused_blocks NUMBER;
  9.   l_unused_bytes NUMBER;
  10.   l_lastusedextfileid NUMBER;
  11.   l_lastusedextblockid NUMBER;
  12.   l_last_used_block NUMBER;
  13.   l_segment_space_mgmt VARCHAR2(255);
  14.   l_unformatted_blocks NUMBER;
  15.   l_unformatted_bytes NUMBER;
  16.   l_fs1_blocks NUMBER;
  17.   l_fs1_bytes NUMBER;
  18.   l_fs2_blocks NUMBER;
  19.   l_fs2_bytes NUMBER;
  20.   l_fs3_blocks NUMBER;
  21.   l_fs3_bytes NUMBER;
  22.   l_fs4_blocks NUMBER;
  23.   l_fs4_bytes NUMBER;
  24.   l_full_blocks NUMBER;
  25.   l_full_bytes NUMBER;

  26.   PROCEDURE p(p_label IN VARCHAR2,
  27.               p_num IN NUMBER) IS
  28.   BEGIN
  29.     dbms_output.put_line(rpad(p_label, 40, '.') ||
  30.                          to_char(p_num, '999,999,999,999'));
  31.   END;
  32. BEGIN
  33.   EXECUTE IMMEDIATE 'select ts.segment_space_management from dba_segments seg,dba_tablespaces ts where seg.segment_name = :p_segname and (:p_partition is null or seg.partition_name = :p_partition) and seg.owner = :p_owner and seg.tablespace_name=ts.tablespace_name'
  34.     INTO l_segment_space_mgmt
  35.     USING p_segname, p_partition, p_partition, p_owner;

  36.   -- exception
  37.   -- when too_many_rows
  38.   -- then
  39.   -- dbms_output.put_line('This must be a partitioned table,use p_partition => ');
  40.   -- return;
  41.   -- end;

  42.   IF l_segment_space_mgmt = 'AUTO' THEN
  43.     dbms_space.space_usage(p_owner, p_segname, p_type, l_unformatted_blocks,
  44.                            l_unformatted_bytes, l_fs1_blocks, l_fs1_bytes,
  45.                            l_fs2_blocks, l_fs2_bytes, l_fs3_blocks, l_fs3_bytes,
  46.                            l_fs4_blocks, l_fs4_bytes, l_full_blocks,
  47.                            l_full_bytes, p_partition);

  48.     p('Unformatted Blocks ', l_unformatted_blocks);
  49.     p('FS1 Blocks (0-25) ', l_fs1_blocks);
  50.     p('FS2 Blocks (25-50) ', l_fs2_blocks);
  51.     p('FS3 Blocks (50-75) ', l_fs3_blocks);
  52.     p('FS4 Blocks (75-100) ', l_fs4_blocks);
  53.     p('Full Blocks ', l_full_blocks);
  54.   ELSE
  55.     dbms_space.free_blocks(segment_owner => p_owner, segment_name => p_segname,
  56.                            segment_type => p_type, freelist_group_id => 0,
  57.                            free_blks => l_free_blks);
  58.   END IF;

  59.   dbms_space.unused_space(segment_owner => p_owner, segment_name => p_segname,
  60.                           segment_type => p_type, partition_name => p_partition,
  61.                           total_blocks => l_total_blocks,
  62.                           total_bytes => l_total_bytes,
  63.                           unused_blocks => l_unused_blocks,
  64.                           unused_bytes => l_unused_bytes,
  65.                           last_used_extent_file_id => l_lastusedextfileid,
  66.                           last_used_extent_block_id => l_lastusedextblockid,
  67.                           last_used_block => l_last_used_block);

  68.   p('Total Blocks ', l_total_blocks);
  69.   p('Total Bytes ', l_total_bytes);
  70.   p('Total MBytes ', trunc(l_total_bytes / 1024 / 1024));
  71.   p('Unused Blocks', l_unused_blocks);
  72.   p('Unused Bytes ', l_unused_bytes);
  73.   p('Last Used Ext FileId', l_lastusedextfileid);
  74.   p('Last Used Ext BlockId', l_lastusedextblockid);
  75.   p('Last Used Block', l_last_used_block);
  76. END ;

  1. 呼叫方法:
  2. SQL> create table t1 as select * from dba_objects ;
  3. SQL> exec dbms_stats.gather_table_stats(user,'T1') ;
  4. SQL> set serverout on ;
  5. SQL> exec show_space('T1') ;
  6. Total Blocks ........................... 2,944
  7. Total Bytes ........................... 24,117,248
  8. Total MBytes ........................... 23
  9. Unused Blocks........................... 123
  10. Unused Bytes ........................... 1,007,616
  11. Last Used Ext FileId.................... 1
  12. Last Used Ext BlockId................... 107,904
  13. Last Used Block......................... 5

  14. PL/SQL 過程已成功完成。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28878983/viewspace-2136101/,如需轉載,請註明出處,否則將追究法律責任。

相關文章