Oracle统计、分析和优化环境配置

Oracle统计、分析和优化环境配置

创建批处理文件Login.bat

用于初始化设置系统环境

Login.bat

@echo off

title eoda

mode con cols=140

color 85

set ORACLE_SID=muphy

sqlplus eoda/foo

创建数据库脚本文件login.sql

用于初始化SQL*PLUS运行环境

define _editor=vim  --设置编辑器为vim

set serveroutput on size 1000000 --设置打开DBMS_OUTPUT并设置默认缓冲区

set trimspool on       --设置去除命令两端的空格

set long 5000   --设置long和clob列时默认显示字节数

set linesize 1000

set pagesize 9999

column plan_plus_exp format a80 

column table_name format a30     

column index_name format a30

column name format a30

column value format a30

column table format a30

set sqlprompt '&_user.@&_connect_identifier.> ' --设置提示符指示谁登陆到那个数据库

控制报告

SET AUTOTRANCE OFF

SET AUTOTRANCE ON EXPLAIN --只显示执行计划

SET AUTOTRANCE ON STATISTICS --只显示统计结果

SET AUTOTRANCE ONLY --不显示查询结果

SET AUTOTRACE TRANCEONLY EXPLAN --只显示执行计划

配置Statspack

$ sqlplus / as sysdba

sys/eoda > @spcreate

创建统计stats视图

需要有查看视图的权限

conn / as sysdba

grant select on v_$statname to eoda;

grant select on v_$mystat to eoda;

grant select on v_$latch to eoda;

grant select on v_$timer to eoda;

conn eoda/foo

drop table run_stats;

set echo on;

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

union all

select 'STAT...Elapsed Time', hsecs from v$timer;

创建存储统计结果的run_stats表

create global temporary table run_stats

( runid varchar2(15),

  name varchar2(80),

  value int )

on commit preserve rows;

创建包runstats_pkg

用于比较两个sql之间的性能,会测量3个要素:耗用时间、系统统计信息和闩定

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_cpu_time;

end;

 

procedure rs_middle

is

begin

  g_run1 := (dbms_utility.get_cpu_time-g_start);

  insert into run_stats

  select 'after 1', stats.* from stats;

  g_start := dbms_utility.get_cpu_time;

end;

 

procedure rs_stop(p_difference_threshold in number default 0)

is

begin

  g_run2 := (dbms_utility.get_cpu_time-g_start);

  dbms_output.put_line( 'Run1 ran in ' || g_run1 || ' cpu hsecs' );

  dbms_output.put_line( 'Run2 ran in ' || g_run2 || ' cpu hsecs' );

  if ( g_run2 <> 0 )

  then

    dbms_output.put_line

    ( 'run 1 ran in ' || round(g_run1/g_run2*100,2) ||

    '% of the time' );

  end if;

  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', 16 ) ||

  lpad( 'Run2', 16 ) || lpad( 'Diff', 16 ) );

  for x in

  ( select rpad( a.name, 30 ) ||

    to_char( b.value-a.value, '999,999,999,999' ) ||

    to_char( c.value-b.value, '999,999,999,999' ) ||

    to_char( ( (c.value-b.value)-(b.value-a.value)),

    '999,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 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', 14 ) || lpad( 'Run2', 19 ) ||

    lpad( 'Diff', 18 ) || lpad( 'Pct', 11 ) );

  for x in

  ( select to_char( run1, '9,999,999,999,999' ) ||

    to_char( run2, '9,999,999,999,999' ) ||

    to_char( diff, '9,999,999,999,999' ) ||

    to_char( round( run1/decode( run2, 0, to_number(0), 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;

/

创建mystat.sql和mystat2.sql查看统计结果

前者统计初始情况,或者统计执行sql之后的情况和报告差异

创建mystat.sql脚本文件

set echo off

set verify off

column value new_val V

define S="&1"

set autotrace off

select a.name, b.value

from v$statname a, v$mystat b

where a.statistic# = b.statistic#

and lower(a.name) = lower('&S')

/

set echo on

创建mystat2.sql脚本文件

set echo off

set verify off

select a.name, b.value V, to_char(b.value-&V,'999,999,999,999') diff

from v$statname a, v$mystat b

where a.statistic# = b.statistic#

and lower(a.name) = lower('&S')

/

set echo on

创建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 ) -- 分区对象的分区名

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;

    -- 内部存储过程,用于格式化打印

    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

   -- 动态sql判断对象是否ASSM

   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;

   -- 如果对象是ASSM 表空间, 调用dbms_space.space_usage

   -- 否则调用dbms_space.free_blocks

   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;

  -- 调用dbms_space.unused_space获取未使用的空间信息

  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;

/

 

posted @ 2019-05-19 17:58 明月心~ 阅读(...) 评论(...) 编辑 收藏