oracle database 11g execution plans and star transformations

Upload: yelena-bytenskaya

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    1/77

    | Print | Contents | Close |

    Gathering Execution PlansLearning Objectives

    After completing this topic, you should be able to

    recognize how to gather execution plans recognize how to display execution plans

    1. Understanding execution plans

    An execution plan is the output of the optimizer and is presented to the execution enginefor implementation. It instructs the execution engine about the operations it must performfor retrieving the data required by a query most efficiently.

    Supple ent

    Selecting the link title opens the resource in a new browser window.

    Learning !id

    Use the learning aid St"le considerations for more information on the styleconsiderations for Oracle 11 g Database used in this course.

    he execution plan of a !"# statement is composed of small building bloc$s called rowsources for serial execution plans. he combination of ro% sources for a statement is alsocalled the execution plan. &y using parent'child relationships( the execution plan can bedisplayed in a tree'li$e structure )text or graphical*.

    he EXPLAIN PLAN statement gathers execution plans chosen by the Oracle optimizerfor the SELECT ( UPDATE( INSERT ( and DELETE statements. he steps of the execution

    plan are not performed in the order in %hich they are numbered. here is a parent'childrelationship bet%een steps.

    he ro% source tree is the core of the execution plan. It sho%s the follo%ing information+

    an ordering of the tables referenced by the statement

    an access method for each table mentioned in the statement

    http://window.print%28%29/http://window.print%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#contentshttp://parent.window.close%28%29/http://parent.window.close%28%29/http://dowindow%28%27../html/laod_ortw_a03_dt_enus_t301_frame.html')http://dowindow%28%27../html/laod_ortw_a03_dt_enus_t301_frame.html')http://dowindow%28%27../html/laod_ortw_a03_dt_enus_t301_frame.html')http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#contentshttp://parent.window.close%28%29/http://dowindow%28%27../html/laod_ortw_a03_dt_enus_t301_frame.html')http://window.print%28%29/
  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    2/77

    a ,oin method for tables affected by ,oin operations in the statement( and

    data operations( such as filter( sort( or aggregation

    In addition to the ro% source tree )or data flo% tree for parallel operations*( the plan table

    contains information about the follo%ing+

    optimization( such as the cost and cardinality of each operation

    partitioning( such as the set of accessed partitions( and

    parallel execution( such as the distribution method of ,oin inputs

    he EXPLAIN PLAN results help you determine %hether the optimizer selects a particularexecution plan( such as nested loops ,oin.

    here are many %ays to retrieve execution plans inside the database. he follo%ing arethe most %ell'$no%n ones+

    EXPLAIN PLAN

    he EXPLAIN PLAN command enables you to vie% the execution plan that the optimizermight use to execute a !"# statement. his command is very useful because it outlinesthe plan that the optimizer may use and inserts it in a table called PLAN_TABLE %ithoutexecuting the !"# statement.

    V$SQL_PLAN

    V$SQL_PLAN provides a %ay to examine the execution plan for cursors that %ere recentlyexecuted. Information in V$SQL_PLAN is very similar to the output of an EXPLAIN PLAN statement. -o%ever( %hile EXPLAIN PLAN sho%s a theoretical plan that can be used if thisstatement %as executed( V$SQL_PLAN contains the actual plan used.

    V$SQL_PLAN_MONITOR # and

    V$SQL_PLAN_MONITOR displays plan'level monitoring statistics for each !"# statementfound in V$SQL_MONITOR. ach ro% in V$SQL_PLAN_MONITOR corresponds to anoperation of the execution plan that is monitored.

    DBA_HIST_SQL_PLAN and STATS$SQL_PLAN

    he Automatic /or$load 0epository( or A/0( infrastructure and statspac$ reports store

    plans from top !"#s. lans are recorded into DBA_HIST_SQL_PLAN andSTATS$SQL_PLAN .

    Other %ays to retrieve execution plans inside the database include the follo%ing+

    lan and ro% source operations are dumped in trace files generated by DBMS_MONITOR.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    3/77

    In the !"# 2anagement &ase or !2&( %hich is a part of the data dictionary that resides in theSYSAUX tablespace. It stores statement log( plan histories( and !"# plan baselines( as profiles.

    he event 13345 trace file( %hich is used to dump 6ost'&ased Optimizer( or 6&O computations(may include a plan.

    !tarting %ith Oracle Database 13 g 0elease 7( %hen you dump process state )or errorstac$ from aprocess*( execution plans are included in the trace file that is generated.

    $uestion

    /hich method of retrieving execution plans from the database enables you torevie% the execution plan that the optimizer might use to execute a !"#statement8

    Options%

    1. A/0

    7. EXPLAIN PLAN command

    5. !2&

    9. V$SQL_PLAN_MONITOR

    !ns&er

    Option 1: Incorrect. The Automatic Workload Repository, or AWR, infrastructureand statspack reports store plans from top SQ s. !lans are recorded into

    DBA_HIST_SQL_PLAN and STATS$SQL_PLAN .

    Option 2: "orrect. The EXPLAIN PLAN command enables you to #iew thee$ecution plan that the optimi%er might use to e$ecute a SQ statement. Thiscommand is #ery useful because it outlines the plan that the optimi%er may useand inserts it in a table called PLAN_TABLE without e$ecuting the SQ statement.

    Option 3: Incorrect. The SQ &anagement 'ase, or S&', is a part of the datadictionary that resides in the SYSAUX tablespace. It stores statement log, planhistories, and SQ plan baselines, as well as SQ profiles.

    Option 4: Incorrect. V$SQL_PLAN_MONITOR displays plan(le#el monitoringstatistics for each SQ statement found in V$SQL_MONITOR. )ach row inV$SQL_PLAN_MONITOR corresponds to an operation of the e$ecution plan that ismonitored.

    Correct ans&er's(%

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    4/77

    7. EXPLAIN PLAN command

    ). Gathering execution plans

    :ou can vie% execution plans by using the EXPLAIN PLAN command and then usingeither the SELECT ...FROM PLAN_TABLE statement or theDBMS_XPLAN.DISPLAY() #;!"# pac$age.

    :ou can also use the !"#< lus Auto race feature to vie% execution plans. o do this( youuse the SET AUTOTRACE ON command.

    he DBMS_XPLAN pac$age supplies five table functions for vie%ing execution plans+

    DISPLAY

    DISPLAY is used to format and to display the contents of a plan table.

    DISPLAY_AWR

    :ou use DISPLAY_AWR to format and display the contents of the execution plan of astored !"# statement in the A/0.

    DISPLAY_CURSOR

    DISPLAY_CURSOR can be used to format and display the contents of the execution plan of any loaded cursor.

    DISPLAY_SQL_PLAN_BASELINE# and

    DISPLAY_SQL_PLAN_BASELINE is used to display one or more execution plans for the!"# statement identified by !"# handle.

    DISPLAY_SQLSET

    :ou can use DISPLAY_SQLSET to format and display the contents of the execution plan of statements stored in a !"# tuning set.

    he EXPLAIN PLAN command is used to generate the execution plan that the optimizeruses to execute a !"# statement. It does not execute the statement( but simply producesthe plan that may be used( and inserts this plan into a table. If you examine the plan( youcan see ho% the Oracle !erver executes the statement.

    Use the EXPLAIN PLAN command first to explain a !"# statement and then retrieve theplan steps by querying PLAN_TABLE .

    PLAN_TABLE is automatically created as a global temporary table to hold the output of anEXPLAIN PLAN statement for all users. PLAN_TABLE is the default sample output tableinto %hich the EXPLAIN PLAN statement inserts ro%s describing execution plans.

    *ote

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    5/77

    *ou can create your own PLAN_TABLE using the+ RA" )- &)/rdbms/admin/utl$plan.s0l script if you want to keep thee$ecution plan information for a long term.

    he EXPLAIN PLAN command inserts a ro% in the plan table for each step of the

    execution plan. he fields in the syntax have the follo%ing meanings+

    text

    :ou can specify an optional identifier for the statement in = text =. :ou should enter a value toidentify each statement so that you can later specify the statement that you %antexplained. his is especially important %hen you share the plan table %ith others or %henyou $eep multiple execution plans in the same plan table.

    your plan table

    he default name for the output table is PLAN_TABLE . he optional name issch !".#" % .

    statement

    he text of the !"# statement is identified in statement .

    In the code example( the EXPLAIN PLAN command inserts the execution plan for the!"# statement in the plan table and adds the optional & !' name tag for futurereference.

    Code

    SQL* EXPLAIN PLAN + SET STATEMENT_ID , -& !' - FOR SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"! 3 FROM h2. !1%'4 s 0 h2.& 1"2#! /#s & 5 WHERE .& 1"2#! /#_6& , &.& 1"2#! /#_6&7E81%"6/ &.SQL*

    :ou can also use this code to use the EXPLAIN PLAN command.

    Code

    EXPLAIN PLANFORSELECT .%"s#_/"! 0 &.& 1"2#! /#_/"! FROM h2. !1%'4 s 0 h2.& 1"2#! /#s &WHERE .& 1"2#! /#_6& ,&.& 1"2#! /#_6&7

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    6/77

    here are various methods available to gather execution plans. :ou are introduced onlyto the EXPLAIN PLAN statement. his !"# statement gathers the execution plan of a!"# statement %ithout executing it( and outputs its result in the PLAN_TABLE table./hatever the method to gather and display the explain plan( the basic format and goalare the same.

    -o%ever( PLAN_TABLE ,ust sho%s you a plan that might not be the one chosen by theoptimizer. PLAN_TABLE is automatically created as a global temporary table and is visibleto all users. PLAN_TABLE is the default sample output table into %hich the EXPLAIN PLAN statement inserts ro%s describing execution plans.

    PLAN_TABLE is organized in a tree'li$e structure and you can retrieve that structure byusing both the ID and PARENT_ID columns %ith a CONNECT BY clause in a SELECT statement. /hile a PLAN_TABLE table is automatically set up for each user( you can usethe utlxplan.sql !"# script to manually create a local PLAN_TABLE in your schema anduse it to store the results of EXPLAIN PLAN.

    he exact name and location of the utlxplan.sql script depends on your operating system.On U>I?( it is located in the "&!6/ directory. It is recommended that you drop andrebuild your local PLAN_TABLE table after upgrading the version of the database becausethe columns might change. his can cause scripts to fail or cause T9PROF to fail( if youare specifying the table.

    Graphic

    The admin directory is located at the path + RA" )- &)/rdbms/admin.

    *ote

    If you want an output table with a different name, first create PLAN_TABLE manually with the utl$plan.s0l script, and then rename the table with the RENAME SQ statement.

    +. ,ispla"ing execution plans

    In this example( the EXPLAIN PLAN command inserts the execution plan of the !"#statement in PLAN_TABLE and adds the optional & !' name tag for future reference.It is important to note that the SELECT statement itself is not executed( but is simplyoptimized to determine the best execution plan in that context. he DISPLAY function ofthe DBMS_XPLAN pac$age can be used to format and display the last statement stored inPLAN_TABLE .

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    7/77

    SQL* EXPLAIN PLAN SET STATEMENT_ID , -& !' - FOR SELECT :FROM !1 + WHERE /"! , -9IN;-7

    E81%"6/ &.

    SQL* SET LINESI ? @5 @ +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( ENAME ,-9IN;-)

    :ou can also use this code to retrieve the same results from the PLAN_TABLE .

    Code

    SQL* EXPLAIN PLAN SET STATEMENT_ID , -& !' - FOR SELECT :

    FROM !1 + WHERE /"! , -9IN;-7

    E81%"6/ &.

    SQL* SET LINESI

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    8/77

    G6%# 2( ENAME ,-9IN;-)

    In the example( you can substitute the name of another plan table instead ofPLAN_TABLE and & !' represents the statement ID . TYPICAL displays the most

    relevant information in the plan+ operation ID( name and option( number of ro%s( bytes(and optimizer cost.

    he last parameter for the DISPLAY function is the one corresponding toG6%# 2_12 &s . his parameter represents a filter predicate or predicates to restrict theset of ro%s selected from the table %here the plan is stored. /hen value is null )thedefault*( the plan displayed corresponds to the last executed explain plan. his parameter can reference any column of the table %here the plan is stored and can contain any !"#construct( for example( subquery or function calls.

    Code

    SQL* EXPLAIN PLAN SET STATEMENT_ID , -& !' - FOR SELECT :FROM !1 + WHERE /"! , -9IN;-7

    E81%"6/ &.

    SQL* SET LINESI

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    9/77

    PLAN_TABLE . This script uses the DISPLAY table function from the DBMS_XPLAN package.

    he ALL option used %ith the DISPLAY function in this example allo%s you to output themaximum user level information. It includes information displayed %ith the TYPICAL

    level( %ith additional information such as PRO ECTION ( ALIAS ( and information aboutREMOTE !"#( if the operation is distributed.

    Code

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY(/>%%0/>%%0-ALL-))7P%"/ h"sh ="%> ? @5 @ +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

    Q> 24 B%'cJ N"! K O c# A%6"s (6& /#6G6 & 4 '1 2"#6'/6&)?

    SEL$ K EMP SEL$P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( ENAME ,-9IN;-)

    C'%>!/ P2' c#6'/ I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    EMP . EMPNO NUMBER0++ 0 ENAME VARCHAR+0 0EMP . OB VARCHAR+0@ 0 EMP . M;R NUMBER0++ 0EMP . HIREDATE DATE0 0 EMP . SAL NUMBER0++ 0EMP . COMM NUMBER0++ 0 EMP . DEPTNO NUMBER0++

    @or finer control on the display output( $ey%ords can be added to the format parameter tocustomize its default behavior. ach $ey%ord either represents a logical group of plantable columns )such as PARTITION * or logical additions to the base plan table output)such as PREDICATE *. @ormat $ey%ords must be separated by either a comma or a

    space.

    he $ey%ords include

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    10/77

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY(/>%%0/>%%0-ALL-))7P%"/ h"sh ="%> ? @5 @ +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

    Q> 24 B%'cJ N"! K O c# A%6"s (6& /#6G6 & 4 '1 2"#6'/6&)?

    SEL$ K EMP SEL$P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( ENAME ,-9IN;-)C'%>!/ P2' c#6'/ I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    EMP . EMPNO NUMBER0++ 0 ENAME VARCHAR+0 0EMP . OB VARCHAR+0@ 0 EMP . M;R NUMBER0++ 0EMP . HIREDATE DATE0 0 EMP . SAL NUMBER0++ 0EMP . COMM NUMBER0++ 0 EMP . DEPTNO NUMBER0++

    ROWS

    If relevant( the ROWS $ey%ord sho%s the number of ro%s estimated by the optimizer.

    BYTES

    he BYTES $ey%ord sho%s the number of bytes estimated by the optimizer( if relevant.

    COST

    he COST $ey%ord sho%s optimizer cost information( if relevant.

    PARTITION # and

    If relevant( the PARTITION $ey%ord sho%s partition pruning information.

    PARALLEL

    he PARALLEL $ey%ord sho%s ? information )distribution method and table queueinformation*( if relevant.

    Other format $ey%ords include the follo%ing+

    Code

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY(/>%%0/>%%0-ALL-))7P%"/ h"sh ="%> ? @5 @ +

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    11/77

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

    Q> 24 B%'cJ N"! K O c# A%6"s (6& /#6G6 & 4 '1 2"#6'/6&)?

    SEL$ K EMP SEL$P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( ENAME ,-9IN;-)C'%>!/ P2' c#6'/ I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    EMP . EMPNO NUMBER0++ 0 ENAME VARCHAR+0 0EMP . OB VARCHAR+0@ 0 EMP . M;R NUMBER0++ 0EMP . HIREDATE DATE0 0 EMP . SAL NUMBER0++ 0EMP . COMM NUMBER0++ 0 EMP . DEPTNO NUMBER0++

    PREDICATE

    If relevant( the PREDICATE $ey%ord sho%s the predicate section.

    PROJECTION

    he PRO ECTION $ey%ord sho%s the pro,ection section( if relevant.

    ALIAS

    If relevant( the ALIAS $ey%ord sho%s the "uery &loc$ >ame;Ob,ect Alias section.

    REMOTE# and

    If relevant( the REMOTE $ey%ord sho%s the information for the distributed query )forexample( remote from serial distribution and remote !"#*.

    NOTE

    he NOTE $ey%ord sho%s the note section of the explain plan( if relevant.

    If the target plan table also stores plan statistics columns )for example( it is a table usedto capture the content of the fixed vie% V$SQL_PLAN_STATISTICS_ALL *( additional

    format $ey%ords can be used to specify %hich class of statistics to display %hen using theDISPLAY function. hese additional format $ey%ords are IOSTATS ( MEMSTATS(ALLSTATS ( and LAST.

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    12/77

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY(/>%%0/>%%0-ALL-))7P%"/ h"sh ="%> ? @5 @ +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

    Q> 24 B%'cJ N"! K O c# A%6"s (6& /#6G6 & 4 '1 2"#6'/6&)?

    SEL$ K EMP SEL$P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( ENAME ,-9IN;-)C'%>!/ P2' c#6'/ I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    EMP . EMPNO NUMBER0++ 0 ENAME VARCHAR+0 0EMP . OB VARCHAR+0@ 0 EMP . M;R NUMBER0++ 0EMP . HIREDATE DATE0 0 EMP . SAL NUMBER0++ 0EMP . COMM NUMBER0++ 0 EMP . DEPTNO NUMBER0++

    *ote

    3ormat keywords can be prefi$ed with the 4(4 sign to e$clude the specified

    information. 3or e$ample, 4 PRO ECTION 4 e$cludes pro5ection information.

    he ADVANCED format is available only from Oracle Database 13 g ( 0elease 7 and laterversions. his output format includes all sections from the ALL format plus the outlinedata that represents a set of hints to reproduce that particular plan. his section may beuseful if you %ant to reproduce a particular execution plan in a different environment. hisis the same section( %hich is displayed in the trace file for event 13345.

    Code

    SELECT 1%"/_#" % _'>#1># FROM#" % (DBMS_XPLAN.DISPLAY(/>%%0/>%%0-ADVANCED PRO ECTION PREDICATE ALIAS-))7P%"/ h"sh ="%> ? @5 @ +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU) T6!

    SELECT STATEMENT ( ) ? ?: TABLE ACCESS FULL EMP ( ) ? ?

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    13/77

    O>#%6/ D"#"

    K: BE;IN_OUTLINE_DATA FULL( SEL$ EMP SEL$ ) OUTLINE_LEAF( SEL$ ) ALL_ROWS DB_VERSION(- . . . -) OPTIMI

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    14/77

    *ote

    The system does not support EXPLAIN PLAN for statements performing implicittype con#ersion of date bind #ariables. With bind #ariables in general, theEXPLAIN PLAN output might not represent the real e$ecution plan.

    :ou can use this syntax to enable AUTOTRACE in various %ays. he options are asfollo%s+

    OFF

    :ou can use the OFF option to disable autotracing !"# statements.

    ON

    :ou use the ON option to enable autotracing !"# statements.

    TRACE or TRACE[ONLY]

    he TRACE or TRACE ONLY option enable autotracing !"# statements and suppressesstatement output.

    EXPLAIN# and

    o display execution plans( but not the statistics( you use the EXPLAIN option.

    STATISTICS

    he STATISTICS option displays statistics( but does not display execution plans.

    If both the EXPLAIN and STATISTICS command options are omitted( execution plansand statistics are displayed by default.

    :ou can control the report by setting the AUTOTRACE system variable. he follo%ing aresome examples+

    SET AUTOTRACE ON

    his AUTOTRACE report includes both the optimizer execution plan and the !"# statementexecution statistics.

    SET AUTOTRACE TRACEONLY EXPLAIN

    his AUTOTRACE report sho%s only theoptimizer execution path %ithout executing the statement.

    SET AUTOTRACE ON STATISTICS

    his AUTOTRACE report sho%s the !"# statement execution statistics and ro%s.

    SET AUTOTRACE TRACEONLY# and

    his is similar to SET AUTOTRACE ON( but it suppresses the printing of the user=s queryoutput( if any. If STATISTICS is enabled( the query data is still fetched( but not printed.

    SET AUTOTRACE OFF

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    15/77

    >o AUTOTRACE report is generated. his is the default.

    $uestion

    /hich AUTOTRACE example should you use to display ro%s and statistics8

    Options%

    1. SET AUTOTRACE ON

    7. SET AUTOTRACE ON STATISTICS

    5. SET AUTOTRACE TRACEONLY

    9. SET AUTOTRACE TRACEONLY EXPLAIN

    !ns&er

    Option 1: Incorrect. This option is used to start tracing statements usingAUTOTRACE. The AUTOTRACE report includes both the optimi%er e$ecution planand SQ statement e$ecution statistics.

    Option 2: "orrect. This option is used to display rows and statistics. TheAUTOTRACE report shows SQ statement e$ecution statistics and rows.

    Option 3: Incorrect. This option is used to get the plan and statistics only. This issimilar to SET AUTOTRACE ON, but it suppresses the printing of the user6s 0ueryoutput.

    Option 4: Incorrect. This option is used to display the e$ecution plan only withoute$ecution. The AUTOTRACE report shows only the optimi%er e$ecution pathwithout e$ecuting the statement.

    Correct ans&er's(%

    7. SET AUTOTRACE ON STATISTICS

    he statistics are recorded by the server %hen your statement executes and indicates thesystem resources required to execute your statement. he results include severalstatistics.

    Code

    SQL* sh' ">#'#2"c">#'#2"c OFFSQL* s # ">#'#2"c #2"c '/%4 s#"#6s#6csSQL* SELECT : FROM ' .12'&>c#s7

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    16/77

    + 2' s s % c# &.S#"#6s#6cs

    3 2 c>2s6= c"%%s & %'cJ #s c'/s6s# /# #s @3 1h4s6c"% 2 "&s 2 &' s6 @ @ 4# s s /# =6" SQL:N # #' c%6 /# +@ 4# s 2 c 6= & =6" SQL:N # G2'! c%6 /# + SQL:N # 2'>/s #'KG2'! c%6 /# ++ s'2#s (! !'24) s'2#s (&6sJ) + 2' s 12'c ss &

    recur !"e c %%

    The 2 c>2s6= c"%%s statistic is the number of recursive calls generated at both the user and system level. The Oracle Database maintains tables used for internal processing. When the Oracle Database needs to ma e a change to these tables, it internally generates an internal !"# statement,which in turn generates a recursive call.

    &' '%(c) *e+

    The & %'cJ #s statistic is the number of times a CURRENT bloc was re$uested.

    c(, ! +e,+ *e+

    The c'/s6s# /# #s statistic is the number of times a consistent read was re$uested for abloc .

    -./ !c % re &

    The 1h4s6c"% 2 "&s statistic is the total number of data bloc s read from dis . This numbere$uals the value of %physical reads direct% plus all reads into buffer cache.

    re&( !0e

    The 2 &' s6 statistic is the total amount of redo generated in bytes.

    he results also include six additional statistics.

    Code

    SQL* sh' ">#'#2"c">#'#2"c OFFSQL* s # ">#'#2"c #2"c '/%4 s#"#6s#6csSQL* SELECT : FROM ' .12'&>c#s7

    + 2' s s % c# &.S#"#6s#6cs

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    17/77

    3 2 c>2s6= c"%%s & %'cJ #s c'/s6s# /# #s @3 1h4s6c"% 2 "&s 2 &' s6 @ @ 4# s s /# =6" SQL:N # #' c%6 /# +@ 4# s 2 c 6= & =6" SQL:N # G2'! c%6 /# + SQL:N # 2'>/s #'KG2'! c%6 /# ++ s'2#s (! !'24) s'2#s (&6sJ) + 2' s 12'c ss &

    '/+e e,+ "! SQL1Ne+ +( c%!e,+

    The 4# s s /# =6" SQL:N # #' c%6 /# statistic is the total number of bytes sent to theclient from the foreground processes.

    '/+e rece!"e& "! SQL1Ne+ 2r(3 c%!e,+

    The 4# s 2 c 6= & =6" SQL:N # G2'! c%6 /# statistic is the total number of bytesreceived from the client over Oracle &et.

    SQL1Ne+ r(u,&+r!- +(42r(3 c%!e,+

    The SQL:N # 2'>/s #'KG2'! c%6 /# statistic is the total number of Oracle &etmessages sent to and received from the client.

    (r+ 53e3(r/6

    The s'2#s ' ! !'24) statistic is the number of sort operations that were performed completely inmemory and did not re$uire any dis writes.

    (r+ 5&! )6

    The s'2#s ' &6sJ) statistic is the number of sort operations that re$uired at least one dis write.

    r(7 -r(ce e&

    The 2' s 12'c ss & statistic is the number of rows processed during the operation.

    he client referred to in the statistics is !"#< lus. Oracle >et refers to the genericprocess communication bet%een !"#< lus and the server( regardless of %hether Oracle>et is installed. :ou cannot change the default format of the statistics report. Also notethat the statistics printed by AUTOTRACE are retrieved from V$SESSTAT .

    Code

    SQL* sh' ">#'#2"c">#'#2"c OFFSQL* s # ">#'#2"c #2"c '/%4 s#"#6s#6csSQL* SELECT : FROM ' .12'&>c#s7

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    18/77

    + 2' s s % c# &.S#"#6s#6cs

    3 2 c>2s6= c"%%s & %'cJ #s c'/s6s# /# #s @3 1h4s6c"% 2 "&s 2 &' s6 @ @ 4# s s /# =6" SQL:N # #' c%6 /# +@ 4# s 2 c 6= & =6" SQL:N # G2'! c%6 /# + SQL:N # 2'>/s #'KG2'! c%6 /# ++ s'2#s (! !'24) s'2#s (&6sJ) + 2' s 12'c ss &

    *ote

    & %'cJ #s indicates reads of the current block from the database.c'/s6s# /# #s are reads of blocks that must satisfy a particular systemchange number, or S"7. 1h4s6c"% 2 "&s indicates reads of blocks from disk.& %'cJ #s and c'/s6s# /# #s are the two statistics that are usuallymonitored. These should be low compared to the number of rows retrie#ed. Sortsshould be performed in memory rather than on disk.

    Su ar"

    he execution plan is the output of the optimizer( %hich is presented to the executionengine. he execution engine then implements the execution plan. here are various%ays to vie% the execution plan.

    he EXPLAIN PLAN command helps generate the execution plan used to execute a !"#statement. his command doesn=t execute the statement( but produces the plan andinserts it into a table named PLAN_TABLE . :ou can retrieve a PLAN_TABLE by using boththe ID and PARENT_ID columns %ith a CONNECT BY clause in a SELECT statement.

    :ou can use the AU O 0A6 !"#< lus facility to get a report on the execution plan andstatistics for !"# D2# statements. hese reports are useful for monitoring and tuning the

    performance of the statements. o use AUTOTRACE( a PLAN_TABLE must be available inyour schema( and you must have the #U! 0A6 role.

    able o0 Contents

    E op o0 page E

    E Learning Objectives E

    http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#pagetophttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#objectivehttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#pagetophttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#objective
  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    19/77

    E1.Understanding execution plans E

    E).Gathering execution plans E

    E+.,ispla"ing execution plans E

    E-.Using the !U O /!CE 0acilit" E

    ESu ar" E

    6opyright F 733G !$ill!oft. All rights reserved.!$ill!oft and the !$ill!oft logo are trademar$s or registered trademar$s of !$ill!oft in the United !tates and certain other countries.

    All other logos or trademar$s are the property of their respective o%ners.

    | Print | Contents | Close |

    Using ie&s and !2/

    Learning Objectives

    After completing this topic, you should be able to

    recognize the views containing execution plan information recognize how to use the AW(

    1. Using the 3S$L4PL!* vie&

    he V$SQL_PLAN vie% provides a %ay of examining the execution plan for cursors thatare still in the library cache. he information in this vie% is very similar to the informationin PLAN_TABLE . -o%ever( EXPLAIN PLAN sho%s a theoretical plan that can be used ifthis statement %ere to be executed( %hereas V$SQL_PLAN contains the actual plan used.

    he execution plan obtained by the EXPLAIN PLAN statement can be different from theexecution plan used to execute the cursor. his is because the cursor might have beencompiled %ith different values of session parameters.

    V$SQL_PLAN sho%s the plan for a cursor rather than for all cursors associated %ith a!"# statement. he difference is that a !"# statement can have more than one cursorassociated %ith it( %ith each cursor further identified by a CHILD_NUMBER.

    @or example( the same statement executed by different users has different cursorsassociated %ith it if the ob,ect that is referenced is in a different schema. !imilarly(

    http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_1http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_2http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_3http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_4http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_5http://window.print%28%29/http://window.print%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#contentshttp://parent.window.close%28%29/http://parent.window.close%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_1http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_2http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_3http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_4http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t3/misc/transcript.html#section_5http://window.print%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#contentshttp://parent.window.close%28%29/
  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    20/77

    different hints can cause different cursors. he V$SQL_PLAN vie% can be used to see thedifferent plans for different child cursors of the same statement.

    *ote

    Another useful #iew is V$SQL_PLAN_STATISTICS , which pro#ides the e$ecutionstatistics of each operation in the e$ecution plan for each cached cursor. Also theV$SQL_PLAN_STATISTICS_ALL #iew concatenates information fromV$SQL_PLAN with e$ecution statistics from V$SQL_PLAN_STATISTICS andV$SQL_WOR9AREA.

    he V$SQL_PLAN vie% contains almost all the PLAN_TABLE columns( in addition toothers. he ADDRESS and HASH_VALUE columns present in PLAN_TABLE have the samevalues+

    ADDRESS and HASH_VALUE andhe ADDRESS and HASH_VALUE columns can be used to ,oin %ith V$SQLAREA to add the

    cursor specific information.

    ADDRESS#HASH_VALUE# and CHILD_NUMBER

    he ADDRESS( HASH_VALUE( and CHILD_NUMBER columns can be used to ,oin %ithV$SQL to add the child cursor'specific information.

    he PLAN_HASH_VALUE column is a numerical representation of the !"# plan for thecursor. &y comparing one PLAN_HASH_VALUE %ith another( you can easily identify%hether the t%o plans are the same or not )rather than comparing the t%o plans line'by'

    line*.

    *ote

    Since racle 8atabase 9:g, SQL_HASH_VALUE in V$SESSION has beencomplemented with SQL_ID , which you retrie#e in many other V$ #iews.SQL_HASH_VALUE is a ;(bit hash #alue, which is moreuni0ue, the bottom ;< bits of which are SQL_HASH_VALUE. It is normallyrepresented as a character string to make it more manageable.

    he V$SQL_PLAN_STATISTICS vie% provides the actual execution statistics for everyoperation in the plan( such as the number of output ro%s and elapsed time. All statistics(except the number of output ro%s( are cumulative.

    @or example( the statistics for a ,oin operation also include the statistics for its t%o inputs.he statistics in V$SQL_PLAN_STATISTICS are available for cursors that have been

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    21/77

    compiled %ith the STATISTICS_LEVEL initialization parameter set to A## or using the;ATHER_PLAN_STATISTICS hint.

    he V$SQL_PLAN_STATISTICS_ALL vie% contains memory'usage statistics for ro%sources that use !"# memory )sort or hash ,oin*. his vie% concatenates information in

    V$SQL_PLAN %ith execution statistics from V$SQL_PLAN_STATISTICS andV$SQL_WOR9AREA.

    ). Lin5ing d"na ic per0or ance vie&s

    V$SQLAREA displays statistics on shared !"# areas and contains one ro% per !"#string.

    Graphic

    In this e$ample, ?+SQ AR)A e$pands into ?+SQ in the ?+SQ STATS layer.?+SQ in the ?+SQ STATS layer is connected ?+SQ -W R@AR)A,?+SQ -! A7, and ?+SQ -! A7-STATISTI"S in the?+SQ -! A7-STATISTI"S-A layer. ?+SQ -! A7 contains estimatedstatistics for each row source and ?+SQ -! A7-STATISTI"S contains e$ecutionstatistics for each row source.

    V$SQLAREA provides statistics on !"# statements that are in memory( parsed( and readyfor execution+

    SQL_ID and

    SQL_ID is the !"# identifier of the parent cursor in the library cache.

    VERSION_COUNT

    VERSION_COUNT is the number of child cursors that are present in the cache under thisparent.

    V$SQL lists statistics on shared !"# areas and contains one ro% for each child of theoriginal !"# text entered+

    ADDRESS

    ADDRESS represents the address of the handle to the parent for this cursor.HASH_VALUE

    HASH_VALUE contains the value of the parent statement in the library cache.

    SQL_ID

    he !"# identifier of the parent cursor in the library cache is represented by SQL_ID .

    PLAN_HASH_VALUE# and

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    22/77

    PLAN_HASH_VALUE is a numeric representation of the !"# plan for this cursor. &ycomparing one PLAN_HASH_VALUE %ith another( you can easily identify if the t%o plansare the same or not )rather than comparing the t%o plans line'by'line*.

    CHILD_NUMBER

    CHILD_NUMBER is the number of this child cursor.

    !tatistics displayed in V$SQL are normally updated at the end of query execution.-o%ever( for long'running queries( they are updated every five seconds. his ma$es iteasy to see the impact of long'running !"# statements %hile they are still in progress.

    V$SQL_PLAN contains the execution plan information for each child cursor loaded in thelibrary cache. he ADDRESS( HASH_VALUE( and CHILD_NUMBER columns can be used to

    ,oin %ith V$SQL to add the child cursor'specific information.

    V$SQL_PLAN_STATISTICS provides execution statistics at the ro% source level for each

    child cursor. he ADDRESS and HASH_VALUE columns can be used to ,oin %ithV$SQLAREA to locate the parent cursor. he ADDRESS( HASH_VALUE( andCHILD_NUMBER columns can be used to ,oin %ith V$SQL to locate the child cursor usingthis area.

    V$SQL_PLAN_STATISTICS_ALL contains memory usage statistics for ro% sources thatuse !"# memory )sort or hash ,oin*. his vie% concatenates information in V$SQL_PLAN %ith execution statistics from V$SQL_PLAN_STATISTICS and V$SQL_WOR9AREA.

    V$SQL_WOR9AREA displays information about %or$ areas used by !"# cursors. ach!"# statement stored in the shared pool has one or more child cursors that are listed inthe V$SQL vie%. V$SQL_WOR9AREA lists all %or$ areas needed by these child cursors.V$SQL_WOR9AREA can be ,oined %ith V$SQLAREA on ) ADDRESS( HASH_VALUE* and %ithV$SQL on ) ADDRESS( HASH_VALUE( CHILD_NUMBER*.

    :ou can use V$SQL_WOR9AREA vie% to find ans%ers to the follo%ing questions+

    /hat are the top 13 %or$ areas that require the most cache area8

    @or %or$ areas allocated in the AUTO mode( %hat percentage of %or$ areas run using maximummemory8

    V$SQLSTATS displays basic performance statistics for !"# cursors( %ith each ro%representing the data for a unique combination of !"# text and optimizer plan )that is(unique combination of SQL_ID and PLAN_HASH_VALUE*. he column definitions forcolumns in V$SQLSTATS are identical to those in the V$SQL and V$SQLAREA vie%s.

    -o%ever( the V$SQLSTATS vie% differs from V$SQL and V$SQLAREA in that it is faster(

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    23/77

    more scalable( and has a greater data retention )the statistics may still appear in thisvie%( even after the cursor has been aged out of the shared pool*.

    *ote

    V$SQLSTATS contains a subset of columns that appear in V$SQL andV$SQLAREA.

    $uestion

    /hich statements accurately describe the V$SQL_PLAN vie%8

    Options%

    1. It is similar to the PLAN_TABLE( but contains the actual plan used

    7. It concatenates information from V$SQL_PLAN_STATISTICS andV$SQL_WOR9AREA

    5. It provides a %ay of examining the execution plan for cursors that are still in thelibrary cache

    9. It provides the execution statistics of each operation in the execution plan for eachcached cursor

    !ns&er

    Option 1: "orrect. EXPLAIN PLAN shows a theoretical plan that can be used ifthis statement were to be e$ecuted, whereas V$SQL_PLAN contains the actual

    plan used.

    Option 2: Incorrect. The V$SQL_PLAN_STATISTICS_ALL #iew concatenatesinformation from V$SQL_PLAN with e$ecution statistics fromV$SQL_PLAN_STATISTICS and V$SQL_WOR9AREA.

    Option 3: "orrect. This #iew pro#ides a way of e$amining the e$ecution plan forcursors that are still in the library cache, and shows the plan for a cursor ratherthan for all cursors associated with a SQ statement.

    Option 4: Incorrect. V$SQL_PLAN_STATISTICS pro#ides the e$ecution statisticsof each operation in the e$ecution plan for each cached cursor.

    Correct ans&er's(%

    1. It is similar to the PLAN_TABLE ( but contains the actual plan used5. It provides a %ay of examining the execution plan for cursors that are still in thelibrary cache

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    24/77

    :ou can query V$SQL_PLAN using the DBMS_XPLAN.DISPLAY_CURSOR() function todisplay the current or last executed statement as in this example.

    Graphic

    The SQ -I8 parameter that is being passed is the following >B5u=9: +>= 5 -))7

    SQL_ID 3 > +>= 5 0 ch6%& />! 2

    SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!

    FROM h2. !1%'4 s 0 h2.& 1"2#! /#s & WHERE.& 1"2#! /#_6& ,&.& 1"2#! /#_6&

    P%"/ h"sh ="%> ? +@ 5 +

    I& O1 2"#6'/ N"! R' s B4# sC's# ( CPU

    SELECT STATEMENT (

    MER;E OIN + + (

    + TABLE ACCESS BY INDEX ROWIDDEPARTMENTS + 3 + + (

    INDEX FULL SCAN DEPT_ID_P9 + (:3 SORT OIN

    3 (+5 5 TABLE ACCESS FULL EMPLOYEES

    (

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    3 "cc ss( E . DEPARTMENT_ID , D . DEPARTMENT_ID ) G6%# 2( E . DEPARTMENT_ID , D . DEPARTMENT_ID )

    +3 2' s s % c# &.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    25/77

    :ou can pass the value of SQL_ID for the statement as a parameter to obtain theexecution plan for a given statement. SQL_ID is the SQL_ID of the !"# statement in thecursor cache.

    :ou can retrieve the appropriate value by querying the SQL_ID column in V$SQL or

    V$SQLAREA. Alternatively( you could select the PREV_SQL_ID column for a specificsession out of V$SESSION . his parameter defaults to null in %hich case the plan of thelast cursor executed by the session is displayed.

    Graphic

    The SQ -I8 parameter that is being passed is the following >B5u=9: +>= 5 -))7

    SQL_ID 3 > +>= 5 0 ch6%& />! 2

    SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!FROM h2. !1%'4 s 0 h2.& 1"2#! /#s & WHERE

    .& 1"2#! /#_6& ,&.& 1"2#! /#_6&

    P%"/ h"sh ="%> ? +@ 5 +

    I& O1 2"#6'/ N"! R' s B4# sC's# ( CPU

    SELECT STATEMENT (

    MER;E OIN + + (

    + TABLE ACCESS BY INDEX ROWIDDEPARTMENTS + 3 + + (

    INDEX FULL SCAN DEPT_ID_P9 + (:3 SORT OIN

    3 (+5 5 TABLE ACCESS FULL EMPLOYEES

    (

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    26/77

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    3 "cc ss( E . DEPARTMENT_ID , D . DEPARTMENT_ID ) G6%# 2( E . DEPARTMENT_ID , D . DEPARTMENT_ID )

    +3 2' s s % c# &.

    o obtain SQL_ID ( execute this query.

    Code

    SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!FROM h2. !1%'4 s 0 h2.& 1"2#! /#s &WHERE .& 1"2#! /#_6& ,&.& 1"2#! /#_6&7

    SELECT SQL_ID0 SQL_TEXT FROM V$SQLWHERE SQL_TEXT LI9E - SELECT .%"s#_/"! 0 - 7

    s"82 !! s SELECT SQL_6&0 s %_# 8# G2'! =$SQL 3 > +>= 5 SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!

    :ou can also pass the CHILD_NUMBER parameter to theDBMS_XPLAN.DISPLAY_CURSOR() function. CHILD_NUMBER is the child number of thecursor to display. If not supplied( the execution plan of all cursors matching the suppliedSQL_ID parameter is displayed. CHILD_NUMBER can be specified only if SQL_ID isspecified.

    Code

    SELECT PLAN_TABLE_OUTPUT FROMTABLE(DBMS_XPLAN.DISPLAY_CURSOR(-3 > +>= 5 -))7

    SQL_ID 3 > +>= 5 0 ch6%& />! 2

    SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!FROM h2. !1%'4 s 0 h2.& 1"2#! /#s & WHERE

    .& 1"2#! /#_6& ,&.& 1"2#! /#_6&

    P%"/ h"sh ="%> ? +@ 5 +

    I& O1 2"#6'/ N"! R' s B4# sC's# ( CPU

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    27/77

    SELECT STATEMENT (

    MER;E OIN + + (

    + TABLE ACCESS BY INDEX ROWIDDEPARTMENTS + 3 + + (

    INDEX FULL SCAN DEPT_ID_P9 + (:3 SORT OIN

    3 (+5 5 TABLE ACCESS FULL EMPLOYEES

    (

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    3 "cc ss( E . DEPARTMENT_ID , D . DEPARTMENT_ID ) G6%# 2( E . DEPARTMENT_ID , D . DEPARTMENT_ID )

    +3 2' s s % c# &.

    he DBMS_XPLAN.DISPLAY_CURSOR() function can also accept the FORMAT parameter.he FORMAT parameter controls the level of detail for the plan. In addition to the standard

    values ) BASIC ( TYPICAL ( SERIAL ( and ALL*( there are additional supported values todisplay run'time statistics for the cursor+

    Code

    SELECT PLAN_TABLE_OUTPUT FROMTABLE(DBMS_XPLAN.DISPLAY_CURSOR(-3 > +>= 5 -))7

    SQL_ID 3 > +>= 5 0 ch6%& />! 2

    SELECT .%"s#_/"! 0 &.& 1"2#! /#_/"!FROM h2. !1%'4 s 0 h2.& 1"2#! /#s & WHERE

    .& 1"2#! /#_6& ,&.& 1"2#! /#_6&

    P%"/ h"sh ="%> ? +@ 5 +

    I& O1 2"#6'/ N"! R' s B4# sC's# ( CPU

    SELECT STATEMENT

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    28/77

    ( MER;E OIN

    + + ( + TABLE ACCESS BY INDEX ROWID

    DEPARTMENTS + 3 + + ( INDEX FULL SCAN DEPT_ID_P9 + (:3 SORT OIN

    3 (+5 5 TABLE ACCESS FULL EMPLOYEES

    (

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    3 "cc ss( E . DEPARTMENT_ID , D . DEPARTMENT_ID ) G6%# 2( E . DEPARTMENT_ID , D . DEPARTMENT_ID )

    +3 2' s s % c# &.

    IOSTATS

    Assuming that the basic plan statistics are collected %hen !"# statements are executed)either by using the ;ATHER_PLAN_STATISTICS hint or by setting thes#"#6s#6cs_% = % parameter to ALL*( the IOSTATS format sho%s I;O statistics for ALL )or only for LAST* executions of the cursor.

    MEMSTATS

    Assuming that the rogram Hlobal Area( or HA( memory management is enabled )that is(the 1 "_" 2 "# _#"2 # parameter is set to a nonzero value*( the MEMSTATS formatallo%s to display memory management statistics )for example( execution mode of theoperator( ho% much memory %as used( number of bytes spilled to dis$( and so on.* hesestatistics only apply to memory'intensive operations( such as hash ,oins( sort( or somebitmap operators.

    ALLSTATS # and

    It is a shortcut for IOSTATS MEMSTATS.

    LAST

    &y default( plan statistics are sho%n for all executions of the cursor. he LAST $ey%ordcan be specified to see only the statistics for the last execution.

    +. Using !uto atic 2or5load /epositor"

    he Automatic /or$load 0epository( or A/0( is part of the intelligent infrastructureintroduced %ith Oracle Database 13 g . his infrastructure is used by many components(

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    29/77

    such as Automatic Database Diagnostic 2onitor( or ADD2( for analysis.

    he A/0 automatically collects( processes( and maintains system performance statisticsfor problem'detection and self'tuning purposes and stores the statistics persistently in thedatabase.

    he statistics collected and processed by the A/0 include

    ob,ect statistics that determine both access and usage statistics of database segments

    time'model statistics based on time usage for activities( displayed in the V$SYS_TIME_MODEL and V$SESS_TIME_MODEL vie%s

    some of the system and session statistics collected in the V$SYSSTAT and V$SESSTAT vie%s

    !"# statements that produce the highest load on the system( based on criteria( such as elapsed

    time( 6 U time( buffer gets( and Active !ession -istory( or A!-( statistics( representing the history of recent sessions

    he database automatically generates snapshots of the performance data once everyhour and collects the statistics in the %or$load repository. he data in the snapshotinterval is then analyzed by ADD2.

    he ADD2 compares the differences bet%een snapshots to determine %hich !"#statements to capture based on the effect on the system load. his reduces the numberof !"# statements that need to be captured over time.

    *ote

    'y using ! /SQ packages such as DBMS_WOR9LOAD_REPOSITORY or racle)nterprise &anager, you can mange the fre0uency and retention period of SQthat is stored in the AWR.

    Although the primary interface for managing the A/0 is nterprise 2anager( monitoringfunctions can be managed %ith procedures in the DBMS_WOR9LOAD_REPOSITORY pac$age.

    !napshots are automatically generated for an Oracle database ho%ever( you can useDBMS_WOR9LOAD_REPOSITORY procedures to manually create( drop( and modify thesnapshots and baselines that are used by the ADD2.

    !napshots and baselines are sets of historical data for specific time periods that are usedfor performance comparisons. o invo$e these procedures( a user must be granted theD&A role.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    30/77

    :ou can manually create snapshots %ith the CREATE_SNAPSHOT procedure if you %antto capture statistics at times different than those of the automatically generatedsnapshots.

    In this CREATE_SNAPSHOT procedure example( a snapshot for the instance is created

    immediately %ith the flush level specified to the default flush level of TYPICAL . :ou canvie% this snapshot in the DBA_HIST_SNAPSHOT vie%.

    Code

    E8 c DBMS_WOR9LOAD_REPOSITORY.CREATE_SNAPSHOT (-ALL-)7

    :ou can drop a range of snapshots using the DROP_SNAPSHOT_RAN;E procedure. ovie% a list of the snapshot IDs along %ith database IDs( chec$ the DBA_HIST_SNAPSHOTvie%. @or example( you can drop the range of snapshots provided in this example.

    In this example( the range of snapshot IDs to drop is specified from 77 to 57. he optionaldatabase identifier is 5513G9G39B. If you do not specify a value for & 6&( the localdatabase identifier is used as the default value.

    A!- data that belongs to the time period specified by the snapshot range is also purged%hen the DROP_SNAPSHOT_RAN;E procedure is called.

    Code

    E8 c DBMS_WOR9LOAD_REPOSITORY.DROP_SNAPSHOT_RAN;E (%' _s/"1_6& ,*++0 h6 h_s/"1_6& ,* +0 & 6& ,* @3@ 3 )7

    :ou can ad,ust the interval and retention of snapshot generation for a specified databaseID. -o%ever( note that this can affect the precision of the Oracle diagnostic tools. heINTERVAL setting specifies ho% often )in minutes* snapshots are automaticallygenerated. he RETENTION setting specifies ho% long )in minutes* snapshots are storedin the %or$load repository. o ad,ust the settings( use theMODIFY_SNAPSHOT_SETTIN;S procedure.

    In this example( the retention period is specified as 95(733 minutes )53 days*( and theinterval bet%een each snapshot is specified as 53 minutes. If NULL is specified( theexisting value is preserved. he optional database identifier is 5513G9G39B. If you do notspecify a value for & 6&( the local database identifier is used as the default value. :oucan chec$ the current settings for your database instance %ith theDBA_HIST_WR_CONTROL vie%.

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    31/77

    E8 c DBMS_WOR9LOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTIN;S(2 # /#6'/

    ,* 3 + 0 6/# 2="% ,* 0 & 6& ,* @3@ 3 )7

    :ou can vie% the A/0 data on Oracle nterprise 2anager screens or in A/0 reports.-o%ever( you can also vie% the statistics directly using

    V$ACTIVE_SESSION_HISTORY and

    he V$ACTIVE_SESSION_HISTORY vie% displays active database session activity(sampled once every second.

    V$ etric vie&s

    V$ metric vie%s provide metric data to trac$ the performance of the system. he metricvie%s are organized into various groups( such as event( event class( system( session(service( file( and tablespace metrics. hese groups are identified in the V$METRIC;ROUP vie%.

    he DBA_HIST vie%s contain historical data stored in the database. his group of vie%sinclude

    DBA_HIST_ACTIVE_SESS_HISTORY

    DBA_HIST_ACTIVE_SESS_HISTORY displays the history of the contents of the sampledin memory active session history for recent system activity.

    DBA_HIST_BASELINE

    Information about the baselines captured in the system is displayed inDBA_HIST_BASELINE .

    DBA_HIST_DATABASE_INSTANCE

    o display information about the database environment( you can useDBA_HIST_DATABASE_INSTANCE .

    DBA_HIST_SNAPSHOT

    DBA_HIST_SNAPSHOT contains information about snapshots in the system.

    DBA_HIST_SQL_PLAN# and

    !"# execution plans are retrieved by DBA_HIST_SQL_PLAN .

    DBA_HIST_WR_CONTROL

    DBA_HIST_WR_CONTROL displays the settings for controlling A/0.

    $uestion

    Identify the statements that accurately describe A/0.

    Options%

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    32/77

    1. It automatically generates snapshots of performance data

    7. It is used to manually collect( process( and maintain system'performance statistics

    5. :ou can manage the frequency and retention period of !"# that is stored in the A/0

    9. :ou can vie% A/0 data on O 2 screens or A/0 reports only

    !ns&er

    Option 1: "orrect. The database automatically generates snapshots of the performance data once e#ery hour and collects the statistics in the workloadrepository.

    Option 2: Incorrect. The AWR automatically collects, processes, and maintainssystem(performance statistics for problem(detection and self(tuning purposes andstores the statistics persistently in the database.

    Option 3: "orrect. 'y using ! /SQ packages, such asDBMS_WOR9LOAD_REPOSITORY or )&, you can mange the fre0uency andretention period of SQ that is stored in the AWR.

    Option 4: Incorrect. *ou can use )& and AWR reports to #iew AWR data.owe#er, you can also 0uery V$ACTIVE_SESSION_HISTORY , the V$ metric

    #iews, and the DBA_HIST #iews.

    Correct ans&er's(%

    1. It automatically generates snapshots of performance data5. :ou can manage the frequency and retention period of !"# that is stored in the

    A/0

    :ou can use the DBMS_XPLAN.DISPLAY_AWR() function to display all stored plans inthe A/0.

    In this example( you pass in a SQL_ID as an argument. SQL_ID is the SQL_ID of the!"# statement in the cursor cache.

    Code

    SQL* SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(-3532> +4=" -))7

    PLAN_TABLE_OUTPUT

    SQL_ID 3532> +4="

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    33/77

    SELECT K: 8"!1% :K : G2'! h2. !1%'4 s /"#>2"% '6/h2.& 1"2#! /#s

    P%"/ h"sh ="%> ? 3 @ + 5 +

    I& O1 2"#6'/ N"! R' s B4# s C's# ( CPU)T6!

    SELECT STATEMENT ( )

    HASH OIN @ ( )? ?

    + TABLE ACCESS FULL DEPARTMENTS ++ + ( )? ?

    + TABLE ACCESS FULL DEPARTMENTS ++ + ( )? ? TABLE ACCESS FULL EMPLOYEES + ( )? ?

    &eside the SQL_ID parameter( the DISPLAY_AWR() function also ta$es thePLAN_HASH_VALUE( DB_ID ( and FORMAT parameters.

    Code

    SELECT #G.: FROM DBA_HIST_SQLTEXT h#0 #" % (DBMS_XPLAN.DISPLAY_AWR(h#.s %_6&0/>%%0 />%%0 -ALL- ))#G WHERE h#.s %_# 8# %6J - F -7

    :ou follo% five steps to complete this example.

    Code

    SQL* SELECT K: 8"!1% :K : FROM h2. !1%'4 s NATURAL OIN h2.& 1"2#! /#s7

    SQL* SELECT s %_6&0 s %_# 8# FROM =$SQL WHERE s %_# 8# LI9E - 8"!1% -7

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    34/77

    SQL* SELECT s %_6&0 s %_# 8# FROM & "_h6s#_s %# 8# WHEREs %_6& ,- 3532> +4=" -7

    SQL* EXEC DBMS_WOR9LOAD_REPOSITORY.CREATE.SNAPSHOT7PLKSQL 12'c &>2 s>cc ssG>%%4 c'!1% # &.SQL* SELECT s %_6&0 s %_# 8# FROM & "_h6s#_s %# 8# WHEREs %_6& ,- 3532> +4=" -7

    SQL*SELECT 1%"/_#" % _'>#1># FROM #" %(DBMS_XPLAN.DISPLAY_AWR(-3532> +4=" ))7

    SQL8 SELECT 41 e9 3-%e 14 1 FROM .r:e3-%(/ee NATURAL JOIN .r:&e- r+3e,+ ;

    *ou first e$ecute the SQ statement.

    SQL8 SELECT LOAD_REPOSITORY:CREATE:SNAPSHOT;PL4SQL -r(ce&ure ucce 2u%%/ c(3-%e+e&:SQL8 SELECT

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    35/77

    Code

    SQL* $ORACLE_HOMEK2& !sK"&!6/K" 2s 21#S1 c6G4 #h R 1'2# T41 W'>%& 4'> %6J "/ HTML 2 1'2#0 '2 " 1%"6/ # 8# 2 1'2#

    S1 c6G4 #h />! 2 'G &"4s 'G s/"1sh'#s #' ch''s G2'!S1 c6G4 #h B 6/ "/& E/& S/"1sh'# I&s S1 c6G4 #h SQL I& E/# 2 ="%> G'2 s %_6&? 13s@2" "S1 c6G4 #h R 1'2# N"!

    :ou can display the plan information in A/0 by using the DISPLAY_AWR table function inthe DBMS_XPLAN #;!"# pac$age.

    @or example( this displays the plan information for a SQL_ID in A/0.

    :ou can retrieve the appropriate value for the !"# statement of interest by querying theSQL_ID in DBA_HIST_SQLTEXT column.

    Code

    SELECT : FROM#" % (DBMS_XPLAN.DISPLAY_AWR(- 13s@2" " -))7

    Su ar"

    he information in V$SQL_PLAN vie% provides a %ay of examining the execution plan forcursors that are still in the library cache. he V$SQL_PLAN_STATISTICS vie% providesthe execution statistics of each operation in the execution plan for each cached cursor.

    All the statistics displayed in V$SQL are updated at the end of query execution. heV$SQL_PLAN_STATISTICS vie% provides execution statistics( and the ADDRESS andHASH_VALUE columns can be used to ,oin %ith V$SQLAREA. he ADDRESS(HASH_VALUE( and CHILD_NUMBER columns can be used to ,oin %ith V$SQL.

    he A/0 automatically collects( processes( and maintains system performance statistics

    and stores them persistently in the database.

    able o0 Contents

    E op o0 page E

    E Learning Objectives E

    http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#pagetophttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#objectivehttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#pagetophttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#objective
  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    36/77

    E1.Using the 3S$L4PL!* vie& E

    E).Lin5ing d"na ic per0or ance vie&s E

    E+.Using !uto atic 2or5load /epositor" E

    ESu ar" E

    6opyright F 733G !$ill!oft. All rights reserved.!$ill!oft and the !$ill!oft logo are trademar$s or registered trademar$s of !$ill!oft in the United !tates and certain other countries.

    All other logos or trademar$s are the property of their respective o%ners.

    | Print | Contents | Close |

    S$L 6onitoring and Execution Plans

    Learning Objectives

    After completing this topic, you should be able to

    recognize how to perform !"# monitoring interpret execution plans

    1. Per0or ing S$L onitoring

    he !"# monitoring feature is enabled by default %hen the STATISTICS_LEVEL initialization parameter is either set to ALL or TYPICAL )the default value*.

    Additionally( the CONTROL_MANA;EMENT_PAC9_ACCESS parameter must be set toDIA;NOSTIC J TUNIN; )the default value* because !"# monitoring is a feature of theOracle Database uning ac$.

    &y default( !"# monitoring is automatically started %hen a !"# statement runs parallel(or %hen it has consumed at least five seconds of the 6 U or I;O time in a singleexecution.

    !"# monitoring is active by default. -o%ever( t%o statement'level hints are available toforce or prevent a !"# statement from being monitored. o force !"# monitoring( use theMONITOR hint. o prevent the hinted !"# statement from being monitored( use theNO_MONITOR hint.

    :ou can monitor the statistics for !"# statement execution using the V$SQL_MONITOR and V$SQL_PLAN_MONITOR vie%s. After monitoring is initiated( an entry is added to the

    http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_1http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_1http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_2http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_3http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_4http://window.print%28%29/http://window.print%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t7/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t7/misc/transcript.html#contentshttp://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t7/misc/transcript.html#contentshttp://parent.window.close%28%29/http://parent.window.close%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_1http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_2http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_3http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t5/misc/transcript.html#section_4http://window.print%28%29/http://library.skillport.com/courseware/Content/cca/od_ortw_a03_dt_enus/output/t7/misc/transcript.html#contentshttp://parent.window.close%28%29/
  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    37/77

    dynamic performance V$SQL_MONITOR vie%. his entry trac$s $ey performance metricscollected for the execution( including the elapsed time( 6 U time( number of reads and%rites( I;O %ait time( and various other %ait times. hese statistics are refreshed in nearreal time as the statement executes( generally once every second.

    After the execution ends( monitoring information is not deleted immediately( but is $ept inthe V$SQL_MONITOR vie% for at least one minute. he entry is eventually deleted so itsspace can be reclaimed as ne% statements are monitored.

    he V$SQL_MONITOR and V$SQL_PLAN_MONITOR vie%s can be used in con,unction%ith the follo%ing vie%s to get additional information about the execution that ismonitored+

    V$SQL

    V$SQL_PLAN

    V$ACTIVE_SESSION_HISTORY

    V$SESSION_LON;OPS ( and

    V$SESSION

    Instead( you can use the !"# 2onitoring 0eport to vie% !"# monitoring data. he !"#monitoring report is also available in a HUI version through nterprise 2anager.

    In this example( it is assumed that you SELECT from SALES from a different session than

    the one used to print the !"# monitoring report.

    Code

    SQL* SELECT c'>/#(:) FROM s"% s7

    In the !"# 2onitoring 0eport( the DBMS_SQLTUNE.REPORT_SQL_MONITOR functionaccepts several input parameters to specify the execution( the level of detail in the report(and the report type ) ? ( - 2#( or ?2#*. &y default( a text report is generated for thelast execution that %as monitored if no parameters are specified.

    After the SELECT statement is started( and %hile it executes( you print the !"#monitoring report from a second session. @rom the report( you can see that the SELECT statement executes currently.

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    38/77

    SQL* s # %'/ SQL* s # %'/ ch>/Js6 SQL* s # %6/ s6 +SQL* SELECT & !s_s %#>/ .2 1'2#_s %_!'/6#'2 FROM &>"%7

    SQL M'/6#'26/ R 1'2#

    SQL T 8#

    SELECT c'>/#(:) FROM s"% s

    ;%' "% I/G'2!"#6'/ S#"#>s ? EXECUTIN; I/s#"/c ID ? S ss6'/ ID ? +5 SQL ID ? G" 2J / J! SQL E8 c>#6'/ ID ? + P%"/ H"sh V"%> ? 3 ++ E8 c>#6'/ S#"2# & ? +K @K+ + ? ? F62s# R G2 sh T6! ? +K @K+ + ? ?++ L"s# R G2 sh T6! ? +K @K+ + ? ?3+

    E%"1s & C1> IO O#h 2 B>GG 2 R "&s T6! (s) T6! (s) W"6#s(s) W"6#s(s) ; #s

    ++ . . @ +5@9 @@9

    he Hlobal Information section in the !"# 2onitoring 0eport gives you some importantinformation. o uniquely identify t%o executions of the same !"# statement( a composite$ey called an execution $ey is generated.

    his execution $ey consists of three attributes( each corresponding to a column inV$SQL_MONITOR+

    Code

    SQL* s # %'/

    SQL* s # %'/ ch>/Js6 SQL* s # %6/ s6 +SQL* SELECT & !s_s %#>/ .2 1'2#_s %_!'/6#'2 FROM &>"%7

    SQL M'/6#'26/ R 1'2#

    SQL T 8#

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    39/77

    SELECT c'>/#(:) FROM s"% s

    ;%' "% I/G'2!"#6'/ S#"#>s ? EXECUTIN; I/s#"/c ID ? S ss6'/ ID ? +5 SQL ID ? G" 2J / J! SQL E8 c>#6'/ ID ? + P%"/ H"sh V"%> ? 3 ++ E8 c>#6'/ S#"2# & ? +K @K+ + ? ? F62s# R G2 sh T6! ? +K @K+ + ? ?++ L"s# R G2 sh T6! ? +K @K+ + ? ?3+

    E%"1s & C1> IO O#h 2 B>GG 2 R "&s T6! (s) T6! (s) W"6#s(s) W"6#s(s) ; #s

    ++ . . @ +5@9 @@9

    a !"# identifier to identify the !"# statement ) SQL_ID *

    an internally generated identifier to ensure that this primary $ey is truly unique ) SQL_EXEC_ID *(and

    a start execution time stamp ) SQL_EXEC_START *

    *ote

    The report also includes some important statistics calculated so far.

    he !"# 2onitoring 0eport then displays the execution path currently used by yourstatement. !"# monitoring gives you the display of the current operation that executes inthe plan. his enables you to detect parts of the plan that are the most time'consuming(so that you can focus your analysis on those parts. he running operation is mar$ed byan arro% in the Id column of the report.

    he ime Active)s* column sho%s ho% long the operation has been active )the delta inseconds bet%een the first and the last active time*.

    Code

    SQL P%"/ M'/6#'26/ D #"6%s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    I& O1 2"#6'/ N"! R' s C's# T6! S#"2#

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    40/77

    (Es#6!) Ac#6= (s) Ac#6=

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    SELECT STATEMENT @ SORT A;;RE;ATE

    * + TABLE ACCESS FULL SALES 5 @ 39 @ +

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,S#"2#s R' s Ac#6=6#4 Ac#6=6#4 D #"6% P2' 2 ss (Ac#>"%) (1 2c /#) (s"!1% )

    3+ 9 . C1> (3)

    3 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    he !tart Active column sho%s( in seconds( %hen the operation in the execution planstarted relative to the !"# statement execution start time. In this report( the table accessfull operation at Id 7 %as the first to start )J1s !tart Active* and ran for the first 75seconds so far.

    he !tarts column sho%s the number of times the operation in the execution plan %asexecuted.

    he 0o%s )Actual* column indicates the number of ro%s produced( and the 0o%s ) stim*column sho%s the estimated cardinality from the optimizer.

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    41/77

    SQL P%"/ M'/6#'26/ D #"6%s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    I& O1 2"#6'/ N"! R' s C's# T6! S#"2#

    (Es#6!) Ac#6= (s) Ac#6=

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    SELECT STATEMENT @ SORT A;;RE;ATE

    * + TABLE ACCESS FULL SALES 5 @ 39 @ +

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,S#"2#s R' s Ac#6=6#4 Ac#6=6#4 D #"6% P2' 2 ss (Ac#>"%) (1 2c /#) (s"!1% )

    3+ 9 . C1> (3) 3 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    he Activity )percent* and Activity Detail )sample K* columns are derived by ,oining theV$SQL_PLAN_MONITOR and V$ACTIVE_SESSION_HISTORY vie%s. Activity )percent*

    sho%s the percentage of database time consumed by each operation of the executionplan. Activity Detail )sample K* sho%s the nature of that activity )such as 6 U or %aitevent*.

    In this report( 133L of the database time is consumed by operation Id 7 ) TABLE ACCESS FULL of SALES* as provided in the Activity Detail )sample K* column. !o far( this activityconsists of four samples( %hich are only attributed to 6 U.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    42/77

    Code

    SQL P%"/ M'/6#'26/ D #"6%s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    I& O1 2"#6'/ N"! R' s C's# T6! S#"2#

    (Es#6!) Ac#6= (s) Ac#6=

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    SELECT STATEMENT @ SORT A;;RE;ATE

    * + TABLE ACCESS FULL SALES 5 @ 39 @

    +

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,S#"2#s R' s Ac#6=6#4 Ac#6=6#4 D #"6% P2' 2 ss (Ac#>"%) (1 2c /#) (s"!1% )

    3+ 9 . C1> (3) 3 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    he last column( rogress( sho%s progress monitoring information for the operation fromthe V$SESSION_LON;OPS vie%. According to this report( so far( the TABLE ACCESSFULL operation is B9L complete. his column only appears in the report after a certainamount of time( and only for the instrumented ro% sources.

    Code

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    43/77

    SQL P%"/ M'/6#'26/ D #"6%s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    I& O1 2"#6'/ N"! R' s C's# T6! S#"2#

    (Es#6!) Ac#6= (s) Ac#6=

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    SELECT STATEMENT @ SORT A;;RE;ATE

    * + TABLE ACCESS FULL SALES 5 @ 39 @ +

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,S#"2#s R' s Ac#6=6#4 Ac#6=6#4 D #"6% P2' 2 ss (Ac#>"%) (1 2c /#) (s"!1% )

    3+ 9 . C1> (3) 3 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    *ote

    The SQ !lan &onitoring 8etails report can also contain the &emory and Tempcolumns. These indicate the amount of memory and temporary space consumedby corresponding operation of the e$ecution plan.

    $uestion

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    44/77

    :ou are examining a !"# monitoring report. he ime Active)s* column displays avalue of 75. /hat does this number present8

    Options%

    1. -o% long the operation has been active

    7. he number of ro%s produced

    5. he number of times the operation in the execution plan %as executed

    9. /hen the operation in the execution plan started relative to the !"# statementexecution start time

    !ns&er

    Option 1: "orrect. The Time Acti#e1s2 column shows how long the operation hasbeen acti#e E the delta in seconds between the first and the last acti#e time.

    Option 2: Incorrect. The Rows 1Actual2 column shows the number of rows produced.

    Option 3: Incorrect. The Starts column shows the number of times the operationin the e$ecution plan was e$ecuted.

    Option 4: Incorrect. The Start Acti#e column shows, in seconds, when theoperation in the e$ecution plan started relati#e to the SQ statement e$ecutionstart time.

    Correct ans&er's(%

    1. -o% long the operation has been active

    ). 7nterpreting an execution plan

    xplain plan output is a representation of a tree of ro% sources. ach step )line in theexecution plan or node in the tree* represents a ro% source. he explain plan utilityindents nodes to indicate that they are the children of the parent above it.

    he order of the nodes under the parent indicates the order of execution of the nodes

    %ithin that level. If t%o steps are indented at the same level( the first one is executed first.

    In the tree format( the leaf at the left on each level of the tree is %here the executionstarts. he steps of the execution plan are not performed in the order in %hich they arenumbered. here is a parent'child relationship bet%een steps.

    Graphic

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    45/77

    There are four le#els in the tree format. The node Root/!arent is on e#el 9. Thisnode is di#ided into two subnodes on e#el

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    46/77

    4. >o% that this parent and its children are completed( %or$ bac$ up the tree( and loo$ at the siblings of theparent ro% source and its parents. xecute as before.

    M. 2ove bac$ up the plan until all ro% sources are executed.

    he standard tree interpretation is as follo%s+

    1. !tart at the top.

    7. 2ove do%n the tree to the left until you reach the left node. his is executed first.

    5. #oo$ at the siblings of this ro% source. hese ro% sources are executed next.

    9. After the children are executed( the parent is executed next.

    4. >o% that this parent and its children are completed( %or$ bac$ up the tree( and loo$ at the siblings of theparent ro% source and its parents. xecute as before.

    M. 2ove bac$ up the tree until all ro% sources are exhausted.

    *ote

    If you remember the few basic rules of e$plain plans and with some e$perience,you can read most plans easily.

    $uestion

    !equence the steps you ta$e to interpret a standard explain plan.

    Options%

    A. !tart at the top and move do%n ro% sources until you get to one that produces databut does not consume any

    &. #oo$ at the siblings of the start ro% source

    6. /or$ bac$ up the tree and loo$ at the siblings of the parent ro% source and itsparents and execute as before

    D. 2ove bac$ up the plan until all ro% sources are exhausted

    !ns&er

    Correct ans&er's(%

    Start at the top and ove do&n ro& sources until "ou get to one thatproduces data but does not consu e an" is ran5ed as the 0irst step.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    47/77

    he first step in interpreting a standard explain plan is to start at the top andthen move do%n the ro% sources until you get to one that produces data( butdoes not consume any. his is the start ro% source.

    Loo5 at the siblings o0 the start ro& source is ran5ed as the second step.

    he second step in interpreting a standard explain plan is to loo$ through thesiblings of the start ro% source( as these are the ro% sources executed next.

    2or5 bac5 up the tree and loo5 at the siblings o0 the parent ro& source andits parents and execute as be0ore is ran5ed as the third step.

    he third step in interpreting a standard explain plan is to %or$ bac$ up thetree and loo$ at the siblings of the parent ro% source and its parents. Afterthe children are executed( the parent is executed next.

    6ove bac5 up the plan until all ro& sources are exhausted is ran5ed as the0inal step.

    he final step in interpreting a standard explain plan is to move bac$ up theplan until all ro% sources are exhausted.

    :ou start %ith an example query to illustrate ho% to interpret an execution plan. he query%ith its associated execution plan and the same plan in the tree format is sho%n in thisexample.

    Graphic

    The e$ample 0uery is the following

    S) )"T /FG RD ) F/ ename,5ob,sal,dname

    3R & emp,dept W )R) dept.deptnoHemp.deptno and not e$ists1S) )"T F 3R & salgradeW )R) emp.sal between losal and hisal2

    The output is displayed in a tabular format with Id, peration, 7ame as columns.

    he query tries to find employees %ho have salaries outside the range of salaries in thesalary grade table. he query is a SELECT statement from t%o tables %ith a subquerybased on another table to chec$ the salary grades.

    Code

    SELECT K: RULE :K /"! 0 ' 0s"%0&/"!FROM !10& 1#WHERE & 1#.& 1#/', !1.& 1#/' "/& /'# 86s#s(SELECT : FROM s"% 2"& WHERE !1.s"%

    # / %'s"% "/& h6s"%)7

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    48/77

    I& O1 2"#6'/ N"!

    SELECT STATEMENT : FILTER + NESTED LOOPS TABLE ACCESS FULL EMP 3 TABLE ACCESS BY INDEX ROWID DEPT : 5 INDEX UNIQUE SCAN P9_DEPT : TABLE ACCESS FULL SAL;RADE

    P2 &6c"# I/G'2!"#6'/ (6& /#6G6 & 4 '1 2"#6'/ 6&)?

    G6%# 2( NOT EXISTS (SELECT FROM SAL;RADE SAL;RADE WHERE HISAL *,?B AND LOSAL ,?B+)) 5 "cc ss( DEPT . DEPTNO , EMP . DEPTNO ) G6%# 2( HISAL *,?B AND LOSAL ,?B+)

    his is the execution order for the query+

    +

    he plan starts %ith a full table scan of EMP (ID, ) .

    8

    he ro%s are passed bac$ to the controlling nested loops ,oin step (ID,+) ( %hich usesthem to execute the loo$up of ro%s in the P9_DEPT index in ID,5 .

    -

    he ROWIDs from the index are used to loo$up the other information from the DEPT table inID,3 .

    )

    ID,+ ( the nested loops ,oin step( is executed until completion.

    9# and

    After ID,+ has exhausted its ro% sources( a full table scan of !A#H0AD in ID, )at thesame level in the tree as ID,+ ( therefore( its sibling* is executed.

    1

    his is used to filter the ro%s from ID+ and ID .

    he children are executed before parents( so although structures for ,oins must be set upbefore the child execution( the children are notated as executed first.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    49/77

    erhaps the easiest %ay is to consider it as the order in %hich execution completes( sofor the NESTED LOOPS ,oin at ID,+ ( the t%o children NID, and ID,3 )together %ith itschild* must have completed their execution before ID,+ can be completed.

    In this example( a plan dump from V$SQL_PLAN %ith STATISTICS_LEVEL is set to ALL.his report includes some important additional information compared to the output of the

    EXPLAIN PLAN command+

    Code

    SQL* ALTER SESSION SET s#"#6s#6cs_% = %,ALL7

    S ss6'/ "%# 2 &.

    SQL* SELECT K: RULE #' !"J s>2 6# 2 12'&>c s :K/"! 0 ' 0s"%0&/"!

    FROM !10& 1# WHERE & 1#.& 1#/' , !1.& 1#/' AND NOT EXISTS(SELECT : FROM s"% 2"&WHERE !1.s"% BETWEEN %'s"% AND h6s"%)7

    /' 2' s s % c# &

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY_CURSOR(/>%%0/>%%0-TYPICAL IOSTATSLAST-))7

    SQL_ID + 3 @!4 =>G0 ch6%& />! 2

    P%"/ h"sh ="%> ? 5 +++

    I& O1 2"#6'/ N"! S#"2#s A R' s B>GG 2s

    : FILTER

    + NESTED LOOPS 3 +5 TABLE ACCESS FULL EMP 3 3 TABLE ACCESS BY INDEX ROWID DEPT 3 3 : 5 INDEX UNIQUE SCAN P9_DEPT 3

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    50/77

    3 3 : TABLE ACCESS FULL SAL;RADE + +

    A R' s corresponds to the number of ro%s produced by the corresponding ro% source

    B>GG 2s corresponds to the number of consistent reads done by the ro% source( and

    S#"2#s indicates ho% many times the corresponding operation %as processed

    @or each ro% from the EMP table( the system gets its ENAME( SAL( OB( and DEPTNO.hen the system accesses the DEPT table by its unique index ) P9_DEPT * to get DNAME

    using DEPTNO from the previous result set.

    he TABLE ACCESS FULL operation on the 2 table (ID, ) is started once. -o%ever(operations from ID 5 and 3 are started 19 times once for each EMP ro%. At this step(ID,+ *( the system gets all ENAME( SAL( OB( and DNAME.

    Code

    SQL* ALTER SESSION SET s#"#6s#6cs_% = %,ALL7

    S ss6'/ "%# 2 &.

    SQL* SELECT K: RULE #' !"J s>2 6# 2 12'&>c s :K/"! 0 ' 0s"%0&/"!

    FROM !10& 1# WHERE & 1#.& 1#/' , !1.& 1#/' AND NOT EXISTS(SELECT : FROM s"% 2"&WHERE !1.s"% BETWEEN %'s"% AND h6s"%)7

    /' 2' s s % c# &

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY_CURSOR(/>%%0/>%%0-TYPICAL IOSTATSLAST-))7

    SQL_ID + 3 @!4 =>G0 ch6%& />! 2

    P%"/ h"sh ="%> ? 5 +++

    I& O1 2"#6'/ N"! S#"2#s A R' s B>GG 2s

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    51/77

    : FILTER + NESTED LOOPS 3 +5 TABLE ACCESS FULL EMP 3 3 TABLE ACCESS BY INDEX ROWID DEPT 3 3 : 5 INDEX UNIQUE SCAN P9_DEPT 3 3 3 : TABLE ACCESS FULL SAL;RADE + +

    he system no% must filter out employees %ho have salaries outside the range ofsalaries in the salary grade table. o do that( for each ro% from ID,+ ( the systemaccesses the SAL;RADE table using a FULL TABLE SCAN operation to chec$ if theemployeePs salary is outside the salary range.

    his operation only needs to be done 17 times in this case because at run time thesystem does the chec$ for each distinct salary( and there are 17 distinct salaries in theEMP table.

    Code

    SQL* ALTER SESSION SET s#"#6s#6cs_% = %,ALL7

    S ss6'/ "%# 2 &.

    SQL* SELECT K: RULE #' !"J s>2 6# 2 12'&>c s :K/"! 0 ' 0s"%0&/"!

    FROM !10& 1# WHERE & 1#.& 1#/' , !1.& 1#/' AND NOT EXISTS(SELECT : FROM s"% 2"&WHERE !1.s"% BETWEEN %'s"% AND h6s"%)7

    /' 2' s s % c# &

    SQL* SELECT : FROM#" % (DBMS_XPLAN.DISPLAY_CURSOR(/>%%0/>%%0-TYPICAL IOSTATSLAST-))7

    SQL_ID + 3 @!4 =>G0 ch6%& />! 2

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    52/77

    P%"/ h"sh ="%> ? 5 +++

    I& O1 2"#6'/ N"! S#"2#s A R' s B>GG 2s

    : FILTER + NESTED LOOPS 3 +5 TABLE ACCESS FULL EMP 3 3 TABLE ACCESS BY INDEX ROWID DEPT 3 3 : 5 INDEX UNIQUE SCAN P9_DEPT 3 3 3 : TABLE ACCESS FULL SAL;RADE + +

    In this second example( the query retrieves names( department names( and addressesfor employees %hose departments are located in !eattle and %ho have managers.

    Graphic

    The e$ample 0uery is the following

    SQ J select /FG DS)-7 1d2 use-nl1m2 F/ m.last-name as dept-manager < , d.department-name; , l.street-address> from hr.employees m 5oinC hr.departments d on 1d.manager-id H m.employee-id2= natural 5oinB hr.locations l K where l.city H 6Seattle6

    : S) )"T STAT)&)7T 9 : 7)ST)8 !S< 9 7)ST)8 !S; < TA' ) A"")SS '* I78)L R WI8 "ATI 7S> ; I78)L RA7M) S"A7 "-"IT*-IL C < TA' ) A"")SS '* I78)L R WI8 8)!ART&)7TS= C I78)L RA7M) S"A7 8)!T- "ATI 7-IL

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    53/77

    B 9 TA' ) A"")SS '* I78)L R WI8 )&! *))SK B I78)L D7IQD) S"A7 )&!-)&!-I8-!@

    @or formatting reasons( the explain plan has the ID in the first column( and PID in thesecond column. he position is reflected by the indentation. he execution plan sho%s

    t%o nested loops ,oin operations.

    :ou follo% the same steps from the previous example to see the execution plan for thisexample+

    1. !tart at the top %ith ID, .

    7. 2ove do%n the ro% sources until you get to the one( that produces data( but does not consume any. Inthis case( ID ( ( +( and consume data. ID,3 is the first ro% source that does not consume any. hisis the start ro% source. ID,3 is executed first. he index range scan produces ROWIDs( %hich are used toloo$up in the LOCATIONS table in ID, .

    5. #oo$ at the siblings of this ro% source. hese ro% sources are executed next. he sibling at the samelevel as ID, is ID,5 . >ode ID,5 has a child ID, ( %hich is executed before it. his is another indexrange scan producing ROWIDs( %hich are used to loo$up in the DEPARTMENTS table in ID,5 .

    9. After the children operation( the parent operation is next. he NESTED LOOPS ,oin at ID,+ is executednext bringing together the underlying data.

    he next three steps are as follo%s+

    1. >o% that this parent and its children are completed( %al$ bac$ up the tree( and loo$ at the

    siblings of the parent ro% source and its parents. xecute as before. he sibling of ID,+ at thesame level in the plan is ID, . his has a child ID, ( %hich is executed first. he index uniquescan produces ROWIDs( %hich are used to loo$up in the EMPLOYEES table in ID, .

    7. 2ove bac$ up the plan until all ro% sources are exhausted. @inally( this is brought together %ith theNESTED LOOPS at ID, ( %hich passes the results bac$ to ID, .

    5. he execution order is 3 Z Z Z 5 Z + Z Q Q 1 Q .

    -ere is the complete description of this plan+

    he inner nested loops is executed first using LOCATIONS as the driving table( using an indexaccess on the CITY column. his is because you search for departments in !eattle only.

    he result is ,oined %ith the DEPARTMENTS table( using the index on the LOCATION_ID ,oincolumn the result of this first ,oin operation is the driving ro% source for the second nested loops

    ,oin.

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    54/77

    he second ,oin probes the index on the EMPLOYEE_ID column of the EMPLOYEES table. hesystem can do that because it $no%s )from the first ,oin* the employee ID of all managers ofdepartments in !eattle. >ote that this is a unique scan because it is based on the primary $ey.

    @inally( the EMPLOYEES table is accessed to retrieve the last name.

    In this example( try to find the order in %hich the plan is executed and deduce %hat is the ,oin order )order in %hich the system ,oins tables*. Again( ID is in the first column and PIDin the second column. he position is reflected by the indentation.

    It is important to recognize %hat the ,oin order of an execution plan is( to be able to findyour plan into a 13345 event trace file.

    Code

    SELECT K: ORDERED >s _h"sh( ) s "1_ '6/_6/1>#s(c) :K!"8(".6)FROM # "0 #+ 0 # cWHERE ".6 , .6 AND ".6 , c.67

    SELECT STATEMENT SORT A;;RE;ATE

    + HASH OIN + TABLE ACCESS FULL T

    3 + HASH OIN5 3 TABLE ACCESS FULL T

    3 TABLE ACCESS FULL T+

    -ere is the interpretation of this plan+

    Code

    SELECT K: ORDERED >s _h"sh( ) s "1_ '6/_6/1>#s(c) :K!"8(".6)FROM # "0 #+ 0 # cWHERE ".6 , .6 AND ".6 , c.67

    SELECT STATEMENT SORT A;;RE;ATE+ HASH OIN

    + TABLE ACCESS FULL T3 + HASH OIN5 3 TABLE ACCESS FULL T

    3 TABLE ACCESS FULL T+

  • 8/10/2019 Oracle Database 11g Execution Plans and Star Transformations

    55/77

    the system first hashes the T table )Operation ID, * into memory

    then it hashes