Académique Documents
Professionnel Documents
Culture Documents
ss
Tamilselvan G
Be
In ac
Co f o t o n
rp ech
or
at
io
n
a
Cl
-3
2007-12-25
Optimizer Statistics
Generating Extended Trace Data
Trace file Walk Through
Tkprof
2007-12-25
1. Optimizer Statistics
2.
3.
4.
System Statistics (IO performance and utilization and CPU performance and
Utilization)
2007-12-25
2007-12-25
1. Optimizer Statistics
If you choose not to use automatic statistics gathering, then you need to
manually collect statistics in all schemas, including sys and system schemas.
Statistics are gathered using the DBMS_STATS package. This PL/SQL
package is also used to modify, view, export, import, and delete statistics.
#
1
Procedure
GATHER_INDEX_STATS
Collects
Index Statistics
GATHER_TABLE_STATS
GATHER_SCHEMA_STATS
GATHER_DICTIONARY_STATS
GATHER_DATABASE_STATS
GATHER_FIXED_OBJECTS_STATS
GATHER_SYSTEM_STATS
2007-12-25
1. Optimizer Statistics
SET_PARAM Procedure
This procedure sets default values for parameters of DBMS_STATS procedures. You
can use the GET_PARAM Function to get the current default value of a parameter.
Syntax
DBMS_STATS.SET_PARAM ( pname IN VARCHAR2, pval IN VARCHAR2);
Pname
Pval
2007-12-25
1. Optimizer Statistics
SET_PARAM Procedure
Example:
SQL> exec dbms_stats.set_param('ESTIMATE_PERCENT','10') ; -- To change estimate percentage
PL/SQL procedure successfully completed.
SQL> exec dbms_stats.set_param('DEGREE','8'); ---- To change degree of parallelism
2007-12-25
Note: 1. When I tried to use FORCE parameter with a value TRUE, I get an error. Check
with your release. The FORCE parameter if set to TRUE will allow the procedure to
gather statistics even if the objects statistics is locked.
2. In 10g Oracle automatically gathers statistics when CREATE INDEX
command is used.
Beacon Infotech Corporation
www.oracleact.com
2007-12-25
STATTAB
STATID
STATOWN
Beacon Infotech Corporation
www.oracleact.com
User statistics table identifier describing where to save the current statistics
Identifier (optional) to associate with these statistics within stattab
Schema containing stattab (if different than ownname)
2007-12-25
begin dbms_stats.gather_schema_stats(
ownname => TAMIL',
estimate_percent => dbms_stats.auto_sample_size,
method_opt => 'for all columns size skewonly',
degree => 7 );
end;
/ Infotech Corporation
Beacon
www.oracleact.com
10
2007-12-25
Note:The component id of the schema to analyze (NULL will result in analyzing schemas of all
RDBMS components). Please refer to comp_id column of DBA_REGISTRY view. The procedure
always gather statistics on 'SYS' and 'SYSTEM' schemas regardless of this argument.
Example: exec DBMS_STATS.GATHER_DICTIONARY_STATS ;
11
2007-12-25
block_sample
method_opt
degree
granularity
cascade
stattab
statid
options
objlist
statown
gather_sys
no_invalidate
Example in :
SQL> begin
dbms_stats.gather_database_stats( estimate_percent => 2, degree => 8);
end;
/
PL/SQL procedure successfully completed.
Beacon Infotech Corporation
www.oracleact.com
12
2007-12-25
to
Example:
SQL> exec dbms_stats.gather_fixed_objects_stats;
PL/SQL procedure successfully completed.
13
2007-12-25
14
2007-12-25
Step 2
Start collecting Statistics
SQL> begin
dbms_stats.create_stat_table(
ownname => 'SYS',
stattab => 'TAMIL_SYSTEM_STATS',
tblspace => 'USERS');
end;
/
SQL> begin
2 dbms_stats.gather_system_stats(
3
'START', 5, 'TAMIL_SYSTEM_STATS',
'OLTP', 'SYS');
4 end;
5 /
Step 3
Execute some scripts for some duration.
Step 4
Stop Collecting SYSTEM Stats
SQL> begin
2 dbms_stats.gather_system_stats(
3 'STOP', 5, 'TAMIL_SYSTEM_STATS',
'OLTP', 'SYS');
4 end;
5 /
COUNT(*)
---------43822
Beacon Infotech Corporation
www.oracleact.com
15
2007-12-25
1.
2.
3.
Session altered.
SQL> select count(*) from user_objects ;
COUNT(*)
---------94
Example:
SQL> alter session set timed_statistics = true ;
Session altered.
Session altered.
16
2007-12-25
COMMAND
Comments
-- To Turn on tracing
COMMIT;
17
2007-12-25
COMMAND
Comments
In init.ora file:
EVENT = 10046 trace name context forever, level 8;
18
2007-12-25
COMMAND
Comments
19
2007-12-25
COMMAND
Comments
20
Stop tracing
2007-12-25
COMMAND
Using oradebug
Comments
Oracle pid: 20, Unix process pid: 1369, image: oracle@hostname (TNS V1-V3)
SQL> oradebug event 10046 trace name context forever, level 12;
Statement processed.
21
2007-12-25
COMMAND
Comments
10
LOGON Trigger
This is my favorite
method to generate
trace data on some one
elses code.
LOGOFF Trigger
create or replace trigger user_logoff_trg
before logoff on database
begin
if USER = 'TAMIL' then
execute immediate
'alter session set events ''10046 trace name context off';
end if;
end;
/
22
2007-12-25
COMMAND
Comments
11
Using DBMS_MONITOR
New feature
in 10g for
tracing a
user session.
DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE(
service_name IN VARCHAR2,
module_name IN VARCHAR2 DEFAULT ANY_MODULE,
action_name IN VARCHAR2 DEFAULT ANY_ACTION,
waits
IN BOOLEAN DEFAULT TRUE,
binds
IN BOOLEAN DEFAULT FALSE,
instance_name IN VARCHAR2 DEFAULT NULL);
DBMS_MONITOR.SERV_MOD_ACT_TRACE_DISABLE(
service_name IN VARCHAR2,
module_name IN VARCHAR2,
action_name
IN VARCHAR2 DEFAULT ALL_ACTIONS,
instance_name IN VARCHAR2 DEFAULT NULL);
23
2007-12-25
COMMAND
11
Using DBMS_MONITOR
Comments
ACTION SID
----------------107
SQL> begin
dbms_monitor.serv_mod_act_trace_enable
('ORDBA', 'SQL*Plus',
DBMS_MONITOR.ALL_ACTIONS, TRUE, TRUE, NULL);
end;
/
PL/SQL procedure successfully completed.
SQL> select /* test dbms monitor */ count(1) from emp ;
COUNT(1)
---------5
To disable tracing,
use dbms_monitor.serv_mod_act_trace_disable procedure.
Beacon Infotech Corporation
www.oracleact.com
24
2007-12-25
25
2007-12-25
Kernel identifies every SQL statement used by the session in a PARSING IN CURSOR section.
Oracle opened a cursor # 2 for setting up the trace event 10046.
The END OF STMT line indicates end of SQL statement.
26
2007-12-25
27
2007-12-25
PARSE #1:c=0,e=442,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=51,148,592,669
BINDS #1:
bind 0: dty=1 mxl=32(30) mal=00 scl=00 pre=00 oacflg=03 oacfl2=0010 size=32 offset=0
bfp=ffffffff7bb65368 bln=32 avl=02 flg=05
value="TV
EXEC #1:c=10000,e=1408,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=51,148,595,144
Database Calls
A database call is a sub routine in the Oracle kernel.
PARSE, EXEC, FTECH, ERROR, UNMAP, SORT UNMAP are the most common types of calls.
Let us study line 41 & 46
c = total CPU time consumed by the Oracle process during the call
e = the amount of wall clock time that elapsed during the call
p = the number of physical Oracle database blocks reads
cr = the number of Oracle database blocks obtained in consistent mode from the buffer cache
or undo blocks
cu = the number of Oracle database blocks in current mode from the buffer cache
mis = the number of library cache misses during the call, if its non-zero, then it motivates hard parse
r = the number of rows returned by the call
dep = recursive depth of the cursor
og = optimizer goal ( 1 = ALL ROWS, 2 = FIRST_ROWS, 3 = RULE, 4 = CHOOSE)
tim = a value expressed in microseconds
Notice the error in tim values between two subsequent values, 51,148,592,669 and 51,148,595,144.
51,148,592,669+ 1408 is not equal to 51,148,595,144. The error is 1067 micro seconds.
Beacon Infotech Corporation
www.oracleact.com
28
2007-12-25
WAIT EVENTS
When a wait event completes and level-8 or level-12 SQL tracing is active, then Oracle emits a WAIT line upon
competition of that event.
In line 47 the #2 is the cursor number.
nam = Name reveals which part of the kernel code is responsible for the portion of your response time
There are 808 WAIT EVENTS in 10g.
ela = The elapsed time in microseconds in 9i/10g, in cent seconds prior to 9i.
p1,p2, p3 = The meaning differs by nam.
select name, PARAMETER1, PARAMETER2, PARAMETER3, WAIT_CLASS
from v$event_name where PARAMETER1 is not null
Note: Wait events are not the same as WAIT TIME (Queueing delay)
with respect to Queueing Theory.
29
2007-12-25
WAIT #1: nam='db file scattered read' ela= 591 p1=4 p2=8475 p3=13
FETCH #1:c=1570000,e=1535135,p=6228,cr=6204,cu=0,mis=0,r=1,dep=0,og=1,tim=51150130789
WAIT #1: nam='SQL*Net message from client' ela= 1532 p1=1650815232 p2=1 p3=0
WAIT #1: nam='SQL*Net message to client' ela= 6 p1=1650815232 p2=1 p3=0
FETCH #1:c=0,e=241,p=0,cr=0,cu=0,mis=0,r=2,dep=0,og=1,tim=51150136462
WAIT #1: nam='SQL*Net message from client' ela= 2143 p1=1650815232 p2=1 p3=0
STAT #1 id=1 cnt=3 pid=0 pos=1 obj=0 op='SORT GROUP BY (cr=6204 pr=6228 pw=0 time=1535323 us)'
STAT #1 id=2 cnt=118 pid=1 pos=1 obj=0 op='HASH JOIN (cr=6204 pr=6228 pw=0 time=1509029 us)'
STAT #1 id=3 cnt=18194 pid=2 pos=1 obj=66386 op='TABLE ACCESS FULL SALES (cr=4657 pr=4655 pw=0 time=1258785 us)'
STAT #1 id=4 cnt=12000 pid=2 pos=2 obj=0 op='MERGE JOIN CARTESIAN (cr=1547 pr=1573 pw=0 time=287550 us)'
STAT #1 id=5 cnt=1 pid=4 pos=1 obj=51510 op='TABLE ACCESS FULL PRODUCT (cr=3 pr=32 pw=0 time=8072 us)'
STAT #1 id=6 cnt=12000 pid=4 pos=2 obj=0 op='BUFFER SORT (cr=1544 pr=1541 pw=0 time=267504 us)'
STAT #1 id=7 cnt=12000 pid=6 pos=1 obj=66384 op='TABLE ACCESS FULL CUSTOMER (cr=1544 pr=1541 pw=0 time=218698 us)'
=====================
Let us study about the STAT lines. These are the lines converted into ROW SOURCE in the tkprof output.
Each STAT line contains the following statistics:
STAT #1 represents the cursor ID
id = Unique ID of the ROW SOURCE operation within the STAT line set
cnt = Number of rows returned by an individual operation
pid = ID of this operations parent operation
pos = Position number.
obj = Object ID
op = Name of the operation ( cr = consistent read, pr = # of Physical Reads, pw = # of Physical Writes,
time = elapsed duration in Microseconds in 9i/10g)
Beacon Infotech Corporation
www.oracleact.com
30
2007-12-25
PARSING IN CURSOR #2 len=6 dep=0 uid=45 oct=44 lid=45 tim=51150143465 hv=3480936638 ad='9474c610'
commit
END OF STMT
PARSE #2:c=0,e=422,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=51150143445
XCTEND rlbk=0, rd_only=1
EXEC #2:c=0,e=381,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=51150144326
WAIT #2: nam='SQL*Net message to client' ela= 5 p1=1650815232 p2=1 p3=0
WAIT #2: nam='SQL*Net message from client' ela= 1129 p1=1650815232 p2=1 p3=0
464 =====================
31
2007-12-25
Tkprof output:
call
count
cpu elapsed disk
query
current
rows
------------ ----------------- ---------- ---------- ---------- ---------Parse
1
0.00
0.00
0
0
0
0
Execute 1
0.01
0.00
0
0
0
0
Fetch
2
1.57
1.53
6228
6204
0
3
------------------- ---------- ---------- ---------- ------------------total
4
1.58
1.53
6228
6204
0
3
===========================================================
2. Unaccounted Time
You will always find some unaccounted time in the trace file because some lag in computing the
elapsed time. Do not expect perfection from the wall clocks.
3. Recursive Calls whose depth > 0
Recursive database calls value of statistics in the set (c, e, p, cr, cu ) is an aggregation of
consumption for the entire recursive family tree.
4. No idle event
All wait events are important to measure response time calculation prior to 10g. In 10g Oracle
categorizes some events as idle events example pmon timer etc.
Beacon Infotech Corporation
www.oracleact.com
32
2007-12-25
4. Tkprof
33
2007-12-25
4. Tkprof
2.
3.
4.
5.
34
2007-12-25
4. Tkprof
where:
What is Cost?
Cost = (
#SRds * sreadtim +
#MRds * mreadtim +
#CPUCycles / cpuspeed
) / sreadtim
Reference:
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96533/ex_plan.htm#19259
You dont see this formula in 10g Performance Tuning Guide manual. I suspect Oracle Corp does
not want too many questions about the validity of this formula any more.
Beacon Infotech Corporation
www.oracleact.com
35
2007-12-25
4. Tkprof
Name
Null?
Type
INV_NUM
CUST_ID
SALE_DATE
DATE
PRODUCT_ID
SALE_QTY
NUMBER(38)
SALE_AMT
NUMBER(12,2)
SOMETXT
VARCHAR2(500)
PK
36
2007-12-25
4. Tkprof.
37
2007-12-25
4. Tkprof.
-------------------------------------------------------------------------------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT
|
| 16 | 1536 | 10752 (6)| 00:01:21 |
| 1 | SORT ORDER BY
|
| 16 | 1536 | 10752 (6)| 00:01:21 |
| 2 | NESTED LOOPS
|
| 16 | 1536 | 10751 (6)| 00:01:21 |
|* 3 | HASH JOIN
|
| 322 | 22862 | 10588 (6)| 00:01:20 |
|* 4 | TABLE ACCESS FULL
| PRODUCT
| 300 | 5700 | 22 (28)| 00:00:01 |
|* 5 | TABLE ACCESS FULL
| SALES
| 4524 | 229K| 10565 (6)| 00:01:20 |
|* 6 | TABLE ACCESS BY INDEX ROWID | CUSTOMER
| 1 | 25 | 1 (0)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN
| CUSTOMER_PK | 1 |
| 0 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------3 - access("S"."PRODUCT_ID"="P"."PRODUCT_ID")
4 - filter("P"."PRODUCT_CAT"='TV')
5 - filter("S"."SALE_DATE">=TO_DATE('2004-01-01 00:00:00', 'yyyy-mm-dd
hh24:mi:ss') AND "S"."SALE_DATE"<=TO_DATE('2004-03-31 23:59:59', 'yyyy-mm-dd
hh24:mi:ss'))
6 - filter("C"."STATE"='GA')
7 - access("S"."CUST_ID"="C"."CUST_ID")
38
2007-12-25
4. Tkprof...
Note: To fetch 38 rows, this SQL consumed 78078 LIO which is very high.
The line TABLE ACCESS FULL SALES (cr=77630 pr=77568 pw=0 time=8821713 us) really
shows the inefficiency of the access plan b/c of a very high consistent and physical reads.
In a OLTP system and in a single table query, I expect 10 to 20 LIO consumption per row
output, if the SQL does not
contain an aggregate function. Look at the elapsed time , 9.15 seconds, very high.
The ration of blocks /row is 2054. So, we need to tune this SQL.
Beacon Infotech Corporation
www.oracleact.com
39
2007-12-25
4. Tkprof.
Note: We have achieved a dramatic improvemnet both in explased time and LIO
requirement. Some times you may achieve the expected ratio because of the
ways rows inserted and the row length.
The current ratio is 133 which is better than the previous (2054).
Beacon Infotech Corporation
www.oracleact.com
40
2007-12-25
4. Tkprof.
Type
call
count
cpu elapsed
disk
query
current rows
------------ ----------------- ---------- ------------------- ---------Parse
1
0.02
0.01
0
308
0
0
Execute
1
0.00
0.00
0
0
0
0
Fetch
21
2.70
2.64
524
171433
0
10000
------------------- ---------- ------------------- ---------- ---------total
23
2.72
2.66
524
171741
0
10000
Rows Row Source Operation in 9i
------- --------------------------------------------------10000 FILTER
170000 TABLE ACCESS FULL MYTABLE
165904 SORT AGGREGATE
165904 FIRST ROW
165904 INDEX RANGE SCAN (MIN/MAX) MYTABLE_UK (object id 68368)
Beacon Infotech Corporation
www.oracleact.com
Null?
41
LAST_PAY_DATE
NET_PAY
NUMBER(10,2)
CUM_GROSS
NUMBER(10,2)
CUM_NET
NUMBER(10,2)
CUM_FED_TAX
NUMBER(10,2)
CUM_STATE_TAX
NUMBER(10,2)
SOMETXT
VARCHAR2(200)
2007-12-25
4. Tkprof.
cpu elapsed
disk
query
-------- ---------- ---------- ---------0.01
0.01
0
356
0.00
0.00
0
0
1.90
1.86
0
170525
------- ------------------- ---------1.91
1.87
0
170881
current
---------0
0
0
---------0
rows
---------0
0
10000
---------10000
42
2007-12-25
4. Tkprof.
My query is:
SQL> desc t1
Name
Null? Type
----------------- -------- -----------A
NUMBER
B
NUMBER
C
NUMBER
D
NUMBER
RDT
DATE
Number of ROWS = 100
SQL> desc t2
Name
Null? Type
----------------- -------- -----------A
NUMBER
B
NUMBER
C
CHAR(1)
D
NUMBER
Number of ROWS = 100
43
2007-12-25
B
C
-------- 20 x
MAX1 MAX2
-------- -------230
320
4. Tkprof.
select t1.a, t1.b, t2.c, t2.d,
max(x1) max1, max(x2) max2
from (select a, b,d , t1_fn(a,b,c) x1 ,
t1_fn(c,a,b) x2 ,
rownum rn
from t1) t1, t2
where t1.a = t2.a and
t1.b = t2.b and
t1.d = t2.d
group by t1.a, t1.b, t2.c, t2.d ;
Now compare function call.
SQL> select count(*) from temp_t1 ;
COUNT(*)
---------200
44
2007-12-25
4. Tkprof.
45
2007-12-25
4. Tkprof.
2.
3.
4.
5.
6.
7.
8.
46
2007-12-25
47
2007-12-25