Featured

    Featured Posts

How to find user's current running sql query ?

The below script shows user's current running sql query :
----------------------------------------------------------------------
select sql_text from v$sqlarea                                                                                        where (address, hash_value) in (select sql_address, sql_hash_value from v$session 
where username like '&username');



How to Find The Main Database Wait Events In A Particular Time Interval ??

First determine the snapshot id values for the period in question.
In this example we need to find the SNAP_ID for the period 10 PM to 11 PM on the 30th of March, 2016.

select snap_id,begin_interval_time,end_interval_time
from dba_hist_snapshot
where to_char(begin_interval_time,’DD-MON-YYYY’)=’30-Mar-2016
′
and EXTRACT(HOUR FROM begin_interval_time) between 22 and 23;
set verify off
select * from (
select active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from dba_hist_active_sess_history active_session_history
where event is not null
and SNAP_ID between &ssnapid and &esnapid
group by active_session_history.event
order by 2 desc)
where rownum

How to find List Of Users Currently Waiting in Oracle Database?

List Of Users Currently Waiting
col username format a12
col sid format 9999
col state format a15
col event format a50
col wait_time format 99999999
set pagesize 100
set linesize 120

select s.sid, s.username, se.event, se.state, se.wait_time from v$session s, v$session_wait se
where s.sid=se.sid and se.event not like ‘SQL*Net%’ and se.event not like ‘%rdbms%’ and s.username is not null
order by se.wait_time;

How to Find Top Wait Events Since Instance Startup in Oracle Database?

Top Wait Events Since Instance Startup
col event format a60
select event, total_waits, time_waited
from v$system_event e, v$event_name n
where n.event_id = e.event_id
and n.wait_class !=’Idle’
and n.wait_class = (select wait_class from v$session_wait_class
where wait_class !=’Idle’
group by wait_class having
sum(time_waited) = (select max(sum(time_waited)) from v$session_wait_class
where wait_class !=’Idle’
group by (wait_class)))
order by 3;

How to find Top Recent Wait Events in Oracle Database ?

Top Recent Wait Events
col EVENT format a60

select * from (
select active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history
where active_session_history.event is not null
group by active_session_history.event
order by 2 desc)
where rownum < 6
/

How To Find and Resolve Database Blocking In Oracle ?

Database blocking is a situation where the statement run by one user locks a record or set of records and another statement run by the same user or different user requires a conflicting lock type on the record or records, locked by the first user.
Database blocking issue is a very common scenario in any application.
How to identify the blocking session? Or more specifically how to identify the blocking rows and objects? How to resolve database blocking issue? Let’s see one by one.
How to Identify the blocking session:

1. DBA_BLOCKERS  : Gives information only about the blocking session.
SQL> select * from dba_blockers;
HOLDING_SESSION
—————
252
2. v$LOCK  : Gives details of blocking and waiting session.
SQL> select l1.sid, ‘ IS BLOCKING ‘, l2.sid
from v$lock l1, v$lock l2
where l1.block =1 and l2.request > 0
and l1.id1=l2.id1
and l1.id2=l2.id2
/
SID ‘ISBLOCKING’         SID
———- ————- ———-
244  IS BLOCKING         252
To get more specific details use the below query:
SQL> select s1.username || ‘@’ || s1.machine
|| ‘ ( SID=’ || s1.sid || ‘ )  is blocking ‘
|| s2.username || ‘@’|| s2.machine || ‘ ( SID=’ || s2.sid || ‘ ) ‘ AS blocking_status
from v$lock l1, v$session s1, v$lock l2, v$session s2
where s1.sid=l1.sid and s2.sid=l2.sid
and l1.BLOCK=1 and l2.request > 0
and l1.id1 = l2.id1
and l2.id2 = l2.id2 ;
BLOCKING_STATUS
——————————————————————————–
PK@host1( SID=244 )  is blocking PY@Host2)
How to Identify the locked object:
SQL> select * from v$lock ;
ADDR             KADDR                          SID TY        ID1        ID2             LMODE   REQUEST   CTIME     BLOCK
—————- —————-                    ———- –       ———- ———- ——-    ———-       ———- ———-
0000000451723DE8 0000000451723E20        244 TX    1310745    3139497          6          0         166           1
000000046032AFE0 000000046032B000        252 TX    1310745    3139497          0          6          33             0
TYPES OF LOCKS -  UL, TX amd TM
1. UL is a user-defined lock This is a lock defined with the DBMS_LOCK package.
2. TX lock is a row transaction lock; it’s acquired once for every transaction that changes data. Number of objects are being changed does not matter. The ID1 and ID2 columns point to the rollback segment and transaction table entries for that transaction.
3. TM lock is a DML lock. It’s acquired once for each object that’s being changed. The ID1 column identifies the object being modified.
So to find the object that is being blocked we can use ID1 from the v$lock.
SQL> select object_name from dba_objects where object_id=307193;
OBJECT_NAME
————–
OBJ1
How to Identify the locked row ?
SQL> select do.object_name,
row_wait_obj#, do.data_object_id, row_wait_file#, row_wait_block#, row_wait_row#,
dbms_rowid.rowid_create ( 1, do.data_object_id, ROW_WAIT_FILE#, ROW_WAIT_BLOCK#, ROW_WAIT_ROW# )
from v$session s, dba_objects do
where sid=252
and s.ROW_WAIT_OBJ# = do.OBJECT_ID ;
OBJECT_NAME
——————————————————————————–
ROW_WAIT_OBJ# DATA_OBJECT_ID ROW_WAIT_FILE# ROW_WAIT_BLOCK# ROW_WAIT_ROW#
————- ————– ————– ————— ————-
DBMS_ROWID.ROWID_C
——————
OBJ1
307193         307193              5             455             0
AABK/5AAFAAAAHHAAA
From this, we get the row directly:
SQL> select * from obj1 where rowid=’ ABGC/5AAFAAGFTRDAA’ ;
Getting the sql query that is being blocked
If you got the sid it should be easy by using the following sql :
SQL>select s.sid, q.sql_text from v$sqltext q, v$session s
where q.address = s.sql_address
and s.sid = 252;
SID SQL_TEXT
—– —————————————————————-
252 update obj1 set bar=:”SYS_B_0
″ where bar=:”SYS_B_1″
Finding the blocking session SID and Serial#.
SQL> Select blocking_session, sid, serial#, wait_class,seconds_in_wait From v$session where blocking_session is not NULL order by blocking_session;
BLOCKING_SESSION     SID    SERIAL#  WAIT_CLASS     SECONDS_IN_WAIT
—————- ———- ———- ————————————————–
244                             252  11049       Application        1634
Solution to resolve locking
Kill the blocking session.
SQL> alter system kill session 244,11049
′ immediate;
System altered.



Blocking Sessions in oracle database

Blocking Sessions:

Applies to: Oracle 9i/10g/11g :
Blocking session occurs when one session acquired an exclusive lock on an object and doesn't release it,  another session (one or more) want to modify the same data.First session will block the second until it completes its job

Finding blocking sessions:
Using v$session:
SELECT
   s.blocking_session,
   s.sid,
   s.serial#,
   s.seconds_in_wait
FROM
   v$session s
WHERE
   blocking_session IS NOT NULL
Using v$lock:
select * from v$lock where block=1;
select count(*) from gv$lock where block=1;
select sid from v$lock where block=1;

Which Session is blocking?
select
(select username from v$session where sid=a.sid) blocker,
a.sid,' is blocking ',(select username from v$session where sid=b.sid) blockee,b.sid
from v$lock a, v$lock b
where a.block = 1 and b.request > 0
and a.id1 = b.id1
and a.id2 = b.id2;

Finding the query of the sessions:

SELECT a.sql_text, b.sql_hash_value FROM   v$sqltext a,v$session b
WHERE  a.address = b.sql_address
AND    a.hash_value = b.sql_hash_value
AND    b.sid = &1
ORDER BY a.piece;

Complete Details of blocking sessions:

select distinct
a.sid "waiting sid"
, d.sql_text "waiting SQL"
, a.ROW_WAIT_OBJ# "locked object"
, a.BLOCKING_SESSION "blocking sid"
, c.sql_text "SQL from blocking session"
from v$session a, v$active_session_history b, v$sql c, v$sql d
where a.event='enq: TX - row lock contention'
and a.sql_id=d.sql_id
and a.blocking_session=b.session_id
and c.sql_id=b.sql_id
and b.CURRENT_OBJ#=a.ROW_WAIT_OBJ#
and b.CURRENT_FILE#= a.ROW_WAIT_FILE#
and b.CURRENT_BLOCK#= a.ROW_WAIT_BLOCK#

Find the Unix process id from SID:

select spid from  v$process where background is null
 and  addr in (select paddr from   v$session where  sid=&session_id);

Find SID from SPID: (Not very much required here)

select s.username, s.status,  s.sid,     s.serial#,
        p.spid,     s.machine, s.process, s.lockwait
 from   v$session s, v$process p
 where  s.process  = '&unix_pid'
 and    s.paddr    = p.addr;


Blocking Session has to be released by taking concurrence with application team:

How to release a blocking Session:

Ge the SID details:

select sid,SERIAL#,status,username from v$session where sid=<Blocking Session>;
select SID,MACHINE,TERMINAL,PROGRAM,MODULE from v$session where sid=<Blocking Session>;

Disconnecting the Session:
alter system disconnect session '<SID>,<Serial#>' IMMEDIATE;
Kill the Server Process:
kill -9 <Unix Process Id from SID>

Corrective Action:
For a blocking session only two corrective actions:
    1. Disconnect the blocking session
    2. Wait for completing the blocking session

Preventive Action:
Application has to be designed/corrected as no two or more sessions required the same data at the same time to be modified.


How to view locked objects in a Oracle Database ?

Below query can be used to view locked objects in a Oracle database..

SELECT C.OWNER,C.OBJECT_NAME,C.OBJECT_TYPE,B.SID,B.SERIAL#,B.STATUS,B.OSUSER,B.MACHINE
FROM V$LOCKED_OBJECT A ,V$SESSION B,DBA_OBJECTS C
WHERE B.SID = A.SESSION_ID AND A.OBJECT_ID = C.OBJECT_ID

####### : If you want to kill a session after finding out the locked session below is the statement to execute..



ALTER SYSTEM KILL SESSION '[SID],[SERIAL#]' ;

**********************************************************************************************************

How to check current active sessions in a Oracle Database ?

=================================================================================
Below query can be used to view currently connected sessions in a Oracle DB.
It is useful in situations where your DB is slow to response and you need to find out what sessions are exactly causing it.

SELECT USERNAME,SID,SERIAL#,TERMINAL,LOGON_TIME,STATUS,(SELECT DISTINCT SQL_TEXT
FROM SYS.V_$SQL_SHARED_MEMORY WHERE HASH_VALUE=SQL_HASH_VALUE)
FROM V$SESSION
WHERE STATUS='ACTIVE' AND
USERNAME IS NOT NULL



************************************************************************

https://marthadba.blogspot.in/

Copyright © MARTHADBA|About Us |Disclaimer | Contact Us |Sitemap |Designed By CodeNirvana