SQL MOnitoring.doc

Embed Size (px)

Citation preview

  • 7/29/2019 SQL MOnitoring.doc

    1/3

    Problem - find blocking sessions

    Recipie #1 - find blocking sessions with v$session

    SELECTs.blocking_session,s.sid,s.serial#,s.seconds_in_wait

    FROMv$session s

    WHEREblocking_session IS NOT NULL

    Recipie #2 - find blocking sessions using v$lock

    SELECTl1.sid || ' is blocking ' || l2.sid blocking_sessions

    FROMv$lock l1, v$lock l2

    WHEREl1.block = 1 ANDl2.request > 0 AND

    l1.id1 = l2.id1 ANDl1.id2 = l2.id2

    Recipie #3 - blocking sessions with all available information

    SELECT s1.username || '@' || s1.machine|| ' ( SID=' || s1.sid || ' ) is blocking '|| s2.username || '@' || s2.machine || ' ( SID=' || s2.sid || ' ) ' AS blocking_statusFROM v$lock l1, v$session s1, v$lock l2, v$session s2WHERE s1.sid=l1.sid AND s2.sid=l2.sidAND l1.BLOCK=1 AND l2.request > 0AND l1.id1 = l2.id1AND l2.id2 = l2.id2 ;

    Recipie #4 - identifying blocked objects

    The following queries shows you all the TM locks:

    SELECT sid, id1 FROM v$lock WHERE TYPE='TM'SID ID1

    The ID you get from this query refers to the actual database object which can help you to identify theproblem, look at the next query:

    SELECT object_name FROM dba_objects WHERE object_id=20127

    There queries should help you to identify the cause of your blocking sessions!

    Problem - Finding Oracle Session ID (SID)The current Oracle session ID is often used when you want to analyze data from v$session and similar views.

    Recipie #1 - Finding the Oracle Session IDThere are several ways to get the Oracle session ID, here's the probably most used version:

    SELECT SYS_CONTEXT('USERENV', 'SID') FROM DUAL;

    Recipie #2 - combining Session ID and v$session

  • 7/29/2019 SQL MOnitoring.doc

    2/3

    Here's a simple example showing you how to use the current session ID in combination with v$session:

    SELECT osuser, programFROM v$sessionWHERE sid=SYS_CONTEXT('USERENV', 'SID');

    Recipe #3 - alternative ways to get session ID

    just in case you wonder, there are more ways to get the current session ID:

    SELECT sidFROM v$mystatWHERE ROWNUM = 1;

    Problem - Finding locked database object

    SELECT DO.owner,DO.object_name,DO.object_type,lo.session_id,lo.oracle_username

    FROM dba_objects DO, v$locked_object loWHERE DO.object_id = lo.object_id

    Show long running SQL Statements

    SELECT s.username,sl.sid,sq.executions,sl.last_update_time,sl.sql_id,sl.sql_hash_value,opname,target,elapsed_seconds,time_remaining,sq.sql_fulltext

    FROM v$session_longops slINNER JOIN v$sql sq ON sq.sql_id = sl.sql_idINNER JOIN v$session s ON sl.SID = s.SID AND sl.serial# = s.serial#WHERE time_remaining > 0

    Get details about long running operations

    SELECT osuser,sl.sql_id,sl.sql_hash_value,opname,target,elapsed_seconds,

    time_remainingFROM v$session_longops slinner join v$session s ON sl.SID = s.SID AND sl.SERIAL# = s.SERIAL#WHERE time_remaining > 0

    Determine Database Uptime

    Recipe #1 - Query v_$instance to get the uptime

  • 7/29/2019 SQL MOnitoring.doc

    3/3

    SELECT host_name,instance_name,TO_CHAR(startup_time, 'DD-MM-YYYY HH24:MI:SS') startup_time,FLOOR(sysdate-startup_time) days

    FROM sys.v_$instance;This query will return some additional information like the host and database instancename as well as the number of days the database is running in the last column.

    Recipe #2 - Query v$session to get database uptime

    You'll often see articles telling you to query the session with SID 1, but this won't work everywhere. Thefollowing query seems to be more reliable, it checks the process with the name PMON, which is part of thedatabase and reads its logon time:

    SELECT TO_CHAR(logon_time, 'DD-MM-YYYY HH24:MI:SS')FROM v$sessionWHERE program LIKE '%PMON%'