利用statspack来获取生成环境中top SQL及其执行计划

当面对复杂的生产环境中TopSQL问题时,可通过调整Statspack级别来获取更详细的SQL语句信息。文章介绍如何设置Statspack以获得高级别快照,并通过特定案例展示了如何利用这些信息来定位和解决性能问题。

作者: fuyuncat

来源: www.HelloDBA.com

在一些特殊情况下,生产环境中top SQL会让DBA捉摸不定。比如一个由复杂分支条件所生成的动态语句;第三方软件或者代码本身对会话环境进行了修改,导致无法正确重现问题语句的查询计划;语句过长导致无法获取正确的全部语句,等等。这时,我们可以考虑设置statspack为更高级别(默认级别为5),以获取相关语句的详细信息。具体过程如下:

获取级别6的statspack的快照:

SQL> exec statspack.SNAP(i_snap_level => 6);

N分钟后….

SQL> exec statspack.SNAP(i_snap_level => 6);

获取statspack报告

SQL> @?/rdbms/admin/spreport.sql
 
... …
Specify the number of days of snapshots to choose from
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed.  Pressing <return> without
specifying a number lists all completed snapshots.
 
 
 
Listing all Completed Snapshots
 
                                                       Snap
Instance     DB Name        Snap Id   Snap Started    Level Comment
------------ ------------ --------- ----------------- ----- --------------------
edgar        EDGAR                1 28 Dec 2005 15:32     5
 
                                 11 03 Jan 2006 09:54     5
                                 12 03 Jan 2006 09:54     5
 
                                 21 11 May 2007 13:33     6
                                 31 11 May 2007 13:34     6

在选取快照时,可以看到我们刚才生成的两个快照级别为6。

查看报告,可以看到报告内容比我们普通的statspack报告更加详细(这些不是本文重点,不做具体解释)。仔细观察top SQL部分,你会发现报告中多出了一列“Old Hash Value”,找到你需要的语句,记下它的这个hash值。

    CPU                  CPU per             Elapsd                   Old
  Time (s)   Executions  Exec (s)  %Total   Time (s)    Buffer Gets  Hash Value
---------- ------------ ---------- ------ ---------- --------------- ----------
     13.51           55       0.25   64.2      13.69          13,528 2547576380
Module: SQL*Plus
select a, b from ttt where b like 'aaa%'
 
… …

我们用Oracle提供的另一个statspack 报告脚本来生成这条SQL的报告:

SQL> @?/rdbms/admin/sprepsql.sql
 
… …
Listing all Completed Snapshots
 
                               Snap                    Snap
Instance     DB Name             Id   Snap Started    Level Comment
------------ ------------ --------- ----------------- ----- --------------------
edgar        EDGAR                1 28 Dec 2005 15:32     5
 
                                 11 03 Jan 2006 09:54     5
                                 12 03 Jan 2006 09:54     5
 
                                 21 11 May 2007 13:33     6
                                 31 11 May 2007 13:34     6
 
… …

还是选择刚才的两个快照:

Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 21
Begin Snapshot Id specified: 21
 
Enter value for end_snap: 31
End   Snapshot Id specified: 31
 

这儿要求输入hash值,把刚才得到的hash值输入:

 
Specify the old (i.e. pre-10g) Hash Value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for hash_value: 2547576380
 

生成了SQL报告,看下报告,里面有SQL的完整语句和它的查询计划。

SQL Text
~~~~~~~~
select a, b from ttt where b like 'aaa%'
 
Plans in shared pool between Begin and End Snap Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Shows the Execution Plans found in the shared pool between the begin and end
snapshots specified.  The values for Rows, Bytes and Cost shown below are those
which existed at the time the first-ever snapshot captured this plan - these
values often change over time, and so may not be indicative of current values
-> Rows indicates Cardinality, PHV is Plan Hash Value
-> ordered by Plan Hash Value
 
--------------------------------------------------------------------------------
| Operation                      | PHV/Object Name     |  Rows | Bytes|   Cost |
--------------------------------------------------------------------------------
|SELECT STATEMENT                |     2 |   60    |      |     56 |
|  TABLE ACCESS FULL            |                     |     2 |   60 |       56 |
--------------------------------------------------------------------------------

我们有一个案例就是通过这个方法定位到问题愿意,具体参见《NLS参数设置导致的性能问题案例》

要注意的是,获取高级别的statspack报告将会消耗更多的系统性能,因此不建议将日常监控的statspack级别设置过高。

转载于:https://www.cnblogs.com/jimeper/archive/2007/09/27/908404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值