概述
方法一:
通过TOP D 1 –> P(SORT BY CPU USGAE) 找出耗CPU最高的SPID ,然后以这个SPID为参数,查最耗CPU的SQL
SELECT c.SPID,a.*
from v$sqlarea a, v$session b ,v$process c
where a.address = decode(b.SQL_ADDRESS,’00′,b.prev_sql_addr,b.SQL_ADDRESS ) and b.paddr = c.addr
and c.SPID = :SPID
方法二:
在
Oracle性能诊断和日常监控中,最耗CPU的语句通常也是我们最需要关心的语句。所以在Oracle10g的awr中,将cpu
time和elapsed
time最高的语句加入到了报表,并且放到了SQL语句部分的前两位。那么在平时的监控中,也可以通过shell脚本实时捕获系统中CPU耗用最多的进程
中正在执行的SQL,以更加有效和及时的诊断和发现问题。
首先写一个根据spid来或者其SQL的脚本get_by_spid.sql
#!/bin/ksh
# creator:NinGoo
# function: get sql statement by spid
# parameter: spid
# useage: get_by_spid.sh spid
sqlplus -S /nolog
col SERIAL# format 999999
col sid format 99999
col username format a10
col machine format a12
col program format a32
col sql_text format a81
set lines 1000
set pages 1000
set verify off
col sql_hash_value new_value hash_value head hash_value
select sid,serial#,username,program,sql_hash_value,
to_char(logon_time,'yyyy/mm/dd hh24:mi:ss') as login_time
from v$session
where paddr in ( select addr from v$process where spid=$1);
select sql_text
from v$sqltext_with_newlines
where hash_value = &hash_value
order by piece;
exit;
EOF
然后再在另外一个shell脚本topsql.sh中获得系统中CPU耗用最多的oracle server process的spid,循环调用第一个脚本获得SQL
#!/bin/ksh
# creator:NinGoo
# function: get top cpu sql
# parameter: N
# useage: topsql.sh N
if [ $# -eq 0 ]; then
echo "Usage: `basename $0` N"
exit 1
fi
topcpu=`ps auxw|grep LOCAL|sort -rn +2 |head -$1|awk '{print $2}'`
i=0
for spid in $topcpu
do
i=`expr $i + 1`
echo "