ITPub博客

首页 > 数据库 > Oracle > TOM大神的show_space()初体验

TOM大神的show_space()初体验

原创 Oracle 作者:呱呱牛 时间:2016-06-27 15:42:21 0 删除 编辑
    在某次公开课上,发现老师使用存错过程show_space()来打印出表的块使用信息,非常方便。经过查询资料终于找到Tom kyte 大神的原作,我在这里体验一次使用SQL脚本的便利性。

点击(此处)折叠或打开

  1. -- -----------------------------------------------------------------------------------
  2.   -- Author : Tom Kyte
  3.   -- Description : Displays free and unused space for the specified object.
  4.   -- Call Syntax : EXEC Show_Space('Tablename');
  5.   -- Requirements : SET SERVEROUTPUT ON
  6.   -- Last Modified: June 22, 2010
  7.   -- This enhance version has all the fixes for ASSM, LMT, partitions etc (Oracle version 10gr2 +)
  8.   -- -----------------------------------------------------------------------------------
  9.   create or replace procedure show_space(p_segname in varchar2,
  10.                                          p_owner in varchar2 default user,
  11.                                          p_type in varchar2 default 'TABLE',
  12.                                          p_partition in varchar2 default NULL)
  13.   -- this procedure uses authid current user so it can query DBA_*
  14.   -- views using privileges from a ROLE and so it can be installed
  15.   -- once per database, instead of once per user that wanted to use it
  16.   authid current_user as
  17.   l_free_blks number;
  18.   l_total_blocks number;
  19.   l_total_bytes number;
  20.   l_unused_blocks number;
  21.   l_unused_bytes number;
  22.   l_LastUsedExtFileId number;
  23.   l_LastUsedExtBlockId number;
  24.   l_LAST_USED_BLOCK number;
  25.   l_segment_space_mgmt varchar2(255);
  26.   l_unformatted_blocks number;
  27.   l_unformatted_bytes number;
  28.   l_fs1_blocks number;
  29.   l_fs1_bytes number;
  30.   l_fs2_blocks number;
  31.   l_fs2_bytes number;
  32.   l_fs3_blocks number;
  33.   l_fs3_bytes number;
  34.   l_fs4_blocks number;
  35.   l_fs4_bytes number;
  36.   l_full_blocks number;
  37.   l_full_bytes number;
  38.   -- inline procedure to print out numbers nicely formatted
  39.   -- with a simple label
  40.   procedure p(p_label in varchar2, p_num in number) is
  41.   begin
  42.     dbms_output.put_line(rpad(p_label, 40, '.') ||
  43.                          to_char(p_num, '999,999,999,999'));
  44.   end;
  45. begin
  46.   -- this query is executed dynamically in order to allow this procedure
  47.   -- to be created by a user who has access to DBA_SEGMENTS/TABLESPACES
  48.   -- via a role as is customary.
  49.   -- NOTE: at runtime, the invoker MUST have access to these two
  50.   -- views!
  51.   -- this query determines if the object is a ASSM object or not
  52.   begin
  53.     execute immediate 'select ts.segment_space_management
  54.             from dba_segments seg, dba_tablespaces ts
  55.            where seg.segment_name = :p_segname
  56.              and (:p_partition is null or
  57.                 seg.partition_name = :p_partition)
  58.              and seg.owner = :p_owner
  59.              and seg.segment_type = :p_type
  60.              and seg.tablespace_name = ts.tablespace_name'
  61.       into l_segment_space_mgmt
  62.       using p_segname, p_partition, p_partition, p_owner, p_type;
  63.   exception
  64.     when too_many_rows then
  65.       dbms_output.put_line('This must be a partitioned table, use p_partition => ');
  66.       return;
  67.   end;
  68.   -- if the object is in an ASSM tablespace, we must use this API
  69.   -- call to get space information, else we use the FREE_BLOCKS
  70.   -- API for the user managed segments
  71.   if l_segment_space_mgmt = 'AUTO' then
  72.     dbms_space.space_usage(p_owner,
  73.                            p_segname,
  74.                            p_type,
  75.                            l_unformatted_blocks,
  76.                            l_unformatted_bytes,
  77.                            l_fs1_blocks,
  78.                            l_fs1_bytes,
  79.                            l_fs2_blocks,
  80.                            l_fs2_bytes,
  81.                            l_fs3_blocks,
  82.                            l_fs3_bytes,
  83.                            l_fs4_blocks,
  84.                            l_fs4_bytes,
  85.                            l_full_blocks,
  86.                            l_full_bytes,
  87.                            p_partition);
  88.     p('Unformatted Blocks ', l_unformatted_blocks);
  89.     p('FS1 Blocks (0-25) ', l_fs1_blocks);
  90.     p('FS2 Blocks (25-50) ', l_fs2_blocks);
  91.     p('FS3 Blocks (50-75) ', l_fs3_blocks);
  92.     p('FS4 Blocks (75-100)', l_fs4_blocks);
  93.     p('Full Blocks ', l_full_blocks);
  94.   else
  95.     dbms_space.free_blocks(segment_owner => p_owner,
  96.                            segment_name => p_segname,
  97.                            segment_type => p_type,
  98.                            freelist_group_id => 0,
  99.                            free_blks => l_free_blks);
  100.     p('Free Blocks', l_free_blks);
  101.   end if;
  102.   -- and then the unused space API call to get the rest of the
  103.   -- information
  104.   dbms_space.unused_space(segment_owner => p_owner,
  105.                           segment_name => p_segname,
  106.                           segment_type => p_type,
  107.                           partition_name => p_partition,
  108.                           total_blocks => l_total_blocks,
  109.                           total_bytes => l_total_bytes,
  110.                           unused_blocks => l_unused_blocks,
  111.                           unused_bytes => l_unused_bytes,
  112.                           LAST_USED_EXTENT_FILE_ID => l_LastUsedExtFileId,
  113.                           LAST_USED_EXTENT_BLOCK_ID => l_LastUsedExtBlockId,
  114.                           LAST_USED_BLOCK => l_LAST_USED_BLOCK);
  115.   p('Total Blocks', l_total_blocks);
  116.   p('Total Bytes', l_total_bytes);
  117.   p('Total MBytes', trunc(l_total_bytes / 1024 / 1024));
  118.   p('Unused Blocks', l_unused_blocks);
  119.   p('Unused Bytes', l_unused_bytes);
  120.   p('Last Used Ext FileId', l_LastUsedExtFileId);
  121.   p('Last Used Ext BlockId', l_LastUsedExtBlockId);
  122.   p('Last Used Block', l_LAST_USED_BLOCK);
  123. end;
 因为是在sys用户下创建的show_space()存储过程,如果想被其他用户可以使用如何操作呢,方法如下:使用同义词的方式

点击(此处)折叠或打开

  1. SQL> create public synonym show_space for sys.show_space;

  2. Synonym created.

  3. SQL> grant execute on show_space to public ;

  4. Grant succeeded.
这样其他用户就可以正常的使用show_space()存储过程来统计表的块的信息了。





来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27039319/viewspace-2121051/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-05-29

  • 博文量
    96
  • 访问量
    286807