oracle数据库常用脚本

数据库top sql

select distinct st.snap_id, To_Char(Sn.Begin_Interval_Time, 'yyyy-mm-dd hh24:mi:ss') || '~' || To_Char(Sn.end_Interval_Time, ' hh24:mi:ss') 时间范围, St.Sql_Id, (select s.USERNAME from v$session s where s.SQL_ID = St.sql_id and rownum = 1) as "执行用户", round(St.Cpu_Time_Delta / 900 / 1000000, 2) as "平均CPU消耗个数", round(st.elapsed_time_delta / decode(st.executions_delta, 0, 1, st.executions_delta) / 1000000, 2) as "SQL平均执行时间(s)", St.Executions_Delta as "执行次数", st.plan_hash_value, St.Buffer_Gets_Delta, st.rows_processed_delta, (select distinct sql_text from gv$sql a where a.sql_id = St.sql_id) sql_text From Dba_Hist_Sqlstat St, Dba_Hist_Snapshot Sn Where st.snap_id = sn.snap_id And st.dbid in (select dbid from v$database) And sn.begin_interval_time > (sysdate - (30 / 1440)) and round(St.Cpu_Time_Delta / 900 / 1000000, 2) > 1.5 Order By "平均CPU消耗个数" desc


GG进程延迟

select hostname, sid, program, status, group_name, lag_time, time_since_chkpt, to_char(snap_date, 'yyyy-mm-dd hh24:mi:ss') snap_date from ggmpdata.ggmon_vw_status a where ( (substr(TIME_SINCE_CHKPT, 1, instr(TIME_SINCE_CHKPT, ':') - 1) * 60 * 60 + substr(TIME_SINCE_CHKPT, instr(TIME_SINCE_CHKPT, ':') + 1, instr(TIME_SINCE_CHKPT, ':', 1, 2) - instr(TIME_SINCE_CHKPT, ':') - 1) * 60 + substr(TIME_SINCE_CHKPT, instr(TIME_SINCE_CHKPT, ':', 1, 2) + 1) > 300)) and sid = 'cfs1' and group_name = 'D_NBIZ'


占用临时表空间sql

select se.username, se.sid, se.serial#, se.status, se.last_call_et, se.machine, se.osuser, su.extents, su.blocks, su.blocks * (SELECT VALUE FROM V$PARAMETER WHERE NAME = 'db_block_size') / 1024 / 1024 || 'M' as temp_size, su.segtype, su.sqlhash, se.SQL_ID, sa.sql_text from v$session se, v$sort_usage su, v$sqlarea sa where su.session_addr = se.saddr and su.sql_id = sa.sql_id and su.sqladdr = sa.address and (su.extents > trunc((select total_extents from v$sort_segment) * 0.05) or su.blocks > trunc((select total_blocks from v$sort_segment) * 0.05))


sql执行时间超过1个小时

select DISTINCT s.inst_id,s.USERNAME,s.sid,ceil((sysdate-s.SQL_EXEC_START)*24*60*60) time ,s.sql_id,sq.SQL_TEXT,s.MACHINE,s.OSUSER, 'exec dbmgr.prc_kill_session('''||s.username||''','''||s.sid||''','''||s.serial#||''');' kill_sql from gv$session s ,gv$sql sq where 1=1 and s.inst_id=sq.inst_id and s.sql_id=sq.sql_id and s.status = 'ACTIVE' and s.WAIT_CLASS<>'Idle' and s.username is not null and s.username not in ('GBDSQP','DBMGR') and s.sql_id is not null and s.SQL_EXEC_START<sysdate-1/24 order by time desc


堵塞超过600秒

select /*+ rule */ decode(request, 0, 'Holder', 'Waiter') holder, lo.inst_id, lo.sid SID, sq.SQL_ID, se.SERIAL#, se.username username, sq.sql_text SQL, ctime wait_sec, lo.REQUEST from gv$lock lo, gv$session se, gv$sqlarea sq where (id1, id2, lo.type) in (select id1, id2, type from gv$lock where request > 0 and ctime > 600 and (id1, id2, lo.type) not in (select id1, id2,lo.type from gv$session s,gv$lock lo where s.SID=lo.SID and s.sql_id='0bvyj0pxgb4uk') ) and lo.sid = se.sid and lo.inst_id=se.inst_id and se.sql_hash_value = sq.hash_value(+) and se.username not in ( 'SYS','DBQUA','DBMGR') order by holder


db长事务

select * from ( select lg.sid, lg.SERIAL# se#, s.USERNAME, s.MACHINE , lg.SQL_ID lgsql, s.SQL_ID ssql, round(sofar/totalwork*100,2)||'%' as progress, time_remaining 预计剩余时长, elapsed_seconds 运行时长, sq.SQL_TEXT, s.EVENT from gv$session_longops lg ,gv$session s ,gv$sqlarea sq where sofar < totalwork and lg.sid=s.SID and s.sql_id=sq.sql_id order by time_remaining desc ) where rownum<21


未使用变量

select substr(sql_text, 1, 50) "SQL", count(*) cnt, sum(sharable_mem) "TotExecs" FROM v$sqlarea WHERE executions =1 and to_char(sysdate,'hh24') in ('10','16') GROUP BY substr(sql_text, 1, 50) HAVING count(*) > 5000 ORDER BY 2


占用回滚段

SELECT s.sid,s.username 用户名,s.status 状态,round(s.last_call_et / 60)||'分钟' 占用时间,round(r.RSSIZE / 1024 / 1024)||'M' 回滚段 FROM V$session s, V$transaction t, V$rollstat r WHERE s.saddr = t.ses_addr AND t.xidusn = r.usn AND ((r.curext = t.start_uext - 1) OR ((r.curext = r.extents - 1) AND t.start_uext = 0)) and s.last_call_et /60 > 30 and s.username not in ('SYS','APPS') and r.rssize > 50*1024*1024 order by last_call_et desc


用户执行sql超过20分钟

select last_call_et 已执行时间_秒, username 用户名,a.MACHINE 机器名, a.sql_id, a.sid, a.SERIAL#, b.sql_text SQL from v$session a, v$sql b where a.status = 'ACTIVE' and a.last_call_et > 1200 and a.username like '%DBMONO%' and a.sql_id = b.sql_id


查看锁超时

select s.INST_ID,s.USERNAME,s.SID,s.SERIAL#,s.STATUS,k.CTIME,s.SQL_ID, 'exec prc_kill_session(v_username =>'''||s.USERNAME||''' ,v_sid =>'''||s.sid||''',v_serial# =>'''||s.SERIAL#||'''); ' kill_sql from gv$lock k, gv$session s where k.INST_ID = s.INST_ID and s.USERNAME = 'EDSOP' and k.SID = s.SID and k.LMODE in (3, 6) and k.type in ('TM','TX') and k.CTIME > 600


猜你喜欢

转载自blog.51cto.com/9615915/2135120