Life Without Tools: Monitoring Database Activity With The Power Of SQL

Ari Kaplan
Independent Consultant

Topic Overview 
Which users are logged on to the database?  What SQL statement is a particular user account

executing? 
Setting up an interactive script for viewing users and SQL  What is my datafile activity like?  Which rollback segments are being used by which user

accounts? 
What has the largest number of concurrent users been?  Which locks are being held?  Which users have hogged up the most CPU usage?  What is the archiving status of the database?

etc) Rollback Segment Information (shrinks. optimal size.Quick Overview of Typical Monitoring Views Data Dictionary View What It hows DB _FREE_SP E Free chunks of tablespace Data File location an sizes Rea s/Writes to atafiles Detaile share pool information (free memory. Information on connections 200 system-wi e statistics Initialization parameter settings Locks within the atabase Segments an how many extents. next extent ) DB _D T _FILES V$FILEST T V$SG ST T V$ROLLST T V$SESSION V$SYSST T V$P R METER V$LO DB _SEGMENTS .

20) Machine FROM v$session ORDER BY schemaname.1. . osuser substr(machine. schemaname.Who Is Logged On? The $SESSION view contains information on all active sessions: SELECT sid.

Who Is Logged On? SELECT sid.20) Machine FROM v$session ORDER BY schemaname. A sample output from the above SQL follows: SID 1 2 3 4 7 13 6 14 12 SCHEMANAME SYS SYS SYS SYS SYSTEM SYSTEM WWW_DBA WWW_DBA PRODMAN OSUSER MACHINE plat oracle oracle oracle mr_boss headq headq uk_office uk_office ny_office . osuser. substr(machine.1. schemaname.

What QL tate ent Is A articular User Account xecuting? SELECT sql_text FROM v$sqlarea WHERE (address. sql_hash_value FROM v$session WHERE sid = &sid_number). . hash_value) IN (SELECT sql_address.

.What QL tate ent Is A articular User Account xecuting? SELECT sql_text FROM v$sqlarea WHERE (address. sql_hash_value FROM v$session WHERE sid = &sid_number). A sample output from the above SQL follows: Enter value for sid_number: 17 sid_number: old 1: WHERE SID = &sid_number new 1: WHERE USERNAME = 17 SQL_TEXT Update EMP set first_name = Ari where first_name = Arie . hash_value) IN (SELECT sql_address.

1. schemaname. osuser. SQL Script ³SQL2. hash_value) IN (SELECT sql_address. sql_hash_value from v$session WHERE sid = $SID_NUM) / EOF .20) Machine FROM v$session ORDER BY schemaname. Who is logged in to the database? UNIX Script ³SQL_RUN´ echo ³Enter the SID for which SQL you wish to see:´ read SID_NUM sqlplus system/$PASSWD@DATABASE << EOF echo ³Enter the database to connect to:´ read DATABASE echo ³Enter the password for the SYSTEM user:´ stty -echo echo ³Password:\c´ read PASSWD stty echo echo ³´ sqlplus system/$PASSWD\@DATABASE << EOF @sql1.Setting Up An Interactive Script or Viewing Users And SQL SQL Script ³SQL1.SQL´ SELECT sql_text sql_t xt FROM v$sqlarea WHERE (address. What SQL is sql_hash_value used? FROM v$session WHERE sid= sid_number.sql EOF select sql_text from v$sqlarea where (address.SQL´ SELECT sid. hash_value) in (select sql_addresss. substr(machine.

phyrds. 258 1 /u05/oradata/PRODDB/ctxindex01.dbf 1759 49649 1 1 /u03/oradata/PRODDB/temp01.0.dbf 455377 397 4 14.928 1 /u02/oradata/PRODDB/rbs01.dbf 0 0 0 0 /u04/oradata/PRODDB/tools01. FILE_NAME PHYRDS PHYWRTS BLOCKS/READ BLOCKS/WRITE /u01/oradata/PRODDB/system01.dbf 91528 11593 6.file_id=v$filestat. decode(phyrds.What is y datafile activity like? SELECT file_name.0.file_id=v$filestat.phyblkrd/phyrds) ³Blocks/Read´ decode(phywrts.0.0.phyblkwrt/phywrts) ³Blocks/Write´ FROM dba_data_files.248 1 /u02/oradata/PRODDB/rbs02.dbf 187293 15846 1.dbf 0 0 0 0 /u05/oradata/PRODDB/ctxdata01.file#. v$filestat WHERE dba_data_files.130 1 .dbf 332860 616291 1. dba_data_files.959 1 /u05/oradata/PRODDB/ctxdata02.file#.dbf 10243 3478 4. phywrts.

This is issued first. Use V OLLSTAT and V OLLNAME to get general roll ack seg ent activity Link V OLLNAME with V LOCK and V SESSION to deter ine which users are using which roll ack seg ents. . More infor ation on the users can e collected y further joining with the V OCESS view.Which oll ack Seg ents Are eing Used y Which User Accounts?     SET T A SACTIO USE OLL ACK SEGMENT roll ack seg ent_na e specifies a roll ack seg ent to ediately after COMMIT or use. or i OLL ACK.

Which oll ack Seg ents Are eing Used y Which User Accounts?     When a user is doing roll ack activity. The V LOCK view contains a SID column which links to the SID column of V SESSION. The ID1 column contains the object id of the object (rollback segment) being locked. . The USN (Unique Serial Number) of V OLLNAME links with V LOCK s ID1 column. Oracle creates a latch (lock of type TX ) on an extent of a roll ack seg ent. The V SESSION view links to the V OCESS view via the ADDR column.

Which Rollback Segments Are eing Used y Which User Accounts? NOTE: Only relevant columns are listed with tables V$ROLLNAME V$LOCK NAME USN V$PROCESS ADDR PID SPID USERNAME TERMINAL ID1 SID TYPE LMODE V$SESSION SID PADDR .

ORACLE PID.usn UNION SELECT µ2) ROLLBACK SEG. EOF . SID.idl1(+)/65536)=r.---------.pid). v\$session s v\ v\ v\ WHERE l.Which Rollback Segments Are eing Used y Which User Accounts? echo ³Enter the password for the SYSTEM user:´ read PASSWD sqlplus system/$PASSWD << EOF spool temp.name.spid. TERMINAL¶ FROM dual UNION SELECT µ1) µ FROM dual UNION SELECT µ3) ---------------.addr = s.paddr AND l.terminal.sid (+) AND p.17) || rpad(p.----------.---------.username. SYSTEM PID.11) || rpad(s.lmode(+)=6 AND trunc(l.type(+)=µTX¶ AND l.--------¶ FROM dual --------¶ ORDER BY 1.¶NO TRANSACTION¶).6)|| rpad(p.lst set head off set pagesize 0 connect system\$PASSWD@DBNAME system\ SELECT to_char(rownum+3)||¶) µ||rpad(r. v\$rollname r. v\$process p.11) || rpad(nvl(p.8) FROM v\$lock l.17)||rpad(to_char(p.sid. TRANSACTION.sid = s.

TRANSACTION. SYSTEM PID. ORACLE PID.----------------.-----------14 oracle ? 21 oracle ? .--------------.--------------4) R01 70 1632 5) BIG_ROLL 65 1492 SID.Which Rollback Segments Are eing Used y Which User Accounts? Below is a sample output from the preceding SQL: 1) 2) ROLLBACK SEGMENT. 3)------------------------3)------------------------. TERMINAL ---.

name = µlogons cumulative¶.12)||chr(9)|| µhighwater mark=µ||b. v$license b. .12). v$database c WHERE a.value.11)||chr(9)||chr(9)|| rpad(µ current logons=µ||(to_number(b.sessions_current)-1).1.What Has The Largest Number Of Concurrent Users een? set pagesize 24 set linesize 100 SELECT rpad(c.sessions_current)µcumulative logons=µ||rpad(substr(a.sessions_highwater||chr(9) FROM v$sysstat a.name||¶:¶.20)||chr(10)|| logons=µ||(to_number(b.

What Has The Largest Number Of Concurrent Users een? Output from the preceding SQL: PINDB: current logons=298 cumulative logons=7967 highwater mark=391 .

1.12)||chr(9)|| µhighwater mark=µ||b.name = µlogons cumulative¶.12).lst |grep ³LOG´ > $LOGDIR/archive`date ³+%m%d´`.value.sessions_highwater||chr(9) FROM v\$sysstat a. v\$database c WHERE a. EOF done cat $LOGDIR/temp_users.sessions_current)-1).log rm $LOGDIR/temp_users.11)||chr(9)||chr(9)|| rpad(µ current logons=µ||(to_number(b.lst .What Has The Largest Number Of Concurrent Users een? export LOGDIR=$ORACLE_HOME/udump Loop through echo ³Enter the password for the SYSTEM user:´ four read PASSWD databases for SID in PRODDB TESTDB TRAINDB DEVDB do sqlplus system/$PASSWD@$SID << EOF >> $LOGDIR/temp_users.20)||chr(10)|| µcumulative logons=µ||rpad(substr(a. v\$license b.name||¶:¶.lst set linesize 100 SELECT rpad(c.

What Has The Largest Number Of Concurrent Users een? Output from the preceding SQL for four databases: PRODDB: current logons=298 cumulative logons=7967 TESTDB: current logons=15 cumulative logons=187 TRAINDB: current logons=8 cumulative logons=613 DEVDB: current logons=23 cumulative logons=1024 highwater mark=391 highwater mark=15 highwater mark=25 highwater mark=30 .

These are created by the ORACLE_HOME/rdbms/admin/catblock. -.sql to analyze these views.Six views show information more clearly: DBA_LOCK: locks held and locks requested.Which Locks Are eing Held? -.is blocking? DBA_LOCK_INTERNAL: locks held and locks requested DBA_DML_LOCKS: DML locks (subset of DBA_LOCK) DBA_DDL_LOCKS: DDL locks (subset of DBA_LOCK) DBA_WAITERS: which sessions are holding and waiting for locks? DBA_BLOCKERS: which sessions are holding locks but are not waiting on another lock themselves? -. .V LOCK holds information on internal Oracle locks.Use ORACLE_HOME/rdbms/admin/utllockt.sql script -.

-----------------------Share None 113054 Not Blocking Exclusive 0 Exclusive 123 RowRow-X (SX) 123 None Not Blocking None Not Blocking None Not Blocking .----------------------LAST_CON ERT BLOCKING_OTHERS -----------------.Which Locks Are Being Held? SELECT * FROM DBA_LOCK. SESSION_ID --------------LOCK_ID1 -----------2 212 4 1 310 196612 310 438580 TYPE ------------------LOCK_ID2 -----------Media Recovery 0 Redo Thread 0 Transaction 399886 DML 0 MODE_HELD MODE_REQUESTED --------------.

     . The highest level in the tree is where HOLDING_SESSION is null.sql script.Which Locks Are Being Held? The previous views can be neatly arranged with the ORACLE_HOME/rdbms/admin/utllockt.sql script creates two temporary tables: LOCK_HOLDERS and DBA_LOCKS_TEM DBA_LOCKS_TEM is created to improve performance over using the slower DBA_LOCKS view. Output uses the CONNECT BY and START WITH clauses to make a hierarchical tree structure. The utllockt.

-----Exclusive 196613 406415 None Transaction Exclusive . WAITING_ LOCK_ LOCK_ SESSION TYPE MODE_ MODE_ LOCK_ ID1 ID2 REQUESTED HELD ---------.3*level³WAITING SESSION´.3*level-1)) || waiting_sessions µ.------133 319 ------------. mode_requested. lock_type. lock_id2 FROM lock_holders CONNECT BY PRIOR waiting_session=holding_session START WITH holding_session IS NULL.------.-------. mode_held.Which Locks Are Being Held? SELECT lpad(µ µ. lock_id1.

Which Users Have Hogged Up The Most CPU Usage? V$SESSION V$SESSTAT SID STATISTIC# VALUE SADDR SID SERIAL# AUDSID PADDR USER# USERNAME like %CPU used by this session% COMMAND OWNERID TADDR LOCKWAIT STATUS SERVER SCHEMA# SCHEMANAME OSUSER PROCESS MACHINE TERMINAL V$STATNAME NAME STATISTIC# CLASS PROGRAM TYPE SQL_ADDRESS SQL_HASH_VALUE MODULE MODULE_HASH ACTION ACTION_HASH CLIENT_INFO FIXED_TABLE_SEQUENCE ROW_WAIT_OBJ# ROW_WAIT_FILE# ROW_WAIT_BLOCK# ROW_WAIT_ROW# LOGON_TIME LAST_CALL_ET PDML_ENABLED FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER .

--------------OLAP_MAN (390) 15230 PIN (35) 4324 OPERATOR (17) 157 SYS (11) 0 .name.sid=ss. PARAMETER -------------------------------CPU used by this session CPU used by this session CPU used by this session CPU used by this session USER_PROCESS VALUE -----------------------.sid ORDER BY sn.Which Users Have Hogged Up The Most CPU Usage? SELECT substr(sn. statistic# = sn. v$sesstat se. se.statistic# AND sn. v$statname sn WHERE se. se_value DESC.30) parameter.value FROM v$session ss.username ||¶ (¶|| se_sid ||¶) µ user_process.name. ss.name LIKE µ%CPU used by this session%¶ AND se.1.

What Is The Archiving Status Of The Database? Why is archiving important? What happens when the archive volume fills up? How do you find the archiving status? How do you turn on archiving? SVRMGRL> ARCHIVE LOG LIST. Database log mode No Archive Mode Automatic archival Disabled Archive Destination ?/dbs/arch Oldest online log sequence 4525 Current log sequence 4528 .

µLOG_ARCHIVE_DEST=µ|| c.0.log_mode. NAME=PINDB LOG_MODE=NOARCHIVELOG LOG_ARCHIVE_START=TRUE LOG_ARCHIVE_DEST=/u01/oracle/product/8. v$parameter d WHERE b.value.value. µLOG_ARCHIVE_START=µ|| b. d.name. v$parameter b. µLOG_MODE=µ|| a. v$parameter c.What Is The Archiving Status Of The Database? SELECT µNAME=µ|| a.3/dbs/arch LOG_ARCHIVE_FOMAT=_%s_%t.name=µlog_archive_format¶.name=µlog_archive_format¶.name=µlog_archive_start¶ AND c.name=µlog_archive_dest¶ AND d. µLOG_ARCHIVE_FORMAT=µ|| d.value FROM v$database a.log .

org.orafans. This paper will be downloadable from the web page as well.oracle.server comp.org. www.oracle.databases.Where to Now? There are many discussion Newsgroups on the internet for you to give questions and get answers: comp.deja.orafaq.com. www. Other good sites with links: www.tools comp.com .orasearch.com.revealnet. www.oracle.databases.ioug.misc These can be accessed through a newsgroup program or www.databases.com Ari s free Oracle Tips web page at: There are over 370 tips and answers to questions that have been posed to me over the years. www.

Sign up to vote on this title
UsefulNot useful