Beruflich Dokumente
Kultur Dokumente
James F. Koopmann
Founder & President
jkoopmann@pinehorse.com
www.pinehorse.com
www.pinehorse.com www.dbcorral.com
James F. Koopmann
Where to Find Me on the NET
N-years of IT/core RDBMS experience Speaker
Oracle (V6-10g) 8i & 9i OCP
Contributing Author
technical articles a month 2 / Month
Forum expert / moderator Daily
www.pinehorse.com www.dbcorral.com
Content
System Statistics
Session Statistics
Merging System & Session Statistics
zz
www.pinehorse.com
Session Tracing
TKPROF
www.dbcorral.com
The Database System
It’s All About Throughput
User / Job Throughput
Request
(workload) Database User User
Completes Decides Queues
Request What to Next
Sorts, Reads, Writes, Enqueues, Redo log file sync, Do Next Transaction
activity, Buffer cache activity, Parsing,… db file sequential read,…
Can we relate the workload in our systems against the statistics that inhibit its’ throughput
SYSTEM .vs. SESSION Statistics
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
Buffer Cache
Why: Disk is slow, memory is fast
The more information in memory, the better performance we
will experience
Where: V$BUFFER_POOL_STATISTICS
V$DB_CACHE_ADVICE
(2K, 4K, 8K, 16K, 32K, KEEP, RECYCLE)
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
Buffer Cache - Advisory
SQL> SELECT name, block_size, size_for_estimate,
estd_physical_read_factor, estd_physical_reads
FROM V$DB_CACHE_ADVICE
WHERE advice_status = 'ON';
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
Buffer Cache - Advisory Report
BLOCK Cache Estd Phys Estd Phys
NAME SIZE Size Read Factor Reads Its all about reads
-------- ----- ----- ----------- ----------
DEFAULT 8192 48 2.1133 343669940
DEFAULT 8192 96 1.7266 280783364
DEFAULT 8192 144 1.4763 240091867
DEFAULT 8192 192 1.3573 220733606 1. Don’t reduce the size of
DEFAULT 8192 240 1.2801 208181172
DEFAULT 8192 288 1.2165 197842182 your cache if you are
DEFAULT 8192 336 1.1686 190052917 going to incur more
DEFAULT 8192 384 1.1202 182180544
DEFAULT 8192 432 1.0877 176884743 physical reads
DEFAULT 8192 480 1.0602 172420984
DEFAULT 8192 528 1.0196 165812231
DEFAULT 8192 544 1.00
1 162626093 CURRENT
DEFAULT 8192 576 .9765 158797352
DEFAULT 8192 624 .9392 152735392
DEFAULT 8192 672 .9216 149879874 2. Don’t increase the size of
DEFAULT 8192 720 .9013 146571255 your cache if you are not
DEFAULT 8192 768 .885 143928671
DEFAULT 8192 816 .8726 141908868 going to reduce the
DEFAULT 8192 864 .8607 139972381 number of reads
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
Segment Level Statistics
Why: Know thy transaction better by understanding segment access and
reduce ‘hot tables / objects”
Detection of what tables or indexes are being accessed the most will
allow you to fine tune memory structures and access methodologies.
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
V$SEGMENT_STATISTICS : reads / writes
SQL> > SELECT owner,object_type,object_name,value
FROM V$SEGMENT_STATISTICS
WHERE (statistic_name like '%read%‘
OR statistic_name like '%write%')
ORDER BY value desc
www.pinehorse.com www.dbcorral.com
SYSTEM Statistics
V$SEGMENT_STATISTICS : reads / writes
OWNER OBJECT_TYPE
------- -------------
OBJECT_NAME
-------------------
VALUE
--------
Result
OCPOLTP TABLE OLTP_MANAGER 15752896 What objects are causing
OCPOLTP INDEX PK_OLTP_MANAGER 10716704 the performance
OCPOLTP INDEX PK_OLTP_SALES 10615296
bottleneck in relation
OCPOLTP TABLE OLTP_SALES_VALUE 8491008
OCPOLTP TABLE OLTP_SALES 7050448 to reads and writes.
…
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SESSION
Column Description
SADDR Identifies a unique Oracle session address
STATUS Tells us the status of the session. We will be concerned with ACTIVE sessions, those
that are executing SQL
PROCESS This is the operating system process id for the connection. Only given here as a
reference so that you can go look on the O/S side.
SQL_ADDRESS Used with SQL_HASH_VALUE to identify the SQL statement that is currently being
executed.
SQL_HASH_VALUE Used with SQL_ADDRESS to identify the SQL statement that is currently being
executed. This SQL_HASH_VALUE is unique, or should be unique, to the same SQL
statement no matter when it is executed. Thus ‘select * from dual’ will always produce
the same SQL_HASH_VALUE.
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Sessions Activity
SELECT sid,
to_char(logon_time,'MMDDYYYY:HH24:MI') logon_time,
username,
type,
status,
process,
sql_address,
sql_hash_value
FROM v$session
WHERE username is not null
We have lift-off
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SESSION_WAIT
SELECT a.sid,
c.username,
SQL_HASH_VALUE,
b.event,
b.state,
b.wait_time,
b.p1,
b.p2,
b.p
FROM V$SESSION a, V$SESSION_WAIT b, DBA_USERS c, V$PROCESS d
WHERE a.sid = b.sid
AND a.user# = c.user_id
AND a.paddr = d.addr
Where did it go
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SESS_IO
SELECT sess_io.sid,
sess_io.block_gets,
sess_io.consistent_gets,
sess_io.physical_reads,
sess_io.block_changes,
sess_io.consistent_change
FROM V$SESS_IO sess_io,
V$SESSION session
WHERE sesion.sid = sess_io.sid
AND sesion.username is not null
Yup, Active sessions are do’n I/O. Anyone STUCK in the mud?
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SQLAREA
Column Description
SQL_TEXT This is the first 1000 characters of the SQL being executed by the user.
If more than 1000 characters you should use V$SQL_TEXT which is
described latter in the article.
OPTIMIZER_MODE The optimizer mode being utilized by the query
HASH_VALUE This is the hash value to the parent statement in the library cache
10g CPU_TIME The accumulated microseconds of CPU time used by the SQL
10g ELAPSED_TIME The accumulated microseconds elapsed time used by the SQL
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting the active SQL a user is executing
SELECT sesion.sid,
sesion.username,
optimizer_mode,
hash_value,
address,
cpu_time,
elapsed_time,
sql_text
FROM v$sqlarea sqlarea, v$session sesion
WHERE sesion.sql_hash_value = sqlarea.hash_value
AND sesion.sql_address = sqlarea.address
AND sesion.username is not null
SQL_TEXT
--------------------------------------------------
select a.table_name from dba_tables a,dba_tables b
Look (quick) what I’m doing Ma
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting the active SQL a user is executing
SELECT sesion.sid,
sesion.serial#,
sesion.username,
sesion.sql_id,
sesion.sql_child_number,
optimizer_mode,
hash_value,
address,
sql_text
FROM v$sqlarea sqlarea, v$session sesion
WHERE sesion.sql_hash_value = sqlarea.hash_value
AND sesion.sql_address = sqlarea.address
AND sesion.username is not null
SQL_TEXT
--------------------------------------------------
SELECT count(*) FROM sys.dba_tables WHERE owner = :1
Same old join to get SQL with a #$(& bind variable
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SQL_BIND_CAPTURE
10g Column Description
HASH_VALUE This is the hash value to the parent statement in the library
cache
NAME The name of the bind variable.
STATISTICS_LEVEL=ALL
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting bind variable contents for SQL that is executing
SELECT sesion.sid,
sesion.username,
sesion.sql_id,
sesion.sql_child_number,
sql_bind_capture.name,
sql_bind_capture.value_string
FROM v$sql_bind_capture sql_bind_capture, v$session sesion
WHERE sesion.sql_hash_value = sql_bind_capture.hash_value
AND sesion.sql_address = sql_bind_capture.address
AND sesion.username is not null
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting the active SQL a user is executing
SELECT sesion.sid,
sesion.serial#,
sesion.username,
sesion.sql_id,
sesion.sql_child_number,
optimizer_mode,
hash_value,
address,
sql_text
FROM v$sqlarea sqlarea, v$session sesion
WHERE sesion.sql_hash_value = sqlarea.hash_value
AND sesion.sql_address = sqlarea.address
AND sesion.username is not null
SQL_TEXT
--------------------------------------------------
SELECT count(*) FROM sys.dba_tables WHERE owner = :1
Same old join to get SQL with a #$(& bind variable
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Additional V$SESSION column information
Column Description
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extract the EXACT Execution Plan
9i
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extract the EXACT Execution Plan
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID 5qk509xugpmpv, child number 1
-------------------------------------
SELECT count(*) FROM sys.dba_tables WHERE owner = :1
Plan hash value: 580517646
----------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time|
----------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 242 (100)| |
| 1 | SORT AGGREGATE | | 1 | 149 | | |
| 2 | NESTED LOOPS | | 1 | 149 | 242 (1)| 03 |
| 3 | MERGE JOIN CARTESIAN | | 1 | 136 | 218 (1)| 03 |
| 4 | NESTED LOOPS OUTER | | 1 | 81 | 194 (1)| 03 |
| 5 | NESTED LOOPS OUTER | | 1 | 78 | 193 (1)| 03 |
| 6 | NESTED LOOPS OUTER | | 1 | 70 | 192 (1)| 03 |
|
|
7 |
8 |
|* 9 |
| 10 |
NESTED LOOPS OUTER
NESTED LOOPS
HASH JOIN
NESTED LOOPS
WOW !!
|
|
|
|
|
|
|
|
1 |
1 |
8 |
7 |
59 |
54 |
368 |
119 |
191
191
183
4
(1)| 03 |
(1)| 03 |
(1)| 03 |
(0)| 01 |
| 11 | TABLE ACCESS BY INDEX ROWID| USER$ | 1 | 14 | 1 (0)| 01 |
|* 12 | INDEX UNIQUE SCAN | I_USER1 | 1 | | 0 (0)| |
| 13 | TABLE ACCESS FULL | TS$ | 7 | 21 | 3 (0)| 01 |
|* 14 | TABLE ACCESS FULL | TAB$ | 16 | 464 | 178 (0)| 03 |
|* 15 | TABLE ACCESS BY INDEX ROWID | OBJ$ | 1 | 8 | 1 (0)| 01 |
|* 16 | INDEX UNIQUE SCAN | I_OBJ1 | 1 | | 0 (0)| |
|* 17 | INDEX UNIQUE SCAN | I_OBJ1 | 1 | 5 | 0 (0)| |
| 18 | TABLE ACCESS CLUSTER | SEG$ | 1 | 11 | 1 (0)| 01 |
|* 19 | INDEX UNIQUE SCAN | I_FILE#_BLOCK# | 1 | | 0 (0)| |
| 20 | TABLE ACCESS BY INDEX ROWID | OBJ$ | 1 | 8 | 1 (0)| 01 |
|* 21 | INDEX UNIQUE SCAN | I_OBJ1 | 1 | | 0 (0)| |
| 22 | TABLE ACCESS CLUSTER | USER$ | 1 | 3 | 1 (0)| 01 |
|* 23 | INDEX UNIQUE SCAN | I_USER# | 1 | | 0 (0)| |
| 24 | BUFFER SORT | | 1 | 55 | 217 (1)| 03 |
|* 25 | FIXED TABLE FULL | X$KSPPI | 1 | 55 | 24 (0)| 01 |
|* 26 | FIXED TABLE FIXED INDEX | X$KSPPCV (ind:2) | 1 | 13 | 24 (0)| 01 |
----------------------------------------------------------------------------------------------------
www.pinehorse.com www.dbcorral.com
SESSION Statistics
V$SQL_PLAN_STATISTICS
9i Column Description
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting plan statistics for execution of a SQL statement
SELECT sesion.sid,
sesion.username,
sql_plan_statistics.operation_id “Plan Id",
sql_plan_statistics.last_output_rows "Rows",
sql_plan_statistics.last_cr_buffer_gets "Consistent Gets",
sql_plan_statistics.last_disk_reads "Disk Reads"
FROM v$sql_plan_statistics sql_plan_statistics,
v$session sesion
WHERE sesion.sql_hash_value = sql_plan_statistics.hash_value
AND sesion.sql_address = sql_plan_statistics.address
AND sesion.username is not null
True statistical values accumulated for a SQL statement and each step of the Execution Plan
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Extracting plan statistics for execution of a SQL statement
SID USERNAME Plan Id Rows Consistent Gets Disk Reads
--- -------------- ---------- ---------- --------------- ----------
149 JKOOPMANN 1 1 8594 0
149 JKOOPMANN 2 643 8594 0
149 JKOOPMANN 3 643 8594 0
149 JKOOPMANN 4 643 8594 0
149 JKOOPMANN 5 643 7540 0
149 JKOOPMANN 6 643 6369 0
149 JKOOPMANN 7 643 4614 0
149 JKOOPMANN 8 643 3969 0
149 JKOOPMANN 9 1572 823 0
149 JKOOPMANN 10 7 11 0
149 JKOOPMANN 11 1 2 0
149 JKOOPMANN 12 1 1 0
149 JKOOPMANN 13 7 9 0
149 JKOOPMANN 14 1572 812 0
149 JKOOPMANN 15 643 3146 0
149 JKOOPMANN 16 1572 1574 0
149 JKOOPMANN 17 50 645 0
149 JKOOPMANN 18 555 1755 0
149 JKOOPMANN 19 555 645 0
149 JKOOPMANN 20 526 1171 0
149 JKOOPMANN 21 526 645 0
149 JKOOPMANN 22 526 1054 0
149 JKOOPMANN 23 526 2 0
149 JKOOPMANN 24 643 0 0
149 JKOOPMANN 25 1 0 0
149 JKOOPMANN
www.pinehorse.com 26 643 0 0 www.dbcorral.com
SESSION Statistics
$SQL_OPTIMIZER_ENV
10g Column Description
HASH_VALUE This is the hash value to the parent statement in the library cache
www.pinehorse.com www.dbcorral.com
SESSION Statistics
Optimizer environment settings for Executing SQL
SELECT sesion.sid,
sesion.username,
name,
isdefault,
value
FROM v$sql_optimizer_env sql_optimizer_env, v$session sesion
WHERE sesion.sql_hash_value = sql_optimizer_env.hash_value
AND sesion.sql_address = sql_optimizer_env.address
AND sesion.username is not null
www.pinehorse.com www.dbcorral.com
Merging SYSTEM and SESSION Statistics
Up to now it has been “REAL-TIME” monitoring
7.1 The Good Old Days
Catching a session waiting on a resource was literally hit and miss.
DBAs would sit in front of the terminal screen and continually hit the ENTER key to watch
activity in the V$SESSION_WAIT view trying to get a glimpse of a bottleneck.
This in itself was a very tedious task
Paranoia would set in (was I going to miss something)
?.? Does Anyone Really Monitor Databases Like THIS Any More
We NOW use or build our own monitoring tools to simulate resource capturing
www.pinehorse.com www.dbcorral.com
Merging SYSTEM and SESSION Statistics
Things to Overcome
Throughput of our workload is all that matters
• What workload is in our system (SQL)
• When did it happen
• How long did it happen
• What resources where consumed
• How much resources where consumed
• To Whom did it happen
• How was the user effected
The Solution
• Proper mapping of resources to workload
www.pinehorse.com www.dbcorral.com
Merging SYSTEM and SESSION Statistics
Why Do We Care
SYSTEM
HIGH level statistics have been global in nature and do not give the granularity needed
to tune for throughput.
SESSION
Low level statistics have been too granular and we have not had the ability to tie them
to true workloads submitted to our systems.
The MERGE
Your ability as a DBA/Developer to detect who/what is accessing the database, the
SQL being executed, and the exact resources consumed over a period of time is
paramount in your ability to properly manage and tune your database.
www.pinehorse.com www.dbcorral.com
Defined Connection To Notice Action Plan
Time Slice Four sessions are executing this SQL Look at SQL through Hash Value
statement (1245814300)
SQL HASH VALUE This SQL statement is consuming Research resource consumed
29.14 minutes across all sessions (row cache lock)
Top SQL is sorted to top SQL is coming from two machines Change SQL, Application or tune
O/S, DB, Session defined user SQL is being executed from sqlplus resource
www.pinehorse.com www.dbcorral.com
Defined Connection To Notice Action Plan
Time Slice Our tuning of SQL (124581430) paid Look at New SQL through Hash
off Value (1195313771)
SQL HASH VALUE Total Resource for SQL (124581430) is Research resource consumed
under 10 minutes (db file sequential read)
Top SQL is sorted to top Almost complete removal of resource
(row cach lock)
O/S, DB, Session defined user New top SQL & resource to work on
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
‘True’ Session Wait Activity
7.1 The Good Old Days
Catching a session waiting on a resource was literally hit and miss.
DBAs would sit in front of the terminal screen and continually hit the ENTER key to
watch activity in the V$SESSION_WAIT view trying to get a glimpse of a bottleneck.
This in itself was a very tedious task
Paranoia would set in (was I going to miss something)
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
V$ACTIVE_SESSION_HISTORY
To The Rescue
Oracle samples statistical data through kernel code for V$SESSION_WAIT type statistics
Stores the statistics in a rotating buffer space, 2 Meg per CPU
Access these statistics through V$ACTIVE_SESSION_HISTORY
One row of sampled activity for each session that was active
As this buffer gets full because of the activity on the system, Oracle will move the captured
statistics to disk as part of the Automatic Workload Repository (AWR) snapshot mechanism
and it will now be available through the DBA_HIST_ACTIVE_SESS_HISTORY view.
Depending on the activity of your system you may need to go to both views
We can go back in time and look at the true cause of performance bottlenecks
Warning :
Statistics may be lost if the buffer rotates before the AWR snapshot mechanism kicks in.
The snapshot mechanism only takes a sampling of the data in $ACTIVE_SESS_HISTORY
All you mice out there, Tom & Jerry – Watch Out! ………well…..….sort of
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
What resource is/was currently in high demand
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.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
GROUP BY active_session_history.event
ORDER BY 2 desc
EVENT TTL_WAIT_TIME
------------------------------ -------------
log file switch completion 2673862
row cache lock 1934383
log file parallel write 1072568
log file sequential read 715168
control file sequential read 429337
db file sequential read 229237
db file parallel write 168395
latch: redo allocation 18783
db file scattered read 14254
buffer busy waits 7624
SYSTEM_EVENT type Statistics for the last 30 minutes (timed based statistics)
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
What user is/was waiting the most?
SELECT active_session_history.session_id, dba_users.username,
SUM(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
FROM v$active_session_history active_session_history,
dba_users
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
AND active_session_history.user_id = dba_users.user_id
GROUP BY active_session_history.session_id, dba_users.username
ORDER BY 3 DESC
SESSTAT type Statistics for last 30 minutes, Who/What is my hog at a particular time
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
What SQL is “currently” using the most resources?
SELECT active_session_history.user_id,
dba_users.username,
sqlarea.sql_text,
SUM(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
FROM v$active_session_history active_session_history,
v$sqlarea sqlarea,
dba_users
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
AND active_session_history.sql_id = sqlarea.sql_id
AND active_session_history.user_id = dba_users.user_id
GROUP BY active_session_history.user_id,sqlarea.sql_text, dba_users.username
ORDER BY 4
Just Plain New (No more FALSE top 10 SQL hogs from V$SQLAREA)
www.pinehorse.com www.dbcorral.com
True Session Wait Activity
What object is causing the highest resource waits
SELECT dba_objects.object_name,
dba_objects.object_type,
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,
dba_objects
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
AND active_session_history.current_obj# = dba_objects.object_id
GROUP BY dba_objects.object_name, dba_objects.object_type,
active_session_history.event
ORDER BY 4 DESC
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
V$SESSION
Session Variable Description
SID Identifier for the connected session.
SERIAL# Serial number of the connected.
SERVICE_NAME Service name of the session and represents the database connected to.
MODULE Name to represent the module or code being executed.
Monitor resources used by section of code
Used to initiate traces and statistical gathering
ACTION Name to represent the action being performed by the module.
Monitor resources used by statement being executed
Used to initiate traces and statistical gathering
CLIENT_IDENTIFIER Identifier to represent the client session
Monitor an individual or group of users
Used to initiate traces and statistical gathering
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Set ACTION
EXEC DBMS_APPLICATION_INFO.SET_ACTION (action_name);
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Set your CLIENT_IDENTIFIER
EXEC DBMS_SESSION.SET_IDENTIFIER(v_user_identifier);
CLIENT_IDENTIFIER
------------------------------------
James?F.?Koopmann:PINEHORSE\FINE-ALE
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Logon Trigger
Setting CLIENT_IDENTIFIER, MODULE & ACTION
CREATE OR REPLACE TRIGGER LOGON_TRIGGER
AFTER LOGON ON DATABASE
DECLARE
v_user_identifier varchar2(64);
BEGIN
DBMS_APPLICATION_INFO.SET_MODULE(
module_name => 'LOGON_TRIGGER',
action_name => 'SELECT FROM DUAL');
DBMS_SESSION.SET_IDENTIFIER(v_user_identifier);
dbms_lock.sleep(5);
DBMS_APPLICATION_INFO.SET_MODULE(NULL,NULL);
END;
/
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
V$SESSION After Setting MODULE & ACTION name
SELECT sid,
client_identifier,
action,
module
FROM V$SESSION
Proper setting of the ACTION and MODULE name are imperative for collecting
statistics around specific code functions and transactions. Once they are set you
can now begin getting those fine grain details about the area of code that is
performing poorly. Next time we start looking at how to actually pull out of
current sessions that fine grain information to zero in on where we need to
concentrate our tuning efforts.
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Individual Session Activity History
SELECT session_id, client_id, event,
SUM(wait_time + time_waited) ttl_wait_time
FROM v$active_session_history active_session_history
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
GROUP BY session_id, client_id, event
ORDER BY 4 DESC
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Accumulated Wait Activity for User Defined Area
SELECT client_id, event,
SUM(wait_time + time_waited) ttl_wait_time
FROM v$active_session_history active_session_history
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
GROUP BY client_id, event
ORDER BY 3 DESC
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
What Code consumes most resources
SELECT module, action,
SUM(wait_time + time_waited) ttl_wait_time
FROM v$active_session_history active_session_history
WHERE active_session_history.sample_time
BETWEEN TO_DATE('20050208:0540','YYYYMMDD:HH24MI')
AND TO_DATE('20050208:0550','YYYYMMDD:HH24MI')
GROUP BY module,action
ORDER BY 3 DESC
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
DBMS_MONITOR Package
Notes
Program Description
CLIENT_ID_STAT_ENABLE Enable / Disable • Statistics can be seen through V$CLIENT_STATS.
Statistics gathering for
CLIENT_ID_STAT_DISABLE A given Client Identifier
CLIENT_ID_TRACE_ENABLE Enable / Disable • Trace data may be written to many trace files as the
Tracing for trace may effect multiple connected sessions with
CLIENT_ID_TRACE_DISABLE A given Client Identifier the same client identifier.
SERV_MOD_ACT_STAT_ENABLE Enable / Disable • Statistics can be seen through
Statistics gathering for V$SERV_MOD_ACT_STATS.
SERV_MOD_ACT_STAT_DISABLE (Service Name/MODULE/ACTION)
combination.
SERV_MOD_ACT_TRACE_ENABLE Enable / Disable • Trace data may be written to many trace files as the
Tracing for trace may effect multiple connected sessions with
SERV_MOD_ACT_TRACE_DISABLE (Service Name/MODULE/ACTION) the same Service Name, Module, or Action.
combination.
SESSION_TRACE_ENABLE Enable / Disable • Session must exist
Tracing for • Traces only a given session
SESSION_TRACE_EISABLE a session identifier (SID) • Enables / Disables a trace for the instance
submitter is connected to.
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Determine Connected Sessions for Tracing
SELECT sid, serial#,
client_identifier,
service_name,
action,
module
FROM V$SESSION
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
DBMS_MONITOR Package
Program Example How to Enable
CLIENT_ID_STAT_ENABLE DBMS_MONITOR.CLIENT_ID_STAT_ENABLE(client_identifier);
CLIENT_ID_STAT_DISABLE
SERV_MOD_ACT_STAT_DISABLE Action_name, may be an empty field if not defined for the session.
•Use DBMS_MONITOR.ALL_ACTIONS variable to reference ALL sessions that have an
action_name defined.
•To reference ALL sessions that do not have this variable defined, use NULL.
•The contents of this field is actually ‘###ALL_ACTIONS’ and you should avoid naming any
actions or module names with this.
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
DBA_ENABLED_AGGREGATIONS
SELECT * FROM DBA_ENABLED_AGGREGATIONS ;
Column Description
AGGREGATION_TYPE This is the type of statistical aggregation being done. This relates to the actual
procedure called in the DBMS_MONITOR package. In Listing 1 I we have called the
CLIENT_ID_STAT_ENABLE and SERV_MOD_ACT_STAT_ENABLE procedures
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Views that Contain Statistical Information
Table Description
V$ACTIVE_SESSION_HISTORY •The V$ACTIVE_SESSION_HISTORY view is a holding area for sampled session data
performed on your behalf from the Oracle engine. This data is sampled once per second
and is a great resource for determining where the true bottleneck lies in your system.
•Use this view in conjunction with setting the CLIENT_IDENTIFIER, ACTION, and MODULE
to aggregate (GROUP BY) and find the largest consumers of resources within your system.
DBA_HIST_ACTIVE_SESS_HISTORY •This view is just a historical representation of the V$ACTIVE_SESSION_HISTORY view
above. While it is good for some historical information, be warned that it does not contain
all the collected statistics that were captured in the V$ACTIVE_SESSION_HISTORY view.
V$CLIENT_STATS •If you have enabled any statistics for a CLIENT_IDENTIFIER you will see the aggregated
statistics for that CLIENT_IDENTIFIER in this view that are currently active.
•These statistics are only good for current activity and troubleshooting and thus should
only be used for getting a glimpse of what clients are consuming the most resources.
•Very valuable for a quick current view but if you need to drill to a particular client you will
end up going to V$ACTIVE_SESSION_HISTORY.
V$SERVICE_STATS •Provides a reduced set of statistics that can be used to determine how well an instance is
performing for the requests made upon it.
•This is typically the SID_NAME but beware it may be different if you are not connecting
through TNS.
V$SERV_MOD_ACT_STATS •This view provides an aggregated view for the combination of SERVICE_NAME, MODULE,
and ACTION you have defined when you enabled aggregated statistics.
Just be careful to name these aggregates appropriately so that you can quickly determine
where bottlenecks reside.
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
DBMS_MONITOR Package (Trace Control)
Program Example How to Enable
CLIENT_ID_TRACE_ENABLE DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE(client_id, waits, binds);
CLIENT_ID_TRACE_DISABLE waits : If TRUE, wait information is present in the trace.
Binds : If TRUE, bind information is present in the trace.
SERV_MOD_ACT_TRACE_ENABLE DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE
(service_name, module_name, action_name, waits, binds, instance_name);
•Use DBMS_MONITOR.ALL_ACTIONS variable to reference ALL sessions that have an
SERV_MOD_ACT_TRACE_DISABLE
action name defined.
•To reference ALL sessions that do not have this variable defined, use NULL.
•The contents of this field is actually ‘###ALL_ACTIONS’ and you should avoid naming any
actions or module names with this.
instance_name restricts tracing to the named instance_name.
SESSION_TRACE_ENABLE DBMS_MONITOR.SESSION_TRACE_ENABLE(session_id, serial_number, waits, binds)
session_id, Must provide. If just a session_id is given than all serial numbers will be traced.
SESSION_TRACE_DISABLE If session_id and serial_number are NULL than you trace your own current session.
Enable tracing for CLIENT_IDENTIFIER with wait information but no bind information
EXECUTE DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE(‘PAULANER\Administrator:PINEHORSE\PAULANER’,TRUE,FALSE);
EXECUTE DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE(‘James?F.?Koopmann:PINEHORSE\FINE-ALE’,TRUE,FALSE);
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
DBA_ENABLED_TRACES
SELECT * FROM DBA_ENABLED_TRACES ;
Column Description
TRACE_TYPE Type of the trace:
(CLIENT_ID, SERVICE, SERVICE_MODULE, SERVICE_MODULE_ACTION)
NOT SESSION
PRIMARY_ID This is the CLIENT_IDENTIFIER or SERVICE_NAME in the call
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Part of a Trace File
Dump file d:\oracle\product\10.1.0\admin\lager\udump\lager_ora_162.trc
*** ACTION NAME:() 2005-02-08 05:29:13.515
*** MODULE NAME:(SQL*Plus) 2005-02-08 05:29:13.515
*** SERVICE NAME:(pinehorse.com) 2005-02-08 05:29:13.515
*** CLIENT ID:(PAULANER\Administrator:PINEHORSE\PAULANER) 2005-02-08 05:29:
*** SESSION ID:(161.20) 2005-02-08 05:29:13.515
=====================
PARSING IN CURSOR #1 len=18 dep=0 uid=64 oct=47 lid=64 tim=2985552493 hv=21
BEGIN work; END;
END OF STMT
PARSE #1:c=0,e=208,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=2985552444
*** ACTION NAME:(SELECTSeqNbr) 2005-02-08 05:29:13.578
*** MODULE NAME:(GetTransactionNumber) 2005-02-08 05:29:13.578
=====================
PARSING IN CURSOR #2 len=32 dep=1 uid=64 oct=3 lid=64 tim=2985568906 hv=124
SELECT WORKSEQ.NEXTVAL FROM DUAL
END OF STMT
PARSE #2:c=0,e=165,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,tim=2985568889
EXEC #2:c=0,e=257,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,tim=2985571308
=====================
PARSING IN CURSOR #3 len=129 dep=2 uid=0 oct=6 lid=0 tim=2985572102 hv=2635
update seq$ set increment$=:2,minvalue=:3,maxvalue=:4,cycle#=:5,order$=:6,c
END OF STMT
PARSE #3:c=0,e=159,p=0,cr=0,cu=0,mis=0,r=0,dep=2,og=4,tim=2985572087
EXEC #3:c=15625,e=790,p=0,cr=1,cu=2,mis=0,r=1,dep=2,og=4,tim=2985581702
STAT #3 id=1 cnt=1 pid=0 pos=1 obj=0 op='UPDATE (cr=1 pr=0 pw=0 time=592 u
FETCH #2:c=15625,e=15799,p=0,cr=1,cu=3,mis=0,r=1,dep=1,og=1,tim=2985587476
*** ACTION NAME:(INSERT) 2005-02-08 05:29:13.593
*** MODULE NAME:(LoadTransx) 2005-02-08 05:29:13.593
Can you say grep –i {search_pattern} *.trc
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Syntax & Example for trcsess
trcsess [output = output_file_name]
[session = session_Id]
[clientid = client_Id]
[service = service_name]
[action = action_name]
[module = module_name]
[trace_files]
Example:
Consolidate all trace files for everyone that has been in the Transaction Module (GetTransactionNumber).
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
What Can TKPROF Do
1. Formats all the cryptic information in a more readable form that is understandable for you
and me.
2. Breaks out each statement executed during the trace period and presents various
statistics that enable us to determine where bottlenecks lay and where we might spend our
time tuning application code.
3. Can generate a SQL script that allows us to store the captured statistics into a database.
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Syntax & Example for TKPROF
tkprof trace_file report_file
[waits = yes|no]
[sort = option]
[print = n]
[aggregate = yes|no]
[insert = filename3]
[sys = yes|no]
[table = schema.table]
[explain = user/password]
[record = filename4]
[width = n]
Example:
Produce report for all users within the Transaction Module (GetTransactionNumber).
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
TKPROF Report
TKPROF: Release 10.1.0.2.0 - Production on Tue Feb 8 20:29:57 2005
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
www.pinehorse.com www.dbcorral.com
Oracle Session Tracing
Tracing Enhancements
The world of tracing Oracle sessions has changed drastically with Oracle 10g.
Not so much the mechanisms but in the way we can initiate a trace for one session or a group of
sessions.
We are now able to "tag" sessions as they are connected to Oracle and move through
applications.
We can now accurately pinpoint where in the application a session is, the amount of resources
being consumed, and more importantly if the session is having difficulty and is in need of
tuning.
This is extremely important in a multi-tier environment where connection polling takes place and
we would be lost if it where not for these new tracing features.
www.pinehorse.com www.dbcorral.com
Zeroing in on Performance in Oracle 10g
James F. Koopmann
Founder & President
jkoopmann@pinehorse.com
www.pinehorse.com
www.pinehorse.com www.dbcorral.com