print_table,show_space,stat等,一些常用工具

atlantisholic發表於2011-03-21

1.print_table

==============================================================

CREATE OR REPLACE PROCEDURE print_table (
   p_query      IN   VARCHAR2,
   p_date_fmt   IN   VARCHAR2 DEFAULT 'dd-mon-yyyy hh24:mi:ss'
)
-- this utility is designed to be installed ONCE in a database and used
-- by all.  Also, it is nice to have roles enabled so that queries by
-- DBA's that use a role to gain access to the DBA_* views still work
-- that is the purpose of AUTHID CURRENT_USER
AUTHID CURRENT_USER
IS
   l_thecursor     INTEGER           DEFAULT DBMS_SQL.open_cursor;
   l_columnvalue   VARCHAR2 (4000);
   l_status        INTEGER;
   l_desctbl       DBMS_SQL.desc_tab;
   l_colcnt        NUMBER;
   l_cs            VARCHAR2 (255);
   l_date_fmt      VARCHAR2 (255);

   -- small inline procedure to restore the sessions state
   -- we may have modified the cursor sharing and nls date format
   -- session variables, this just restores them
   PROCEDURE restore
   IS
   BEGIN
      IF (UPPER (l_cs) NOT IN ('FORCE', 'SIMILAR'))
      THEN
         EXECUTE IMMEDIATE 'alter session set cursor_sharing=exact';
      END IF;

      IF (p_date_fmt IS NOT NULL)
      THEN
         EXECUTE IMMEDIATE    'alter session set nls_date_format='''
                           || l_date_fmt
                           || '''';
      END IF;

      DBMS_SQL.close_cursor (l_thecursor);
   END restore;
BEGIN
   -- I like to see the dates print out with times, by default, the
   -- format mask I use includes that.  In order to be "friendly"
   -- we save the date current sessions date format and then use
   -- the one with the date and time.  Passing in NULL will cause
   -- this routine just to use the current date format
   IF (p_date_fmt IS NOT NULL)
   THEN
      SELECT SYS_CONTEXT ('userenv', 'nls_date_format')
        INTO l_date_fmt
        FROM DUAL;

      EXECUTE IMMEDIATE    'alter session set nls_date_format='''
                        || p_date_fmt
                        || '''';
   END IF;

   -- to be bind variable friendly on this ad-hoc queries, we
   -- look to see if cursor sharing is already set to FORCE or
   -- similar, if not, set it so when we parse -- literals
   -- are replaced with binds
   IF (DBMS_UTILITY.get_parameter_value ('cursor_sharing', l_status, l_cs) = 1
      )
   THEN
      IF (UPPER (l_cs) NOT IN ('FORCE', 'SIMILAR'))
      THEN
         EXECUTE IMMEDIATE 'alter session set cursor_sharing=force';
      END IF;
   END IF;

   -- parse and describe the query sent to us.  we need
   -- to know the number of columns and their names.
   DBMS_SQL.parse (l_thecursor, p_query, DBMS_SQL.native);
   DBMS_SQL.describe_columns (l_thecursor, l_colcnt, l_desctbl);

   -- define all columns to be cast to varchar2's, we
   -- are just printing them out
   FOR i IN 1 .. l_colcnt
   LOOP
      IF (l_desctbl (i).col_type NOT IN (113))
      THEN
         DBMS_SQL.define_column (l_thecursor, i, l_columnvalue, 4000);
      END IF;
   END LOOP;

   -- execute the query, so we can fetch
   l_status := DBMS_SQL.EXECUTE (l_thecursor);

   -- loop and print out each column on a separate line
   -- bear in mind that dbms_output only prints 255 characters/line
   -- so we'll only see the first 200 characters by my design...
   WHILE (DBMS_SQL.fetch_rows (l_thecursor) > 0)
   LOOP
      FOR i IN 1 .. l_colcnt
      LOOP
         IF (l_desctbl (i).col_type NOT IN (113))
         THEN
            DBMS_SQL.COLUMN_VALUE (l_thecursor, i, l_columnvalue);
            DBMS_OUTPUT.put_line (   RPAD (l_desctbl (i).col_name, 30)
                                  || ': '
                                  || SUBSTR (l_columnvalue, 1, 200)
                                 );
         END IF;
      END LOOP;

      DBMS_OUTPUT.put_line ('-----------------');
   END LOOP;

   -- now, restore the session state, no matter what
   restore;
EXCEPTION
   WHEN OTHERS
   THEN
      restore;
      RAISE;
END;

===========================================================

2.show_space

============================================================

create or replace procedure show_space
( p_segname in varchar2,
  p_owner   in varchar2 default user,
  p_type    in varchar2 default 'TABLE',
  p_partition in varchar2 default NULL )
-- this procedure uses authid current user so it can query DBA_*
-- views using privileges from a ROLE and so it can be installed
-- once per database, instead of once per user that wanted to use it
authid current_user
as
    l_free_blks                 number;
    l_total_blocks              number;
    l_total_bytes               number;
    l_unused_blocks             number;
    l_unused_bytes              number;
    l_LastUsedExtFileId         number;
    l_LastUsedExtBlockId        number;
    l_LAST_USED_BLOCK           number;
    l_segment_space_mgmt        varchar2(255);
    l_unformatted_blocks number;
    l_unformatted_bytes number;
    l_fs1_blocks number; l_fs1_bytes number;
    l_fs2_blocks number; l_fs2_bytes number;
    l_fs3_blocks number; l_fs3_bytes number;
    l_fs4_blocks number; l_fs4_bytes number;
    l_full_blocks number; l_full_bytes number;

        -- inline procedure to print out numbers nicely formatted
        -- with a simple label
    procedure p( p_label in varchar2, p_num in number )
    is
    begin
        dbms_output.put_line( rpad(p_label,40,'.') ||
                              to_char(p_num,'999,999,999,999') );
    end;
begin
   -- this query is executed dynamically in order to allow this procedure
   -- to be created by a user who has access to DBA_SEGMENTS/TABLESPACES
   -- via a role as is customary.
   -- NOTE: at runtime, the invoker MUST have access to these two
   -- views!
   -- this query determines if the object is a ASSM object or not
   begin
      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'
             into l_segment_space_mgmt
            using p_segname, p_partition, p_partition, p_owner;
   exception
       when too_many_rows then
          dbms_output.put_line
          ( 'This must be a partitioned table, use p_partition => ');
          return;
   end;


   -- if the object is in an ASSM tablespace, we must use this API
   -- call to get space information, else we use the FREE_BLOCKS
   -- API for the user managed segments
   if l_segment_space_mgmt = 'AUTO'
   then
     dbms_space.space_usage
     ( p_owner, p_segname, p_type, l_unformatted_blocks,
       l_unformatted_bytes, l_fs1_blocks, l_fs1_bytes,
       l_fs2_blocks, l_fs2_bytes, l_fs3_blocks, l_fs3_bytes,
       l_fs4_blocks, l_fs4_bytes, l_full_blocks, l_full_bytes, p_partition);

     p( 'Unformatted Blocks ', l_unformatted_blocks );
     p( 'FS1 Blocks (0-25)  ', l_fs1_blocks );
     p( 'FS2 Blocks (25-50) ', l_fs2_blocks );
     p( 'FS3 Blocks (50-75) ', l_fs3_blocks );
     p( 'FS4 Blocks (75-100)', l_fs4_blocks );
     p( 'Full Blocks        ', l_full_blocks );
  else
     dbms_space.free_blocks(
       segment_owner     => p_owner,
       segment_name      => p_segname,
       segment_type      => p_type,
       freelist_group_id => 0,
       free_blks         => l_free_blks);

     p( 'Free Blocks', l_free_blks );
  end if;

  -- and then the unused space API call to get the rest of the
  -- information
  dbms_space.unused_space
  ( segment_owner     => p_owner,
    segment_name      => p_segname,
    segment_type      => p_type,
    partition_name    => p_partition,
    total_blocks      => l_total_blocks,
    total_bytes       => l_total_bytes,
    unused_blocks     => l_unused_blocks,
    unused_bytes      => l_unused_bytes,
    LAST_USED_EXTENT_FILE_ID => l_LastUsedExtFileId,
    LAST_USED_EXTENT_BLOCK_ID => l_LastUsedExtBlockId,
    LAST_USED_BLOCK => l_LAST_USED_BLOCK );

    p( 'Total Blocks', l_total_blocks );
    p( 'Total Bytes', l_total_bytes );
    p( 'Total MBytes', trunc(l_total_bytes/1024/1024) );
    p( 'Unused Blocks', l_unused_blocks );
    p( 'Unused Bytes', l_unused_bytes );
    p( 'Last Used Ext FileId', l_LastUsedExtFileId );
    p( 'Last Used Ext BlockId', l_LastUsedExtBlockId );
    p( 'Last Used Block', l_LAST_USED_BLOCK );
end;
/

========================================================

3.stat統計系統資訊

========================================================

--grant select on sys.v_$mystat to test;
--grant select on sys.v_$statname to test;
--grant select on sys.v_$latch to test;
------------------------------------------------------------------------
create or replace view stats
as select 'STAT...'||a.name name,b.value from v$statname a,v$mystat b
where a.statistic#=b.statistic#
union all
select 'LATCH.'||name,gets from v$latch;


-----------------------------------------------------------------------------
drop table run_stats;
create  global temporary table run_stats(
runid varchar2(15),
name  varchar2(80),
value int)
on commit preserve rows;


---------------------------------------------------------------------------
create or replace package runstats_pkg
as
 procedure rs_start;
 procedure rs_middle;
 procedure rs_stop(p_difference_threshold in number default 0);
end;
/

create or replace package body runstats_pkg
as
 g_start number;
 g_run1 number;
 g_run2 number;
procedure rs_start
is
begin
 delete from run_stats;
 insert into run_stats select 'before',stats.* from stats;
 g_start:=dbms_utility.get_time;
end;

procedure rs_middle
is
begin
 g_run1:=(dbms_utility.get_time-g_start);
 insert into run_stats select 'after 1',stats.* from stats;
 g_start:=dbms_utility.get_time;
end;

procedure rs_stop(p_difference_threshold in number default 0)
is
begin
 g_run2:=(dbms_utility.get_time-g_start);
 dbms_output.put_line('Run1 ran in '||g_run1||'hsecs');
 dbms_output.put_line('Run2 ran in '||g_run2||'hsecs');
 dbms_output.put_line('run 1 ran in '||round(g_run1/g_run2*100,2)||'% of the time');
 dbms_output.put_line(chr(9));
 insert into run_stats select 'after 2', stats.* from stats;
 dbms_output.put_line( rpad( 'Name', 30 ) || lpad( 'Run1', 12 ) ||lpad( 'Run2', 12 ) || lpad( 'Diff', 12 ) );

 for x in ( select rpad( a.name, 30 ) ||
  to_char( b.value-a.value, '999,999,999' ) ||
  to_char( c.value-b.value, '999,999,999' ) ||
  to_char( ( (c.value-b.value)-(b.value-a.value)), '999,999,999' ) data
  from run_stats a, run_stats b, run_stats c
  where a.name = b.name
  and b.name = c.name
  and a.runid = 'before'
  and b.runid = 'after 1'
  and c.runid = 'after 2'
  and (c.value-a.value) > 0
  and abs( (c.value-b.value) - (b.value-a.value) )> p_difference_threshold
  order by abs( (c.value-b.value)-(b.value-a.value))
  ) loop
  dbms_output.put_line( x.data );
 end loop;

 dbms_output.put_line( chr(9) );
 dbms_output.put_line( 'Run1 latches total versus runs -- difference and pct' );
 dbms_output.put_line( lpad( 'Run1', 12 ) || lpad( 'Run2', 12 ) ||lpad( 'Diff', 12 ) || lpad( 'Pct', 10 ) );

 for x in ( select to_char( run1, '999,999,999' ) ||
  to_char( run2, '999,999,999' ) ||
  to_char( diff, '999,999,999' ) ||
  to_char( round( run1/run2*100,2 ), '99,999.99' ) || '%' data
  from ( select sum(b.value-a.value) run1, sum(c.value-b.value) run2,
    sum( (c.value-b.value)-(b.value-a.value)) diff
    from run_stats a, run_stats b, run_stats c
    where a.name = b.name
    and b.name = c.name
    and a.runid = 'before'
    and b.runid = 'after 1'
    and c.runid = 'after 2'
    and a.name like 'LATCH%'
   )
  ) loop
  dbms_output.put_line( x.data );
 end loop;
end;

end;
/

=============================================================

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

相關文章