Performance Instrumentation for PL/SQL: When, Why, How

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    Course or presentation name © 2008 Method R Corp.

    Collect properly scoped , un-aggregated profile data for each task while the task is exhibiting the behavior you want to record Login Triggers Unique code changes

    Collect properly scoped , un-aggregated profile data for each task while the task is exhibiting the behavior you want to record Login Triggers Unique code changes

    … costs you money

    Your slow stuff will clog up even your fast stuff.

    “ Slow. Why?” Ok …What does “slow” mean?

    129.98 seconds Why?

    129.98 seconds (this is R data) Why? Where is the time spent? (we need profile data)

    Real-life performance instrumentation

    Real-life performance instrumentation

    Real-life performance instrumentation

    Test 1: I delivered the presentation to my cats. I used a stop watch to keep a running time My cats were thrilled to participate My test presentation was just short of 2 hours

    Emit a line-by-line execution of each step with a time stamp Available in any environment (e.g. development, production…) Always ready to be activated Easy to perform Light weight Uses built-in functionality as much as possible

    Enterprise Manager V$ views

    No idea of who are the clients Large amount of activity by unnamed modules and actions

    Know who the client is and be able to drilldown See specific module and action that are the “Top Consumers” Able to trace right from DBConsole and Grid Control

    Enterprise Manager V$ views

    Enterprise Manager V$ views

    Emit a line-by-line execution of each step with a time stamp Available in any environment (e.g. development, production…) Always ready to be activated Easy to perform Light weight Uses built-in functionality as much as possible

    Create a simple logon trigger to set CLIENT_IDENTIFIER CREATE OR REPLACE TRIGGER client_id_logon_trg AFTER LOGON ON DATABASE DECLARE my_service SYS.V_$SESSION.SERVICE_NAME%TYPE; my_clientid SYS.V_$SESSION.CLIENT_IDENTIFIER%TYPE; my_ip_address SYS.V_$SESSION.TERMINAL%TYPE; my_os_user SYS.V_$SESSION.OSUSER%TYPE; my_audsid SYS.V_$SESSION.AUDSID%TYPE; my_program SYS.V_$SESSION.PROGRAM%TYPE; CLIENT_ID_DELIM CHAR(1) := '~'; BEGIN IF USER NOT IN ('SYS') AND USER IS NOT NULL THEN my_clientid := SYS_CONTEXT('USERENV', 'CLIENT_IDENTIFIER'); IF my_clientid IS NULL THEN my_service := SYS_CONTEXT('USERENV', 'SERVICE_NAME'); my_ip_address := NVL(SYS_CONTEXT('USERENV', 'IP_ADDRESS') ,SYS_CONTEXT('USERENV', 'TERMINAL')); my_os_user := SYS_CONTEXT('USERENV', 'OS_USER'); my_audsid := TO_NUMBER(SYS_CONTEXT('USERENV', 'SESSIONID')); SELECT PROGRAM INTO my_program FROM SYS.V_$SESSION WHERE AUDSID = my_audsid AND ROWNUM = 1; DBMS_SESSION.SET_IDENTIFIER(my_os_user || CLIENT_ID_DELIM || my_ip_address || CLIENT_ID_DELIM || my_program || CLIENT_ID_DELIM || my_service); END IF; END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('client_id_logon_trg: Exception thrown'); END client_id_logon_trg;

    select column_name, table_name from dba_tab_cols where column_name like 'CLIENT_ID%' order by table_name COLUMN_NAME TABLE_NAME ------------------------------ ------------------------------ CLIENT_ID ALL_SCHEDULER_JOBS CLIENT_ID ALL_SCHEDULER_JOB_LOG CLIENT_ID ALL_SCHEDULER_WINDOW_LOG CLIENT_ID DBA_AUDIT_EXISTS CLIENT_ID DBA_AUDIT_OBJECT CLIENT_ID DBA_AUDIT_SESSION CLIENT_ID DBA_AUDIT_STATEMENT CLIENT_ID DBA_AUDIT_TRAIL CLIENT_ID DBA_COMMON_AUDIT_TRAIL CLIENT_ID DBA_FGA_AUDIT_TRAIL CLIENT_ID DBA_HIST_ACTIVE_SESS_HISTORY CLIENT_ID DBA_SCHEDULER_JOBS CLIENT_ID DBA_SCHEDULER_JOB_LOG CLIENT_ID DBA_SCHEDULER_WINDOW_LOG CLIENT_IDENTIFIER GLOBAL_CONTEXT CLIENT_ID GV_$ACTIVE_SESSION_HISTORY CLIENT_IDENTIFIER GV_$CLIENT_STATS CLIENT_IDENTIFIER GV_$GLOBALCONTEXT CLIENT_IDENTIFIER GV_$SESSION CLIENT_ID KET$_CLIENT_CONFIG CLIENT_ID KET$_CLIENT_TASKS CLIENT_IDENTIFIER MGMT_USER_CONTEXT CLIENT_ID SCHEDULER$_EVENT_LOG CLIENT_ID SCHEDULER$_JOB CLIENT_ID SCHEDULER$_LIGHTWEIGHT_JOB CLIENT_ID USER_AUDIT_OBJECT CLIENT_ID USER_AUDIT_SESSION CLIENT_ID USER_AUDIT_STATEMENT CLIENT_ID USER_AUDIT_TRAIL CLIENT_ID USER_SCHEDULER_JOBS CLIENT_ID USER_SCHEDULER_JOB_LOG CLIENT_ID V_$ACTIVE_SESSION_HISTORY CLIENT_IDENTIFIER V_$CLIENT_STATS CLIENT_IDENTIFIER V_$GLOBALCONTEXT CLIENT_IDENTIFIER V_$SESSION CLIENT_ID WRH$_ACTIVE_SESSION_HISTORY CLIENT_ID WRH$_ACTIVE_SESSION_HISTORY_BL 37 rows selected.

    Valid values of dest parameter are 1 = Trace file, 2 = Alert log, 3 = Both

    Monitor sessions by querying v$session

    Valid values of dest parameter are 1 = Trace file, 2 = Alert log, 3 = Both

    Monitor sessions by querying v$session

    http://sourceforge.net/projects/hotsos-ilo/

    Turning on trace is usually a run-time decision ( DBMS_MONITOR ) The developer will simply include BEGIN_TASK ILO calls that will check to see if someone has requested that a trace be initiated For development testing, the developer simply calls SET_MARK_ALL_TASKS_INTERESTING (TRUE, TRUE) to express their intent to trace Calls to this method will not typically be present in production code Except perhaps via a menu option (Help > Debug > Trace)

    2 Favorites

    Performance Instrumentation for PL/SQL: When, Why, How - Presentation Transcript

    1. Performance Instrumentation for PL/SQL When, Why, How Karen Morton
    2. Your speaker…
      • Karen Morton
        • Sr. Principal Database Engineer
        • Educator, DBA, developer, consultant, researcher, author, speaker, …
      • Come see me…
        • karenmorton.blogspot.com
        • An Oracle user group near you
      • Why guess?
      • When you can know .
      • Method R.
      • “ R” stands for
      • response time .
      • Target the
      • right task
      1
      • Collect its
      • R
      • Details
      2
      • Forecast, act
      3
      • Stop when cost of
      • additional repairs exceeds
      • cost of the problem
      • Go to Step 1
      4
      • The hardest part is
      • getting the “right”
      • information
      • Collecting properly scoped,
      • Un-aggregated profile data
      • If you can’t measure it, you can’t manage it.
      • ― David Garvin
      • There are two types of performance problems in this world...
      • 1
      • Response time problems.
      • 2
      • Inefficiencies that aren’t response time problems.
      • Yet.
      • Therefore...
      • You must be able to attack response time problems for specific tasks that the business cares about.
      • Example...
      • Posting hurts.
      • Fix it.
      • Why should you care?
    3.  
      • You need to be able to attack inefficiencies that aren’t yet noticeable as user response time problems.
      • Example...
      • Posting takes 3 hours.
      • It should take 2, but no user really cares.
      • Why should *you* care?
      • Because
      • costs you money
      waste
    4. TCO $$ Waste Labor $$ Hardware $$ Software $$
      • And waste...
      • ...makes other work go slower…
      • ...even your fast stuff
      • recap...
      • You must be able to attack
      • 1. Response time problems
      • 2. Efficiency problems
      • Here’s where
      • instrumentation
      • comes in
    5. Why is this program slow?
      • SQL> exec p
      • PL/SQL procedure successfully completed.
      • SQL>
    6. What is “slow”?
      • SQL> set timing on
      • SQL> exec p
      • PL/SQL procedure successfully completed.
      • Elapsed 00:02:09:98
      • SQL>
    7. So we open up our code.
      • create or replace procedure p
      • as
      • begin
      • q;
      • r;
      • s;
      • end;
      • /
    8. Let’s find out.
      • create or replace procedure p as
      • t0 number;
      • t1 number;
      • t2 number;
      • t3 number;
      • begin
      • t0 := 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;
      • /
    9. So now we know.
      • SQL> set timing on
      • SQL> exec p
      • Procedure q: 1
      • Procedure r: 114
      • Procedure s: 15
      • Total R : 130
      • Elapsed 00:02:09:99
      • SQL>
      • What was the co $ t?
      • Response time increased
      • by .01 seconds
      • (From 129.98 to 129.99)
      • Code additions
      • 4 variable declarations
      • 8 lines of code
      • These new lines
      • of code are called performance instrumentation .
      • With this data,
      • we know where
      • to focus our attention.
      • Where would you start
      • if you didn’t have this data?
      • What would happen if
      • you started work on
      • procedure Q or S ?
      Procedure q: 1 Procedure r: 114 Procedure s: 15 Total R : 130
      • Why guess?
      • When you can know .
      • Profiling is a
      • requirement of good
      • development hygiene
      • It makes your apps
      • faster and cheaper
      • 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
      • Performance
      • instrumentation
      • in real life
      • I just started a new job
      • and need to drive 15 miles from home to the office each day.
      • I decide to leave home at
      • 7:40am expecting to be
      • there by 8:00am.
    10. Day 1 Depart 7:40am Arrive 8:20am
      • Total Drive Time
      • 40 minutes
      • Argh!
      • It’s not good to be
      • late for your first
      • day on a new job!
      • What am I going to do?
      • First, let me find out how my drive time was spent.
    11. 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
      • Try another route?
      • Buy a faster car?
      • Hire private air transport?
    12. Conclusion Leave earlier to allow for traffic fluctuations
      • Why guess?
      • When you can know .
      • What is instrumentation
      • and why is it important?
      • Measures the
      • attributes of a system
    13. Creates monitoring mechanisms
    14. Tracks and measures performance
      • What are the objectives
      • of proper instrumentation?
      • Easy to
      • activate
      • Capture timings of
      • business-critical
      • tasks
      • Lightweight
      • Leverages
      • built-in functionality
    15. What are the benefits of proper instrumentation?
      • Appropriately
      • scoped trace
      • information
      • More meaningful
      • information
    16. This?
    17. Or this?
    18. This?
    19. Or this?
      • Why did this take so long?
      • What would happen if…?
      • Is this thing efficient?
      • Am I done yet?
      Easily answer questions like:
      • Provide final word
      • on disputes
    20. How do I instrument my code?
      • Start with
      • supplied packages
      • DBMS_SESSION
      • set_identifier
      • identifies current
      • session to make
      • tracing easier
    21. Lets instance know which end users are on which connection
      • DBMS_SESSION.set_identifier
      • (client_id =>'jqpublic~ora11r1')
      • 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')
      • SELECT client_identifier,…
      • FROM V$SESSION
      • … plus almost 40 other views
      • DBMS_SYSTEM
      • ksdddt
      • writes date/time stamp
      • ksdwrt
      • writes any message
      • DBMS_SYSTEM.ksdddt
        • *** 2008-10-02 15:36:51.314
      • DBMS_SYSTEM.ksdwrt
      • (dest=>1, ' Here I am! ' )
        • Here I am!
      • DBMS_APPLICATION_INFO
      • set_module
      • register business tasks
      • and related logical
      • units of work
      • read_module
      • determine current
      • business task and
      • logical unit of work
      • set_session_longops
      • monitor progress
      • of long-running
      • operations
      • DBMS_APPLICATION_INFO.
      • set_module(
      • module_name=> ' Order Entry ' ,
      • action_name=> ' Get Order Items ' )
      • SELECT module, action, …
      • FROM V$SESSION
      • – or –
      • FROM V$SESSION_LONGOPS
      • … plus almost 40 other views
      • DBMS_MONITOR
      • client_id_trace_enable
      • database_trace_enable
      • serv_mod_act_trace_enable
      • session_trace_enable
      • start extended
      • SQL tracing
      • DBMS_MONITOR.
      • serv_mod_act_trace_enable(
      • service_name=> ' ora11r1 ' ,
      • module_name=> ' Order Entry ' ,
      • action_name=> ' Get Order Items ' )
      • 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
      • SELECT *
      • FROM DBA_ENABLED_TRACES
          • TRACE_TYPE SERVICE_MODULE_ACTION
          • PRIMARY_ID ora11r1
          • QUALIFIER_ID1 Order Entry
          • QUALIFIER_ID2 Get Order Items
          • WAITS TRUE
          • BINDS TRUE
          • PLAN_STATS FIRST_EXEC
          • INSTANCE_NAME <null>
      • client_id_stat_enable
      • serv_mod_act_stat_enable
      • gather performance
      • metrics
      • DBMS_MONITOR.
      • client_id_stat_enable
      • (client_id =>'jqpublic~ora11r1')
      • CLIENT_IDENTIFIER STAT_NAME TIME_SECS
      • ----------------- --------------------------- ----------
      • jqpublic~ora11r1 DB CPU .009
      • jqpublic~ora11r1 DB time .009
      • jqpublic~ora11r1 sql execute elapsed time .007
      • jqpublic~ora11r1 parse time elapsed .001
      • . . .
      SELECT * FROM V$CLIENT_STATS
    22. SELECT * FROM V$SERV_MOD_ACT_STATS when using serv_mod_act_stat_enable
    23. Where should I place instrumentation?
      • Set module & action
      • below the
      • BEGIN statement
      • Update module & action
      • for any new
      • business tasks
      • Place above all
      • END and RETURN
      • statements
      • (set to null)
      • Place inside all
      • EXCEPTION
      • handlers
      • (set to null)
      • Avoid placing
      • calls inside
      • LOOPs
      • Example...
    24. 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;
    25. 08:00:00 MODULE = ‘Human resources’ ACTION= ‘get employees’ 08:02:04 MODULE = null ACTION = null
    26. 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); EXCEPTION WHEN 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;
    27. 08:10:00 MODULE = ‘Human resources’ ACTION = ‘get employees and jobs’ 08:12:04 MODULE = null ACTION = null 08:10:01 MODULE = ‘Human resources’ ACTION = ‘get employees’ 08:13:32 <end>
      • Problems can occur
      • when one similarly
      • instrumented procedure
      • calls another
      • Remember to
      • store module & action
      • in variables
      • and “reset” as needed
      • throughout code
    28. 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;
      • Instrumentation
      • Library for Oracle
      • ( ILO )
      • Used to simplify the
      • process of instrumenting
      • your code
      • 3 packages
      • ILO_TASK
      • ILO_TIMER
      • ILO_SYSUTIL
      • Requires
      • Oracle version
      • 9.2.0.1
      • or above
      • Makes
      • instrumenting
      • code
      • easy
      • Encapsulates & simplifies
      • the use of
      • dbms_application_info
      • and
      • dbms_session
      • Download from
      • SourceForge.net
      Open Source !
      • Can be modified to
      • meet your needs
      • Place 1 st call
      • below the
      • BEGIN statement
      • ILO_TASK.begin_task(
      • module_name=> ' Human Resources ' ,
      • action_name=> ' Get Employees ' )
      • Change for any new
      • business tasks
      • ILO_TASK.begin_task(
      • module_name=> ' Human Resources ' ,
      • action_name=> ' Get Jobs ' )
      • Place end calls
      • above all
      • END and RETURN
      • statements
      • and inside all
      • EXCEPTION
      • handlers
      • ILO_TASK.end_task
      • That’s
      • it!
    29. PROCEDURE get_emp_instr_better IS fnlist_stack fnlist_tab; lnlist_stack lnlist_tab; BEGIN ILO_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; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('get_emp_instr_better ERROR'); ILO_TASK.END_TASK; END get_emp_instr_better;
      • Turning on trace
      • using ILO
      • Activating trace is a
      • run-time decision
      • The developer adds
      • BEGIN_TASK & END_TASK
      • calls to the code
      • The DBA starts
      • tracing with
      • DBMS_MONITOR
      • The developer can start
      • tracing with ilo_task
      • SET_MARK_ALL_TASKS_INTERESTING
      • (TRUE, TRUE)
      • Case
      • study
      • The Problem
      • Business critical task
      • taking 40 seconds
      • MAX tolerated
      • execution time
      • 15 seconds
      • Weeks spent searching for root cause and fix
      • Task was a single
      • PL/SQL package
      • with 2,300 lines
      • of code
      • Code was executed
      • over 100 times
      • per hour
      • Running on
      • multi-node RAC
      • Trace collection not
      • very helpful due to
      • improper scoping
      • Needed instrumentation
      • to provide
      • proper scoping
      • Code contained
      • 36 business tasks
      • all marked with
      • in-line comments
      • In-line comments
      • used to guide
      • instrumentation
      • with ILO
      • Spent
      • 40 minutes
      • to instrument
      • 36 tasks
      • Instrumented
      • code placed
      • into production
      • Tracing activated
      • using dbms_monitor
      • for the instrumented
      • module & actions
      • Problem
      • code
      • was executed
      • Tracing
      • deactivated
      • Trace file
      • retrieved
      • and profiled
    30. Majority of time spent waiting for gc cr multi block request
    31. Further drilldown showed one insert and one delete to be largest “R” contributors
    32. Reviewed the insert statement which was actually inserting via a SELECT
    33. 1) Noted excessive LIO 2) Plan showed use of full table scan
      • The Fix
    34. Added index on ts_row_seq
      • Problem code was
      • executed and
      • traced again
    35. New response time 10.118 seconds
      • Total time to
      • find and fix
      • the problem:
      • less than 1 hour
      • Why guess?
      • When you can know .
      • Summary
      • When should you consider
      • performance
      • instrumentation?
      • Any time there is code
      • whose performance
      • you might ever
      • care about.
      • Why should
      • performance
      • instrumentation
      • matter?
      • Because when it
      • isn’t in place,
      • managing performance
      • is too complex
      • and expensive.
      • How should
      • performance
      • instrumentation
      • be done?
      • By adding a few
      • extra lines of code
      • to register
      • each business task.
      • Performance
      • doesn’t need
      • to be hard
      • Let your software
      • help you!
      • It’s a mindless task
      • your computer is willing
      • to do for you…
      • … if you just
      • ask it to.
      • Why guess?
      • When you can know .
      • Thank you!
    36. Questions & Answers
    37.  

    + Karen MortonKaren Morton, 2 years ago

    custom

    1796 views, 2 favs, 2 embeds more stats

    Performance instrumentation is a little extra code more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1796
      • 1716 on SlideShare
      • 80 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 153
    Most viewed embeds
    • 77 views on http://karenmorton.blogspot.com
    • 3 views on https://wiki.ordina.nl

    more

    All embeds
    • 77 views on http://karenmorton.blogspot.com
    • 3 views on https://wiki.ordina.nl

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories