`
itspace
  • 浏览: 984991 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

show_space&runstats

阅读更多
收录2个tkyte写的监控脚本,以备后用
1.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 who 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 an 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;
/


2.runstats
引用
The table we need is very simple:

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

then you can create this view:

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;

Now the test harness package itself is very simple. Here it is:

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

/*
exec runStats_pkg.rs_start;
exec runStats_pkg.rs_middle;
exec runStats_pkg.rs_stop;
*/



分享到:
评论

相关推荐

    runstats.sql

    ### runstats.sql 知识点解析 #### 一、脚本概述 `runstats.sql` 是一个用于评估不同方法在 Oracle 数据库上执行效率的测试脚本。此脚本通过对比两种不同方法所需的时间和资源消耗来判断哪种方法更为高效。 #### ...

    DB2如何评估索引碎片是否是缓慢的RUNSTATS根

    RUNSTATS命令的性能对数据库维护周期的效率有直接影响,因此,当索引碎片化影响了RUNSTATS的执行速度时,就需要特别关注。 索引碎片通常是由表空间内的物理数据碎片化导致的,这可能是因为频繁的INSERT和UPDATE操作...

    Python库 | runstats-2.0.0-cp37-cp37m-win_amd64.whl

    资源分类:Python库 所属语言:Python 资源全名:runstats-2.0.0-cp37-cp37m-win_amd64.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python-runstats:一次计算统计量和回归量的Python模块

    RunStats:一站式计算统计和回归 是Apache2许可的Python模块,用于在线统计和在线回归。 统计信息和回归摘要均通过一次计算。 以前的值不会记录在摘要中。 长时间运行的系统通常会生成汇总性能的数字。 可能是响应...

    DB2调优技巧

    ### DB2调优技巧详解 #### 一、性能优化的核心要素 DB2调优的核心在于提升性能,确保数据库能够高效、稳定地运行。这不仅涉及硬件配置,也涵盖软件设置与应用设计。以下是对关键性能要素的深入解析: ...

    DB2数据库优化.doc

    DB2 数据库优化是提升数据库性能的关键步骤,主要包括运行统计(Runstats)和重组(Reorg)两大方面。本文将详细介绍这两个概念以及何时和如何执行它们。 **一、Runstats** 1. **Runstats的作用**:Runstats命令...

    DB2 日常维护指南,第 3 部分

    本文将详细介绍如何制定和执行DB2数据库的日常维护规范,重点包括运行runstats和reorg这两个重要的数据库维护活动。 首先,DB2优化器依赖于表和索引的统计信息来选择查询的最优访问路径。统计信息如果过时或不准确...

    DB2数据库管理最佳实践笔记-10日常运维.docx

    - **Runstats**:Runstats的主要作用是收集统计信息,以便DB2优化器能够更好地决定查询执行路径。通过Runstats,可以了解表中数据的大致情况,包括行数、不同值的数量等,从而帮助优化器选择最优的执行计划。 - **...

    数据库优化(完整版)实用资料.doc

    数据库优化是提升数据库性能的关键环节,它涉及到对数据库的运行统计信息收集(runstats)和重组操作(reorg)。这两个方面对于确保数据库高效运行和优化查询执行计划具有重要作用。 一、runstats runstats 是...

    DB2数据库管理最佳实践笔记-10日常运维.pdf

    本文将深入探讨其中的重要工具——Runstats、Reorg和Rebind,以及它们在DB2数据库维护中的应用。 **1. Runstats - 统计信息收集** Runstats是一个用于收集表和索引统计信息的工具,它的主要目的是为DB2优化器提供...

    DB2数据库管理最佳实践笔记-10日常运维 (2).docx

    1. Runstats:Runstats是收集统计信息的工具,它的主要任务是为DB2优化器提供关于表和索引的详细信息,以便优化器在执行查询时选择最佳的访问路径。统计信息包括表和索引的行数、唯一值的数量以及数据分布。数据分布...

    DB2数据库管理最佳实践笔记-10日常运维 (2).pdf

    1. **Runstats**:Runstats是一个用于收集数据库对象(如表和索引)统计信息的工具。这些信息为DB2优化器提供决策依据,以确定执行SQL语句的最佳路径。统计信息包括行数、唯一值的数量、数据分布等。数据分布有两种...

    DB2数据库分区DPF

    - `runstats on table <table_name> with distribution and detailed indexes all`:收集表`<table_name>`上的统计信息,包括分布信息和详细索引信息。 9. **监控与维护**: - 使用`db2 list nodes`与`db2 list ...

    在DB2数据库里面如何更新执行计划

    在实际项目中,通常将RUNSTATS与REORG结合使用,先执行REORG,再运行RUNSTATS,最后使用REBIND更新数据库以获得最新的执行计划。 对于数据快速变化的表,例如在电信移动行业的月末汇总表,RUNSTATS可能无法提供准确...

    IBM WebSphere Portal系统性能调优

    db2 -x -r "runstats on table (SELECT rtrim(concat('runstats on table', concat(rtrim(tabSchema), concat('.', concat(rtrim(tabName), 'on all columns with distribution on all columns and sampled detailed...

    sql_performance_turning

    本文将详细介绍一系列用于提高 SQL 性能的工具和技术,包括 SQL*Plus、EXPLAIN PLAN、AUTOTRACE、TKPROF、Runstats、Statspack、DBMS_PROFILER 以及 JDeveloper。这些工具不仅能够帮助我们理解查询执行的过程,还...

    DB2常用命令

    ' from syscat.tables where tabschema='DB2' and type='T'"`:此命令用于构建一个SQL语句列表,该列表包括对所有属于模式`DB2`的表进行`runstats`操作的命令,以优化其分布和详细索引。 - `db2 "call 清空(12)"`:...

Global site tag (gtag.js) - Google Analytics