\ 查询负载很高前35个进程
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head -35
\ 查询前35负载高的sPID
ps aux|grep -v PID|sort -rn -k +3|head -35|awk '{print$2}'
\ 把得到的spid替换进来(!!!注意不要少了逗号和分号!!!)
select sql_id from v$session
where paddr in(
select addr from v$process
where spid in(
'30907',
'31801',
'31463',
'31150',
'32289',
'31895',
'590',
'31952',
'611',
'32152',
'661',
'32542',
'32203',
'2019',
'2215'
)
)
查询结果显示,sql_id为 fm0738ywgbyxk 出现很多(异常sql_id)
---注:请替换成您实际的sql_id
---注:请替换成您实际的sql_id
---注:请替换成您实际的sql_id
---fm0738ywgbyxk
---12个进程
SELECT sid,serial#,sql_id from v$session where sql_id in ( 'fm0738ywgbyxk' ); SELECT count(*) from v$session where sql_id in ( 'fm0738ywgbyxk' );
---随机抽一条,查看一下具体是执行的啥内容
select sql_text from v$sqlarea a,v$session b where a.SQL_ID=b.PREV_SQL_ID and b.SID='291';
select c.SCHOOL_ID WAREHOUSE_ID, count( ARCHIVE_ID) SIGN,c.BUSINESS_TYPE from TB_CONTRACT c,FES_STAFF_ARCHIVE st where c.state!=2000 and st.id=c.ARCHIVE_ID and c.PAY_DATE between to_date('2020-02-01 00:00:00','yyyy-MM-dd HH24:MI:SS') and to_date('2020-04-27 23:59:59','yyyy-MM-dd HH24:MI:SS') and c.BUSINESS_TYPE in (17,21) group by c.SCHOOL_ID ,c.BUSINESS_TYPE
--- 生成kill语句
SELECT 'alter system kill session ''' || SID || ',' || serial# || ''';' FROM v$session WHERE sql_id IN ('fm0738ywgbyxk');
--- 执行生成的kill语句,如下:
alter system kill session '291,42431';
alter system kill session '483,24520';
alter system kill session '667,23176';
alter system kill session '768,57609';
alter system kill session '861,65267';
alter system kill session '1243,1739';
alter system kill session '1335,44615';
alter system kill session '1522,31640';
alter system kill session '1622,30425';
alter system kill session '1716,7696';
alter system kill session '1808,44494';
alter system kill session '2096,58865';
写个脚本定期清除一下吧,以防哪天CPU又被干满了:
[oracle@node2 kill_session]$ mkdir -p /home/oracle/script/kill_session \创建目录
[oracle@node2 kill_session]$ vim /home/oracle/script/kill_session/kill_cpu_high_load.sh \创建脚本
#!/bin/bash
#liangjia
load_value_tmp=`uptime |awk -F "," '{print$5}' | sed 's/ //g'`
load_value=`echo $load_value_tmp |awk '{ print int($0) }'`
dir=/home/oracle/script/kill_session
# -----------------------------------
if [ $load_value -ge 5 ] ;then
# 具体跟据自己的CPU核数写,比如我服务器是10核,当脚本扫描到CPU负载大于5时就执行kill前5个orcl的session操作
# 获取spid
# ps aux|head -1;ps aux|grep -v PID|grep oracl|sort -rn -k +3|head -5
spids=`ps aux|grep -v PID|grep oracl|sort -rn -k +3|head -10|awk '{print$2}'`
cat /dev/null > /tmp/spid2.sql
for spid in $spids
do
echo "select sql_id from v$session where paddr in( select addr from v$process where spid in($spid));" >> /tmp/spid2.sql
done
# 查找spid为spids的所有sql_id
source /home/oracle/.bash_profile
cat /dev/null > /tmp/sql_id2.sql
sqlplus / as sysdba << EOF
spool on
set heading off
spool /tmp/sql_id2.sql
@/tmp/spid2.sql
spool off
quit
EOF
# sql_id去重,删除空行
sort -n /tmp/sql_id2.sql |uniq |egrep -v "^(null|^SYS" |sed '/^$/d' > $dir/1-sql_id.sql
cat /dev/null > $dir/2-kill_sql_id.sql
# 生成查找sql_id会话的语句
cat $dir/1-sql_id.sql | while read line
do
echo "SELECT 'alter system kill session ''' || SID || ',' || serial# || ''';' FROM v$session WHERE sql_id IN ('$line');" >> $dir/2-kill_sql_id.sql
done
# 查找sql_id为sql_id.sql中的所有session会话sid和serial#,并生成kill session 会话语句
source /home/oracle/.bash_profile
cat /dev/null > /tmp/kill_session2.sql
sqlplus / as sysdba << EOF
spool on
set heading off
spool /tmp/kill_session2.sql
@/home/oracle/script/kill_session/2-kill_sql_id.sql
spool off
quit
EOF
# 过滤要执行的sql语句
egrep "^alter" /tmp/kill_session2.sql > $dir/3-kill_session.sql
# 执行sql脚本,杀死高负载session会话
source /home/oracle/.bash_profile
sqlplus / as sysdba << EOF
whenever oserror exit 1;
@/home/oracle/script/kill_session/3-kill_session.sql
quit
EOF
else
echo nothing to do !!!
fi
#mv $dir/on.lst /tmp/
exit 0
#----
放张图上来看看
添加任务计划
crontab -e
# 杀死spid为占用大量CPU负载的会话链接
*/1 * * * * /home/oracle/script/kill_session/kill_cpu_high_load.sh >/tmp/kill.log 2>&1