1 1/23/2016 sql tuning kyle hailey explain plans, erss, tcf, rtsm
DESCRIPTION
Content Copyright 2006 Kyle Hailey EXPLAIN PLAN – review ERSS – extended rows source statistics TCF - tuning by cardinality feedback RTSM – real time SQL monitoring related Object Statistics Plan Variation, Hints and query transformations Hints, Outlines, Profiles, SQL Plan BaselinesTRANSCRIPT
105/03/23
SQL Tuning
Kyle Haileyhttp://oraclemonitor.comhttp://perfvision.com/ftp/emea2010
Explain Plans, ERSS, TCF, RTSM
Bibliography•Troubleshooting Oracle Performance (TOP)
• Christian Antognini (TOP)
•Cost –Based Oracle Fundaments (CBOF)• Jonathan Lewis
•SQL Tuning• Dan Tow
•Blogs & Websites• Wolfgang Breitling - TCF
• Greg Rahn – TCF, dynamic_sampling
• Kerry Osborne – plan stability
• Randolf Geist – same plans different access
205/03/23
Content
Copyright 2006 Kyle Hailey
• EXPLAIN PLAN – review • ERSS – extended rows source statistics• TCF - tuning by cardinality feedback• RTSM – real time SQL monitoringrelated• Object Statistics• Plan Variation,• Hints and query transformations• Hints, Outlines, Profiles, SQL Plan Baselines
Explain Plan Summary
10g* use
dbms_xplan.display_cursor()
Real executed plan V$sql_plan is source Other methods deceptive**
* Trick for 9i available** Except tracing (starting v7.? )
Explain Plan
Copyright 2006 Kyle Hailey
1. EXPLAIN PLAN (v6?)
• Calculated • Ignores bind variables• No execution • dbms_xplan.display()
2.SQL_TRACE (v7?)
•Reliable, trace, force parse and use tkprof•Tkprof can hide/rollup real exec path
3.AUTOTRACE (v8.1.7)
•Uses Explain Plan4.V$SQL_PLAN(_statistics)_all)) (v9iR2)
• Actual executed plan (when set)• 9i trick with dbms_xplan.display • 10g use dbms_xplan.display_cursor
Estimated
Actual+StatsEasy
Estimated
Actualclunky
Runs query
Fast
Explain Plan
•Set UpSQL> @?/rdbms/admin/utlxplan.sqlSQL> Create public synonym plan_table for plan_table; SQL> Grant all on plan_table to public ;
•Using: SQL> delete from plan_table; SQL> EXPLAIN PLAN for [your query]* SQL> @?/rdbms/admin/utlxpls.sql
NOTE “?” = $ORACLE_HOME in sqlplus
* @?/rdbms/admin/utlxplp.sql - Parallel query explain
Explain Plan 8iSQL> EXPLAIN PLAN FOR 2 SELECT * 3 FROM emp e, dept d 4 WHERE e.deptno = d.deptno 5 AND e.ename = 'SMITH';Explained.
SQL> @$ORACLE_HOME/rdbms/admin/utlxpls.sql
Plan Table--------------------------------------------------------------------------------| Operation | Name | Rows | Bytes| Cost | Pstart| Pstop |--------------------------------------------------------------------------------| SELECT STATEMENT | | | | | | || NESTED LOOPS | | | | | | || TABLE ACCESS FULL |EMP | | | | | || TABLE ACCESS BY INDEX RO|DEPT | | | | | || INDEX UNIQUE SCAN |PK_DEPT | | | | | |--------------------------------------------------------------------------------8 rows selected.
• Estimated • No Execution Stats
Explain Plan 9i , 10g9.2.0 (uses dbms_xplan.display)----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | || 1 | NESTED LOOPS | | | | ||* 2 | TABLE ACCESS FULL | EMP | | | || 3 | TABLE ACCESS BY INDEX ROWID| DEPT | | | ||* 4 | INDEX UNIQUE SCAN | PK_DEPT | | | |----------------------------------------------------------------------------
2 - filter("E"."ENAME"='SMITH') 4 - access("E"."DEPTNO"="D"."DEPTNO")
10.2.0 (uses dbms_xplan.display) -------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time -------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 117 | 3 (0)| 00:00:01 | 1 | NESTED LOOPS | | 1 | 117 | 3 (0)| 00:00:01 |* 2 | TABLE ACCESS FULL | EMP | 1 | 87 | 2 (0)| 00:00:01 | 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 30 | 1 (0)| 00:00:01 |* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01-------------------------------------------------------------------------------------------
2 - filter("E"."ENAME"='SMITH') 4 - access("E"."DEPTNO"="D"."DEPTNO")
Predicate Filters
Estimations
Empty
SQL Trace
alter session set max_dump_file_size=unlimited;
alter session set timed_statistics = true;
alter session set statistics_level = all ;
-- 9i alter session set "_rowsource_execution_statistics" = true;
alter session set tracefile_identifier = XXX_TRACE_XXX ;
alter session set sql_trace = true ;
-- alter session set events ‘10046 trace name context forever, level 12';From
http://tonguc.wordpress.com/2006/12/30/introduction-to-oracle-trace-utulity-and-understanding-the-fundamental-performance-equation/
Tracefile Analyzer
http://www.ubtools.com/ubtools/products/itrprof/itrprof_user_manual.html
http://www.oracledba.ru/orasrp/
Still have to make session hard parseTkprof can hide multiple plans for same statement
SQL Trace
Copyright 2006 Kyle Hailey
Sql_trace=true, then tkprof8.1.7Rows Row Source Operation------- --------------------------------------------------- 1 NESTED LOOPS 1 TABLE ACCESS FULL EMP 1 TABLE ACCESS BY INDEX ROWID DEPT 1 INDEX UNIQUE SCAN (object id 276222)
9.2.0 Rows Row Source Operation------- --------------------------------------------------- 1 NESTED LOOPS 1 TABLE ACCESS FULL EMP 1 TABLE ACCESS BY INDEX ROWID DEPT 1 INDEX UNIQUE SCAN PK_DEPT (object id 675001)
10.2.0Rows Row Source Operation------- --------------------------------------------------- 1 NESTED LOOPS (cr=6 pr=0 pw=0 time=100 us) 1 TABLE ACCESS FULL EMP (cr=4 pr=0 pw=0 time=69 us) 1 TABLE ACCESS BY INDEX ROWID DEPT (cr=2 pr=0 pw=0 time=32 us) 1 INDEX UNIQUE SCAN PK_DEPT (cr=1 pr=0 pw=0 time=17 us)(object id 156597)
With extended rows source statistics
Jonathan Lewis : 9.2.0.2 through to 9.2.0.5, enabling sql_trace gave you some row source execution statistics in the STAT# lines of trace files. This feature disappeared in 9.2.0.6 because of the overheads, re-appearing in 10g when the parameter to set a sampling frequency was introduced.
autotrace
Copyright 2006 Kyle Hailey
•Examplesset autotrace offset autotrace on -- explain and statistics
set autotrace traceonly -- don’t print query results
set autotrace on explainset autotrace on statistics
set autotrace traceonly explainset autotrace traceonly statistics
•Requirements@?/rdbms/admin/utlxplan.sql@?/sqlplus/admin/plustrce.sql
-- run as system creates plustracegrant plustrace to [user];
autotrace 8i 9i 10g
Copyright 2006 Kyle Hailey
8.1.7 0 SELECT STATEMENT Optimizer=CHOOSE 1 0 NESTED LOOPS 2 1 TABLE ACCESS (FULL) OF 'EMP' 3 1 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' 4 3 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)
9.2.0Execution Plan---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=1 Bytes=57) 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=1 Bytes=20) 2 1 NESTED LOOPS (Cost=3 Card=1 Bytes=57) 3 2 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=1 Bytes=37) 4 2 INDEX (RANGE SCAN) OF 'PK_DEPT' (NON-UNIQUE)
10.2.0 (starts using dbms_xplan )----------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 117 | 3 (0)| 00:00:01 || 1 | NESTED LOOPS | | 1 | 117 | 3 (0)| 00:00:01 ||* 2 | TABLE ACCESS FULL | EMP | 1 | 87 | 2 (0)| 00:00:01 || 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 30 | 1 (0)| 00:00:01 ||* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |----------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("E"."ENAME"='SMITH') 4 - access("E"."DEPTNO"="D"."DEPTNO")
Wow, look at these statistics, looks great huh?
Estimates
Estimates
Autotrace subtleties set autotrace onselect id from t1 where n1 > :m_n1;
----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU) | Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 50 | 350 | 1 (0)|00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| T1 | 50 | 350 | 1 (0)|00:00:01 ||* 2 | INDEX RANGE SCAN | T1_I1| 21 | | 1 (0)|00:00:01|---------------------------------------------------------------------------------
select id from t1 where n1 > :m_n1; select * from table (dbms_xplan.display_cursor());
------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 5 (100)| ||* 1 | TABLE ACCESS FULL| T1 | 1000 | 7000 | 5 (0)| 00:00:01 |-------------------------------------------------------------------------------
•Autotrace uses Explain Plan command• Autotrace doesn’t uses bind variable peeking
Same QueryDifferent Plan
9i dbms_xplan.display()
new in 9i
• dbms_xplan.display()– simply old explain plan formatting
• V$SQL_PLAN– exciting ! live execution plans
9i dbms_xplan.display()SQL> EXPLAIN PLAN FOR SELECT * FROM emp e, dept d WHERE e.deptno = d.deptno AND e.ename = 'SMITH';Explained.SQL> set linesize 132SQL> SELECT * FROM TABLE(dbms_xplan.display);PLAN_TABLE_OUTPUT----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | || 1 | NESTED LOOPS | | | | ||* 2 | TABLE ACCESS FULL | EMP | | | || 3 | TABLE ACCESS BY INDEX ROWID| DEPT | | | ||* 4 | INDEX UNIQUE SCAN | PK_DEPT | | | |----------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------
2 - filter("E"."ENAME"='SMITH') 4 - access("E"."DEPTNO"="D"."DEPTNO")20 rows selected.
Predicate filters
9i v$sql_plan directReal plan !col operation for a35SELECT LPAD(' ',depth)|| P.OPERATION||'_'|| P.OPTIONS||' '|| P.OBJECT_NAME operationFROM V$SQL_PLAN PWHERE p.hash_value=&hash_valueorder by child_number, ID/
OPERATION-----------------------------------SELECT STATEMENT_ NESTED LOOPS_ TABLE ACCESS_FULL EMP TABLE ACCESS_BY INDEX ROWID DEPT INDEX_UNIQUE SCAN PK_DEPT
9i v$sql_plan via dbms_xplan
create or replace view liveplan as select sp.*, to_char(hash_value)|| '_' ||to_char(child_number) statement_id, sysdate timestamp
from v$sql_plan sp;
Idea from James Morle :
select * from table(dbms_xplan.DISPLAY('LIVEPLAN','1792773701_0 ' ));
PLAN_TABLE_OUTPUT----------------------------------------------------------------------------
---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 4 || 1 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 2 || 2 | NESTED LOOPS | | 1 | 57 | 4 ||* 3 | TABLE ACCESS FULL | EMP | 1 | 37 | 2 ||* 4 | INDEX RANGE SCAN | PK_DEPT | 1 | | 1 |---------------------------------------------------------------------------
9i v$sql_plan via dbms_xplanHow do you get the hash_value of last execution?
Session 1Select sid from v$mystat where rownum=1;21
SELECT *FROM emp e, dept dWHERE e.deptno = d.deptnoAND e.ename = 'SMITH';
select * from table(dbms_xplan.DISPLAY('LIVEPLAN','3760741946_0' ));
Session 2
Select sql_hash_value from v$session where sid=21;3760741946
Can’t get child_number in 9i but can in 10g
Select prev_hash_value from v$session where sid = (Select sid from v$mystat where rownum=1);
Select prev_sql_id from v$session where sid = (Select sid from v$mystat where rownum=1);10g
9i
Yes
NoWorkaround
10g dbms_xplan.display_cursor()
10g introduces
• dbms_xplan.display_cursor()
Grants (as sys)grant select on v_$session to scott;grant select on v_$SQL_PLAN_STATISTICS_ALL to scott;grant select on v_$sql to scott;grant select on v_$sql_plan to scott;
Options dbms_xplan.display_cursor(); -- last executed cursor
dbms_xplan.display_cursor(‘sql_id’, child_number, ‘options’)
Real plan!Easy FormattingEasy access
10g dbms_xplan.display_cursor()
select count(*) from scott.dept where deptno=10;select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------SQL_ID 46upz85274d4g, child number 0-------------------------------------select count(*) from scott.dept where deptno=10
Plan hash value: 2236899148
------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 1 (100)| || 1 | SORT AGGREGATE | | 1 | 3 | | ||* 2 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 3 | 1 (0)| 00:00:01 |------------------------------------------------------------------------------
Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("DEPTNO"=10)
Real Plan
Estimations
10g dbms_xplan.display_cursor()
Format
select * from table(dbms_xplan.display_cursor(‘sql_id’,child_number,’arguments’));
Examples:
select * from table( dbms_xplan.display_cursor());
select * from table ( dbms_xplan.display_cursor (null,null, 'ADVANCED'));
select * from table ( dbms_xplan.display_cursor ('811a0zb26fw0x',0, 'BASIC'));
10g dbms_xplan.display_cursor()
display_cursor(‘sql_id’,child_number,’arguments’));
predicate info note
Query Block Name
Column projection info outlines
binds variables
BASIC null X XTYPICAL X XSERIAL X XALL X X X XADVANCED X X X X X XPREDICATES XNOTE XALIAS XPROJECTION XOUTLINES XPEEK_BINDS X
+ include- filter out
10g dbms_xplan.display_cursorvariable x varchar2(10);exec :x := ‘X’;select * from dual where dummy=:x;select * from table ( dbms_xplan.display_cursor (null,null,
'ADVANCED -OUTLINE -NOTE'));
2305/03/23
SQL_ID 3gfhcbpynm1wj, child number 0
select * from dual where dummy=:x
Plan hash value: 272002086 .| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time || 0 | SELECT STATEMENT | | | | 2 (100)| ||* 1 | TABLE ACCESS FULL| DUAL | 1 | 2 | 2 (0)| 00:00:01 |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$1 / DUAL@SEL$1
Peeked Binds (identified by position):1 - :DUM (VARCHAR2(30), CSID=178): 'X'
Predicate Information (identified by operation id):
1 - filter("DUMMY"=:X)
Column Projection Information (identified by operation id):
1 - "DUMMY"[VARCHAR2,1]
ALIASPEEK_BINDSPREDICATESPROJECTIONOUTLINESNOTE
10g dbms_xplan.display_cursor
Outline Data------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('10.2.0.1') ALL_ROWS OUTLINE_LEAF(@"SEL$1") FULL(@"SEL$1" "DUAL"@"SEL$1") END_OUTLINE_DATA */
Note-------------- Warning: basic plan statistics not available. These are only collected when: * hint 'gather_plan_statistics' is used for the statement or * parameter 'statistics_level' is set to 'ALL', at session or system level
2405/03/23
select * from table ( dbms_xplan.display_cursor (null,null, 'BASIC +OUTLINE +NOTE'));
Usage of: Outlines Profiles SQL Baselines
ALIASPEEK_BINDSPREDICATESPROJECTIONOUTLINESNOTE
10g dbms_xplan.display_cursor
2505/03/23
predicate Information (identified by operation id):--------------------------------------------------
2 - filter(("B"."EFFDT"= AND "B"."EFFSEQ"=)) 3 - access("E"."OFF_CYCLE"="A"."PAY_OFF_CYCLE_CAL" 5 - access("B"."COMPANY"="A"."COMPANY" AND "B"."PAYGROUP"="A"."PAYGROUP") 7 - access("A"."RUN_ID"='PD2' AND "A"."PAY_CONFIRM_RUN"='N') 8 - access("C"."EMPLID"="B"."EMPLID" AND "C"."EMPL_RCD#"="B"."EMPL_RCD#") 9 - access("D"."RETROPAY_SEQ_NO"="C"."RETROPAY_SEQ_NO") 11 - filter(("C"."RETROPAY_PRCS_FLAG"='C' AND "C"."RETROPAY_LOAD_SW"='Y'))
Access - uses structures (index, hash table etc)Filters – filters a return set
preferable to use access when possible because filter means working on the result set where as access determines how you get the data
10g dbms_xplan.display_cursor
• http://oracle-randolf.blogspot.com/2009/07/planhashvalue-how-equal-and-stable-are_26.html
look for diffs even though the sql_id and plan_hash are the same
2605/03/23
HASH_VALUE PLAN_HASH_VALUE CHILD_NUMBER THE_HASH ARE_H---------- --------------- ------------ ---------- ----- 138930541 3776869225 0 3686240584 166324347 1231101765 1 2381182910 166324347 1231101765 2 2381182910 174856728 4804429 0 1186229766 DIFF! 174856728 4804429 1 2540836549
Filter Predicate CPU cost------------------------------------------------------------ ------------TO_NUMBER("V1")=1 AND "N2"=18 AND "N1"=998 1,070,604
Filter Predicate CPU cost------------------------------------------------------------ ------------"N1"=998 AND "N2"=18 AND TO_NUMBER("V1")=1 762,786
Jonathan Lewis CBO p 22-25
Explain Plan Summary
2705/03/23
Version Method 8i or lower sql_trace 9i v$sql_plan10g dbms_xplan.display_cursor()11g “”
For accurate explain plans
ERSS
ERSS : Extended Row Source Statistics- An enhancement to the explain plan -
Starting in Oracle 9i statistics on Rows
CPUPhysical IOLogical IOElapsed time
became available on each step of the execution plan of an Oracle statement in
V$SQL_PLAN_STATISTICS_ALL
NOTE: only when collection is enabled
Real Statistics !!
ERSS across versions
•statistics_level=all•/*+ gather_plan_statistics */•sql_trace=true
•_rowsource_execution_statisticsset to true•Or sql_trace 9.2.0.2 to 9.2.0.5
9i ERSSERSS in
V$SQL_PLAN_STATISTICS(_ALL)
Enable
Warning: Can be expensive resource wise
Alter session set "_rowsource_execution_statistics"=true;
Or
Alter session set SQL_TRACE= true ; -- in 9.2.0.2 through to 9.2.0.5
9i – v$sql_planReal plancol operation for a25 SELECT LPAD(' ',depth)||P.OPERATION||'_'||P.OPTIONS||' '||P.OBJECT_NAME operation , p.last_output_rows nrows -- , p.starts starts , LAST_CR_BUFFER_GETS bgets , LAST_DISK_READS pread , LAST_DISK_WRITES pwrites , LAST_ELAPSED_TIME elapsed FROM V$SQL_PLAN_statistics_all P WHERE p.hash_value=&hash_value order by child_number/
OPERATION NROWS BGETS PREAD PWRITES ELAPSED------------------------- ---------- ---------- ---------- ---------- ----------HASH JOIN_ 1 7 0 0 924 TABLE ACCESS_FULL EMP 1 3 0 0 84 TABLE ACCESS_FULL DEPT 4 4 0 0 51
Microseconds
10g ERSS
ERSS – Extended Row Source Statistics
1. enable ERSS (any one of the following)
a) alter session set statistics_level=all;– Attention “_rowsource_execution_stats”=false disables it
b) Select /*+ gather_plan_statistics */ … from …;c) Alter session set “_rowsource_execution_stats”=true;d) setting sql_trace
2. Get Plan Execution Statisticsselect * from table(dbms_xplan.display_cursor(null,null, 'ALLSTATS LAST'));
10g ERSS
alter session set statistics_level=all;select count(*) from scott.dept where deptno=10;select * from table(dbms_xplan.display_cursor(null,null, 'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------SQL_ID ckq08mgb67bp2, child number 1------------------------------------- select count(*) from scott.dept where deptno=10Plan hash value: 2236899148
----------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |----------------------------------------------------------------------------------------| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.01 | 1 ||* 2 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 1 | 1 |00:00:00.01 | 1 |----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):---------------------------------------------------
2 - access("DEPTNO"=10)
Estimated rows
Actual rows
ERSS Pitfalls
•alter session set statistics_level=all;
• sets _rowsource_statistics_sampfreq =1;
sometimes more expensive, but more accurate
value on 9i with sql_trace=true
•Select /*+ gather_plan_statistics */
• sets _rowsource_statistics_sampfreq =128;
Less expensive less accurate
Timings can be incorrect
http://jonathanlewis.wordpress.com/2007/04/26/heisenberg/
ERSS summary
•Starting in 9i, Oracle records Row source statistics•Rows
•CPU
•IO
•Elapsed_time
•Have to enable with• Statistics_level = all (more accurate, more expensive, 10g +)
• /*+ gather_plan_statistics */ (less accurate, less expensive, 10g+)
• Sql_trace = true (expensive accurate v9, less expensive less accurate 10g)
3505/03/23
TCF : Intro
• When is a SQL execution plan optimal?• Only sure way: find one that is more efficient
• Better questions probably:• fast enough
• Depends on your users and application
• resource usage acceptable• Depends on your machine resources
• How would you check if you could
•=> TCF
3605/03/23
TCF•Tuning by Cardinality Feedback
• Compare Estimated Rows vs Actual Rows
• Find discrepancies
• Large discrepancies could indicate wrong plan
Wolfgang Breitling http://www.centrexcc.com/Tuning%20by%20Cardinality%20Feedback.pdf
Greg Rahnhttp://structureddata.org/2007/11/21/troubleshooting-bad-execution-plans
TCF Key StatsEstimated: (explain plan)
• CARDINALITY - rows per iteration
Actual: (ERSS)• STARTS – iterations• OUTPUT_ROWS - total rows
Thus these “should” be equal cardinality output_rows/starts
starts*cardinality output_rows
Estimated Rows Actual Rows
Why should you care? If you write your own scripts, otherwise use display_cursor
V$sql_plan_statistics
TCF dbms_xplan.display_cursor()
Arguments:
-ROWS-BYTES-COST
Estimates ActualName Rows Bytes TempSpc
cost(% CPU) Time E-Rows starts A-Rows Buffers Reads Writes A-Time 0Mem 1MEM
0/1/M
BASIC X null X X X X XTYPICAL X X X X X XSERIAL X X X X X XALL X X X X XADVANCED X X X X XMEMSTATS X X X X X X X XALLSTATS X X X X X X X X X X XRUNSTATS_LAST X X X X X X X XRUNSTATS_TOT X X X X X X X XIOSTATS X X X X X X X X
Cumulative
10g TCF dbms_xplan.display_cursor()
0Mem 1MEM 0/1/M Used-Mem Used-Tmp Max-Tmp MEMSTATS X X TOT LAST LAST TOTALLSTATS X X TOT LAST LAST TOT
By default statistics are total but can add “LAST”Example:
Other:TQ IN-OUT PQ Distrib pstart pstop Instance
PARTITION X X XPARALLEL X XREMOTE X
select * from table ( dbms_xplan.display_cursor (null,null,'MEMSTATS LAST'));
Memory:x 1K, ie 10K = 10M
10g TCF dbms_xplan.display_cursor()
4105/03/23
----------------------------------------------------------------------------------------------------------|Operation |Starts|E-Rows|A-Rows| A-Time |Buffers|Reads|Writes|OMem|1Mem |UsedMem|UsedTmp|-----------------------------------------------------------------------------------------------------------|HASH GROUP BY | 1 | 1 | 1 |0:04.13 | 8153 | 721 | 721 | | | | || FILTER | 1 | | 1909 |0:04.12 | 8153 | 721 | 721 | | | | || HASH JOIN | 1 | 406 | 3413 |0:03.95 | 4725 | 721 | 721 |855K| 855K|578K(0)| | | TABLE ACCESS FULL | 1 | 15 | 15 |0:00.01 | 7 | 0 | 0 | | | | | | HASH JOIN | 1 | 812 | 3413 |0:03.90 | 4718 | 721 | 721 |830K| 830K|131K(0)| | | TABLE ACCESS BY INDEX R| 1 | 5 | 1 |0:00.01 | 3 | 0 | 0 | | | | | | INDEX RANGE SCAN | 1 | 5 | 1 |0:00.01 | 2 | 0 | 0 | | | | | | HASH JOIN | 1 |28213 | 111K|0:03.12 | 4715 | 721 | 721 |815K|2165K|909K(1)| 6144 | | HASH JOIN | 1 |27456 | 115K|0:01.58 | 3206 | 0 | 0 |035K|1035K|830K(0)| | | TABLE ACCESS FULL | 1 |13679 |13679 |0:00.03 | 184 | 0 | 0 | | | | | | TABLE ACCESS FULL | 1 |27456 | 122K|0:00.37 | 3022 | 0 | 0 | | | | | | TABLE ACCESS FULL | 1 |40000 |40000 |0:00.12 | 1509 | 0 | 0 | | | | | | SORT AGGREGATE | 1831 | 1 | 1831 |0:00.07 | 1833 | 0 | 0 | | | | | | FIRST ROW | 1831 | 1 | 1617 |0:00.04 | 1833 | 0 | 0 | | | | | | INDEX RANGE SCAN (MIN/M| 1831 | 1 | 1617 |0:00.02 | 1833 | 0 | 0 | | | | | | SORT AGGREGATE | 1593 | 1 | 1593 |0:00.06 | 1595 | 0 | 0 | | | | | | FIRST ROW | 1593 | 1 | 1593 |0:00.04 | 1595 | 0 | 0 | | | | | | INDEX RANGE SCAN (MI| 1593 | 1 | 1593 |0:00.02 | 1595 | 0 | 0 | | | | | ----------------------------------------------------------------------------------------------------------
Cumulative
dbms_xplan.display_cursor(null,null,’ALLSTATS’)
TCF : Discrepancies
4205/03/23
TCF: Less Discrepancy
4305/03/23
Cumlative Statistics
Copyright 2006 Kyle Hailey
Customers try to find the slowest part of the query
Note, by default costs include cost of child steps. For graphical bar we subtract out the child steps, makes identifying the actual step that was most costly
TCF – skew reasonsPossible reasons for cardinality discrepancies
• Incorrect statistics• Data out of range issues
• Incorrect selectivity• Skew
• Histograms
• Data correlation • extended statistics,• profiles, outlines, sql plan baselines• dynamic_sampling=4• Cardinality hint
• Functions in predicate • extended statistics
• Incorrect algorithm in CBO (ie bug)
Joins are much harder to calculate than Filters
TCF CARDINALITY Hint
•/*+ dynamic_sampling(4) */
•Samples 32 blocks for all tables that have 2 or more columns referenced in the where clause
•/*+ dynamic_sampling(table 4) */ •Specific table in query
•Extended Statistics 11g (top p131)dbms_stats.create_extended_stats(ownname=> ‘owner’, tabname => ‘table’, extension => ‘(field1,field2)’);dbms_stats.create_extended_stats(ownname=> ‘owner’, tabname => ‘table’, extension => ‘(upper((pad))’);
10g TCF summary
•Compare actual rows to estimated rows
•Use dbms_xplan.display_cursor • Argument : RUNSTATS_TOT or RUNSTATS_LAST
• Memory and Sorting: ALLSTATS or ALLSTATS LAST
•Enable ERSS collection with• Statistics_level = all
• /*+ gather_plan_statistics */
• sql_trace=true
4705/03/23
On 9i use v$sql_plan_statistics_all directly
Real Time SQL Monitoringhttp://askdba.org/weblog/2009/09/11gr2-monitoring-real-time-sql-plan-execution-from-oracle-enterprise-manager/
ASH
4905/03/23
10.1 10.2.0 10.2.0.3 11.1.0 11.2.0 SAMPLE_ID SAMPLE_ID SAMPLE_ID SAMPLE_ID SAMPLE_IDSAMPLE_TIME SAMPLE_TIME SAMPLE_TIME SAMPLE_TIME SAMPLE_TIME
IS_AWR_SAMPLESESSION_ID SESSION_ID SESSION_ID SESSION_ID SESSION_IDSESSION_SERIAL# SESSION_SERIAL# SESSION_SERIAL# SESSION_SERIAL# SESSION_SERIAL#SESSION_TYPE SESSION_TYPE SESSION_TYPE SESSION_TYPE SESSION_TYPE
FLAGS FLAGSUSER_ID USER_ID USER_ID USER_ID USER_IDSQL_ID SQL_ID SQL_ID SQL_ID SQL_ID
IS_SQLID_CURRENTSQL_CHILD_NUMBER SQL_CHILD_NUMBER SQL_CHILD_NUMBER SQL_CHILD_NUMBER SQL_CHILD_NUMBERSQL_OPCODE SQL_OPCODE SQL_OPCODE SQL_OPCODE SQL_OPCODE
SQL_OPNAMEFORCE_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE
TOP_LEVEL_SQL_ID TOP_LEVEL_SQL_IDTOP_LEVEL_SQL_OPCODE TOP_LEVEL_SQL_OPCODE
SQL_PLAN_HASH_VALUE SQL_PLAN_HASH_VALUE SQL_PLAN_HASH_VALUE SQL_PLAN_HASH_VALUE SQL_PLAN_HASH_VALUESQL_PLAN_LINE_ID SQL_PLAN_LINE_IDSQL_PLAN_OPERATION SQL_PLAN_OPERATIONSQL_PLAN_OPTIONS SQL_PLAN_OPTIONSSQL_EXEC_ID SQL_EXEC_IDSQL_EXEC_START SQL_EXEC_START
PLSQL_ENTRY_OBJECT_ID PLSQL_ENTRY_OBJECT_ID PLSQL_ENTRY_OBJECT_IDPLSQL_ENTRY_SUBPROGRAM_ID PLSQL_ENTRY_SUBPROGRAM_ID PLSQL_ENTRY_SUBPROGRAM_IDPLSQL_OBJECT_ID PLSQL_OBJECT_ID PLSQL_OBJECT_IDPLSQL_SUBPROGRAM_ID PLSQL_SUBPROGRAM_ID PLSQL_SUBPROGRAM_ID
QC_SESSION_ID QC_INSTANCE_ID QC_INSTANCE_ID QC_INSTANCE_ID QC_INSTANCE_IDQC_INSTANCE_ID QC_SESSION_ID QC_SESSION_ID QC_SESSION_ID QC_SESSION_ID
QC_SESSION_SERIAL# QC_SESSION_SERIAL#EVENT EVENT EVENT EVENT EVENTEVENT_ID EVENT_ID EVENT_ID EVENT_ID EVENT_IDEVENT# EVENT# EVENT# EVENT# EVENT#SEQ# SEQ# SEQ# SEQ# SEQ#
P1TEXT P1TEXT P1TEXT P1TEXTP1 P1 P1 P1 P1
P2TEXT P2TEXT P2TEXT P2TEXTP2 P2 P2 P2 P2
P3TEXT P3TEXT P3TEXT P3TEXTP3 P3 P3 P3 P3
WAIT_CLASS WAIT_CLASS WAIT_CLASS WAIT_CLASSWAIT_CLASS_ID WAIT_CLASS_ID WAIT_CLASS_ID WAIT_CLASS_ID
•SQL_PLAN_LINE_ID
•SQL_PLAN_OPERATION
•SQL_PLAN_OPTIONS
•SQL_EXEC_ID
•SQL_EXEC_START
5005/03/23
Object Statistics
•Collected automatically in 10g
•Backups kept for 30 days
•Can revert to older
•Can compare current to another day
•Can collect without posting
5105/03/23
Object Stats Improvement
•dbms_stats • LOCK : lock_schema_stats (table)
• UNLOCK: unlock_schema_stats (table)
• RESTORE: restore_schema_stats (database ,dictionary ,table, system)
dbms_stats.restore_schema_stats(ownname => ‘SCOTT’,
as_of_timestamp => systimestamp – 1);
dbms_stats.lock_schema_stats(ownname => ‘SCOTT’)
Copyright 2006 Kyle Hailey
New 10g
Object Stats
•Object statistics are gathered automatically• Job window runs in midnight-8am , M-F, all day Sat Sun
•“No_invalidate” defaults to “auto_invalidate”• False (9i) invalidates cursors immediately
• True – leaves cursors as is
• Auto_invalidate – invalidates them over time , avoids invalidation spike
Copyright 2006 Kyle Hailey
New 10g
When are Stats “stale”
•Table modification stats are collected by default (monitoring based on statistics level all or typical, off when basic)
(was monitoring option on table definition in 9i)
•Considered stale when 10% of rows have been modified (not changeable in 10g but configurable in 11g)
Copyright 2006 Kyle Hailey
SELECT inserts, updates, deletes, truncatedFROM all_tab_modificationsWHERE table_name = 'ORDERS';
INSERTS UPDATES DELETES TRUNCATED---------- ---------- ---------- ---------- 387 33 166 NO
Histograms• Histograms gathered by default
• Method_opt = all columns size auto
if skew and if referenced in SQL where clause
(was “all columns size 1” in 9i )
• Method_opt sets
• Whether to gather histograms• Null means don’t gather
• Which columns to gather histograms for• All columns
• Size skewonly – only gather for columns with skew
• Size auto – only gather for columns with skew and who have been used in the where* clause of an SQL statement
• Size repeat – only gather for cols that already have histograms
• Maximum # of buckets for the histogram
• Size 1-254 : number of buckets
Copyright 2006 Kyle Hailey
New 10g
Columns used in where clause
Copyright 2006 Kyle Hailey
SELECT c.name, cu.timestamp,cu.equality_preds AS equality, cu.equijoin_preds AS equijoin,cu.nonequijoin_preds AS noneequijoin, cu.range_preds AS range,cu.like_preds AS "LIKE", cu.null_preds AS "NULL"FROM sys.col$ c, sys.col_usage$ cu, sys.obj$ o, sys.user$ uWHERE c.obj# = cu.obj# (+)AND c.intcol# = cu.intcol# (+)AND c.obj# = o.obj#AND o.owner# = u.user# AND u.name = user ORDER BY c.col#;
NAME TIMESTAMP EQUALITY EQUIJOIN NONEEQUIJOIN RANGE LIKE NULL--------------- --------- -------- -------- ------------ ----- ---- ----OID 28-FEB-10 1 71 0 0 0 0SCHEMA 28-FEB-10 110 120 0 0 0 0RETROPAY_SEQ_NO 25-FEB-10 0 12 0 0 0 0EMPLID 25-FEB-10 12 12 0 0 0 0RETROPAY_PGM_ID 25-FEB-10 0 12 0 0 0 0RETROPAY_SEQ_NO 25-FEB-10 0 12 0 0 0 0
Diff Stats
•10.2.0.4
Copyright 2006 Kyle Hailey
dbms_stats.diff_table_stats_in_history( ownname => 'SCOTT', tabname => 'SOE', time1 => systimestamp - 14, time2 => NULL, pctthreshold => 10);
dbms_stats.alter_stats_history_retention(retention => 14) (by default 31 days)
Monitoring Stats
Copyright 2006 Kyle Hailey
col operation for a28col start_time for a38col duration for a15SELECT operation, start_time, (end_time-start_time) DAY(1) TO SECOND(0) AS duration FROM dba_optstat_operations ORDER BY start_time DESC;
OPERATION START_TIME DURATION---------------------------- -------------------------------------- ------------gather_database_stats(auto) 27-FEB-10 06.36.01.078000 PM -08:00 +0 00:01:16gather_database_stats(auto) 24-FEB-10 10.00.04.546000 PM -08:00 +0 00:02:35gather_database_stats(auto) 22-FEB-10 10.00.03.406000 PM -08:00 +0 00:10:44gather_database_stats(auto) 20-FEB-10 11.34.43.640000 PM -08:00 +0 00:01:24gather_database_stats(auto) 18-FEB-10 10.00.03.015000 PM -08:00 +0 00:01:09gather_schema_stats 18-FEB-10 02.28.47.000000 PM -08:00 +0 00:00:06
System Statistics
•System statistics collected by default
•Thus CPU cost model used by optimizer• 9i by default used the I/O cost model
•Multi-block cost was equal single block read
• 10g to test I/O cost model can use hint (maybe after upgrade)
• /*+ no_cpu_costing */
•Or set at system level with “_optimizer_cost_model=io”
Copyright 2006 Kyle Hailey
New 10g
System Statistics
•Two kinds1. Noworkload statistics
• Collected by default with a synthetic benchmark
2. Workload statistics
• Collected manually during normal application workload
•Tracks:• CPU speed : CPUSPEEDNW
• IO seek time : IOSKEEKTIM
• IO transfer speed : IOTFSPEED
• Single block read time: SREADTIM
• Multi-block read time: MREADTIM
• Multi-block read count : MBRC
Copyright 2006 Kyle Hailey
System Statistics
Copyright 2006 Kyle Hailey
SQL> select * from sys.aux_stats$;
SNAME PNAME PVAL1 PVAL2--------------- --------------- ---------- -----------------SYSSTATS_INFO STATUS COMPLETEDSYSSTATS_INFO DSTART 08-30-2005 15:04SYSSTATS_INFO DSTOP 08-30-2005 15:04SYSSTATS_INFO FLAGS 1SYSSTATS_MAIN CPUSPEEDNW 484.974958SYSSTATS_MAIN IOSEEKTIM 10SYSSTATS_MAIN IOTFRSPEED 4096SYSSTATS_MAIN SREADTIMSYSSTATS_MAIN MREADTIMSYSSTATS_MAIN CPUSPEEDSYSSTATS_MAIN MBRCSYSSTATS_MAIN MAXTHRSYSSTATS_MAIN SLAVETHR
Manually:dbms_stats.gather_system_stats(gathering_mode => 'noworkload')
System Statistics
Copyright 2006 Kyle Hailey
dbms_stats.gather_system_stats(gathering_mode => 'start')dbms_stats.gather_system_stats(gathering_mode => ‘stop')orDbms_stats.gather_system_stats(gathering_mode => 'interval', interval => 30);
SQL> select * from sys.aux_stats$;
SNAME PNAME PVAL1 PVAL2--------------- --------------- ---------- -----------------SYSSTATS_INFO STATUS COMPLETEDSYSSTATS_INFO DSTART 02-26-2010 22:30SYSSTATS_INFO DSTOP 02-26-2010 22:34SYSSTATS_INFO FLAGS 1SYSSTATS_MAIN CPUSPEEDNW 484.974958SYSSTATS_MAIN IOSEEKTIM 10SYSSTATS_MAIN IOTFRSPEED 4096SYSSTATS_MAIN SREADTIM 8.792SYSSTATS_MAIN MREADTIM 9.376SYSSTATS_MAIN CPUSPEED 614SYSSTATS_MAIN MBRC 12
Summary
6305/03/23
Vers Method Set Extended Stats Read Extended Stats
8i sql_trace N/A
9i v$sql_plan sql_trace=true v$sql_plan_statistics
10g dbms_xplan.display_cursor()
cheap less accurate:• /*+ gather_plan_statistics */• Sql_trace=true;Expensive, more accurate• Statistics_level=all
dbms_xplan.display_cursor(null,null,’allstats last’)
For accurate explain plans
Object Statistics in 10g•Collected automatically in•Backups kept for 30 days•Can revert to older•Can compare current to another day•Can collect without posting
System Statistics 10g•Dbms_stats.gather_system_stats(gathering_mode => 'interval', interval => 30);