Sunday, February 17, 2019

Identify the blocking sessions

Below two methods helps to find the blocking sessions
Method-1:
Below query gives you the sid's which are getting effected.
1)select sid from v$lock where block=1;


2) find the sql text for this sessions.

select sql_text from v$sqltext where hash_value=( select prev_hash_value from v$session where sid='&sid');

3) check for the other detials like their sid, serail#, osuser, machine and their status ( Active / Inactive) by passing the sid from previous query of step 2.
check for all sid's from all of the results you may not get any sql text with one sid which will be active in status that is the one main culprit blocking session which is holding lock for other sessions to execute.


select sid||' - '||serial#||' - '||osuser||' - '||username||' - '||machine||' - '||status||' - '||logon_time
from v$session where sid=&sid;

Identify the holder session which is active for more confirmation you can also check the holders and waiters.

check holders & waiters:
========================
select decode(request,0,'Holder: ','Waiter: ')||sid sess, id1, id2, lmode, request, type from v$lock
where (id1, id2, type) IN (SELECT id1, id2, type from v$lock where request>0) ORDER BY id1, request;

This query results with more details the top one is the holder and others are waiters.
which is notthing but the active session which you can see with the pervous query.

5) kill the holder session

ALTER SYSTEM KILL SESSION '&sid, &serial';


Method-2:

SELECT 'alter system kill session ''' || s.sid || ',' || s.SERIAL# || ''';' a,
'ps -ef |grep LOCAL=NO|grep ' || p.SPID SPID,
'kill -9 ' || p.SPID
FROM gv$session s, gv$process p
WHERE ( (p.addr(+) = s.paddr) AND (p.inst_id(+) = s.inst_id))
AND s.sid = &sid;

you will get the result like
alter system kill session 'SID, SERIAL#';
ps -ef |grep LOCAL=NO|grep PID
kill -9 ProcessID

Connect to Oracle yum linux server

Oracle Linux 7
# cd /etc/yum.repos.d
# wget http://public-yum.oracle.com/public-yum-ol7.repo
Oracle Linux 6
# cd /etc/yum.repos.d
# wget http://public-yum.oracle.com/public-yum-ol6.repo
Oracle Linux 5
# cd /etc/yum.repos.d
# wget http://public-yum.oracle.com/public-yum-el5.repo

Get standby redo log details

select sl.group#
, sl.sequence#
, ceil(sl.bytes / 1048576) mb
, l.member
from v$standby_log sl
, v$logfile l
where sl.group# = l.group#
;

Monitor recovery progress from standby database

SELECT TO_CHAR(START_TIME,'DD-MON-YYYY HH24:MI:SS') "Recovery Start Time",
  TO_CHAR(item)
  ||' = '
  ||TO_CHAR(sofar)
  ||' '
  ||TO_CHAR(units) "Progress", comments
FROM v$recovery_progress
WHERE start_time=
  (SELECT MAX(start_time) FROM v$recovery_progress
  );
 

SQL plan monitor while query executing

STEP-1) Get sql_id from v$sql_monitor
SELECT sql_id FROM  v$sql_monitor;

STEP-2) Get the plan details
 SELECT sid,
  sql_id,
  status,
  plan_line_id,
  plan_operation,
  plan_options, 
  output_rows 
FROM v$sql_plan_monitor
--WHERE status not like 'DONE%'
 ORDER BY sid,plan_line_id;

Find the blocking sessions

SELECT
s.inst_id,
s.blocking_session,
s.sid,
s.serial#,
s.seconds_in_wait,
s.event
FROM
gv$session s
WHERE blocking_session IS NOT NULL
and s.seconds_in_wait > 15;

Saturday, February 16, 2019

Get bind variable values of the application query using sql_id

SELECT t.sql_id,
  b. last_captured,
  t.sql_text sql,
  b.hash_value,
  b.name bind_name,
  b.value_string bind_value
FROM gv$sql t,
  gv$sql_bind_capture b
WHERE t.sql_id      =b.sql_id
AND b.value_string IS NOT NULL
AND t.sql_id        ='&sql_id' --'6mz9xrh5nc007'
ORDER BY b.last_captured DESC;