《剑破冰山》读书之开发常用工具

一、awr使用
 
从8版本开始oracle就提供了强大的性能分析工具,可以通过statspack报表观察和分析。
statspack使用起来不是太方便,需要自己安装和规划安装statspack的用户和表空间,还需要直接设置历史信息清理策略,否则表空间会无限扩大。
从10g开始提供了awr代替statspack,进行了很大的改进。
awr自动工作负载信息库。
Automatic Workload Repository
安装oracle时候自动安装,无需另外安装。
已经考虑了统计信息收集的清理问题,在默认情况下报了一周的统计信息。
可以和statspack共存。
 
默认收集7天的采集信息。
SQL> select * from dba_hist_wr_control ;
 
      DBID SNAP_INTERVAL                           RETENTION                               TOPNSQL
---------- --------------------------------------- --------------------------------------- ----------
1991219404 +00000 01:00:00.0                       +00007 00:00:00.0                       DEFAULT
可以通过 dbms_workload_repository工具包实现。
将间隔时间设置为30分钟,保留时间设置为7天。
SQL> begin
  2  dbms_workload_repository.modify_snapshot_settings(interval=>30,retention=>10*24*60);
  3  end ;
  4  /
此时可以查看一下awr设置信息
 
SQL> select * from dba_hist_wr_control ;
 
      DBID SNAP_INTERVAL                           RETENTION                               TOPNSQL
---------- --------------------------------------- --------------------------------------- ----------
1991219404 +00000 00:30:00.0                       +00010 00:00:00.0                       DEFAULT
 
报表使用:
使用脚本生成
SQL> @/usr/oracle/product/10.2.0/rdbms/admin/awrrpt.sql
生成文件的位置为:
可以通过find命令查找一下具体位置
[root@xxtdwhtest ~]# find /usr/oracle/  -name awrrpt_1_630_640.html
/usr/oracle/product/10.2.0/network/admin/awrrpt_1_630_640.html
 
可以把里面的内容下载下来,linux下载命令sz 需要下载的文件名。
[root@xxtdwhtest admin]# ls
awrrpt_1_630_640.html  awrrpt_2_631_640.txt  samples     sqlnet.log
awrrpt_1_631_639.html  listener.ora          shrept.lst  tnsnames.ora
[root@xxtdwhtest admin]# sz awrrpt_*
rz
正在开始 zmodem 传输。 按 Ctrl+C 取消。
正在传输 awrrpt_1_630_640.html...
  100%     237 KB   39 KB/s 00:00:06       0 错误
正在传输 awrrpt_1_631_639.html...
  100%     236 KB  236 KB/s 00:00:01       0 错误
正在传输 awrrpt_2_631_640.txt...
  100%     101 KB  101 KB/s 00:00:01       0 错误
 
 
 
如何分析awr报表。
 

Load Profile

Per SecondPer Transaction
Redo size:1,122.8877,285.67
Logical reads:48.053,306.90
Block changes:6.44443.18
Physical reads:7.55519.99
Physical writes:0.3222.37
User calls:0.4128.37
Parses:0.5839.71
Hard parses:0.042.44
Sorts:0.4329.41
Logons:0.010.44
Executes:1.57108.13
Transactions:0.01 

可以看Transactions来分析系统的压力负责。

Top 5 Timed Events

EventWaitsTime(s)Avg Wait(ms)% Total Call TimeWait Class
db file sequential read115,819204227.6User I/O
CPU time 117 15.9 
db file scattered read10,0926068.1User I/O
latch: shared pool25212481.6Concurrency
control file parallel write10,7921211.6System I/O

看等待时间。db file sequential read于sql执行有关,

如果日志写log_file switch等待时间产生的原因,一般是当日志组写完后,lgwr试图写下一个log file ,如果这时候数据库没有完成记录在第一个logfile中的dirty块,就会出现这个事件,标识dbwr写速度太慢,解决方法就是增加额外的dbwr或者增加日志组和日志文件大小。

具体每个日志的平均读写的大小量计算公式:
根据

Instance Activity Stats

StatisticTotalper Secondper Trans
CPU used by this session7,1090.2215.13
CPU used when call started1,0980.032.34
CR blocks created1,3880.042.95
Cached Commit SCN referenced35,1941.0974.88
Commit SCN cached380.000.08
DB time3,334,772103.097,095.26
DBWR checkpoint buffers written5,4920.1711.69
DBWR checkpoints00.000.00
DBWR object drop buffers written00.000.00
DBWR revisited being-written buffer00.000.00
DBWR thread checkpoint buffers written00.000.00
DBWR transaction table writes1,1370.042.42
DBWR undo block writes3,5550.117.56
IMU CR rollbacks140.000.03
IMU Flushes6,5950.2014.03
IMU Redo allocation size634,93219.631,350.92
IMU commits3250.010.69
IMU contention30.000.01
IMU ktichg flush230.000.05
IMU pool not allocated00.000.00
IMU recursive-transaction flush20.000.00
IMU undo allocation size2,963,81691.626,305.99
IMU- failed to get a private strand00.000.00
Misses for writing mapping10.000.00
SMON posted for undo segment recovery00.000.00
SMON posted for undo segment shrink10.000.00
SQL*Net roundtrips to/from client11,3050.3524.05
SQL*Net roundtrips to/from dblink00.000.00
active txn count during cleanout5140.021.09
application wait time00.000.00
background checkpoints completed00.000.00
background checkpoints started00.000.00
background timeouts103,5243.20220.26
branch node splits00.000.00
buffer is not pinned count681,22621.061,449.42
buffer is pinned count814,01425.161,731.94
bytes received via SQL*Net from client1,857,03657.413,951.14
bytes received via SQL*Net from dblink00.000.00
bytes sent via SQL*Net to client2,571,36779.495,470.99
bytes sent via SQL*Net to dblink00.000.00
calls to get snapshot scn: kcmgss193,4885.98411.68
calls to kcmgas114,6253.54243.88
calls to kcmgcs8820.031.88
change write time1,4150.043.01
cleanout - number of ktugct calls1,0520.032.24
cleanouts and rollbacks - consistent read gets70.000.01
cleanouts only - consistent read gets3370.010.72
cluster key scan block gets138,3094.28294.27
cluster key scans47,0911.46100.19
commit batch performed50.000.01
commit batch requested50.000.01
commit batch/immediate performed420.000.09
commit batch/immediate requested420.000.09
commit cleanout failures: block lost810.000.17
commit cleanout failures: buffer being written20.000.00
commit cleanout failures: callback failure1180.000.25
commit cleanout failures: cannot pin00.000.00
commit cleanouts9,2080.2819.59
commit cleanouts successfully completed9,0070.2819.16
commit immediate performed370.000.08
commit immediate requested370.000.08
commit txn count during cleanout8680.031.85
concurrency wait time1,7400.053.70
consistent changes4,2560.139.06
consistent gets1,331,33341.162,832.62
consistent gets - examination501,37515.501,066.76
consistent gets direct1430.000.30
consistent gets from cache1,331,19041.152,832.32
current blocks converted for CR00.000.00
cursor authentications1380.000.29
data blocks consistent reads - undo records applied1,4880.053.17
db block changes208,2936.44443.18
db block gets222,9106.89474.28
db block gets direct1,1430.042.43
db block gets from cache221,7676.86471.84
deferred (CURRENT) block cleanout applications4,9390.1510.51
dirty buffers inspected2,6840.085.71
enqueue conversions6,5330.2013.90
enqueue releases322,5289.97686.23
enqueue requests322,5289.97686.23
enqueue timeouts00.000.00
enqueue waits10.000.00
exchange deadlocks00.000.00
execute count50,8211.57108.13
failed probes on index block reclamation00.000.00
free buffer inspected210,3896.50447.64
free buffer requested248,3867.68528.48
heap block compress3830.010.81
hot buffers moved to head of LRU13,9040.4329.58
immediate (CR) block cleanout applications3440.010.73
immediate (CURRENT) block cleanout applications2,8850.096.14
index crx upgrade (found)00.000.00
index crx upgrade (positioned)22,8430.7148.60
index fast full scans (full)7080.021.51
index fetch by key302,2939.34643.18
index scans kdiixs185,1092.63181.08
leaf node 90-10 splits1280.000.27
leaf node splits2880.010.61
lob reads1,8800.064.00
lob writes7,8090.2416.61
lob writes unaligned7,8090.2416.61
logons cumulative2050.010.44
messages received6,1910.1913.17
messages sent6,1910.1913.17
no buffer to keep pinned count00.000.00
no work - consistent read gets725,51922.431,543.66
opened cursors cumulative31,1890.9666.36
parse count (failures)30.000.01
parse count (hard)1,1480.042.44
parse count (total)18,6640.5839.71
parse time cpu3340.010.71
parse time elapsed3,8070.128.10
physical read IO requests125,8643.89267.80
physical read bytes2,002,092,03261,890.124,259,770.28
physical read total IO requests143,1444.42304.56
physical read total bytes2,283,642,88070,593.634,858,814.64
physical read total multi block requests10,0920.3121.47
physical reads244,3967.55519.99
physical reads cache244,2537.55519.69
physical reads cache prefetch118,5323.66252.20
physical reads direct1430.000.30
physical reads direct (lob)1400.000.30
physical reads direct temporary tablespace00.000.00
physical reads prefetch warmup00.000.00
physical write IO requests6,4560.2013.74
physical write bytes86,122,4962,662.28183,239.35
physical write total IO requests40,5641.2586.31
physical write total bytes665,353,72820,567.901,415,646.23
physical write total multi block requests2,7490.085.85
physical writes10,5130.3222.37
physical writes direct1,1430.042.43
physical writes direct (lob)50.000.01
physical writes direct temporary tablespace1,1380.042.42
physical writes from cache9,3700.2919.94
physical writes non checkpoint5,9720.1812.71
pinned buffers inspected80.000.02
prefetch warmup blocks aged out before use00.000.00
prefetched blocks aged out before use15,8870.4933.80
process last non-idle time32,3181.0068.76
recursive aborts on index block reclamation00.000.00
recursive calls533,38116.491,134.85
recursive cpu usage6,2560.1913.31
redo blocks written74,8792.31159.32
redo buffer allocation retries00.000.00
redo entries107,5203.32228.77
redo log space requests00.000.00
redo log space wait time00.000.00
redo ordering marks2,1180.074.51
redo size36,324,2641,122.8877,285.67
redo subscn max counts4,0050.128.52
redo synch time300.000.06
redo synch writes500.000.11
redo wastage472,74014.611,005.83
redo write time7260.021.54
redo writer latching time00.000.00
redo writes1,7370.053.70
rollback changes - undo records applied630.000.13
rollbacks only - consistent read gets1,3810.042.94
rows fetched via callback112,4893.48239.34
session connect time83,3312.58177.30
session cursor cache hits39,5441.2284.14
session logical reads1,554,24348.053,306.90
session pga memory30,538,792944.0464,976.15
session pga memory max57,539,6241,778.71122,424.73
session uga memory90,199,764,2002,788,320.57191,914,391.91
session uga memory max152,440,8724,712.36324,342.28
shared hash latch upgrades - no wait23,4500.7249.89
shared hash latch upgrades - wait00.000.00
sorts (disk)00.000.00
sorts (memory)13,8210.4329.41
sorts (rows)8,629,066266.7518,359.71
sql area evicted6080.021.29
sql area purged390.000.08
summed dirty queue length2,7770.095.91
switch current to new buffer1660.010.35
table fetch by rowid270,3158.36575.14
table fetch continued row2080.010.44
table scan blocks gotten286,9418.87610.51
table scan rows gotten10,571,952326.8122,493.51
table scans (long tables)190.000.04
table scans (short tables)7,5640.2316.09
total number of times SMON posted280.000.06
transaction rollbacks420.000.09
undo change vector size13,595,072420.2628,925.69
user I/O wait time26,4560.8256.29
user calls13,3350.4128.37
user commits4590.010.98
user rollbacks110.000.02
workarea executions - multipass00.000.00
workarea executions - onepass00.000.00
workarea executions - optimal10,0220.3121.32
write clones created in background00.000.00
write clones created in foreground60.000.01
中的
average redo write size=(redo blocks written/redo writes)*512 bytes
=(74,879/1,737)*512 bytes=22K
这个值非常小了,可以是每次写的数据量只有22k,远远小于log_buffer的大小。
 
可以查看一下log_buffer的大小SQL> show parameter log_buffer;
NAME_COL_PLUS_SHOW_PARAM
------------------------------------------------------------------------------
TYPE
-----------
VALUE_COL_PLUS_SHOW_PARAM
------------------------------------------------------------------------------
log_buffer
integer
6108160
大小为6M
 
topsql优化
整体优化思路:
1、将redo放到写的快的磁盘上而非raid5.
2、插入语句带序列,增加序列的cache。
3、增大redo file的大小,增加log_buffer的大小。
4、批量提交sql。
优化后的标准是cpu time出现在top 5的第一位。
 
 
rac的 datablock gc     状态busy,rac争用一般dml引起争用,查询一般是不会争用的,可以查出来开销比较大的dml语句。将这种查询定位到一个实例去处理,争用大大减少。
在rac设计的时候避免不同节点大规模频繁修改同一数据块,这样可以避免大量的cache fusion于全局等待事件的发生。可以用不同业务单独运行在不同的rac节点的方式来优化rac环境。
 
 
二、sql_trace/10046事件。
 
10046事件是sql_trace的增强版
trace的时间包括三种开启和关闭的方法。
1、当前session的
开启alter session set sql_trace=true;
关闭alter session set sql_trace=false;
 
10046事件的关闭和开启
开启SQL> alter session set events '10046 trace name context forever,level 12';
关闭SQL> alter session set events '10046 trace name context off';
其中level代表了10046的级别
一共有4个级别:
1- 启动标准的sql_trace功能,相当于sql_trace
4-level 1 加上绑定变量。
8-level1+等待事件跟踪。
12 -level1 +level 4+level 8
 
2、全局session
尽量避免使用,对系统的性能影响非常严重。
开启
SQL> alter system set sql_Trace=true;
System altered.
关闭
SQL> alter system set sql_Trace=false;
System altered.
 
 
3、指定用户的session
对于郑州登录的用户进行设置,利用dbms_system包实现。
si,se,nm是v$session总的sid,serial#和username。
le代表trace的级别,le=0标识跟踪结束,ev表示跟踪时间的类型,ev=10046表示跟踪10046事件。
SQL> select sid,serial#,username from v$session;
 
       SID    SERIAL# USERNAME
---------- ---------- ------------------------------
       139          9
       141         14
       143         24 SYS
       144          8
       145          7
       147         10
       149         15
       150        165
       152          1
       155          3
       156          3
       158         44 SYS
       159          3 SYS
       160          1
       161          1
       162          1
       163          1
       164          1
       165          1
       166          1
 
       SID    SERIAL# USERNAME
---------- ---------- ------------------------------
       167          1
       168          1
       169          1
       170          1
开启158 sys用户的10046事件:
SQL> exec dbms_system.set_ev(158,44,10046,12,'SYS');
PL/SQL procedure successfully completed.
关闭:
SQL> exec dbms_system.set_ev(158,44,10046,0,'SYS');
PL/SQL procedure successfully completed.
 
 
对trace文件进行格式化,便于分析。
tkprof  tracefile outputfile [option]
 
一般trace文件放置在oracle安装目录下的admin/db_name/udump下面
如:
tkprof tracefile outputfile sys=no sort=prsela,exeela,fchela
其中prsela表示对一个游标解析消耗的时间。
exeela表示针对游标执行消耗的时间。
fchela游标获取数据行花费的时间。
sys表示是否接卸sys对数据字典的递归查询语句,只有在分析细节的时候开考了开启,多数情况下关闭。
 
 
 
 
 
三、计时和剖析工具
 
在sqlplus中对执行时间查看可以用set timing on和set timing off来设置。
SQL> select count(*) from all_objects;
  COUNT(*)
----------
     50500
SQL> set timing on
SQL> select count(*) from all_objects;
  COUNT(*)
----------
     50500
Elapsed: 00:00:00.60
SQL> set timing off
SQL> select count(*) from all_objects;
  COUNT(*)
----------
     50500
 
缩写形式为set timi on 和set timi off
 
SQL> set timi off
SQL> select count(*) from all_objects;
  COUNT(*)
----------
     50500
SQL> set timi on
SQL> select count(*) from all_objects;
  COUNT(*)
----------
     50500
Elapsed: 00:00:00.47
 
如果要分析性能,仅仅开执行时间是远远不够的,系统包函数
dbms_utility.get_time 可以返回执行时候的时间整数,两个时间点想减得出代码块执行的时间,单位是毫秒10ms
函数没有参数,所有后面的括号可以省略。
 
 
oracle到10g时候还提供了dbms_utility.get_cpu_time函数。这个函数和前面的dbms_utility.get_time的区别是,get_time=get_cpu_time +其他非cpu时间,比如等待时间。
 
对于pl/sql的性能 oracle从8i开始提供了dbms_profiler包,叫剖析器。
 
配置环境。
1、用sysdba角色执行
SQL> @/usr/oracle/product/10.2.0/rdbms/admin/profload.sql
2、用pl/sql剖析执行者执行
SQL> @/usr/oracle/product/10.2.0/rdbms/admin/proftab.sql
配置完成。
 
涉及到的表
SQL> desc plsql_profiler_runs
Name            Type           Nullable Default Comments
--------------- -------------- -------- ------- --------
RUNID           NUMBER                                   主键,从序列plsql_profiler_runnumber生成的唯一运行标识
RELATED_RUN     NUMBER         Y                         相关运行的runid
RUN_OWNER       VARCHAR2(32)   Y                         启动运行的用户
RUN_DATE        DATE           Y                         运行开始时间
RUN_COMMENT     VARCHAR2(2047) Y                         运行的注释
RUN_TOTAL_TIME  NUMBER         Y                         运行的时间,一纳秒为单位
RUN_SYSTEM_INFO VARCHAR2(2047) Y                         尚未使用
RUN_COMMENT1    VARCHAR2(2047) Y                         额外的注释
SPARE1          VARCHAR2(256)  Y    未使用
 
SQL> desc plsql_profiler_units;
Name           Type         Nullable Default Comments
-------------- ------------ -------- ------- --------
RUNID          NUMBER                                 主键,参照plsql_profiler_runs
UNIT_NUMBER    NUMBER                                 主键一部分,内部生成的库单元号
UNIT_TYPE      VARCHAR2(32) Y                         库单元类型
UNIT_OWNER     VARCHAR2(32) Y                         库单元所有者名字
UNIT_NAME      VARCHAR2(32) Y                         库单元名字时间戳
UNIT_TIMESTAMP DATE         Y                         用来检测单元多次运行的变化
TOTAL_TIME     NUMBER                0                一纳秒为单位的这个单元的花费时间,概要工具不设置这个参数,为分析工具提供方便
SPARE1         NUMBER       Y                         未使用
SPARE2         NUMBER       Y                 未使用
 
SQL> desc plsql_profiler_data
Name        Type   Nullable Default Comments
----------- ------ -------- ------- --------
RUNID       NUMBER                            主键,参照plsql_profiler_runs
UNIT_NUMBER NUMBER                            主键一部分,内部生成的库单元号
LINE#       NUMBER                           主键一部分,非空,单元内部源代码的行号
TOTAL_OCCUR NUMBER Y                         line#所在的行执行的总次数
TOTAL_TIME  NUMBER Y                         纳秒为单位的line#所在行执行的总时间
MIN_TIME    NUMBER Y                          纳秒为单位的line#所在行执最小执行时间
MAX_TIME    NUMBER Y                          纳秒为单位的line#所在行执行最大执行时间
SPARE1      NUMBER Y                         以下都未使用
SPARE2      NUMBER Y                        
SPARE3      NUMBER Y                        
SPARE4      NUMBER Y                        
 
可以看出用plsql_profiler_units表示用来运行某次的单元数据,而用plsql_profiler_data记录详细的语句级别的累积信息。
也可以单独用一个用户将这三个表放进去。然后授予必要访问权限给予其他需要执行剖析的用户。这样好处是不用在需要执行剖析的用户schema中产生额外的表。
SQL> @/usr/oracle/product/10.2.0/rdbms/admin/profload.sql
 
SQL> create user profiler identified by profiler default tablespace users quota unlimited on users;
User created.
Elapsed: 00:00:00.28
 
SQL> grant connect to profiler;
Grant succeeded.
Elapsed: 00:00:00.02
SQL> create public synonym plsql_profiler_units for profiler.plsql_profiler_units;
SQL> create public synonym plsql_profiler_data for profiler.plsql_profiler_data;
SQL> create public synonym plsql_profiler_runs for profiler.plsql_profiler_runs;
SQL> create public synonym plsql_profiler_runnumber for profiler.plsql_profiler_runnumber;
 
SQL> GRANT CREATE TABLE TO PROFILER;
SQL> GRANT RESOURCE TO PROFILER;
SQL> @/usr/oracle/product/10.2.0/rdbms/admin/proftab.sql
 
SQL>GRANT SELECT ON PLSQL_PROFILER_RUNNUMBER TO PUBLIC ;
SQL>GRANT SELECT,INSERT ,UPDATE,DELETE ON PLSQL_PROFILER_DATA TO PUBLIC ;
SQL>GRANT SELECT,INSERT ,UPDATE,DELETE ON PLSQL_PROFILER_UNITS TO PUBLIC ;
SQL>GRANT SELECT,INSERT ,UPDATE,DELETE ON PLSQL_PROFILER_RUNS TO PUBLIC ;
已经设置完毕
 
用法
dbms_profiler.start_profiler('用户自定义的注释');
......
需要剖析的代码块
......
dbms_profiler.stop_profiler;--结束一次运行的信息记录。
 
 
过程代码:
CREATE OR REPLACE PROCEDURE a_proc_test1(i_u IN INTEGER) IS
 v_flag BOOLEAN;
BEGIN
 FOR i IN 2 .. i_u
 LOOP
  v_flag := TRUE;
  FOR j IN 2 .. trunc(power(i, 0.5))
  LOOP
   IF MOD(i, j) = 0
   THEN
    v_flag := FALSE;
    EXIT;
   END IF;
  END LOOP;
  IF v_flag = TRUE
  THEN
   NULL;
  END IF;
 END LOOP;
END a_proc_test1;
 
测试代码:
 
declare
  -- Local variables here
  i integer;
begin
  -- Test statements here
  i:=dbms_profiler.start_profiler(run_comment=>'test1:'||sysdate);
  a_proc_test1(10000);
  i:=dbms_profiler.stop_profiler;
end;
 
查询语句:
 select runid,run_date,run_comment,run_total_time from plsql_profiler_runs order by runid;
     RUNID RUN_DATE    RUN_COMMENT                                                                      RUN_TOTAL_TIME
---------- ----------- -------------------------------------------------------------------------------- --------------
         1 2011/3/26 1 test1:2011-03-26 14:20:03                                                            1030591000
 
select  u.runid,u.unit_number,u.unit_type,u.unit_owner,u.unit_name,d.line#,d.total_occur,d.total_time,d.min_time,d.max_time
from plsql_profiler_units u
join plsql_profiler_data d on u.runid=d.runid and u.unit_number=d.unit_number
where u.runid=1
order by u.unit_number,d.line#
可以下载prof.zip包可以生成更详细的过程分析报告。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25296295/viewspace-690325/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25296295/viewspace-690325/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值