0 ratings0% found this document useful (0 votes) 20 views6 pages15 SQL Script For Monitoring
SQL Script for Monitoring
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
15 DB Monitoring
SQL Scripts for DBAs
Asfaw Gedamu1. List of active sessions in the database
set echo off
set linesize 95
set head on
set feedback on
col sid head "Sid" form 9999 trunc
col serial# form 99999 trunc head "Ser"
col username form ab teune
col osuser form a7 trunc
col machine form 20 trunc head "Client |Machine"
col program form 215 trunc head "Client |Program”
col login form all
col “last cali" form 9999999 trunc head "Last Call|In Secs"
col status form aé trunc
select sid,serial#, substr (username, 1,10) username, substr(osuser, 1,10) osuser,
substr(program| |module, 1,15) program, substr (machine, 1,22) machine,
to_char(Logon_time, 'ddéon hh24:mi') Login,
last_call_et “last call", status
from gv$session where status="ACTIVE"
order by 1
/
2. Current tablespace usage:
set feedback off
sel pagesize 70;
set Linesize 2000
set head on
COLUMN Tablespace format a25 heading ‘Tablespace Name*
COLUMN autoextensible format all heading ‘AutoExtend’
COLUMN files in tablespace format 999 heading 'Files'
COLUMN total_tablespace_space format 99999999 heading 'Totalspace*
COLUMN total_used_space format 99999999 heading 'UsedSpace’
COLUMN total_tablespace_free_space format 99999999 heading 'FreeSpace"
COLUMN total used pet format 9999 heading 'tUsed*
COLUMN total free pet format 9999 heading ‘tree!
COLUMN max size of tablespace format 99999999 heading 'ExtendUpte!
COLUM total auto used pet format 999.99 heading 'MaxSUsed'
COLUMN total_aute_fres_pct format 999.99 heading 'MaxtFree!
WITH ths_auto AS
(SELECT DISTINCT tablespace _name, autoextensible
FROM dba data files
WHERE autoextansible
files AS
(SELECT tablespace_name, COUNT (*) tbs_files,
SUM (BYTES/1024/1024) total_ths_bytes
FROM dba_data_ files
GROUP BY tableapa:
fragments AS
(SELECT tablespace_name, COUNT (*) ths_fragments,
SUM (BYTES) /1024/1024 total_tbs_free_ bytes,
MAX (BYTES) /1024/1024 max_free_shunk_bytes|
FROM dba_free_space
"YES"),
name),GROUP BY tablespace_name),
AUTOEXTEND AS
(SELECT tablespace_name, SUM (size _to_grow) total_growth tbs
FROM (SELECT tablespace name, SUM (maxbytes)/1024/1024 size to_grow
ROW dba data files
WHERE autoextensible = 'YES*
GROUP BY tablespace_nane
oNzoN
SELECT tablespace name, SUM (BYTES) /1024/1024 size _to_grow
FROM dba data files
WHERE autoextensible = ‘No’
GROUP BY tablespace name)
GROUP BY tablespace_name)
SELECT c, instance_name, 2.tablespace_name Tablespace,
CASE ths _auto.autoextensible
WHEN "YES
THEN 'YES*
ELSE ‘NO!
END AS autoextensible,
files.ths files files_in_tablespace,
files.total_ths_bytes total_tablespace_space,
(files.total_ths bytes - fragments.total_ths free bytes
) total used space,
fragments.total tbs free bytes total tablespace free space,
round({ { (files.total_tbs bytes - fragments.total_tbs_free bytes)
/ files.total_ths_bytes
)
* 100
)) total _used pet,
round(((Fragments.total_tbs free bytes / files.total_tbs bytes) * 100
)) total_free_pet
FROW dba_tabléspaces a,vSinstance c , files, fragments, AUTOEXTEND, ths_auto
WHERE a.tablespace_name ~ files. tablespace name
AND a.tablespace_name = fragments. tablespace_name
AND a.lablespace name = AUTOZXTEND. tablespace name
AND a.tablespace_name = tbs_auto.tablespace_name(+)
order by total_free_pet;
3. Find the blocking session detail
SELECT
sinst_id,
-blocking_session,
tsid,
iseriall,
s.seconds_in_wait
FROW
gv$session 5
WHERE
blocking session IS NOT NULL;4. Monitor TEMP tablespace usage:
select a.tablespace name tablespace,
4. TEMP_TOTAZ_MB,
sum (2;used blocks * d.block size) / 1024 / 1024 TEMP_USED_MB,
G.TEMP TOTAL MB - sum (2.used blocks * d.block size) 7 1024 / 1024
TEMP_PREE MB
from v$sort_segment a,
‘
select b.name, c.block_size, sum (c.bytes) / 1024 / 1024 TSMP_TOTAL_MB
from vStablespace b, vStempfile c
where b.tsit= c.tat
group by b.name, c.block_size
da
where a.tablespace_name = d.name
group by a.tablespace name, d.TEMP_TOTAL MB;
5.
d the long running queries:
select sid, inst_id, opname, totalwork, sofar, start_time, time_remaining
from gy$session_longops
where totalwork@>sofar
/
6. Get os spid from sid:
set lines 123
col USERNAME for al5
col OSUSER for ab
col MACHINE for alS
col PROGRAM for a20
select b.spid, a,username, a.program , a.osuser ,a.machine, a.sid, a.serial#,
a.status from gv§session 2, gvSprocess b
wheze addr=paddr(+) and sid-ssid;
7. Get sid from os spid:
col sid format 999999
col username format a20
col osuser format al
select b.spid,a.sid, a.serial#,a.username, a.osuser
from v§session a, vSprocess b
where a.paddr= badder
and b.spid="sspia"
order by b.spid;
8. Monitor undo tablespace usage:
select a.tablespace_name, SIZEMB, USAGEMS, (SIZEMB - USAGEMB) FREEMEfrom (select sum(bytes} / 1024 / 1024 SIZEMB, b.tablespace_name
from dba_data_files a, dba_tablespaces b
where a.tablespace name = b.tablespace_name
and b.contents = 'ONDO"
group by b.tablespace name) a,
(select c.tablespace name, sum(bytes) / 1024 / 1024 USAGEMB
from DBA_UNDO_EXTENTS ©
where status <> 'EXPIRED’
group by ¢.tablespace_name) b
where a.tablespace_name = b.tablespace_name;
9, Get sql_text of an SID:
col sql_text form a80
set lines 120
select sql_text from gvSsqltext where hash_value:
(select sql_hash_value from qvSsession where sid
order by piace
/
1 and inst_id=sinst_id)
10. Locks present in the database
col session_id head ‘Sid’ form 9999
col object fame head "Table|Locked" form
col oracle_username head "Oracle|Username" form al0 truncate
col os user name head "OS|Username" form al0 truncate
col process head "Client|Process|1D" form 99999999
col mods held form al5
select 16.session_id, lo.oracle username, lo.os_user_name,
1o.process, do.object_name,
decode (10. locked mode,0, 'None',1, 'Null',2, ‘Row Share (SS)',
3, "Row Excl (SX)",4, 'Share',5, "Share Row Excl (SSX)',6, ‘Exclusive’,
to char(1e.lecked mode)} mode’ held
from v$locked object lo, dba objects de
where lo.objest_id = do.cbject_id
order by 1,5
/
11. Find the sessi
ns generating lot of redo:
set lines 2000
set pages 1000
col sid for 99999
col name for 3209
col username for al4
col PROGRAM for aZ1
col MODULE for a25
select s.sid,sn.SERIAL#,n.name, round(value/1024/1024,2) redo_mb,
sn.username,sn.status, substr (sn.program,1,21) “program”, sn.type,
sn.module,sn.sql_id
from v§sesstat s join vSstatname n on a.statistict = s.statistic#join v§session sn on sn.sid = s.sid where n.name like 'redo size! and
S.value!-0 order by
redo_mb desc;
12, Find the session generating undo data:
select a.sid, a.serial#, s.username, b.used_urec used_undo_record,
b,used_ublk Lsed_undo_blocks
from v§session a, v$transaction b
where a.saddr-b.ses_addr ;
13. Find temp usage of the sessions:
SELECT b. tablespace,
ROUND { (b.blocks*p. value) /1024/1024), 2) ||"M" AS temp_size,
a-inst_id as Instance,
a.sid||','||a.serial# AS sid serial,
NVL(a-username, '(eracle)') AS username,
a.program,
a.status,
a.sql_id
FROM GvSsession a,
gvésert_usage b,
gvSparameter p
WHERE p.name = ‘db_block_size"
AND a.saddr = b.seasion addr
AND a.inst_ideb.inst_id
AND a.inst_idep.inst id
‘ORDER BY Lemp size desc
/
14, Monitor rollback transactions:
select state, UNDOBLOCKSDONE, UNDOBLOCKSTOTAL,
UNDOBLOCKSDONE/UNDOBLOCKSTOTAL*100
from gv$fast_start_transactions;
15. Top queries with
h elapsed_time:
— Queries in last 1 hour ( Run from Toad, for proper view)
Select
module, parsing_schema_name, inst_id, sql_id, CHILD_NUMBER, sql_plan_baseline, sql__
profile,plan_hash_value,sql_fulltext,
to_char{last_active time, 'DD/MM/YY HH24:MI:SS" ), executions,
elapsed_tine/executions/1000/1000,
rows_pracessed, sql_plan_baseline from gvSsql where last_active_time>sysdate-
1/2
and executions <> 0 order by elapsed_time/executions desc