www.DanHotka.com
Dan HotkaAuthor – Instructor
Oracle ACE Director
www.DanHotka.com
www.DanHotka.com, LLC(c) www.danhotka.com LLC.
Any reproduction or copying of this manual without the express written consent of www.danhotka.com LLC is expressly prohibited.
Limitation on Warranty. THERE ARE NO WARRANTIES, EXPRESS OR IMPLIED, WITH RESPECT THERETO, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. PURCHASER SHALL BE SOLELY RESPONSIBLE FOR THE SELECTION, USE, EFFICIENCY AND SUITABILITY OF USE OF INFORMATION CONTAINED HEREIN TO ANY PARTICULAR APPLICATION OR PROBLEM. WWW.DANHOTKA.COM LLC SHALL HAVE NO LIABILITY THEREFOR.
Limitation of Liability. IN NO EVENT SHALL WWW.DANHOTKA.COM LLC BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, WITHOUT LIMITATION, ANY DAMAGES RELATINGTO LOSS OF DATA, AND ANY INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS, ARISING OUT OF OR IN ANY WAY RELATED TO YOUR USE OF THE INFORMATION CONTAINED IN THIS MANUAL. IN THE EVENT THAT THE FORGOING IS HELD UNINFORCABLE THE PARTIES AGREE THAT WWW.DANHOTKA.COM LLC'S LIABILITY TO YOU HEREUNDER, IF ANY, SHALL IN NO EVENT EXCEED THE FEE PAID BY THE INJURED PARTY FOR THE MANUAL TO WWW.DANHOTKA.COM. LLC.
Dan HotkaAuthor/Instructor/Oracle [email protected] 279 3361
Last Updated: 4/2006
www.DanHotka.com
Dan is a Training Consultant
Dan Hotka– Oracle Authored Expert
• 31 Years in IT – 26 years working with Oracle• 11 books – hundreds of articles
www.DanHotka.com– Training at your Desk!
• Web-based training• Morning lectures with hands-on lab exercises• Almost like being there!
– Flat Fee Training…1 Course Fee Price for up to 15 Attendees!
• Price includes my portable computer lab! • On-site or over the Web!
Training Courses Include:• Oracle Advanced PL/SQL• Oracle SQL Tuning • TOAD and Unix/Linux• Intro to Oracle, Intro to PL/SQL• Oracle Discoverer/Oracle
AnalyticsRegister for my quarterly Newsletter
• Full of tips and techniques, downloads, book reviews, etc
www.DanHotka.com
Training at your Desk!
Web-based Training at your Desk!– April 4 - 8 Advanced PL/SQL– April 18 – 21 Intro to PL/SQL– May 2 - 6 SQL Performance Tuning– June 6 - 10 Advanced PL/SQL
www.DanHotka.com
Additional Reading
Oracle SQL Tuning: A Close Look at Explain Plans– By Dan Hotka;– www.Amazon.com
www.DanHotka.com
New Book
ISBN: 978-0321649102
www.DanHotka.com
Introductions
Please tell me a little about your background– Show of Hands:
• DBA’s• Developers• Working with: Oracle11, 10, 9, 8, 7 – prior to 7?• Years of Oracle experience < 1, 1-3, 4-8, 8+• No tuning experience• Some tuning experience• Extensive tuning experience
www.DanHotka.com
Agenda
Explain Plan Tools– Xplan.display– JS Tuner
Understanding Explain Plans– Reading the Explain Plan– What does it mean– Parallel Explain Plan Review
www.DanHotka.com
Additional Information
Ask me for:– White Papers– Free Tools– Scripts
www.DanHotka.com
Explain Plan Tools
www.DanHotka.com
Explain Plan Tools
DBMS_XPLAN.DISPLAY– Available with Oracle 8.1.7+– Comes with Database– Used via SQL*Plus
JS Tuner– Available for Oracle8+– Download from www.DanHotka.com– Executable Jar File
www.DanHotka.com
Explain Plan Tools
PLAN_TABLE– Use <oracle home>/rdbms/admin/utlxplan.sql for current
plan_table– Use the ‘explain plan for’ syntax to populate this table– Use tools to populate this table (TOAD, SQL Developer)– Use SHOW_PLAN.sql to display contents
• Available on my website
www.DanHotka.com
Using the Tools
All tools use the PLAN_TABLE– <oracle_home>/rdbms/admin
/utlxplan.sql
www.DanHotka.com
Explain Plan Tools
www.DanHotka.com
Explain Plan Tools
www.DanHotka.com
Explain Plan Tools
JSTuner– Incorporates SQL Scripts with enhanced Trace– Index Info– Includes index statistics– Enhanced Explain Plan
• Works with V8+• Works with rule-based optimizer!
Available from www.DanHotka.com
www.DanHotka.com
www.DanHotka.com
Lets do the Book Draw
www.DanHotka.com
Understanding the Explain Plan
www.DanHotka.com
Reading Explain Plans
Oracle:– Parses the SQL
• Checks SQL syntax• Checks for available indexes/stats• Reads from bottom to the top
– Arrives at an Execution Plan• Decides how it will access the tables and indexes
– Executes the SQL• Oracle9/10 – peaks once at bind variables• Oracle11 – tracks explain plans with various bind vars
– Called ‘Bind Aware’• Runs the Execution plan
www.DanHotka.com
Reading Explain Plans
Explain Plan– Visualizes the execution plan– Uses the Plan_Table
• Which varies slightly from release to release• Utlxplan.sql• <Oracle Home>/RDBMS/ADMIN
– Used heavily to tune SQL
www.DanHotka.com
Reading Explain Plans
How does it work?– Reads from bottom up– Syntax checks/tracks available indexes– Regular queries - generally does the table joins first
• Utilizes Where clause predicates– Parallel queries – generally does partition pruning first– Sometimes accesses tables again– Oracle does rewrites
• Views to joins, sub queries to joins, sub queries to inline views, etc
www.DanHotka.com
Reading Explain Plans
How does it work?– RBO - follows rules for index selection
• Arrives at an execution plan in single pass– CBO - tries a variety of combinations
• Combinations called ‘permutations’– Generally # perms = # tables * # where predicates
• Regular queries:– Uses lowest cost method– *** Queries in this course are regular queries unless otherwise
noted• Parallel queries (covered in my class)
– Uses fastest access method
www.DanHotka.com
Reading Explain Plans
CBO Permutations– 9i+ Query Transformations
• Complex View Merging– Converts views to joins
• Subquery Unesting– Converts subqueries to inline views
• Join Predicate Push Down– Moves where clause predicates into subquery
- Hints for each item above- No_Query_Transformation prevents this behavior - Discussed later in this chapter
www.DanHotka.com
Single Table SQL
Single Table SQL
www.DanHotka.com
Single Table SQL
Single Table SQL Answers
www.DanHotka.com
Multiple Table SQL
Multiple Table SQL
www.DanHotka.com
Multiple Table SQL Answers
Multiple Table SQL Answers
www.DanHotka.com
Reading Explain Plans
www.DanHotka.com
Reading Explain Plans
www.DanHotka.com
Reading Explain Plans
Access Rule DescriptionAND-EQUAL Index values will be used to join rows.CONCATENATION SQL statement UNION command.FILTER FILTERs apply ‘other criteria’ in the query to further qualify the matching rows. The ‘other criteria’ include correlated sub queries, and HAVING clause.TABLE ACCESS When not associated with a join condition, they act like Filter…processing additional Where Clause predicates.VIEW OF Processed SQL from a view.FOR UPDATE SQL statement clause ‘for update of’ placed row level locks on affected rows.INDEX (UNIQUE) SQL statement utilized a unique index to search for a specific value.INDEX (RANGE SCAN) SQL statement contains a non-equality or BETWEEN condition.
www.DanHotka.com
Reading Explain Plans
Access Rule DescriptionINLIST ITERATOR SQL statement has an IN clause, or, values being treated as an IN clauseHASH JOIN SQL statement initiated a hash-join operation.MERGE JOIN SQL statement references two or more tables, sorting the two result sets being joined over the join columns and then merging the results via the join columns.MERGE JOIN (CARTESIAN) SQL statement references two or more tables but without a joining column (generally not a good thing)NESTED LOOPS This operation is one form of joining tables, as opposed to a merge join. One row is retrieved from the row source identified by the first child operation, and then joined to all matching rows in the other table, identified in the second child operation.NONUNIQUE INDEX (RANGE SCAN) The RANGE SCAN option indicates that ORACLE expects to return multiple matches (ROWIDs) from the index search
www.DanHotka.com
Reading Explain Plans
Access Rule DescriptionBITMAP CONVERSION Bitmap Index being mergedBITMAP MERGE Generally used with Bitmap Range ScanBITMAP MINUS Bitmap Index handling a not = condition.BITMAP INDEX SINGLE VALUE Bitmap index being used for single value lookup.BITMAP INDEX (RANGE SCAN) Bitmap index being used for multiple value lookup.
www.DanHotka.com
Reading Explain Plans
Access Rule DescriptionPARTITION (CONCATTENATED) SQL statement will access a partitioned object and merge the retrieved rows from the accessed partitions.PARTITION (SINGLE) SQL statement will access a single partition.PARTITION (EMPTY) The SQL statement makes reference to an empty partition.SORT (ORDER BY) SQL statement contains an ORDER BY SORT (AGGREGATE) SQL statement initiated a sort to resolve a MIN or MAX type function.SORT (GROUP BY) SQL statement contains a GROUP BY TABLE ACCESS (FULL) All rows are retrieved from the table without using an index.TABLE ACCESS (BY ROWID) A row was retrieved from a table based on the ROWID of the row.
www.DanHotka.com
Reading Explain Plans
Index Scans– Unique Scan
• via root -> branch -> leaf for a single row access– Range Scan
• via root -> branch -> leaf for first row, then leaf to leaf for remaining rows
– Full Scan (Index-Full)• Scans leaf blocks using single-block access
– Fast Full Scan (Index-FFS)• scans all index leaf blocks using multi-block
read– Index Skip Scan (Index-SS)
• Useful for multi-column indexes, accessing only 2nd column and first column has low cardinality
www.DanHotka.com
Reading Explain Plans
More Information:– Database Performance Tuning Guide & Reference
• Chapter 9 – Using Explain Plans• Oracle Documentation
– Download from www.Oracle.com search on Documentation
www.DanHotka.com
Reading Explain Plans
Oracle only joins 2 tables at a timeThe smaller the initial result sets, the faster the whole query runs– Drive off the item that will eliminate the most rows first
www.DanHotka.com
Reading Explain Plans
Nested Loops– Inner table looped for each row
returned in outer table– Lg table should be outer– Sm table (or unique indexed
lookup) should be inner– Rows returned to the result set
that qualify the driving WHERE clause
– Cost = outer access + (inner table access * outer cardinality)
Merge Scan Join– Both tables are sorted– Rows are inserted into result set
based on key value– THEN WHERE clause applied – Cost = outer access + inner
access + sort costsHash Join
– Hashes join keys and caches object into a hash table
– Driving table should be smaller of the 2
– Cost = inner cost + (outer cost * inner cardinality/hash partitions)
www.DanHotka.com
Reading Explain Plans
Nested Loop Join
– driving table
– Default order(rule)
Merge Scan Join
– sort & match
Hash Join
– Full scans with no sorts
– Join column to row address
www.DanHotka.com
Reading Explain Plans
Nested Loop Join
– If join condition is ‘ANDed’, make a compound index on the inner table
– Inner and Outer join column should have same data type
– Outer Table: Larger of Result set
– Inner Table: Smaller of Result set
– Foreign key indexes helps CBO choose between nested loops and hash joins
www.DanHotka.com
Reading Explain PlansMerge Scan Join
– large portion of rows are being joined
– Both Tables have larger result sets
– Helpful if using indexes on merged columns (rows returned in proper order)
Hash Join
– Smaller tables being joined on ‘=‘ condition
– Outer Table: Smaller of Result Set
– Inner Table: Larger of Result Set
– Use Trace Event 10104 (Hash Area Trace) or 10053 (CBO Trace) to size correctly
• HASH_AREA_SIZE
www.DanHotka.com
Join Formula
RBO Join Formula– Indexes, type of indexes, Full Table Scans
CBO Join Formula– High # values matching High # values
• Tends to choose Merge Join– High # values matching Low # values
• Tends to choose Nested Loop– Low # values matching Low # values
• Tends to choose Hash Join
www.DanHotka.com
Oracle Optimizer gives you choices:– Rule-based
• Based on a set of rules (Index existence, SQL coding)
• Does NOT consider object statistics– Cost-based
• Uses object statistics (from DBMS_STATS command)
• User has more control in tuning• 2 goals:
– First row: response time for interactive apps (OLTP)– All rows: throughput for batch processing (DSS)
• Makes Assumptions– Hints
• Cost Based ‘directives’, always used unless Oracle can’t use them• Can be specified in combinations
The Oracle Optimizers
www.DanHotka.com
The Oracle OptimizersCost-based Optimizer
www.DanHotka.com
Cost-based Optimizer
Phys IO + Logical IO/1000 + NetIO*1.5
Cost Factor is an estimated number of physical I/O operations Oracle thinks it will have to do, based on statistics. Assumes even data distribution when using indexes (unless histograms are implemented). Costs out each join to select driving table
Cost Factor Heavily relies upon assumed multiblock read-ahead8i Cost reflects number of multiblock reads based on statistics9i Cost reflects machine speed at performing multiblock reads along with 8i 10g Takes into account high water mark, 10g index_fast_full_scans considers number of leaf blocks
www.DanHotka.com
Cost-based Optimizer
CBO Formulas• Mra Multi block read ahead• Selectivity discussed with Where Clause section• Clustering Factor discussed with Index section
– Full Table Scan = num rows / mra– Fast Full Scan = num rows / mra– Unique Scan = #branch levels + 1– Range Scan = (#branch levels + selectivity) * (#leaf blocks +
selectivity) * Clustering Factor– Index-Only Scan = (#branch levels + selectivity) * #leaf blocks
Source: Fallacies of the CBO: Wulfgang Brietling
www.DanHotka.com
The Oracle OptimizersCost-based Optimizer
www.DanHotka.com
Oracle Documentation
Oracle Documentation– Complete database doc set– www.Oracle.com
• Search on Documentation• 380,000+ mbytes zip file• www.oracle.com/technology/documentation/database.html
– Has doc sets back to Oracle9r2
View Oracle Documentation
www.DanHotka.com
SQL Tuning Class
Hands-on LabsInstructor-led courses– Understanding Explain Plans– Index Review
• How they are used/not used– Controlling the Explain Plan
– Using the Tools• Oracle Trace• StatsPack• AWR
– PL/SQL Tuning/Profiling– SGA Topics
• Cursor management• Multiple buffer caches• Result Cache
– Finding the SQL problems
SQL Tuning Tips and Techniques– Training at your Desk! (sm)
– Onsite
www.DanHotka.com
What have we learned?
Explain Plan Tools– Xplan.display– JS Tuner
Understanding Explain PlansThank you! Keep in touch…