database_sessions
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| database_sessions [2018/12/06 21:05] – created 91.177.234.129 | database_sessions [2021/03/18 21:11] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Database_sessions ====== | + | * [[https:// |
| - | =====Sessions sorted by login time===== | + | ==== List Users Created on the Database (including dba role grantee) |
| - | < | + | < |
| - | =====Free space in TEMP tablespaces===== | + | CLEAR BREAKS |
| - | < | + | CLEAR COLUMNS |
| + | CLEAR COMPUTES | ||
| + | |||
| + | COL username HEA " | ||
| + | COL default_tablespace HEA " Default Tablespace " FOR a20; | ||
| + | COL temporary_tablespace HEA " | ||
| + | COL grole HEA " DBA " FOR a5; | ||
| + | |||
| + | SELECT | ||
| + | LPAD( DECODE( p.granted_role, | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | ( | ||
| + | | ||
| + | grantee, granted_role | ||
| + | | ||
| + | dba_role_privs | ||
| + | | ||
| + | granted_role = ' | ||
| + | ) p | ||
| + | WHERE | ||
| + | | ||
| + | ORDER BY | ||
| + | | ||
| + | / | ||
| + | </ | ||
| + | ==== List logged on users with O/S info ==== | ||
| + | < | ||
| + | COL orauser HEA " | ||
| + | COL osuser HEA " O/S User " FOR a10 TRUNC | ||
| + | COL ssid HEA " | ||
| + | COL sserial HEA " | ||
| + | COL ospid HEA "O/S Pid" FOR a7 | ||
| + | COL slogon HEA " | ||
| + | COL sstat HEA " | ||
| + | COL auth HEA " | ||
| + | COL conn HEA " | ||
| + | |||
| + | SELECT | ||
| + | ' | ||
| + | ' | ||
| + | LPAD( s.sid, 4 ) ssid, LPAD( s.serial#, 6 ) sserial, | ||
| + | LPAD( p.spid, 6 ) ospid, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | ( | ||
| + | | ||
| + | DISTINCT sid, authentication_type | ||
| + | | ||
| + | v$session_connect_info | ||
| + | ) sc | ||
| + | WHERE | ||
| + | | ||
| + | ORDER BY | ||
| + | | ||
| + | / | ||
| + | |||
| + | </code> | ||
| + | ==== Check if Session Tracing is On ==== | ||
| + | < | ||
| + | set lines 180 | ||
| + | col module for a45 | ||
| + | col sql_trace_waits for a20 | ||
| + | col sql_trace_binds for a20 | ||
| + | col sql_trace for a20 | ||
| - | =====Live TEMP free space monitoring===== | + | select username, |
| - | < | + | / |
| - | =====Sessions currently holding segments in TEMP tablespace===== | + | </ |
| - | < | + | ==== Enable Session Tracing ==== |
| - | =====More detailed sessions currently holding segments in TEMP tablespace===== | + | < |
| - | < | + | EXEC DBMS_MONITOR.SESSION_TRACE_ENABLE(& |
| + | </ | ||
| + | ==== Disable Session Tracing ==== | ||
| + | </ | ||
| + | EXEC DBMS_MONITOR.SESSION_TRACE_DISABLE(& | ||
| + | </ | ||
| + | ==== Lists all Locked Objects ==== | ||
| + | < | ||
| + | SET PAGESIZE 60 | ||
| + | SET LINESIZE 300 | ||
| + | |||
| + | COLUMN owner FORMAT A20 | ||
| + | COLUMN username FORMAT A20 | ||
| + | COLUMN object_owner FORMAT A20 | ||
| + | COLUMN object_name FORMAT A30 | ||
| + | COLUMN locked_mode FORMAT A15 | ||
| + | |||
| + | SELECT b.session_id AS sid, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 1, 'Null (NULL)', | ||
| + | 2, 'Row-S (SS)', | ||
| + | 3, 'Row-X (SX)', | ||
| + | 4, 'Share (S)', | ||
| + | 5, ' | ||
| + | 6, ' | ||
| + | | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | WHERE a.object_id = b.object_id | ||
| + | ORDER BY 1, 2, 3, 4 | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Setting sessions and transactions parameters using processes ==== | ||
| + | If building a new database, | ||
| + | < | ||
| + | " | ||
| + | |||
| + | It is calculated from PROCESSES parameter: | ||
| + | |||
| + | 10g -> ((1.1 * PROCESSES) + 5) | ||
| + | 11g -> ((1.5 * PROCESSES) + 22) | ||
| + | 12.2-> ((1.5 * PROCESSES) + 22) | ||
| + | |||
| + | You may leave SESSIONS and let Database choose its value automatically." | ||
| + | |||
| + | TRANSACTIONS = (1.1 * SESSIONS) | ||
| + | </ | ||
| + | or if updating an existing database... | ||
| + | < | ||
| + | select (&& | ||
| + | , ((& | ||
| + | from | ||
| + | undef processes | ||
| + | </ | ||
| + | Then again, you could just set processes and let the database work out what to set sessions and transactions to... | ||
| + | < | ||
| + | alter system reset sessions scope=spfile sid=' | ||
| + | alter system reset transactions scope=spfile sid=' | ||
| + | </ | ||
| + | |||
| + | ==== Sessions sorted by login time ==== | ||
| + | < | ||
| + | set lines 200 | ||
| + | set pages 999 | ||
| + | col killer | ||
| + | col osuser | ||
| + | col login_time | ||
| + | | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | | ||
| + | , | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | ==== Free space in TEMP tablespaces ==== | ||
| + | < | ||
| + | select tablespace_name temp_name | ||
| + | , tablespace_size/ | ||
| + | , allocated_space/ | ||
| + | , free_space/ | ||
| + | from | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Live TEMP free space monitoring ==== | ||
| + | < | ||
| + | select a.tablespace_name | ||
| + | , d.mb_total | ||
| + | , sum (a.used_blocks * d.block_size) / 1024 / 1024 mb_used | ||
| + | , d.mb_total - sum (a.used_blocks * d.block_size) / 1024 / 1024 mb_free | ||
| + | from | ||
| + | , ( | ||
| + | | ||
| + | , | ||
| + | , | ||
| + | | ||
| + | , | ||
| + | | ||
| + | | ||
| + | , | ||
| + | ) d | ||
| + | where a.tablespace_name = d.name | ||
| + | group by a.tablespace_name | ||
| + | , d.mb_total | ||
| + | / | ||
| + | </ | ||
| + | ==== Sessions currently holding segments in TEMP tablespace ==== | ||
| + | < | ||
| + | select b.tablespace | ||
| + | , b.segfile# | ||
| + | , b.segblk# | ||
| + | , b.blocks | ||
| + | , a.sid | ||
| + | , a.serial# | ||
| + | , a.username | ||
| + | , a.osuser, a.status | ||
| + | from v$session a | ||
| + | , | ||
| + | where a.saddr = b.session_addr | ||
| + | order by b.tablespace | ||
| + | , b.segfile# | ||
| + | , b.segblk# | ||
| + | , b.blocks | ||
| + | / | ||
| + | </ | ||
| + | ==== More detailed sessions currently holding segments in TEMP tablespace ==== | ||
| + | < | ||
| + | set lines 1000 pages 1000 | ||
| + | col sid_serial for a10 | ||
| + | col username | ||
| + | col osuser | ||
| + | col spid for a10 | ||
| + | col module | ||
| + | col program | ||
| + | col tablespace for a10 | ||
| + | |||
| + | select s.sid || ',' | ||
| + | , s.username | ||
| + | , s.osuser | ||
| + | , p.spid | ||
| + | , s.module | ||
| + | , s.program | ||
| + | , sum (t.blocks) * tbs.block_size / 1024 / 1024 mb_used | ||
| + | , t.tablespace | ||
| + | , count(*) sort_ops | ||
| + | from | ||
| + | , v$session s | ||
| + | , dba_tablespaces tbs | ||
| + | , v$process p | ||
| + | where t.session_addr = s.saddr | ||
| + | and s.paddr = p.addr and t.tablespace = tbs.tablespace_name | ||
| + | group by s.sid | ||
| + | , s.serial# | ||
| + | , s.username | ||
| + | , s.osuser | ||
| + | , p.spid | ||
| + | , s.module | ||
| + | , s.program | ||
| + | , tbs.block_size | ||
| + | , t.tablespace | ||
| + | order by sid_serial | ||
| + | / | ||
| + | </ | ||
| or | or | ||
| - | < | + | < |
| + | SET PAGESIZE 50 | ||
| + | SET LINESIZE 300 | ||
| + | |||
| + | COLUMN tablespace FORMAT A20 | ||
| + | COLUMN temp_size FORMAT A20 | ||
| + | COLUMN sid_serial FORMAT A20 | ||
| + | COLUMN username FORMAT A20 | ||
| + | COLUMN program FORMAT A50 | ||
| + | |||
| + | SELECT b.tablespace, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | | ||
| + | WHERE p.name | ||
| + | AND a.saddr = b.session_addr | ||
| + | AND a.inst_id=b.inst_id | ||
| + | AND a.inst_id=p.inst_id | ||
| + | ORDER BY b.tablespace, | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== SQL statements used by TEMP sessions ==== | ||
| + | < | ||
| + | select s.sid || ',' | ||
| + | , s.username | ||
| + | , t.blocks * tbs.block_size / 1024 / 1024 mb_used | ||
| + | , t.tablespace | ||
| + | , t.sqladdr address | ||
| + | , q.hash_value | ||
| + | , q.sql_text | ||
| + | from | ||
| + | , v$session s | ||
| + | , v$sqlarea q | ||
| + | , dba_tablespaces tbs | ||
| + | where t.session_addr = s.saddr | ||
| + | and t.sqladdr | ||
| + | and t.tablespace | ||
| + | order by s.sid | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Drop a schema but drop its sessions first ==== | ||
| + | Sometimes dropping a user proves difficult because database processes owned by the user are still running. This script will kill all sessions owned by the schema before dropping it. | ||
| + | < | ||
| + | set serveroutput on | ||
| + | variable schema2drop varchar2(50); | ||
| + | execute : | ||
| + | declare | ||
| + | open_count integer; | ||
| + | begin | ||
| + | dbms_output.enable(null); | ||
| + | -- prevent any further connections | ||
| + | execute immediate 'alter user ' | ||
| + | |||
| + | --kill all sessions | ||
| + | for session in ( | ||
| + | select sid, serial# | ||
| + | from v$session | ||
| + | where username = ': | ||
| + | ) | ||
| + | loop | ||
| + | dbms_output.put_line (' | ||
| + | execute immediate 'alter system disconnect session ''' | ||
| + | end loop; | ||
| + | |||
| + | -- killing is done in the background, so we need to wait a bit | ||
| + | loop | ||
| + | select count(*) | ||
| + | into open_count | ||
| + | from v$session where username = ''' | ||
| + | exit when open_count = 0; | ||
| + | dbms_lock.sleep(0.5); | ||
| + | end loop; | ||
| - | =====SQL statements used by TEMP sessions===== | + | -- finally, it is safe to issue the drop statement |
| - | < | + | dbms_output.put_line ('drop user ' |
| + | execute immediate 'drop user ' | ||
| + | end; | ||
| + | </ | ||
| + | or same thing in SQL*Plus | ||
| + | < | ||
| + | </ | ||
| - | =====Show a users current sql===== | + | ==== Show a users current sql ==== |
| + | < | ||
| | | ||
| | | ||
| Line 25: | Line 355: | ||
| ( | ( | ||
| select sql_address | select sql_address | ||
| - | , sql_hash_value | + | , sql_hash_value |
| from | from | ||
| where 1=1 | where 1=1 | ||
| and username like '& | and username like '& | ||
| ); | ); | ||
| - | =====Top 10 SQL statements===== | + | </ |
| + | ==== Top 10 SQL statements ==== | ||
| Lists all SQL statements in the library cache with CPU time > &1 &1 % of the total of all statements | Lists all SQL statements in the library cache with CPU time > &1 &1 % of the total of all statements | ||
| - | < | + | < |
| + | define CPUTimePct = '& | ||
| + | @sqlplus_settings | ||
| + | @nls_settings | ||
| + | @header_title 155 DC 'All statements in the library cache with a total CPU time >= & | ||
| - | =====Show what SQL statements are being run by all user sessions===== | + | COLUMN cpu_time_total |
| - | < | + | COLUMN sql_text |
| + | COLUMN pct FORMAT | ||
| + | COLUMN buffer_gets_per_exec FORMAT 99G999G999G999 HEADING " | ||
| + | COLUMN wait_per_exec | ||
| + | COLUMN cpu_per_exec | ||
| + | COLUMN disk_reads_per_exec | ||
| + | COLUMN executions | ||
| + | COLUMN sql_id | ||
| - | =====Show blocking sessions using Oracle standard script===== | + | SET FEEDBACK |
| + | SET ARRAYSIZE 1 | ||
| + | SET LONG 2000 | ||
| + | SET RECSEP | ||
| + | SET PAGESIZE | ||
| + | |||
| + | WITH total_cpu_time | ||
| + | AS ( SELECT sum(cpu_time) total_cpu FROM v$sqlarea ) | ||
| + | SELECT cpu_time/ | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | FROM v$sqlarea, | ||
| + | | ||
| + | WHERE cpu_time >= & | ||
| + | ORDER BY cpu_time desc; | ||
| + | |||
| + | undefine CPUTimePct | ||
| + | |||
| + | SET ARRAYSIZE 20 | ||
| + | SET LONG 200 | ||
| + | </ | ||
| + | |||
| + | ==== Show what SQL statements are being run by all user sessions ==== | ||
| + | < | ||
| + | col username | ||
| + | col sid for 99999 | ||
| + | col sql_text | ||
| + | set lines 1000 pages 1000 long 100000 | ||
| + | select a.username | ||
| + | , to_char(a.logon_time,' | ||
| + | , a.sid | ||
| + | , a.serial# | ||
| + | , b.sql_text | ||
| + | from | ||
| + | , v$sqlarea b | ||
| + | where 1=1 | ||
| + | and a.sql_address = b.address; | ||
| + | </ | ||
| + | |||
| + | ==== Incative sessions still holding lots of memory can probably be killed (after consulting the relevant user) ==== | ||
| + | < | ||
| + | SELECT | ||
| + | s.sid, | ||
| + | ,s.status | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | -- idle time | ||
| + | -- days separately | ||
| + | , | ||
| + | -- hours | ||
| + | substr(' | ||
| + | -- minutes | ||
| + | substr(' | ||
| + | --seconds | ||
| + | substr(' | ||
| + | FROM v$session s, v$process p | ||
| + | WHERE s.username IS NOT NULL | ||
| + | -- use outer join to show sniped sessions in | ||
| + | -- v$session that don't have an OS process | ||
| + | AND p.addr(+) = s.paddr | ||
| + | and s.sid=nvl(& | ||
| + | ORDER BY | ||
| + | idle_time desc; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Show SQL transaction usage ==== | ||
| + | For example after killing a session with alter system disconnect session immediate, it takes a long time to die, use this script to see how long it will take to disappear by checking how many undo blocks still remain occupied. | ||
| + | < | ||
| + | select vs.sid | ||
| + | , vs.username | ||
| + | , vt.xidusn | ||
| + | , vt.used_urec | ||
| + | , vt.used_ublk | ||
| + | from | ||
| + | , v$transaction vt | ||
| + | where vs.saddr = vt.ses_addr | ||
| + | / | ||
| + | </ | ||
| + | or a little more detailed... | ||
| + | < | ||
| + | select s.username username, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | , t.used_ublk | ||
| + | from | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | where s.paddr = p.addr | ||
| + | and s.taddr = t.addr (+) | ||
| + | and t.xidusn = r.usn (+) | ||
| + | and r.usn = n.usn (+) | ||
| + | and s.username=' | ||
| + | order by 1 | ||
| + | / | ||
| + | </ | ||
| + | produces | ||
| + | < | ||
| + | USERNAME | ||
| + | --------------- -------------------------------------------------- ---------------------- ---------- | ||
| + | SYS | ||
| + | SYS | ||
| + | </ | ||
| + | |||
| + | ==== Other useful tables to look at for blocking objects ==== | ||
| + | < | ||
| + | V$ACCESS | ||
| + | DBA_DDL_LOCKS | ||
| + | V$LOCKED_OBJECT | ||
| + | </ | ||
| + | ==== List Database Sessions including any blocking ==== | ||
| + | < | ||
| + | SET HEADING ON | ||
| + | SET LINESIZE 300 | ||
| + | SET PAGESIZE 60 | ||
| + | COLUMN username FORMAT A21 | ||
| + | COLUMN osuser FORMAT A8 | ||
| + | COLUMN sid FORMAT 9,999,999 | ||
| + | COLUMN serial# FORMAT 9,999,999 | ||
| + | COLUMN lockwait FORMAT A6 | ||
| + | COLUMN status FORMAT A8 | ||
| + | COLUMN module FORMAT A20 | ||
| + | COLUMN machine FORMAT A24 | ||
| + | COLUMN program FORMAT A20 | ||
| + | COLUMN logon_time FORMAT A20 | ||
| + | |||
| + | SELECT LPAD(' ', (level-1)*2, | ||
| + | s.osuser, | ||
| + | s.sid, | ||
| + | s.serial#, | ||
| + | s.lockwait, | ||
| + | s.status, | ||
| + | s.module, | ||
| + | s.machine, | ||
| + | s.program, | ||
| + | TO_CHAR(s.logon_Time,' | ||
| + | FROM | ||
| + | CONNECT BY PRIOR s.sid = s.blocking_session | ||
| + | START WITH s.blocking_session IS NULL | ||
| + | / | ||
| + | </ | ||
| + | ==== Blocking Locks ==== | ||
| + | * DBA_LOCKS | ||
| + | * DBA_DML_LOCKS | ||
| + | * DBA_DDL_LOCKS | ||
| + | * DBA_WAITERS | ||
| + | * DBA_BLOCKERS | ||
| + | |||
| + | Sessions blocking others | ||
| + | < | ||
| + | SET LINESIZE 300 | ||
| + | SET PAGESIZE 60 | ||
| + | COLUMN username FORMAT A15 | ||
| + | COLUMN osuser FORMAT A8 | ||
| + | COLUMN sid FORMAT 99999 | ||
| + | COLUMN serial# FORMAT 99999 | ||
| + | COLUMN process_id FORMAT A5 | ||
| + | COLUMN wait_class FORMAT A12 | ||
| + | COLUMN seconds_in_wait FORMAT 9999 | ||
| + | COLUMN state FORMAT A17 | ||
| + | COLUMN blocking_session 9999 | ||
| + | COLUMN blocking_session_state a10 | ||
| + | COLUMN module FORMAT a10 | ||
| + | COLUMN logon_time FORMAT A20 | ||
| + | |||
| + | SELECT | ||
| + | NVL(a.username, | ||
| + | a.osuser, | ||
| + | a.inst_id, | ||
| + | a.sid, | ||
| + | a.serial#, | ||
| + | a.sql_id, | ||
| + | d.spid AS process_id, | ||
| + | a.wait_class, | ||
| + | a.seconds_in_wait, | ||
| + | a.state, | ||
| + | a.blocking_instance, | ||
| + | a.blocking_session, | ||
| + | a.blocking_session_status, | ||
| + | a.module, | ||
| + | TO_CHAR(a.logon_Time,' | ||
| + | FROM | ||
| + | gv$session a, | ||
| + | gv$process d | ||
| + | WHERE | ||
| + | a.paddr | ||
| + | AND | ||
| + | | ||
| + | AND | ||
| + | a.status = ' | ||
| + | AND | ||
| + | a.blocking_session IS NOT NULL | ||
| + | ORDER BY 1,2 | ||
| + | / | ||
| + | </ | ||
| + | ==== Blocking locks (verbose) ==== | ||
| + | < | ||
| + | -- Shows blocked session, blocking session and details of the programs and machines they are running from. | ||
| + | -- In addition, shows the CTIME value which details how long the blocking session has had hold of the lock | ||
| + | SELECT | ||
| + | sysdate, | ||
| + | c.blocker_inst, | ||
| + | c.blocker_sid, | ||
| + | c.blocker_username, | ||
| + | c.blocker_sql_id, | ||
| + | c.blocker_program, | ||
| + | c.blocker_machine, | ||
| + | c.blocker_info, | ||
| + | c.blocker_action, | ||
| + | c.blocker_client, | ||
| + | c.blockee_inst, | ||
| + | c.blockee_sid, | ||
| + | c.blockee_username, | ||
| + | c.blockee_sql_id, | ||
| + | c.blockee_program, | ||
| + | c.blockee_machine, | ||
| + | c.blockee_action, | ||
| + | c.blockee_info, | ||
| + | c.blockee_client, | ||
| + | d.os_user_name locked_os_user_name, | ||
| + | d.oracle_username locked_username, | ||
| + | b.owner object_owner, | ||
| + | b.object_name, | ||
| + | b.object_type, | ||
| + | c.ctime "Time Locked (s)" | ||
| + | FROM | ||
| + | gv$locked_object d, | ||
| + | dba_objects b, | ||
| + | ( | ||
| + | SELECT | ||
| + | ( SELECT username FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_username, | ||
| + | ( SELECT sql_id FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_sql_id, | ||
| + | ( SELECT program FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_program, | ||
| + | ( SELECT machine FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_machine, | ||
| + | ( SELECT action FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_action, | ||
| + | ( SELECT client_info FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_info, | ||
| + | ( SELECT client_identifier FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_client, | ||
| + | y.sid blocker_sid, | ||
| + | y.inst_id blocker_inst, | ||
| + | ( SELECT username FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_username, | ||
| + | ( SELECT sql_id FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_sql_id, | ||
| + | ( SELECT program FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_program, | ||
| + | ( SELECT machine FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_machine, | ||
| + | ( SELECT action FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_action, | ||
| + | ( SELECT client_info FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_info, | ||
| + | ( SELECT client_identifier FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_client, | ||
| + | z.sid blockee_sid, | ||
| + | z.inst_id blockee_inst, | ||
| + | z.ctime | ||
| + | FROM | ||
| + | gv$lock y, | ||
| + | gv$lock z | ||
| + | WHERE | ||
| + | y.block = 1 AND | ||
| + | z.request > 0 AND | ||
| + | y.id1 = z.id1 AND | ||
| + | y.id2 = z.id2 | ||
| + | ) c | ||
| + | WHERE | ||
| + | | ||
| + | | ||
| + | | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Show blocking sessions using Oracle standard script ==== | ||
| Run this if never run before (to create needed tables) | Run this if never run before (to create needed tables) | ||
| - | < | + | < |
| + | @?/ | ||
| + | </ | ||
| then to show locking processes in a tree structure (non indented process is blocking indented sessions) | then to show locking processes in a tree structure (non indented process is blocking indented sessions) | ||
| - | < | + | < |
| + | @?/ | ||
| + | </ | ||
| - | =====Show blocking sessions===== | + | ==== Show blocking sessions ==== |
| - | | + | < |
| - | | + | select s1.username||' |
| - | s2.username || ' | + | |
| - | | + | from |
| - | | + | , v$session s1 |
| - | , | + | , v$lock l2 |
| - | , | + | , v$session s2 |
| - | , | + | where 1=1 |
| - | | + | and s1.sid |
| - | | + | and s2.sid |
| - | | + | and l1.block |
| - | | + | and l2.request > 0 |
| - | | + | and l1.id1 |
| - | | + | and l2.id2 |
| - | | + | / |
| + | </ | ||
| - | =====Show blocking sessions with lock type===== | + | ==== Show blocking sessions with lock type ==== |
| - | < | + | < |
| + | select nvl(s.username,' | ||
| + | , nvl(s.terminal,' | ||
| + | , l.sid||',' | ||
| + | , u1.name||' | ||
| + | , decode ( l.lmode | ||
| + | ,1, 'No Lock' | ||
| + | ,2, 'Row Share' | ||
| + | ,3, 'Row Exclusive' | ||
| + | ,4, ' | ||
| + | ,5, 'Share Row Exclusive' | ||
| + | ,6, ' | ||
| + | , null) | ||
| + | , decode ( l.request | ||
| + | ,1, 'No Lock' | ||
| + | ,2, 'Row Share' | ||
| + | ,3, 'Row Exclusive' | ||
| + | ,4, ' | ||
| + | ,5, 'Share Row Exclusive' | ||
| + | ,6, ' | ||
| + | , null) | ||
| + | from v$lock | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | where l.sid = s.sid | ||
| + | and | ||
| + | and | ||
| + | and | ||
| + | and | ||
| + | order by 1,2,5 | ||
| + | / | ||
| + | </ | ||
| - | =====Blocking sessions in tree layout===== | + | ==== Blocking sessions in tree layout ==== |
| - | from www.guyharrison.net | + | from www.guyharrison.net. this one seems to be the best! |
| - | < | + | < |
| + | set wrap off | ||
| + | column sid format a8 | ||
| + | column object_name format a20 | ||
| + | column sql_text format a150 | ||
| + | set echo on | ||
| + | WITH sessions AS | ||
| + | | ||
| + | sid, blocking_session, | ||
| + | FROM v$session) | ||
| + | SELECT LPAD(' | ||
| + | | ||
| + | FROM sessions s | ||
| + | LEFT OUTER JOIN dba_objects | ||
| + | ON (object_id = row_wait_obj# | ||
| + | LEFT OUTER JOIN v$sql | ||
| + | USING (sql_id) | ||
| + | WHERE sid IN (SELECT blocking_session FROM sessions) | ||
| + | OR blocking_session IS NOT NULL | ||
| + | | ||
| + | START WITH blocking_session IS NULL | ||
| + | / | ||
| + | </ | ||
| ... and prepare the killer statement | ... and prepare the killer statement | ||
| - | < | + | < |
| + | set wrap off lines 2000 echo on | ||
| + | column sid for a8 | ||
| + | column username | ||
| + | column terminal | ||
| + | column object_name for a20 | ||
| + | column sql_text | ||
| + | column killer | ||
| + | with sessions as | ||
| + | ( | ||
| + | select / | ||
| + | , nvl(username,' | ||
| + | , serial# | ||
| + | , nvl(terminal,' | ||
| + | , blocking_session | ||
| + | , row_wait_obj# | ||
| + | , sql_id | ||
| + | , 'alter system disconnect session ''' | ||
| + | from | ||
| + | ) | ||
| + | select lpad(' | ||
| + | , object_name | ||
| + | , substr(sql_text, | ||
| + | , decode(level, | ||
| + | from | ||
| + | left outer join dba_objects on (object_id = row_wait_obj# | ||
| + | left outer join v$sql using (sql_id) | ||
| + | where sid in (select blocking_session from sessions) | ||
| + | or | ||
| + | connect by prior sid = blocking_session | ||
| + | start with blocking_session | ||
| + | / | ||
| + | </ | ||
| - | =====Show all ddl locks in the system===== | + | ==== Kill a session with a force timeout! |
| - | <code> | + | Saw this flashing by in a database upgrade log. Maybe a useful hidden trick. |
| - | =====Show all locks, internal also===== | + | < |
| - | < | + | ALTER SYSTEM KILL SESSION '626,37999' force timeout 0; |
| + | </ | ||
| - | =====Show | + | ==== Show all ddl locks in the system |
| - | < | + | < |
| + | set lines 1000 | ||
| + | col object for a40 | ||
| + | select ses.username | ||
| + | , ddl.session_id | ||
| + | , ses.serial# | ||
| + | , owner || ' | ||
| + | , ddl.type | ||
| + | , ddl.mode_held | ||
| + | from | ||
| + | , v$session | ||
| + | where owner like ' | ||
| + | and ddl.session_id | ||
| + | </code> | ||
| + | ==== Show all locks, internal also ==== | ||
| + | < | ||
| + | select nvl(b.username,' | ||
| + | , session_id | ||
| + | , lock_type | ||
| + | , mode_held | ||
| + | , mode_requested | ||
| + | , lock_id1 | ||
| + | , lock_id2 | ||
| + | from | ||
| + | , sys.v_$session | ||
| + | where 1=1 | ||
| + | and a.session_id = b.sid | ||
| + | and b.username | ||
| + | and b.sid = & | ||
| + | / | ||
| + | </ | ||
| - | =====Generate kill statement for ddl locking sessions===== | + | ==== Show object locks ==== |
| - | < | + | < |
| + | set linesize 150; | ||
| + | set head on; | ||
| + | col sid_serial form a13 | ||
| + | col ora_user for a15; | ||
| + | col object_name for a35; | ||
| + | col object_type for a10; | ||
| + | col lock_mode for a15; | ||
| + | col last_ddl for a8; | ||
| + | col status for a10; | ||
| + | break on sid_serial; | ||
| + | SELECT l.session_id||',' | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 0, ' | ||
| + | 1, ' | ||
| + | 2, 'Row-S (SS)', | ||
| + | 3, 'Row-X (SX)', | ||
| + | 4, ' | ||
| + | 5, ' | ||
| + | 6, ' | ||
| + | TO_CHAR(l.locked_mode) | ||
| + | ) lock_mode, | ||
| + | | ||
| + | | ||
| + | FROM dba_objects o, gv$locked_object l, v$session v | ||
| + | WHERE o.object_id = l.object_id | ||
| + | and l.SESSION_ID=v.sid | ||
| + | order by 2,3; | ||
| + | </ | ||
| - | =====Display any long operations===== | + | ==== Generate kill statement for ddl locking sessions ==== |
| - | < | + | < |
| - | =====Show waits for (blocking) datapump sessions===== | + | col killer for a65 |
| - | < | + | |
| - | =====Show current sessions that perform a lot of hard parses===== | + | |
| - | < | + | , |
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | ==== Generate killer statements for a particular schema / user ==== | ||
| + | < | ||
| + | set lines 1000 pages 100 | ||
| + | col username for a15 | ||
| + | col status | ||
| + | select ses.username | ||
| + | , ses.status | ||
| + | , 'alter system disconnect session ''' | ||
| + | from | ||
| + | where 1=1 | ||
| + | and lower (ses.username) like lower(' | ||
| + | order by ses.status | ||
| + | / | ||
| + | </ | ||
| + | ==== Generate killer statements from PL/SQL for INACTIVE sessions ==== | ||
| + | < | ||
| + | begin | ||
| + | for i in (select * from v$session where status=' | ||
| + | LOOP | ||
| + | EXECUTE IMMEDIATE(q' | ||
| + | END LOOP; | ||
| + | end; | ||
| + | </ | ||
| + | ==== Display any long operations ==== | ||
| + | < | ||
| + | set lines 100 | ||
| + | set pages 100 | ||
| + | col username | ||
| + | col message | ||
| + | col remaining format 9999 | ||
| + | select username | ||
| + | , to_char(start_time, | ||
| + | , time_remaining remaining | ||
| + | , message | ||
| + | from | ||
| + | where 1=1 | ||
| + | and time_remaining != 0 | ||
| + | order by time_remaining desc | ||
| + | </ | ||
| + | ==== Show waits for (blocking) datapump sessions ==== | ||
| + | < | ||
| + | set pages 100 lines 1000 | ||
| + | col p1 for 999999999999 | ||
| + | col p2 for 999999999999 | ||
| + | col p3 for 999999999999 | ||
| + | select sid | ||
| + | , event | ||
| + | , p1 | ||
| + | , p2 | ||
| + | , p3 | ||
| + | , seconds_in_wait | ||
| + | from | ||
| + | where sid in | ||
| + | ( | ||
| + | | ||
| + | | ||
| + | , | ||
| + | , | ||
| + | | ||
| + | | ||
| + | ) | ||
| + | </ | ||
| + | ==== Show current sessions that perform a lot of hard parses ==== | ||
| + | < | ||
| + | set pages 1000 lines 1000 | ||
| + | col username for a15 | ||
| + | select vss.sid | ||
| + | , vs.username | ||
| + | , vsn.name | ||
| + | , vss.value | ||
| + | , round((sysdate-vs.logon_time)*24) hours_connected | ||
| + | from | ||
| + | , v$statname vsn | ||
| + | , v$session | ||
| + | where 1=1 | ||
| + | and vss.sid | ||
| + | and vss.statistic# | ||
| + | and vss.value | ||
| + | and vsn.name | ||
| + | </ | ||
| - | =====All active sql===== | + | ==== All active sql ==== |
| - | < | + | < |
| + | set serveroutput on | ||
| + | set feedback off | ||
| + | column username format a20 | ||
| + | column sql_text format a55 word_wrapped | ||
| + | begin | ||
| + | dbms_output.enable(null); | ||
| - | =====Identify the number of SQL statements in the library cache that are not using bind variables===== | + | for x in |
| + | ( | ||
| + | select username||' | ||
| + | , to_char(LOGON_TIME,' | ||
| + | , to_char(sysdate,' | ||
| + | , sql_address | ||
| + | , sql_hash_value | ||
| + | from | ||
| + | where 1=1 | ||
| + | and status | ||
| + | and rawtohex(sql_address) != ' | ||
| + | and username is not null | ||
| + | ) loop | ||
| + | for y in | ||
| + | ( | ||
| + | select sql_text | ||
| + | from | ||
| + | where 1=1 | ||
| + | and address = x.sql_address | ||
| + | ) loop | ||
| + | if ( y.sql_text not like ' | ||
| + | dbms_output.put_line( ' | ||
| + | dbms_output.put_line( x.username ); | ||
| + | dbms_output.put_line( x.logon_time || ' ' || x.current_time || ' SQL#=' || x.sql_hash_value); | ||
| + | dbms_output.put_line( substr( y.sql_text, 1, 250 ) ); | ||
| + | end if; | ||
| + | end loop; | ||
| + | end loop; | ||
| + | end; | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Identify the number of SQL statements in the library cache that are not using bind variables ==== | ||
| These SQL statements cause expensive hard parse events in the shared pool | These SQL statements cause expensive hard parse events in the shared pool | ||
| - | < | + | < |
| + | select count(' | ||
| + | , sum(decode(executions, | ||
| + | , sum(sharable_mem)/ | ||
| + | , sum(decode ( executions | ||
| + | , 1, sharable_mem | ||
| + | , 0 | ||
| + | ) | ||
| + | )/ | ||
| + | from | ||
| + | where sharable_mem > 0; | ||
| + | </ | ||
| - | =====List number of open cursors by user===== | + | ==== List number of open cursors by user ==== |
| - | < | + | < |
| + | set pages 999 | ||
| + | select sess.username | ||
| + | , sess.sid | ||
| + | , sess.serial# | ||
| + | , stat.value cursors | ||
| + | from | ||
| + | , v$statname sn | ||
| + | , v$session sess | ||
| + | where 1=1 | ||
| + | and sess.username | ||
| + | and sess.sid | ||
| + | and stat.statistic# | ||
| + | and sn.name | ||
| + | order by value; | ||
| + | </ | ||
| - | =====Show SQL statements generating the high number of cursors===== | + | ==== Show SQL statements generating the high number of cursors ==== |
| Use SID from query above. | Use SID from query above. | ||
| - | < | + | < |
| + | select sid | ||
| + | , sql_text | ||
| + | , count(' | ||
| + | , user_name | ||
| + | from | ||
| + | where 1=1 | ||
| + | and sid = &SID | ||
| + | group by sid | ||
| + | , sql_text | ||
| + | , user_name | ||
| + | order by 3; | ||
| + | </ | ||
| + | |||
| + | ==== Show current setting for max open cursors and highest number used since startup ==== | ||
| + | < | ||
| + | select max(a.value) highest_open_crsrs | ||
| + | , p.value | ||
| + | from | ||
| + | , v$statname | ||
| + | , v$parameter p | ||
| + | where 1=1 | ||
| + | and a.statistic# | ||
| + | and b.name | ||
| + | and p.name | ||
| + | group by p.value; | ||
| + | </ | ||
| - | =====Show current setting for max open cursors and highest number used since startup===== | ||
| - | < | ||
database_sessions.1544130326.txt.gz · Last modified: 2018/12/06 21:05 by 91.177.234.129
