HelloDBA [English]
搜索Internet 搜索 HelloDBABA
  Oracle技术站。email: fuyuncat@gmail.com  MSN: fuyuncat@hotmail.com   acoug  acoug 

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

[English]

作者: fuyuncat

来源: www.HelloDBA.com

日期: 2008-11-07 14:51:07

分享到  新浪微博 腾讯微博 人人网 i贴吧 开心网 豆瓣 淘宝 推特 Facebook GMail Blogger Orkut Google Bookmarks

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

获取级别6statspack的快照:

 

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级别设置过高。

 

Top

Copyright ©2005,HelloDBA.Com 保留一切权利

申明
by fuyuncat