Page 1
Performance Instrumentationfor PL/SQLWhen, Why, How
Karen Morton
Page 2
Your speaker…
Karen MortonSr. Principal Database EngineerEducator, DBA, developer, consultant, researcher, author, speaker, …
Come see me…karenmorton.blogspot.comAn Oracle user group near you
Page 3
Why guess?When you can know.
Page 4
Method R.
“R” stands for response time.
Page 5
Target theright task
11
Page 6
Collect itsR
Details
22
Page 8
Stop when cost of additional repairs exceeds
cost of the problem
Page 10
The hardest part isgetting the “right”
information
Page 11
Collecting properly scoped,Un-aggregated profile data
Page 12
If you can’t measure it, you can’t manage it.
―David Garvin
Page 13
There are two types of performance problems in this
world...
Page 14
1Response time
problems.
Page 15
2Inefficiencies that aren’t response time problems.
Yet.
Page 17
You must be able to attack response time problems for
specific tasks that the business cares about.
Page 19
Posting hurts.Fix it.
Page 20
Why should you care?
Page 22
You need to be able to attack inefficiencies that aren’t yet noticeable as user response
time problems.
Page 24
Posting takes 3 hours.
It should take 2,but no user really
cares.
Page 25
Why should *you* care?
Page 26
Because
costs you money
waste
Page 27
TCO $$
Waste
Labor $$
Hardware $$
Software $$
Page 29
...makesother work
go slower…
Page 30
...even your fast stuff
Page 32
You must be able to attack1. Response time problems2. Efficiency problems
Page 33
Here’s whereinstrumentation
comes in
Page 34
Why is this program slow?
SQL> exec p
PL/SQL procedure successfully completed.
SQL>
Page 35
What is “slow”?
SQL> set timing onSQL> exec p
PL/SQL procedure successfully completed.
Elapsed 00:02:09:98SQL>
Page 36
So we open up our code.
create or replace procedure p asbeginq;r;s;
end;/
Page 37
Let’s find out.
create or replace procedure p ast0 number;t1 number;t2 number;t3 number;
begint0 := dbms_utility.get_time;q;t1 := dbms_utility.get_time ;dbms_output.put_line ('Procedure q: ' || to_char((t1 - t0)/100));r;t2 := dbms_utility.get_time ;dbms_output.put_line ('Procedure r: ' || to_char((t2 - t1)/100));s;t3 := dbms_utility.get_time ;dbms_output.put_line ('Procedure s: ' || to_char((t3 - t2)/100));dbms_output.put_line ('Total R : ' || to_char((t3 - t0)/100));
end;/
Page 38
So now we know.
SQL> set timing onSQL> exec pProcedure q: 1Procedure r: 114Procedure s: 15Total R : 130
Elapsed 00:02:09:99SQL>
Page 39
What was the co$t?
Page 40
Response time increased by .01 seconds (From 129.98 to 129.99)
Page 41
Code additions
4 variable declarations8 lines of code
Page 42
These new lines of code are called
performance instrumentation.
Page 43
With this data,we know where
to focus our attention.
Page 44
Where would you startif you didn’t have this data?
Page 45
What would happen ifyou started work on
procedure Q or S?
Procedure q: 1Procedure r: 114Procedure s: 15Total R : 130
Page 46
Why guess?
When you can know.
Page 47
Profiling is arequirement of good
development hygiene
Page 48
It makes your appsfaster and cheaper
Page 49
Without profiling…
Can’t tell where time goes “Tune” everything you
can Learn about everything
possible “Tuning” continues even
when improvement is impossible
With profiling…
Know where code spends time
Optimize only relevant code path
Learn first in high-impact areas
“Tuning” ceases when objective is achieved
Page 50
Performanceinstrumentation
in real life
Page 51
I just started a new joband need to drive 15 miles from
home to the office each day.
Page 52
I decide to leave home at7:40am expecting to be
there by 8:00am.
Page 53
Day 1
Depart7:40am
Arrive8:20am
Page 54
Total Drive Time
40 minutes
Page 55
Argh!It’s not good to belate for your first
day on a new job!
Page 56
What am I going to do?
Page 57
First, let me find out how
my drive time was spent.
Page 58
Category Miles Elapsed Time (minutes)Neighborhood 1 3
Burke Centre Pkwy 2.5 8
Fairfax County Pkwy 6 12
I-66 2.5 8
Hwy 28 2 5
Westfields Blvd to Office 1.5 4
Total 15.5 40 minutes
Page 59
Try another route?
Page 60
Buy a faster car?
Page 61
Hire private air transport?
Page 62
Conclusion
Leave earlier to allowfor traffic fluctuations
Page 63
Why guess?
When you can know.
Page 64
What is instrumentationand why is it important?
Page 65
Measures the attributes of a system
Page 66
Creates monitoringmechanisms
Page 67
Tracks and measuresperformance
Page 68
What are the objectivesof proper instrumentation?
Page 70
Capture timings ofbusiness-critical
tasks
Page 72
Leveragesbuilt-in functionality
Page 73
What are the benefitsof proper instrumentation?
Page 74
Appropriatelyscoped traceinformation
Page 75
More meaningfulinformation
Page 80
Why did this take so long?What would happen if…?
Is this thing efficient?Am I done yet?
Easily answer questions like:
Page 81
Provide final wordon disputes
DB
As
Dev
elop
ers
SAN
Adm
ins
Net
Adm
ins
OS
Adm
ins
Man
ager
s
Use
rs
Page 82
How do Iinstrumentmy code?
Page 83
Start withsupplied packages
Page 85
set_identifier
identifies currentsession to maketracing easier
Page 86
Lets instance know which end users
are on which connection
Page 87
DBMS_SESSION.set_identifier(client_id =>'jqpublic~ora11r1')
Page 88
LOGON Trigger
SYS_CONTEXT('USERENV', 'CLIENT_IDENTIFIER')SYS_CONTEXT('USERENV', 'SERVICE_NAME')SYS_CONTEXT('USERENV', 'IP_ADDRESS')SYS_CONTEXT('USERENV', 'TERMINAL')SYS_CONTEXT('USERENV', 'OS_USER')SYS_CONTEXT('USERENV', 'SESSIONID')
Page 89
SELECT client_identifier,… FROM V$SESSION
…plus almost 40 other views
Page 90
DBMS_APPLICATION_INFO
Page 91
set_module
register business tasksand related logical
units of work
Page 92
read_module
determine currentbusiness task and
logical unit of work
Page 93
set_session_longops
monitor progressof long-running
operations
Page 94
DBMS_APPLICATION_INFO.set_module(module_name=>'Order Entry',action_name=>'Get Order Items')
Page 95
SELECT module, action, … FROM V$SESSION– or – FROM V$SESSION_LONGOPS
…plus almost 40 other views
Page 97
client_id_trace_enabledatabase_trace_enable
serv_mod_act_trace_enablesession_trace_enable
start extendedSQL tracing
Page 98
DBMS_MONITOR.serv_mod_act_trace_enable(service_name=>'ora11r1',module_name=>'Order Entry',action_name=>'Get Order Items')
Page 99
Trace file will contain
*** ACTION NAME:(Get Order Items) 2009-08-02 01:48:56.573
*** MODULE NAME:(Order Entry) 2009-08-02 01:48:56.573
Page 100
SELECT * FROM DBA_ENABLED_TRACES
TRACE_TYPE SERVICE_MODULE_ACTIONPRIMARY_ID ora11r1QUALIFIER_ID1 Order EntryQUALIFIER_ID2 Get Order ItemsWAITS TRUEBINDS TRUEPLAN_STATS FIRST_EXECINSTANCE_NAME <null>
Page 101
client_id_stat_enableserv_mod_act_stat_enable
gather performancemetrics
Page 102
DBMS_MONITOR.client_id_stat_enable(client_id =>'jqpublic~ora11r1')
Page 103
CLIENT_IDENTIFIER STAT_NAME TIME_SECS----------------- --------------------------- ----------jqpublic~ora11r1 DB CPU .009jqpublic~ora11r1 DB time .009jqpublic~ora11r1 sql execute elapsed time .007jqpublic~ora11r1 parse time elapsed .001. . .
SELECT * FROM V$CLIENT_STATS
Page 104
SELECT * FROM V$SERV_MOD_ACT_STATS
when usingserv_mod_act_stat_enable
Page 105
Where should Iplace instrumentation?
Page 106
Set module & actionbelow the
BEGIN statement
Page 107
Update module & actionfor any new
business tasks
Page 108
Place above allEND and RETURN
statements(set to null)
Page 109
Place inside allEXCEPTION
handlers(set to null)
Page 110
Avoid placingcalls inside
LOOPs
Page 112
PROCEDURE get_emp_simple_instr IS fnlist_stack fnlist_tab; lnlist_stack lnlist_tab; BEGIN DBMS_APPLICATION_INFO.set_module(module_name => 'Human
Resources’,action_name => 'Get Employees'); SELECT first_name, last_name BULK COLLECT INTO fnlist_stack, lnlist_stack FROM employees; DBMS_APPLICATION_INFO.set_module(NULL, NULL);
EXCEPTION WHEN OTHERS THEN DBMS_APPLICATION_INFO.set_module(NULL, NULL); DBMS_OUTPUT.PUT_LINE('get_emp_simple_instr => ERROR');
END get_emp_simple_instr;
Page 113
08:00:00 08:05:00
08:00:00 08:02:04get_emp_simple_instr
08:00:00MODULE = ‘Human resources’ACTION= ‘get employees’
08:02:04MODULE = nullACTION = null
Page 114
PROCEDURE get_emp_jobs_instr_flawed IS jtlist_stack jtlist_tab; lnlist_stack lnlist_tab;
BEGIN DBMS_APPLICATION_INFO.set_module(module_name => 'Human
Resources', action_name => 'Get Employees and Jobs'); get_emp_simple_instr; SELECT last_name, job_title BULK COLLECT INTO lnlist_stack, jtlist_stack FROM employees e, jobs j WHERE e.job_id = j.job_id; DBMS_APPLICATION_INFO.set_module(NULL, NULL);
EXCEPTIONWHEN OTHERS THEN DBMS_APPLICATION_INFO.set_module(NULL, NULL); DBMS_OUTPUT.PUT_LINE('get_emp_jobs_instr_flawed => ERROR');
END get_emp_jobs_instr_bad;
Page 115
08:10:00MODULE = ‘Human resources’ACTION = ‘get employees and jobs’
08:12:04MODULE = nullACTION = null
08:10:00 08:15:00
get_emp_jobs_instr_flawed
08:10:00 08:12:0408:12:04 08:13:32get_emp_simple_instr
08:12:04Module/Action set to NULL
08:10:01MODULE = ‘Human resources’ACTION = ‘get employees’
08:13:32<end>
Page 116
Problems can occurwhen one similarly
instrumented procedurecalls another
Page 117
Remember to store module & action
in variablesand “reset” as needed
throughout code
Page 118
PROCEDURE get_emp_instr_good IS fnlist_stack fnlist_tab; lnlist_stack lnlist_tab; preModuleName VARCHAR2(48) := NULL; preActionName VARCHAR2(32) := NULL;
BEGIN DBMS_APPLICATION_INFO.read_module(
module_name => preModuleName, action_name => preActionName); DBMS_APPLICATION_INFO.set_module(
module_name => 'Human Resources',action_name => 'Get Employees');
SELECT first_name, last_name BULK COLLECT INTO fnlist_stack, lnlist_stack FROM employees; DBMS_APPLICATION_INFO.set_module(
module_name => preModuleName, action_name => preActionName);EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('HR_Package.get_emp_instr_good ERROR'); DBMS_APPLICATION_INFO.set_module(
module_name => preModuleName, action_name => preActionName);END get_emp_instr_good;
Page 119
InstrumentationLibrary for Oracle
( ILO )
Page 120
Used to simplify theprocess of instrumenting
your code
Page 121
3 packagesILO_TASK
ILO_TIMERILO_SYSUTIL
Page 122
RequiresOracle version
9.2.0.1or above
Page 123
Makesinstrumenting
codeeasy
Page 124
Encapsulates & simplifiesthe use of
dbms_application_infoand
dbms_session
Page 125
Download from
SourceForge.net
Open Open Source!!
Page 126
Can be modified tomeet your needs
Page 127
Place 1st callbelow the
BEGIN statement
Page 128
ILO_TASK.begin_task(module_name=>'Human Resources',action_name=>'Get Employees')
Page 129
Change for any newbusiness tasks
Page 130
ILO_TASK.begin_task(module_name=>'Human Resources',action_name=>'Get Jobs')
Page 131
Place end callsabove all
END and RETURNstatements
Page 132
and inside allEXCEPTION
handlers
Page 133
ILO_TASK.end_task
Page 135
PROCEDURE get_emp_instr_better ISfnlist_stack fnlist_tab;lnlist_stack lnlist_tab;
BEGINILO_TASK.BEGIN_TASK(module_name => 'Human Resources' ,action_name => 'Get Employees'); SELECT first_name, last_name BULK COLLECT INTO fnlist_stack, lnlist_stack FROM employees; ILO_TASK.END_TASK;
EXCEPTIONWHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('get_emp_instr_better ERROR');ILO_TASK.END_TASK;
END get_emp_instr_better;
Page 136
Turning on traceusing ILO
Page 137
Activating trace is arun-time decision
Page 138
The developer addsBEGIN_TASK & END_TASK
calls to the code
Page 139
The DBA startstracing with
DBMS_MONITOR
Page 140
The developer can starttracing with ilo_task
SET_MARK_ALL_TASKS_INTERESTING(TRUE, TRUE)
Page 143
Business critical tasktaking 40 seconds
Page 144
MAX toleratedexecution time
15 seconds
Page 145
Weeks spent searching for root cause and fix
Page 146
Task was a singlePL/SQL packagewith 2,300 lines
of code
Page 147
Code was executedover 100 times
per hour
Page 148
Running onmulti-node RAC
Page 149
Trace collection notvery helpful due toimproper scoping
Page 150
Needed instrumentationto provide
proper scoping
Page 151
Code contained36 business tasks
all marked within-line comments
Page 152
In-line commentsused to guide
instrumentation with ILO
Page 153
Spent40 minutes
to instrument36 tasks
Page 154
Instrumentedcode placed
into production
Page 155
Tracing activatedusing dbms_monitorfor the instrumented
module & actions
Page 156
Problemcode
was executed
Page 157
Tracingdeactivated
Page 158
Trace fileretrieved
and profiled
Page 159
Majority of time spent waiting forgc cr multi block request
Page 160
Further drilldown showed one insert and one delete
to be largest “R” contributors
Page 161
Reviewed the insert statementwhich was actually inserting
via a SELECT
Page 162
1) Noted excessive LIO2) Plan showed use of full table scan
Page 164
Added index onts_row_seq
Page 165
Problem code wasexecuted andtraced again
Page 166
New response time10.118 seconds
Page 167
Total time tofind and fix
the problem:
less than 1 hour
Page 168
Why guess?
When you can know.
Page 170
When should you consider performance
instrumentation?
Page 171
Any time there is codewhose performance
you might evercare about.
Page 172
Why shouldperformance
instrumentationmatter?
Page 173
Because when it isn’t in place,
managing performanceis too complexand expensive.
Page 174
How shouldperformance
instrumentationbe done?
Page 175
By adding a fewextra lines of code
to registereach business task.
Page 176
Performancedoesn’t need
to be hard
Page 177
Let your softwarehelp you!
Page 178
It’s a mindless taskyour computer is willing
to do for you…
Page 179
…if you justask it to.
Page 180
Why guess?
When you can know.
Page 182
Questions & Answers