Description
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Student Guide D52163GC20 Edition 2.0 October 2010 D69160 Oracle University and InfoTech (Pvt.) Ltd use only Oracle Database 11g: SQL Tuning Workshop Disclaimer This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle. The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free. Restricted Rights Notice If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United States Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract. Trademark Notice Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Author James Spiller, Tulika Srivastava Technical Contributors and Reviewers Abhinav Gupta, Branislav Valny, Clinton Shaffer, Donna Keesling, Ira Singer, Howard Bradley, Sean Kim, Sue Harper, Teria Kidd This book was published using: Oracle Tutor Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture .................................................................................................1-1 Exploring the Oracle Database Architecture ..................................................................................................1-2 Objectives ......................................................................................................................................................1-3 Oracle Database Server Architecture: Overview ............................................................................................1-4 Connecting to the Database Instance ............................................................................................................1-5 Oracle Database Memory Structures: Overview ............................................................................................1-7 Database Buffer Cache ..................................................................................................................................1-8 Redo Log Buffer .............................................................................................................................................1-9 Shared Pool ...................................................................................................................................................1-10 Processing a DML Statement: Example .........................................................................................................1-11 COMMIT Processing: Example ......................................................................................................................1-13 Large Pool ......................................................................................................................................................1-14 Java Pool and Streams Pool ..........................................................................................................................1-16 Program Global Area (PGA) ..........................................................................................................................1-17 Background Process ......................................................................................................................................1-18 Automatic Shared Memory Management .......................................................................................................1-20 Automated SQL Execution Memory Management .........................................................................................1-21 Automatic Memory Management ...................................................................................................................1-22 Database Storage Architecture ......................................................................................................................1-23 Logical and Physical Database Structures .....................................................................................................1-25 Segments, Extents, and Blocks......................................................................................................................1-27 SYSTEM and SYSAUX Tablespaces.............................................................................................................1-28 Quiz................................................................................................................................................................1-29 Summary ........................................................................................................................................................1-32 Practice 1: Overview ......................................................................................................................................1-33 Introduction to SQL Tuning.............................................................................................................................2-1 Introduction to SQL Tuning ............................................................................................................................2-2 Objectives ......................................................................................................................................................2-3 Reasons for Inefficient SQL Performance ......................................................................................................2-4 Inefficient SQL: Examples ..............................................................................................................................2-6 Performance Monitoring Solutions .................................................................................................................2-8 Monitoring and Tuning Tools: Overview .........................................................................................................2-10 EM Performance Pages for Reactive Tuning .................................................................................................2-11 Tuning Tools: Overview .................................................................................................................................2-12 SQL Tuning Tasks: Overview.........................................................................................................................2-14 CPU and Wait Time Tuning Dimensions ........................................................................................................2-15 Scalability with Application Design, Implementation, and Configuration ........................................................2-16 Common Mistakes on Customer Systems .....................................................................................................2-17 Proactive Tuning Methodology .......................................................................................................................2-19 Simplicity in Application Design......................................................................................................................2-20 Data Modeling ................................................................................................................................................2-21 Table Design ..................................................................................................................................................2-22 Index Design ..................................................................................................................................................2-23 Using Views ...................................................................................................................................................2-24 SQL Execution Efficiency ...............................................................................................................................2-25 Writing SQL to Share Cursors ........................................................................................................................2-27 Performance Checklist ...................................................................................................................................2-29 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents i Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Table of Contents Introduction to the Optimizer ..........................................................................................................................3-1 Introduction to the Optimizer ..........................................................................................................................3-2 Objectives ......................................................................................................................................................3-3 Structured Query Language ...........................................................................................................................3-4 SQL Statement Representation .....................................................................................................................3-6 SQL Statement Implementation .....................................................................................................................3-7 SQL Statement Processing: Overview ...........................................................................................................3-8 SQL Statement Processing: Steps .................................................................................................................3-9 Step 1: Create a Cursor .................................................................................................................................3-10 Step 2: Parse the Statement ..........................................................................................................................3-11 Steps 3 and 4: Describe and Define...............................................................................................................3-12 Steps 5 and 6: Bind and Parallelize ...............................................................................................................3-13 Steps 7 Through 9..........................................................................................................................................3-14 SQL Statement Processing PL/SQL: Example ..............................................................................................3-15 SQL Statement Parsing: Overview.................................................................................................................3-16 Why Do You Need an Optimizer? ..................................................................................................................3-18 Optimization During Hard Parse Operation ....................................................................................................3-20 Transformer: OR Expansion Example............................................................................................................3-21 Transformer: Subquery Unnesting Example ..................................................................................................3-22 Transformer: View Merging Example .............................................................................................................3-23 Transformer: Predicate Pushing Example ......................................................................................................3-24 Transformer: Transitivity Example..................................................................................................................3-25 Cost-Based Optimizer ....................................................................................................................................3-26 Estimator: Selectivity ......................................................................................................................................3-27 Estimator: Cardinality .....................................................................................................................................3-29 Estimator: Cost...............................................................................................................................................3-30 Plan Generator ...............................................................................................................................................3-31 Controlling the Behavior of the Optimizer .......................................................................................................3-32 Optimizer Features and Oracle Database Releases ......................................................................................3-37 Quiz................................................................................................................................................................3-38 Summary ........................................................................................................................................................3-41 Practice 3: Overview ......................................................................................................................................3-42 Interpreting Execution Plans...........................................................................................................................4-1 Interpreting Execution Plans ..........................................................................................................................4-2 Objectives ......................................................................................................................................................4-3 What Is an Execution Plan? ...........................................................................................................................4-4 Where to Find Execution Plans? ....................................................................................................................4-6 Viewing Execution Plans ................................................................................................................................4-8 The EXPLAIN PLAN Command .....................................................................................................................4-9 The EXPLAIN PLAN Command: Example .....................................................................................................4-11 PLAN_TABLE ................................................................................................................................................4-12 Displaying from PLAN_TABLE: Typical .........................................................................................................4-14 Displaying from PLAN_TABLE: ALL ..............................................................................................................4-16 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents ii Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Development Environments: Overview ..........................................................................................................2-30 What Is Oracle SQL Developer? ....................................................................................................................2-31 Coding PL/SQL in SQL*Plus ..........................................................................................................................2-32 Quiz................................................................................................................................................................2-34 Summary ........................................................................................................................................................2-38 Practice 2: Overview ......................................................................................................................................2-39 Application Tracing..........................................................................................................................................5-1 Application Tracing.........................................................................................................................................5-2 Objectives ......................................................................................................................................................5-3 End-to-End Application Tracing Challenge ....................................................................................................5-4 End-to-End Application Tracing......................................................................................................................5-5 Location for Diagnostic Traces .......................................................................................................................5-6 What Is a Service? .........................................................................................................................................5-7 Using Services with Client Applications .........................................................................................................5-8 Tracing Services ............................................................................................................................................5-9 Use Enterprise Manager to Trace Services ...................................................................................................5-11 Service Tracing: Example ..............................................................................................................................5-12 Session Level Tracing: Example ....................................................................................................................5-14 Trace Your Own Session ...............................................................................................................................5-16 The trcsess Utility ...........................................................................................................................................5-17 Invoking the trcsess Utility ..............................................................................................................................5-18 The trcsess Utility: Example ...........................................................................................................................5-20 SQL Trace File Contents ................................................................................................................................5-21 SQL Trace File Contents: Example ................................................................................................................5-23 Formatting SQL Trace Files: Overview ..........................................................................................................5-24 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents iii Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN Command .....................................................................................................................4-18 Displaying from PLAN_TABLE: ADVANCED .................................................................................................4-19 Explain Plan Using SQL Developer................................................................................................................4-20 AUTOTRACE .................................................................................................................................................4-21 The AUTOTRACE Syntax ..............................................................................................................................4-22 AUTOTRACE: Examples ...............................................................................................................................4-23 AUTOTRACE: Statistics .................................................................................................................................4-24 AUTOTRACE Using SQL Developer .............................................................................................................4-26 Using the V$SQL_PLAN View .......................................................................................................................4-27 The V$SQL_PLAN Columns ..........................................................................................................................4-28 The V$SQL_PLAN_STATISTICS View..........................................................................................................4-29 Links Between Important Dynamic Performance Views .................................................................................4-30 Querying V$SQL_PLAN .................................................................................................................................4-32 Automatic Workload Repository (AWR) .........................................................................................................4-34 Managing AWR with PL/SQL .........................................................................................................................4-36 Important AWR Views ....................................................................................................................................4-38 Querying the AWR .........................................................................................................................................4-40 Generating SQL Reports from AWR Data ......................................................................................................4-42 SQL Monitoring: Overview .............................................................................................................................4-43 SQL Monitoring Report: Example...................................................................................................................4-45 Interpreting an Execution Plan .......................................................................................................................4-49 Execution Plan Interpretation: Example 1 ......................................................................................................4-51 Execution Plan Interpretation: Example 2 ......................................................................................................4-55 Execution Plan Interpretation: Example 3 ......................................................................................................4-57 Reading More Complex Execution Plans .......................................................................................................4-59 Reviewing the Execution Plan ........................................................................................................................4-60 Looking Beyond Execution Plans ...................................................................................................................4-62 Quiz................................................................................................................................................................4-63 Summary ........................................................................................................................................................4-67 Practice 4: Overview ......................................................................................................................................4-68 Optimizer Operators ........................................................................................................................................6-1 Optimizer Operators .......................................................................................................................................6-2 Objectives ......................................................................................................................................................6-3 Row Source Operations .................................................................................................................................6-4 Main Structures and Access Paths ................................................................................................................6-5 Full Table Scan ..............................................................................................................................................6-6 Full Table Scans: Use Cases .........................................................................................................................6-7 ROWID Scan..................................................................................................................................................6-9 Sample Table Scans ......................................................................................................................................6-10 Indexes: Overview..........................................................................................................................................6-12 Normal B*-tree Indexes ..................................................................................................................................6-14 Index Scans ...................................................................................................................................................6-15 Index Unique Scan .........................................................................................................................................6-16 Index Range Scan..........................................................................................................................................6-17 Index Range Scan: Descending .....................................................................................................................6-19 Descending Index Range Scan ......................................................................................................................6-20 Index Range Scan: Function-Based...............................................................................................................6-21 Index Full Scan ..............................................................................................................................................6-22 Index Fast Full Scan ......................................................................................................................................6-23 Index Skip Scan .............................................................................................................................................6-24 Index Skip Scan: Example .............................................................................................................................6-26 Index Join Scan..............................................................................................................................................6-27 B*-tree Indexes and Nulls ..............................................................................................................................6-28 Using Indexes: Considering Nullable Columns ..............................................................................................6-29 Index-Organized Tables .................................................................................................................................6-30 Index-Organized Table Scans ........................................................................................................................6-32 Bitmap Indexes ..............................................................................................................................................6-33 Bitmap Index Access: Examples ....................................................................................................................6-35 Combining Bitmap Indexes: Examples ...........................................................................................................6-37 Combining Bitmap Index Access Paths .........................................................................................................6-38 Bitmap Operations .........................................................................................................................................6-39 Bitmap Join Index...........................................................................................................................................6-40 Composite Indexes ........................................................................................................................................6-42 Invisible Index: Overview ...............................................................................................................................6-43 Invisible Indexes: Examples ...........................................................................................................................6-44 Guidelines for Managing Indexes ...................................................................................................................6-45 Investigating Index Usage ..............................................................................................................................6-47 Quiz................................................................................................................................................................6-49 Summary ........................................................................................................................................................6-52 Practice 6: Overview ......................................................................................................................................6-53 Optimizer: Join Operators ...............................................................................................................................7-1 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents iv Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Invoking the tkprof Utility ................................................................................................................................5-26 tkprof Sorting Options ....................................................................................................................................5-28 Output of the tkprof Command .......................................................................................................................5-30 tkprof Output with No Index: Example ............................................................................................................5-35 tkprof Output with Index: Example .................................................................................................................5-36 Quiz................................................................................................................................................................5-37 Summary ........................................................................................................................................................5-40 Practice 5: Overview ......................................................................................................................................5-41 Other Optimizer Operators ..............................................................................................................................8-1 Other Optimizer Operators .............................................................................................................................8-2 Objectives ......................................................................................................................................................8-3 Clusters ..........................................................................................................................................................8-4 When Are Clusters Useful? ............................................................................................................................8-6 Cluster Access Path: Examples .....................................................................................................................8-8 Sorting Operators ...........................................................................................................................................8-9 Buffer Sort Operator .......................................................................................................................................8-11 Inlist Iterator ...................................................................................................................................................8-12 View Operator ................................................................................................................................................8-13 Count Stop Key Operator ...............................................................................................................................8-14 Min/Max and First Row Operators..................................................................................................................8-15 Other N-Array Operations ..............................................................................................................................8-16 FILTER Operations ........................................................................................................................................8-17 Concatenation Operation ...............................................................................................................................8-18 UNION [ALL], INTERSECT, MINUS ..............................................................................................................8-19 Result Cache Operator ..................................................................................................................................8-20 Quiz................................................................................................................................................................8-21 Summary ........................................................................................................................................................8-25 Practice 8: Overview ......................................................................................................................................8-26 Case Study: Star Transformation ...................................................................................................................9-1 Case Study: Star Transformation ...................................................................................................................9-2 Objectives ......................................................................................................................................................9-3 The Star Schema Model ................................................................................................................................9-4 The Snowflake Schema Model.......................................................................................................................9-5 Star Query: Example ......................................................................................................................................9-6 Execution Plan Without Star Transformation .................................................................................................9-7 Star Transformation .......................................................................................................................................9-8 Star Transformation: Considerations ..............................................................................................................9-10 Star Transformation: Rewrite Example ..........................................................................................................9-11 Retrieving Fact Rows from One Dimension ...................................................................................................9-12 Retrieving Fact Rows from All Dimensions ....................................................................................................9-13 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents v Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer: Join Operators ..............................................................................................................................7-2 Objectives ......................................................................................................................................................7-3 Join Methods ..................................................................................................................................................7-4 Nested Loops Join .........................................................................................................................................7-6 Nested Loops Join: Prefetching .....................................................................................................................7-7 Nested Loops Join: 11g Implementation ........................................................................................................7-8 Sort Merge Join ..............................................................................................................................................7-9 Hash Join .......................................................................................................................................................7-11 Cartesian Join ................................................................................................................................................7-12 Join Types ......................................................................................................................................................7-13 Equijoins and Nonequijoins ............................................................................................................................7-14 Outer Joins .....................................................................................................................................................7-15 Semijoins .......................................................................................................................................................7-17 Antijoins .........................................................................................................................................................7-18 Quiz................................................................................................................................................................7-19 Summary ........................................................................................................................................................7-23 Practice 7: Overview ......................................................................................................................................7-24 Optimizer Statistics..........................................................................................................................................10-1 Optimizer Statistics ........................................................................................................................................10-2 Objectives ......................................................................................................................................................10-3 Optimizer Statistics ........................................................................................................................................10-4 Types of Optimizer Statistics ..........................................................................................................................10-5 Table Statistics (DBA_TAB_STATISTICS) ....................................................................................................10-6 Index Statistics (DBA_IND_STATISTICS) .....................................................................................................10-7 Index Clustering Factor ..................................................................................................................................10-9 Column Statistics (DBA_TAB_COL_STATISTICS) ........................................................................................10-11 Histograms .....................................................................................................................................................10-12 Frequency Histograms ...................................................................................................................................10-13 Viewing Frequency Histograms......................................................................................................................10-14 Height-Balanced Histograms .........................................................................................................................10-15 Viewing Height-Balanced Histograms ............................................................................................................10-16 Histogram Considerations ..............................................................................................................................10-17 Multicolumn Statistics: Overview ....................................................................................................................10-18 Expression Statistics: Overview .....................................................................................................................10-20 Gathering System Statistics ...........................................................................................................................10-21 Gathering System Statistics: Example ...........................................................................................................10-23 Mechanisms for Gathering Statistics ..............................................................................................................10-25 Statistic Preferences: Overview .....................................................................................................................10-26 When to Gather Statistics Manually ...............................................................................................................10-28 Manual Statistics Gathering ...........................................................................................................................10-29 Manual Statistics Collection: Factors .............................................................................................................10-30 Managing Statistics Collection: Example .......................................................................................................10-31 Optimizer Dynamic Sampling: Overview ........................................................................................................10-32 Optimizer Dynamic Sampling at Work............................................................................................................10-33 OPTIMIZER_DYNAMIC_SAMPLING.............................................................................................................10-34 Locking Statistics ...........................................................................................................................................10-36 Restoring Statistics ........................................................................................................................................10-37 Export and Import Statistics ...........................................................................................................................10-38 Quiz................................................................................................................................................................10-39 Summary ........................................................................................................................................................10-42 Practice 10: Overview ....................................................................................................................................10-43 Using Bind Variables .......................................................................................................................................11-1 Using Bind Variables ......................................................................................................................................11-2 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents vi Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Joining the Intermediate Result Set with Dimensions ...................................................................................9-14 Star Transformation Plan: Example 1 ............................................................................................................9-15 Star Transformation: Further Optimization .....................................................................................................9-16 Using Bitmap Join Indexes .............................................................................................................................9-17 Star Transformation Plan: Example 2 ............................................................................................................9-18 Star Transformation Hints ..............................................................................................................................9-19 Bitmap Join Indexes: Join Model 1.................................................................................................................9-20 Bitmap Join Indexes: Join Model 2.................................................................................................................9-21 Bitmap Join Indexes: Join Model 3.................................................................................................................9-22 Bitmap Join Indexes: Join Model 4.................................................................................................................9-23 Quiz................................................................................................................................................................9-24 Summary ........................................................................................................................................................9-27 Practice 9: Overview ......................................................................................................................................9-28 SQL Tuning Advisor ........................................................................................................................................12-1 SQL Tuning Advisor .......................................................................................................................................12-2 Objectives ......................................................................................................................................................12-3 Tuning SQL Statements Automatically...........................................................................................................12-4 Application Tuning Challenges .......................................................................................................................12-5 SQL Tuning Advisor: Overview ......................................................................................................................12-6 Stale or Missing Object Statistics ...................................................................................................................12-7 SQL Statement Profiling .................................................................................................................................12-8 Plan Tuning Flow and SQL Profile Creation ...................................................................................................12-9 SQL Tuning Loop ...........................................................................................................................................12-10 Access Path Analysis .....................................................................................................................................12-11 SQL Structure Analysis ..................................................................................................................................12-12 SQL Tuning Advisor: Usage Model ................................................................................................................12-13 Database Control and SQL Tuning Advisor ...................................................................................................12-14 Running SQL Tuning Advisor: Example .........................................................................................................12-15 Schedule SQL Tuning Advisor .......................................................................................................................12-16 Implementing Recommendations ...................................................................................................................12-17 Compare Explain Plan ...................................................................................................................................12-18 Quiz................................................................................................................................................................12-19 Summary ........................................................................................................................................................12-21 Practice 12: Overview ....................................................................................................................................12-22 Using SQL Access Advisor .............................................................................................................................13-1 Using SQL Access Advisor ............................................................................................................................13-2 Objectives ......................................................................................................................................................13-3 SQL Access Advisor: Overview......................................................................................................................13-4 SQL Access Advisor: Usage Model ...............................................................................................................13-6 Possible Recommendations ...........................................................................................................................13-8 SQL Access Advisor Session: Initial Options .................................................................................................13-10 SQL Access Advisor: Workload Source .........................................................................................................13-12 SQL Access Advisor: Recommendation Options ...........................................................................................13-13 SQL Access Advisor: Schedule and Review ..................................................................................................13-14 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents vii Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives ......................................................................................................................................................11-3 Cursor Sharing and Different Literal Values ...................................................................................................11-4 Cursor Sharing and Bind Variables ................................................................................................................11-6 Bind Variables in SQL*Plus ............................................................................................................................11-7 Bind Variables in Enterprise Manager ............................................................................................................11-8 Bind Variables in SQL Developer ...................................................................................................................11-9 Bind Variable Peeking ....................................................................................................................................11-10 Cursor Sharing Enhancements ......................................................................................................................11-12 The CURSOR_SHARING Parameter ............................................................................................................11-14 Forcing Cursor Sharing: Example ..................................................................................................................11-15 Adaptive Cursor Sharing: Overview ...............................................................................................................11-16 Adaptive Cursor Sharing: Architecture ...........................................................................................................11-17 Adaptive Cursor Sharing: Views.....................................................................................................................11-19 Adaptive Cursor Sharing: Example ................................................................................................................11-21 Interacting with Adaptive Cursor Sharing .......................................................................................................11-22 Quiz................................................................................................................................................................11-23 Summary ........................................................................................................................................................11-26 Practice 11: Overview ....................................................................................................................................11-27 Automating SQL Tuning ..................................................................................................................................14-1 Automating SQL Tuning .................................................................................................................................14-2 Objectives ......................................................................................................................................................14-3 SQL Tuning Loop ...........................................................................................................................................14-4 Automatic SQL Tuning ...................................................................................................................................14-5 Automatic Tuning Process .............................................................................................................................14-6 Automatic SQL Tuning Controls .....................................................................................................................14-8 Automatic SQL Tuning Task ..........................................................................................................................14-9 Configuring Automatic SQL Tuning ................................................................................................................14-10 Automatic SQL Tuning: Result Summary.......................................................................................................14-11 Automatic SQL Tuning: Result Details ...........................................................................................................14-12 Automatic SQL Tuning Result Details: Drilldown ...........................................................................................14-13 Automatic SQL Tuning Considerations ..........................................................................................................14-14 Quiz................................................................................................................................................................14-15 Summary ........................................................................................................................................................14-16 Practice 14: Overview ....................................................................................................................................14-17 SQL Plan Management ....................................................................................................................................15-1 SQL Plan Management ..................................................................................................................................15-2 Objectives ......................................................................................................................................................15-3 Maintaining SQL Performance .......................................................................................................................15-4 SQL Plan Management: Overview .................................................................................................................15-5 SQL Plan Baseline: Architecture ....................................................................................................................15-7 Loading SQL Plan Baselines .........................................................................................................................15-9 Evolving SQL Plan Baselines .........................................................................................................................15-11 Important Baseline SQL Plan Attributes .........................................................................................................15-12 SQL Plan Selection ........................................................................................................................................15-14 Possible SQL Plan Manageability Scenarios .................................................................................................15-16 SQL Performance Analyzer and SQL Plan Baseline Scenario .....................................................................15-17 Loading a SQL Plan Baseline Automatically ..................................................................................................15-18 Purging SQL Management Base Policy .........................................................................................................15-19 Enterprise Manager and SQL Plan Baselines ................................................................................................15-20 Quiz................................................................................................................................................................15-21 Summary ........................................................................................................................................................15-22 Practice 15: Overview Using SQL Plan Management ....................................................................................15-23 Using Optimizer Hints ......................................................................................................................................16-1 Using Optimizer Hints ....................................................................................................................................16-2 Objectives ......................................................................................................................................................16-3 Optimizer Hints: Overview ..............................................................................................................................16-4 Types of Hints ................................................................................................................................................16-5 Specifying Hints .............................................................................................................................................16-6 Rules for Hints................................................................................................................................................16-7 Hint Recommendations ..................................................................................................................................16-8 Optimizer Hint Syntax: Example.....................................................................................................................16-9 Hint Categories ..............................................................................................................................................16-10 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents viii Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Results ........................................................................................................................13-15 SQL Access Advisor: Results and Implementation ........................................................................................13-16 Quiz................................................................................................................................................................13-18 Summary ........................................................................................................................................................13-20 Practice 13: Overview ....................................................................................................................................13-21 Using SQL Developer ......................................................................................................................................17-1 Using SQL Developer ....................................................................................................................................17-2 Objectives ......................................................................................................................................................17-3 What Is Oracle SQL Developer? ....................................................................................................................17-4 Specifications of SQL Developer....................................................................................................................17-5 SQL Developer 2.1 Interface ..........................................................................................................................17-6 Creating a Database Connection ...................................................................................................................17-8 Browsing Database Objects ...........................................................................................................................17-11 Displaying the Table Structure .......................................................................................................................17-12 Browsing Files ................................................................................................................................................17-13 Creating a Schema Object .............................................................................................................................17-14 Creating a New Table: Example.....................................................................................................................17-15 Using the SQL Worksheet ..............................................................................................................................17-16 Executing SQL Statements ............................................................................................................................17-20 Saving SQL Scripts ........................................................................................................................................17-21 Executing Saved Script Files: Method 1.........................................................................................................17-22 Executing Saved Script Files: Method 2.........................................................................................................17-23 Formatting the SQL Code ..............................................................................................................................17-24 Using Snippets ...............................................................................................................................................17-25 Using Snippets: Example ...............................................................................................................................17-26 Debugging Procedures and Functions ...........................................................................................................17-27 Database Reporting .......................................................................................................................................17-28 Creating a User-Defined Report .....................................................................................................................17-30 External Tools ................................................................................................................................................17-31 Setting Preferences........................................................................................................................................17-32 Resetting the SQL Developer Layout .............................................................................................................17-33 Summary ........................................................................................................................................................17-34 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Oracle Database 11g: SQL Tuning Workshop Table of Contents ix Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimization Goals and Approaches ..............................................................................................................16-11 Hints for Access Paths ...................................................................................................................................16-13 The INDEX_COMBINE Hint: Example ...........................................................................................................16-17 Hints for Query Transformation ......................................................................................................................16-19 Hints for Join Orders ......................................................................................................................................16-22 Hints for Join Operations ................................................................................................................................16-23 Additional Hints ..............................................................................................................................................16-25 Hints and Views .............................................................................................................................................16-28 Global Table Hints..........................................................................................................................................16-30 Specifying a Query Block in a Hint .................................................................................................................16-31 Specifying a Full Set of Hints .........................................................................................................................16-32 Summary ........................................................................................................................................................16-33 Practice Appendix B: Overview ......................................................................................................................16-34 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 1 E15222-02 Copyright © 2010. It is also recommended that you have taken the Oracle Database 11g: SQL Fundamentals I course. How This Course Is Organized Oracle Database 11g: SQL Tuning Workshop is an instructor-led course featuring lectures and hands-on exercises. and have taken the Oracle Database 11g: Introduction to SQL course or have equivalent experience.2)E10821-05 Oracle SQL Developer User's Guide Release 2. Online demonstrations and written practice sessions reinforce the concepts and skills that are introduced. you should be familiar with SQL Language statements. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Preface .Profile Before You Begin This Course Before you begin this course.2) Part Number E10592-04 Oracle Database Performance Tuning Guide 11g Release 2 (11. Oracle Database 11g: SQL Tuning Workshop Table of Contents xi Oracle University and InfoTech (Pvt. All rights reserved. Related Publications Oracle Publications Title Oracle Database SQL Reference 11g Release 2 (11. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and schema names. 1. SELECT last_name FROM employees. . Initial cap Italic Quotation marks Object or Term User input. PL/SQL objects. emphasized words or phrases. Typographic Conventions for words within code samples Convention Uppercase Lowercase italic Initial cap Object or term Commands. where hostname is the host on which the password is to be changed This subject is covered in Lesson 3. . . Oracle and/or its affiliates. PL/SQL objects Bold Text that must be entered by a user Copyright © 2010. For more information on the subject see Oracle SQL Reference Manual Do not save changes to the database. Oracle Database 11g: SQL Tuning Workshop Table of Contents xii Oracle University and InfoTech (Pvt. functions Syntax variables Example SELECT employee_id FROM employees CREATE ROLE role Forms triggers Form module: ORD Trigger level: S_ITEM. Enter 300. user interface object names.QUANTITY item Trigger name: When-Validate-Item .The following two lists explain Oracle University typographical conventions for words that appear within regular text or within code samples. Log in as scott Assign a When-Validate-Item trigger to the ORD block. functions. table names Filenames.” 2. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Typographic Conventions . . Lowercase Column names. such as button names Titles of courses and manuals. . . Click the Cancel button. “Working with Objects. table. All rights reserved. paths Triggers. . Typographic Conventions for words within regular text Convention Courier new. CREATE USER scott IDENTIFIED BY tiger. commands. column. OG_ACTIVATE_LAYER (OG_GET_LAYER ('prod_pie_layer')) . placeholders or variables Lesson or module title referenced within a course Example Use the SELECT command to view information stored in the LAST_NAME column of the EMPLOYEES table. Enter hostname. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 3.This course uses simplified navigation paths. 3. Typographic Conventions for Oracle Application Navigation Paths . (B) Click the Approve button. for example— (Help) General Ledger > Journals > Enter Journals —represents the following sequence of actions: 1. The following help navigation path. expand Journals. (N) From the Navigator window. select Enter Journals. such as the following example. In the navigation frame of the help system window. select Query then Find. 2. (M) From the menu. (N) Invoice > Entry > Invoice Batches Summary (M) Query > Find (B) Approve This simplified path translates to the following: 1. 3. Under the General Ledger entry. Oracle Database 11g: SQL Tuning Workshop Table of Contents xiii Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. expand the General Ledger entry. select Invoice then Entry then Invoice Batches Summary. to direct you through Oracle Applications. All rights reserved. Typographic Conventions for Oracle Application Help System Paths This course uses a “navigation path” convention to represent actions you perform to find pertinent information in the Oracle Applications Help System. 4. Review the Enter Journals topic that appears in the document frame of the help system window. Copyright © 2010. Under Journals. 2. Oracle and/or its affiliates. Notations: (N) = Navigator (M) = Menu (T) = Tab (B) = Button (I) = Icon (H) = Hyperlink (ST) = Sub Tab 4. Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle Database 11g: SQL Tuning Workshop Table of Contents xiv . Exploring the Oracle Database Architecture Chapter 1 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Exploring the Oracle Database Architecture . Oracle and/or its affiliates.Page 1 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Chapter 1 Copyright © 2010. All rights reserved. Oracle and/or its affiliates.Exploring the Oracle Database Architecture Copyright © 2010. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Exploring the Oracle Database Architecture . Exploring the Oracle Database Architecture Chapter 1 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 2 Oracle University and InfoTech (Pvt. you should be able to: • List the major architectural components of Oracle Database server • Explain memory structures • Describe background processes • Correlate logical and physical storage structures Objectives This lesson provides an overview of the Oracle Database server architecture.Page 3 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Copyright © 2010. You learn about physical and logical structures and about the various components. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Objectives After completing this lesson. Exploring the Oracle Database Architecture Chapter 1 . All rights reserved.Page 4 Oracle University and InfoTech (Pvt. the physical storage of data can be managed without affecting access to the logical storage structures. They may asynchronously perform input/output (I/O) and monitor other Oracle Database processes to provide increased parallelism for better performance and reliability. Because the physical and logical structures are separate. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Database Server Architecture: Overview . Every time an instance is started. a shared memory area called the System Global Area (SGA) is allocated and the background processes are started. Exploring the Oracle Database Architecture Chapter 1 . The background processes consolidate functions that would otherwise be handled by multiple Oracle Database server programs running for each user process. Copyright © 2010. Oracle and/or its affiliates. An instance consists of memory structures and background processes.Oracle Database Server Architecture: Overview Instance SGA BGP1 BGP2 BGP3 BGPn Database Oracle Database Server Architecture: Overview An Oracle Database server consists of an Oracle Database and one or more Oracle Database instances. The SGA contains data and control information for one Oracle Database instance. The database consists of physical files and logical structures discussed later in this lesson. Note: Oracle Real Application Clusters (Oracle RAC) comprises two or more Oracle Database instances running on multiple clustered computers that communicates with each other by means of an interconnect and access the same Oracle Database.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In a dedicated server configuration. minimizing the number of server processes and maximizing the use Copyright © 2010. and starting other processes in addition to the Oracle Database background processes: • User processes sometimes called client or foreground processes are created to run the software code of an application program.Connecting to the Database Instance • • User process Connection: Bidirectional network pathway between a user process on a client or middle tier and an oracle process on the server Session: Representation of a specific login by a user User process Listener process Dispatcher Shared Server D000 S000 Connection SQL> Select … User process Server process Dedicated Server User SGA Server host Session (Specific connected database user) Connecting to the Database Instance When users connect to an Oracle Database server. Most environments have separate machines for the client processes. An Oracle Database instance can be configured to vary the number of user processes for each server process. All rights reserved. A shared server configuration enables many user processes to share a small number of shared server processes. A user process also manages communication with a corresponding server process through a program interface. The database instance services those users by allocating other memory areas in addition to the SGA. Exploring the Oracle Database Architecture Chapter 1 . a server process handles requests for a single user process. • Oracle Database server creates server processes to handle requests from connected user processes. Oracle and/or its affiliates.Page 5 Oracle University and InfoTech (Pvt. A server process communicates with the user process and interacts with the instance and the database to carry out requests from the associated user process. they are connected to an Oracle Database instance. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Connecting to the Database Instance .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. when a user starts SQL*Plus. • A session represents the state of a current database user login to the database instance. A session lasts from the time a user connects until the user disconnects or exits the database application. Note: Multiple sessions can be created and exist concurrently for a single Oracle database user using the same username. Oracle and/or its affiliates. a user with the username/password of HR/HR can connect to the same Oracle Database instance several times. The application connects to the listener that creates a dedicated server process or handles the connection to a dispatcher. the user must provide a valid database username and password. and communicate through a network). but are very different in meaning: • A connection is a communication pathway between a user process and an Oracle Database instance. Exploring the Oracle Database Architecture Chapter 1 .Page 6 Oracle University and InfoTech (Pvt. For example. .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. For example. and then a session is established for that user. A communication pathway is established by using available interprocess communication mechanisms (on a computer that runs both the user process and Oracle Database) or network software (when different computers run the database application and Oracle Database.The Oracle Database server runs a listener that is responsible for handling network connections. All rights reserved. One or more dispatcher processes are then used to queue user process requests in the SGA and dequeue shared server responses. Copyright © 2010. Connections and sessions are closely related to user processes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED of available system resources. A PGA is suballocated from the aggregated PGA area. such as Oracle backup and recovery operations. Two basic memory structures are associated with an instance: • • System Global Area (SGA): The SGA is shared by all server and background processes.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. For example. Exploring the Oracle Database Architecture Chapter 1 . data that is shared among users. Copyright © 2010.Oracle Database Memory Structures: Overview SGA Database buffer cache Redo log buffer Java pool Server process Shared pool Large pool Streams pool … Server process Background process Aggregated … … PGA Oracle Database Memory Structures: Overview Oracle Database allocates memory structures for various purposes. and I/O server processes - Java pool: Used for session-specific Java code and data in the Java Virtual Machine (JVM) - Streams pool: Used by Oracle Streams to store information about the capture and apply processes Program Global Areas (PGA): Memory regions that contain data and control information about a server or background process. The SGA includes the following data structures: - Database buffer cache: Caches blocks of data retrieved from the database files - Redo log buffer: Caches recovery information before writing it to the physical files - Shared pool: Caches various constructs that can be shared among sessions - Large pool: Optional area used for certain operations. All rights reserved. and private data areas for each connected user. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Database Memory Structures: Overview . memory stores the program code that is run. Oracle and/or its affiliates.Page 7 Oracle University and InfoTech (Pvt. Database Buffer Cache • • • Is a part of the SGA Holds copies of data blocks that are read from data files Is shared by all concurrent processes SGA Server process Database buffer cache DBWn Data files Database writer process Database Buffer Cache The database buffer cache is the portion of the SGA that holds copies of data blocks that are read from data files. All users concurrently connected to the instance share access to the database buffer cache. The first time an Oracle Database server process requires a particular piece of data, it searches for the data in the database buffer cache. If the process finds the data already in the cache (a cache hit), it can read the data directly from memory. If the process cannot find the data in the cache (a cache miss), it must copy the data block from a data file on disk into a buffer in the cache before accessing the data. Accessing data through a cache hit is faster than data access through a cache miss. The buffers in the cache are managed by a complex algorithm that uses a combination of least recently used (LRU) lists and touch count. The DBWn (Database Writers) processes are responsible for writing modified (dirty) buffers in the database buffer cache to disk when necessary. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 8 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Database Buffer Cache Redo Log Buffer • • Is a circular buffer in the SGA (based on the number of CPUs) Contains redo entries that have the information to redo changes made by operations, such as DML and DDL SGA Server process Redo log buffer LGWR Log writer process Redo log files Redo Log Buffer The redo log buffer is a circular buffer in the SGA that holds information about changes made to the database. This information is stored in redo entries. Redo entries contain the information necessary to reconstruct (or redo) changes that are made to the database by INSERT, UPDATE, DELETE, CREATE, ALTER, or DROP operations. Redo entries are used for database recovery, if necessary. Redo entries are copied by Oracle Database server processes from the user’s memory space to the redo log buffer in the SGA. The redo entries take up continuous, sequential space in the buffer. The LGWR (log writer) background process writes the redo log buffer to the active redo log file (or group of files) on disk. LGWR is a background process that is capable of asynchronous I/O. Note: Depending on the number of CPUs on your system, there may be more than one redo log buffer. They are automatically allocated. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 9 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Redo Log Buffer Shared Pool • • Is part of the SGA Contains: Server process – Library cache — Shared parts of SQL and PL/SQL statements – Data dictionary cache – Result cache: — — SQL queries PL/SQL functions – Control structures — SGA Library cache Data dictionary Result cache cache (row cache) Locks Control structures Shared pool Shared Pool The shared pool portion of the SGA contains the following main parts: • The library cache includes the sharable parts of SQL statements, PL/SQL procedures and packages. It also contains control structures such as locks. • The data dictionary is a collection of database tables containing reference information about the database. The data dictionary is accessed so often by Oracle Database that two special locations in memory are designated to hold dictionary data. One area is called the data dictionary cache, also known as the row cache, and the other area is called the library cache. All Oracle Database server processes share these two caches for access to data dictionary information. • The result cache is composed of the SQL query result cache and the PL/SQL function result cache. This cache is used to store results of SQL queries or PL/SQL functions to speed up their future executions. • Control structures are essentially lock structures. Note: In general, any item in the shared pool remains until it is flushed according to a modified LRU algorithm. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 10 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Shared Pool Processing a DML Statement: Example Database SGA DBWn 2 2 Data files Server process 4 Database buffer cache 3 Redo log buffer Shared pool 5 Control files 1 Library cache User process Redo log files Processing a DML Statement: Example The steps involved in executing a data manipulation language (DML) statement are: 1. The server process receives the statement and checks the library cache for any shared SQL area that contains a similar SQL statement. If a shared SQL area is found, the server process checks the user’s access privileges to the requested data, and the existing shared SQL area is used to process the statement. If not, a new shared SQL area is allocated for the statement, so that it can be parsed and processed. 2. If the data and undo segment blocks are not already in the buffer cache, the server process reads them from the data files into the buffer cache. The server process locks the rows that are to be modified. 3. The server process records the changes to be made to the data buffers as well as the undo changes. These changes are written to the redo log buffer before the in-memory data and undo buffers are modified. This is called write-ahead logging. 4. The undo segment buffers contain values of the data before it is modified. The undo buffers are used to store the before image of the data so that the DML statements can be rolled back, if necessary. The data buffers record the new values of the data. 5. The user gets the feedback from the DML operation (such as how many rows were affected by the operation). Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 11 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Processing a DML Statement: Example Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Any changed blocks in the buffer cache are marked as dirty buffers; that is, the buffers are not the same as the corresponding blocks on the disk. These buffers are not immediately written to disk by the DBWn processes. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 12 COMMIT Processing: Example Database SGA DBWn Database buffer cache Data files Server process Redo log SGA buffer 1 Shared pool 3 Control files Library cache User process Redo log files LGWR 2 COMMIT Processing: Example When COMMIT is issued, the following steps are performed: 1. The server process places a commit record, along with the system change number (SCN), in the redo log buffer. The SCN is a number monotonically incremented and is unique within the database. It is used by Oracle Database as an internal time stamp to synchronize data and to provide read consistency when data is retrieved from the data files. Using the SCN enables Oracle Database to perform consistency checks without depending on the date and time of the operating system. 2. The LGWR background process performs a contiguous write of all the redo log buffer entries up to and including the commit record to the redo log files. After this point, Oracle Database can guarantee that the changes are not lost even if there is an instance failure. 3. If modified blocks are still in the SGA, and if no other session is modifying them, then the database removes lock-related transaction information from the blocks. This process is known as commit cleanout. 4. The server process provides feedback to the user process about the completion of the transaction. Note: If not done already, DBWn eventually writes the actual changes back to disk based on its own internal timing mechanism. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 13 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED COMMIT Processing: Example Large Pool • Provides large memory allocations for: – Session memory for the shared server and Oracle XA interface – Parallel execution buffers – I/O server processes Server process – Oracle Database backup and restore operations • Optional pool better suited when using the following: SGA – Parallel execution – Recovery Manager – Shared server I/O buffer Free memory Response queue Request queue Large pool Large Pool You can configure an optional memory area called the large pool to provide large memory allocations for: • Session memory for the shared server, the Oracle XA interface (used where transactions interact with more than one database), or parallel execution buffers • I/O server processes • Oracle Database backup and restore operations By allocating the above memory components from the large pool, Oracle Database can use the shared pool primarily for caching the shared part of SQL and PL/SQL constructs. The shared pool was originally designed to store SQL and PL/SQL constructs. Using the large pool avoids fragmentation issues associated with having large and small allocations sharing the same memory area. Unlike the shared pool, the large pool does not have an LRU list. You should consider configuring a large pool if your instance uses any of the following: • Parallel execution: Parallel query uses shared pool memory to cache parallel execution message buffers. • Recovery Manager: Recovery Manager uses the shared pool to cache I/O buffers during backup and restore operations. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 14 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Large Pool Shared server: In a shared server architecture, the session memory for each client process is included in the shared pool. Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 15 Java Pool and Streams Pool • • Java pool memory is used in server memory for all session-specific Java code and data in the JVM. Streams pool memory is used exclusively by Oracle Streams to: – – Store buffered queue messages Provide memory for Oracle Streams processes Java pool Streams pool Java Pool and Streams Pool Java pool memory is used for all session-specific Java code and data in the JVM. Java pool memory is used in different ways, depending on the mode in which Oracle Database runs. Oracle Streams enables the propagation and management of data, transactions and events in a data stream either within a database, or from one database to another. The Streams pool is used exclusively by Oracle Streams. The Streams pool stores buffered queue messages, and it provides memory for Oracle Streams capture and apply processes. Note: A detailed discussion of Java programming and Oracle Streams is beyond the scope of this course. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 16 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Java Pool and Streams Pool Program Global Area (PGA) • PGA is a memory area that contains: – Session information – Cursor information – SQL execution work areas: — — — — • • Sort area Hash join area Bitmap merge area Bitmap create area Server process User Global Area (UGA) Stack Space User Session Data Cursor Status SQL Area Work area size influences SQL performance. Work areas can be automatically or manually managed. Program Global Area (PGA) The PGA can be compared to a temporary countertop workspace used by a file clerk (the server process) to perform a function on behalf of a customer (client process). The clerk clears a section of the countertop, uses the workspace to store details about the customer’s request, and then gives up the space when the work is done. Generally, the PGA memory is divided into the following areas: • Session memory is the memory allocated to hold a session’s variables (logon information) and other information related to the session. For a shared server, the session memory is shared and not private. • Cursors are handles to private memory structures of specific SQL statements • SQL work areas are allocated to support memory-intensive operators, such as the ones listed in the slide. Generally, bigger work areas can significantly improve the performance of a particular operator at the cost of higher memory consumption. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 17 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Program Global Area (PGA) Background Process RCBG MMON Database buffer cache SGA CKPT PMON CJQ0 QMNn Redo log buffer SMON MMAN Shared pool DBWn LGWR ARCn Background Process The background processes commonly seen in non-RAC, non-ASM environments can include the following: • Database writer process (DBWn): Asynchronously writes modified (dirty) buffers in the database buffer cache to disk • Log writer process (LGWR): Writes the recovery information called redo information in the log buffer to a redo log file on disk • Checkpoint process (CKPT): Records checkpoint information in control files and each data file header • System Monitor process (SMON): Performs recovery at instance startup and cleans up unused temporary segments • Process monitor process (PMON): Performs process recovery when a user process fails • Result cache background process (RCBG): Used to maintain the result cache in the shared pool • Job queue process (CJQ0): Runs user jobs used in batch processing through the Scheduler Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Background Process Archiver processes (ARCn): Copies redo log files to a designated storage device after a log switch has occurred • Queue monitor processes (QMNn): Monitors the Oracle Streams message queues • Manageability monitoring process (MMON): Performs manageability-related background tasks • Memory Manager background process (MMAN): Used to manage SGA and PGA memory components automatically Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 - Page 19 Automatic Shared Memory Management SGA_TARGET + STATISTICS_LEVEL SGA Shared pool Java pool Streams pool Fixed SGA Large pool Database buffer cache Redo log buffer Which size to choose? Automatically tuned SGA components Automatic Shared Memory Management You can use the Automatic Shared Memory Management (ASMM) feature to enable the database to automatically determine the size of each of these memory components within the limits of the total SGA size. Exploring the Oracle Database Architecture Chapter 1 . and any internal allocations during startup. The system uses an SGA size parameter (SGA_TARGET) that includes all the memory in the SGA.Page 20 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic Shared Memory Management . ASMM simplifies the configuration of the SGA by enabling you to specify a total memory amount to be used for all SGA components. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. manually sized components. Oracle and/or its affiliates. according to workload requirements. Note: You must set STATISTICS_LEVEL to TYPICAL or ALL to use ASMM. The Oracle Database then periodically redistributes memory between the automatically tuned components. including all the automatically sized components. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 .Page 21 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automated SQL Execution Memory Management .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Automated SQL Execution Memory Management PGA_AGGREGATE_TARGET Server process … Server process Background process Aggregated … PGA … Which size to choose? Automated SQL Execution Memory Management This feature provides an automatic mode for allocating memory to working areas in the PGA. This feature offers several performance and scalability benefits for decision support system (DSS) workloads and mixed workloads with complex queries. the savings that are gained from improved use of memory translate to better throughput at high loads. The overall system performance is maximized. it is recommended that you leave automatic PGA memory management enabled. In automatic mode. Copyright © 2010. and the available memory is allocated more efficiently among queries to optimize both throughput and response time. you had to manually specify the maximum work area size for each type of SQL operator. such as sort or hash join. Note: In earlier releases of Oracle Database server. You can use the PGA_AGGREGATE_TARGET parameter to specify the total amount of memory that should be allocated to the PGA areas of the instance’s sessions. In particular. Oracle and/or its affiliates. working areas that are used by memory-intensive operators (sorts and hash joins) can be automatically and dynamically adjusted. This proved to be very difficult because the workload changes constantly. Although the current release of Oracle Database supports this manual PGA memory management method that might be useful for specific sessions. Exploring the Oracle Database Architecture Chapter 1 . Automatic memory management automates memory allocation of each SGA component and aggregated PGA. Copyright © 2010. Depending on the database workload. Oracle and/or its affiliates.) Ltd use only Automatic Memory Management Other SGA THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. With Automatic Memory Management. Statistics and memory advisory data are periodically captured in memory by MMON. The Automatic Shared Memory Management feature uses the SGA memory broker that is implemented by two background processes Manageability Monitor (MMON) and Memory Manager (MMAN). redistributing memory as needed between the internal components of the SGA and between the SGA and the aggregated PGAs. MMAN Untunable PGA Free Private SQL areas Buffer cache Large pool Shared pool Java pool Streams pool MEMORY_TARGET + STATISTICS_LEVEL Automatic Memory Management As seen already. Set your MEMORY_TARGET initialization parameter for the database instance and the MMAN background process automatically tunes to the target memory size. MMAN coordinates the sizing of the memory components according to MMON decisions. Note: Currently. HP-UX.Page 22 Oracle University and InfoTech (Pvt.• • • Sizing of each memory component is vital for SQL execution performance. All rights reserved. and Windows. It is difficult to manually size each component. the system automatically adapts the size of each memory’s components to your workload memory needs. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic Memory Management . the size of the various memory areas of the instance directly impacts the speed of SQL processing. it is difficult to size those components manually. AIX. this mechanism is only implemented on Linux. Solaris. These files are critical to the database. If the database server crashes and does not lose any data files.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Without them.Page 23 Oracle University and InfoTech (Pvt. The following additional files are important for the successful running of the database: • Parameter file: Is used to define how the instance is configured when it starts up • Password file: Allows sysdba. physical database structure information). • Data files: Contain the user or application data of the database. and sysasm to connect remotely to the database and perform administrative tasks • Backup files: Are used for database recovery. you cannot open data files to access the data in the database. the instance can recover the database with the information in these files.Database Storage Architecture Control file Data files Online redo log files Parameter file Backup files Archived redo log files Password file Alert log and trace files Database Storage Architecture The files that constitute an Oracle database are organized into the following: • Control file: Contain data about the database itself (that is. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Database Storage Architecture . as well as metadata and the data dictionary • Online redo log files: Allow for instance recovery of the database. sysoper. Oracle and/or its affiliates. All rights reserved. Copyright © 2010. You typically restore a backup file when a media failure or user error has damaged or deleted the original file. Exploring the Oracle Database Architecture Chapter 1 . When an internal error is detected by a process.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using these files and a backup of the database. The alert log of a database is a chronological log of messages and errors. Each instance has one alert log file. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. • Alert log file: These are special trace entries. All rights reserved. Exploring the Oracle Database Architecture Chapter 1 .Page 24 . whereas other information is for Oracle Support Services. Oracle and/or its affiliates. • Trace files: Each server and background process can write to an associated trace file. the process dumps information about the error to its trace file.Archived redo log files: Contain an ongoing history of the data changes (redo) that are generated by the instance. It is recommended that you review this periodically. archive logs enable the recovery of restored data files. That is. Some of the information written to a trace file is intended for the developer. you can recover a lost data file. Oracle University and InfoTech (Pvt. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Each database is logically divided into one or more tablespaces. Tablespaces A database is divided into logical storage units called tablespaces. Tablespaces. or many Undo tablespaces never have 0 Segment Extent Oracle data block OS block Logical and Physical Database Structures The database has logical structures and physical structures. and data files is illustrated in the slide. For example. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Logical and Physical Database Structures . sequences. views. Schemas A schema is a collection of database objects that are owned by a database user. indexes. Databases. Oracle and/or its affiliates. and Data Files The relationship among databases. tablespaces. stored procedures. If it is a TEMPORARY tablespace instead of a tablespace containing data.Page 25 Oracle University and InfoTech (Pvt. such as tables. One or more data files are explicitly created for each tablespace to physically store the data of all logical structures in a tablespace. tablespaces commonly group all of an application’s objects to simplify some administrative operations. 1. Schema objects include structures.Logical and Physical Database Structures Logical Physical Database Schema Tablespace Only 1 with bigfile tablespaces Data file 0. synonyms. Copyright © 2010. You may have a tablespace for different applications. Schema objects are the logical structures that directly refer to the database’s data. which group related logical structures together. the tablespace has a temporary file. Exploring the Oracle Database Architecture Chapter 1 . All rights reserved. to roll back uncommitted transactions for users. All rights reserved. The data of every table in the cluster is stored in the cluster’s data segment. Oracle and/or its affiliates. • Temporary segments: Temporary segments are created by the Oracle Database when a SQL statement needs a temporary work area to complete execution. • Undo segments: One UNDO tablespace is created for each database instance. Specify either a default temporary tablespace for every user. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED clusters. A segment is a set of extents that are allocated for a certain logical structure. Copyright © 2010. Exploring the Oracle Database Architecture Chapter 1 . non-index-organized table has a data segment. . with the exception of external tables and global temporary tables that have no segments. The information in an undo segment is used to generate read-consistent database information and. An extent is a specific number of contiguous data blocks (obtained in a single allocation) that are used to store a specific type of information. schema objects include everything that your application creates in the database. This tablespace contains numerous undo segments to temporarily store undo information. For a partitioned index. A database uses and allocates free database space in Oracle data blocks. Segments The level of logical database storage above an extent is called a segment. For a partitioned table.Page 26 Oracle University and InfoTech (Pvt. When the statement finishes execution.Data Blocks At the finest level of granularity. • Index segments: Each index has an index segment that stores all of its data. the extents of a segment may or may not be contiguous on the disk.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The Oracle Database dynamically allocates space. or a default temporary tablespace that is used across the database. A data block size is specified for each tablespace when it is created. each partition has a data segment. Because extents are allocated as needed. When the existing extents of a segment are full. and partitioned tables in which each table has one or more segments. each partition has an index segment. additional extents are added. and database links. All of the table’s data is stored in the extents of its data segment. the temporary segment’s extents are returned to the instance for future use. during database recovery. One data block corresponds to a specific number of bytes of physical database space on the disk. In general. Each cluster has a data segment. an Oracle database’s data is stored in data blocks. Different types of segments include: • Data segments: Each nonclustered. Extents The next level of logical database space is an extent. If your database supports a data warehouse application that has large tables and indexes. specifying a smaller block size may be beneficial. see the Oracle Database Administrator’s Guide. Segment Extents Data blocks Disk blocks Segments. Exploring the Oracle Database Architecture Chapter 1 . You can have tablespaces with a nonstandard block size. Copyright © 2010. Extents.Segments. Data blocks are mapped to disk blocks. If your database supports a transactional application in which reads and writes are random. you do not need to know the physical address of any of the data in your database. All rights reserved. and Blocks Database objects. the OS maps this to an actual file system or disk block on the storage device. such as tables and indexes are stored as segments in tablespaces. and Blocks • • • • Segments exist in a tablespace. When the database requests a set of data blocks from the operating system (OS). Extents. An extent consists of contiguous data blocks. The minimum Oracle block size is 2 KB. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Segments.Page 27 Oracle University and InfoTech (Pvt. Because of this. The maximum block size depends on your OS. Extents.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This also means that a data file can be striped or mirrored on several disks. Segments are collections of extents. The default size of 8 KB is adequate for most databases. which means that each extent can exist only in one data file. Extents are collections of data blocks. Data blocks are the smallest unit of I/O in the database. The size of the data block can be set at the time of database creation. it should rarely (if ever) be used. For details. Each segment contains one or more extents. a larger block size may be beneficial. Oracle and/or its affiliates. and Blocks . The system default is to create a smallfile tablespace. which are automatically created when the database is created. data dictionary tables). Exploring the Oracle Database Architecture Chapter 1 . The auxiliary SYSAUX tablespace is used for additional database components (such as the Enterprise Manager Repository). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SYSTEM and SYSAUX Tablespaces .Page 28 Oracle University and InfoTech (Pvt.SYSTEM and SYSAUX Tablespaces • • • The SYSTEM and SYSAUX tablespaces are mandatory tablespaces that are created at the time of database creation. You can also create bigfile tablespaces. Copyright © 2010. The SYSTEM tablespace is used for core functionality (for example.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The SYSTEM tablespace is always online when the database is open. whereas this is not possible in the case of the SYSTEM tablespace. They must be online. such as the data dictionary tables. and it must be online for the correct functioning of all database components. Note: The SYSAUX tablespace may be taken offline for performing tablespace recovery. The SYSAUX tablespace is an auxiliary tablespace to the SYSTEM tablespace. Neither of them may be made read-only. which enable the Oracle database to manage ultra large files (up to 8 exabytes in size). A tablespace can be online (accessible) or offline (not accessible). All rights reserved. The SYSAUX tablespace stores many database components. It stores tables that support the core functionality of the database. SYSTEM and SYSAUX Tablespaces Each Oracle Database must contain a SYSTEM tablespace and a SYSAUX tablespace. Oracle and/or its affiliates. Quiz The first time an Oracle Database server process requires a particular piece of data. Shared Pool Answer: a Copyright © 2010. it searches for the data in the: a. Exploring the Oracle Database Architecture Chapter 1 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Database Buffer Cache b.Page 29 Oracle University and InfoTech (Pvt. Redo Log Buffer d. Oracle and/or its affiliates. PGA c. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Exploring the Oracle Database Architecture Chapter 1 .Page 30 Oracle University and InfoTech (Pvt. Schema d. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .Quiz Which of the following is not a database logical structure? a. Data File c. Segment Answer: b Copyright © 2010. Oracle and/or its affiliates. Tablespace b. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Exploring the Oracle Database Architecture Chapter 1 .Page 31 Oracle University and InfoTech (Pvt. a. False Answer: b Copyright © 2010. True b. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . All rights reserved.Quiz The SYSAUX tablespace is used for core functionality and the SYSTEM tablespace is used for additional database components such as the Enterprise Manager Repository. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary . you should have learned how to: • List the major architectural components of the Oracle Database server • Explain memory structures • Describe background processes • Correlate logical and physical storage structures Copyright © 2010. Exploring the Oracle Database Architecture Chapter 1 .Summary In this lesson. All rights reserved. Oracle and/or its affiliates.Page 32 Oracle University and InfoTech (Pvt. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 1: Overview .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Exploring the Oracle Database Architecture Chapter 1 .Page 33 Oracle University and InfoTech (Pvt.Practice 1: Overview This practice covers the following topics: • Listing the different components of an Oracle Database server • Looking at some instance and database components directly on your machine Copyright © 2010. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 34 . Exploring the Oracle Database Architecture Chapter 1 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010. ) Ltd use only Introduction to SQL Tuning . Introduction to SQL Tuning Chapter 2 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 2 Copyright © 2010. All rights reserved.Page 1 Oracle University and InfoTech (Pvt.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Introduction to SQL Tuning Copyright © 2010.) Ltd use only Introduction to SQL Tuning . Oracle and/or its affiliates. Introduction to SQL Tuning Chapter 2 . All rights reserved.Page 2 Oracle University and InfoTech (Pvt. Introduction to SQL Tuning Chapter 2 . All rights reserved. Oracle and/or its affiliates.Objectives After completing this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010.Page 3 Oracle University and InfoTech (Pvt. you should be able to: • Describe what attributes of a SQL statement can make it perform poorly • List the Oracle tools that can be used to tune SQL • List the tuning tasks Objectives This lesson gives you an understanding of the tuning process and the different components of an Oracle Database that may require tuning. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Reasons for Inefficient SQL Performance . cardinality.Page 4 Oracle University and InfoTech (Pvt. the CBO can easily be mislead and generate suboptimal execution plans. • Suboptimal execution plan selection: The CBO can sometimes select a suboptimal execution plan for a SQL statement. are just a couple of examples of inefficient SQL design.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. or predicate selectivity. such as indexes. or the use of more expensive SQL constructs like UNION in place of UNION ALL. • Poorly constructed SQL: If the SQL statement is designed poorly. All rights reserved. The right set of access structures can improve SQL performance by several orders of magnitude. This happens for most part because of incorrect estimates of some attributes of that SQL statement. materialized views. Without accurate optimizer statistics. Oracle and/or its affiliates.Reasons for Inefficient SQL Performance • • • • Stale or missing optimizer statistics Missing access structures Suboptimal execution plan selection Poorly constructed SQL Reasons for Inefficient SQL Performance SQL statements can perform poorly for a variety of reasons: • Stale optimizer statistics: SQL execution plans are generated by the cost-based optimizer (CBO). such as its cost. Introduction to SQL Tuning Chapter 2 . and partitions is a common reason for poor SQL performance. Copyright © 2010. For CBO to effectively choose the most efficient plan. • Missing access structures: Absence of access structures. A missing join condition leading to a Cartesian product. it needs accurate information on the data volume and distribution of tables and indexes referenced in the queries. there is not much that the optimizer can do to improve its performance. such as memory. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED These four main causes of poor SQL optimization can have a drastic impact on performance. All rights reserved. Copyright © 2010. and so on. I/Os. Oracle University and InfoTech (Pvt.Note: Additional reasons for poor performance might be connected with hardware-related issues.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 5 . Oracle and/or its affiliates. Introduction to SQL Tuning Chapter 2 . CPUs. employee_id). Use a simple equality.Inefficient SQL: Examples 1 SELECT COUNT(*) FROM products p WHERE prod_list_price < 1. AVG(unit_cost) ac FROM costs GROUP BY prod_id) c WHERE p.15 * (SELECT avg(unit_cost) FROM costs c WHERE c. (SELECT prod_id. This is a common business question type.Page 6 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The query determines how many products have list prices less than 15% above the average cost of the product.prod_id = c. The query is better written as: SELECT COUNT(*) FROM products p. Oracle and/or its affiliates.prod_id) 2 SELECT * FROM job_history jh. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Inefficient SQL: Examples . which means that the subquery is run for every row found in the outer query. a function-based index may be necessary.2) 3 SELECT * FROM orders WHERE order_id_char = 1205 4 SELECT * FROM employees WHERE to_char(salary) = :sal 5 SELECT * FROM parts_old UNION SELECT * FROM parts_new Inefficient SQL: Examples The slide shows five examples of possibly poorly constructed SQL that could easily result in inefficient execution. This statement has a correlated subquery.prod_id = p.prod_id AND p. 1.2) = substr(to_char(jh.employee_id). 3. the ORDER_ID_CHAR column is a character type. All rights reserved.prod_list_price < 1. You should make the literal match the column type. Introduction to SQL Tuning Chapter 2 . This query applies functions to the join columns. and the constant is a numeric type.15 * c. Copyright © 2010. This query has a condition that forces implicit data type conversion. if you can. restricting the conditions where indexes can be used. Otherwise. employees e WHERE substr(to_char(e.ac 2. Copyright © 2010. If you know that there are no rows in common between the two UNIONed queries. and not convert the column. This means that the function is called for every row in the table. This is better queried as: SELECT * FROM employees WHERE salary = TO_NUMBER(:sal) . All rights reserved. ensures that there are no duplicate rows in the result set.Page 7 Oracle University and InfoTech (Pvt. use UNION ALL instead of UNION. The fourth query uses a data type conversion function in it to make the data types match in the comparison. as opposed to the UNION ALL operator. this requires an extra step. The UNION operator. a unique sort. The problem here is that the TO_CHAR function is applied to the column value. It would be better to convert the literal once.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. This eliminates the unnecessary sort. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 4. Introduction to SQL Tuning Chapter 2 .5. However. to eliminate any duplicates. rather than to the constant. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Performance Monitoring Solutions . Oracle Database 10g introduced new methodologies to monitor your database in two categories: • Proactive monitoring: - Automatic Database Diagnostic Monitor (ADDM) automatically identifies bottlenecks within the Oracle Database. and maintains performance statistics for problem detection and self-tuning purposes. AWR compares the difference between snapshots to determine which SQL statements to capture based on the effect on the system load. working with other Copyright © 2010. Oracle and/or its affiliates. such as Statspack. Active sessions are sampled every second and are stored in a circular buffer in SGA. processes. Snapshots: Snapshots are sets of historical data for specific time periods that are used for performance comparisons by ADDM.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This data is both in memory and stored in the database. and performance views. Introduction to SQL Tuning Chapter 2 . Active Session History (ASH): It provides sampled session activity in the instance. The gathered data can be displayed in both reports and views.Page 8 Oracle University and InfoTech (Pvt. SQL trace files. This reduces the number of SQL statements that must be captured over time.Performance Monitoring Solutions Foreground Automatic In-memory statistics MMON SGA 60 min AWR ASH ADDM Snapshots Alerts Snapshots AST Statspack ADDM results AWR report ASH: Active Session History AWR: Automatic Workload Repository ADDM: Automatic Database Diagnostic Monitor Performance Monitoring Solutions Automatic Workload Repository (AWR): It collects. All rights reserved. Automatic Database Diagnostic Monitor (ADDM) In addition to the classical reactive tuning capabilities of previous releases. Additionally. and implementing the resulting SQL profile recommendations to tune the statement without requiring user intervention. Enterprise Manager provides an integrated performance management console that uses all relevant data sources. Introduction to SQL Tuning Chapter 2 . running SQL Tuning Advisor on them. the Oracle Database sends you an alert message with possible remedial action. Reactive monitoring: - Server-generated alerts: The Oracle Database can automatically detect problematic situations. Using a drill-down method. In reaction to a detected problem. New data sources are introduced to capture important information about your database’s health—for example. ADDM makes recommendations on the options available for fixing these bottlenecks. This automation uses the AUTOTASK framework through a new task called Automatic SQL Tuning Task that runs every night by default. new memory statistics (for current diagnostics) as well as statistics history stored in Automatic Workload Repository (AWR). Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. .Page 9 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED manageability components.- • Oracle Database 11g further automates the SQL tuning process by identifying problematic SQL statements. All rights reserved. you can manually identify bottlenecks with just a few mouse clicks. Note: Accessing Enterprise Manager or tools discussed here may require additional licenses and certain privileges generally reserved for database administrators. - The Oracle Database has powerful new data sources and performance-reporting capabilities. Monitoring and Tuning Tools: Overview Services Alert log SQL traces tkprof trcsess Optimizer statistics Perf views Direct ADDM Hang SGA analyzer and advisors monitor SPA AWR baseline SQL statistics System Session statistics EM Metric AWR perf base reports pages line Statspack Base/ Segment statistics Wait model Histograms Time model Metrics OS statistics Service statistics ASH Alerts SQL report Compared periods ASH report Monitoring and Tuning Tools: Overview Since Oracle Database 10g. Note • SPA stands for SQL Performance Analyzer.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 10 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.sql in Statspack. Introduction to SQL Tuning Chapter 2 . Release 2. the equivalent to sqrepsql. basically. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Monitoring and Tuning Tools: Overview . Copyright © 2010. you can generate SQL reports from AWR data ($ORACLE_HOME/rdbms/admin/awrsqrpt. All rights reserved.sql). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED EM Performance Pages for Reactive Tuning . Introduction to SQL Tuning Chapter 2 . or you see a sudden spike in the activity of the system on the monitor. Oracle and/or its affiliates. The Enterprise Manager (EM) Performance pages use the same data sources as AWR and ADDM to display information about the running of the database and the host system in a manner that is easily absorbed and allows for rapid manual drilldown to the source of the problem.Page 11 Oracle University and InfoTech (Pvt. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010.EM Performance Pages for Reactive Tuning ASH Report Home Run ADDM Performance Nonidle wait classes Top Consumers Top Activity Wait class details Top SQL Duplicate Blocking Hang Instance SQL Sessions Analysis Locks Top Sessions Top Services Top Modules Top Actions Instance Activity Top Files AWR Baselines Top Objects SPA Wait event histograms SQL Tuning Advisor SQL Tuning Sets Enable/Disable aggregation View Enable/Disable SQL trace file SQL trace System statistics EM Performance Pages for Reactive Tuning There are cases where real-time problem diagnosis must be performed. An irate user calls you. and partitions SQL Performance Analyzer: Automates the process of assessing the overall effect of a change. For example. By creating a SQL baseline. SQL Tuning Sets: Serve as a repository for sets of SQL statements. SPM will allow only approved execution plans to be used. to perform analysis on different machines. SQL Access Advisor: Analyzes a SQL statement. By default. such as upgrading a database or adding new indexes. Other plans Copyright © 2010. You can also. They can even be transported from database to database. run the SQL Tuning Advisor on a specific SQL workload to look for ways to improve performance. the SQL Tuning Advisor can run against a workload that is represented by a SQL Tuning Set.Tuning Tools: Overview • • • • • • • Automatic Database Diagnostic Monitor (ADDM) SQL Tuning Advisor SQL Tuning Sets SQL Access Advisor SQL Performance Analyzer SQL Monitoring SQL Plan Management Tuning Tools: Overview Automatic Database Diagnostic Monitor: Continually analyzes the performance data that is collected from the database instance SQL Tuning Advisor: Analyzes SQL statements that have been identified as problematic. on the full SQL workload by identifying performance divergence for each statement SQL Monitoring: Enables you to monitor the performance of SQL statements while they execute SQL Plan Management (SPM): Can be used to control execution plan evolution.Page 12 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Tuning Tools: Overview . and provides advice on materialized views. this is an automated task. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. indexes. in an effort to retune them. Introduction to SQL Tuning Chapter 2 . Oracle and/or its affiliates. at any time. materialized view logs. Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED discovered by the optimizer will be stored in the SQL plan history. but will not be used until they are approved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Copyright © 2010. Introduction to SQL Tuning Chapter 2 . All rights reserved.Page 13 . For example. but it may depend on a new index being built. • Often.SQL Tuning Tasks: Overview • • • • • • Identifying high-load SQL Gathering statistics Generating system statistics Rebuilding existing indexes Maintaining execution plans Creating new index strategies SQL Tuning Tasks: Overview Many SQL tuning tasks should be performed on a regular basis. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Tasks: Overview . Oracle and/or its affiliates. This list of tasks gives you a background of some important tasks that must be performed. Introduction to SQL Tuning Chapter 2 . All rights reserved. the Oracle Database gathers optimizer statistics automatically. there is a beneficial impact on performance by rebuilding indexes. The ADDM is the ideal tool for this particular task. You may see a way to rewrite a WHERE clause. For this. Copyright © 2010. and gives you an idea of what dependencies you may have as you tune SQL: • Identifying high-load SQL statements is one of the most important tasks you should perform. removing nonselective indexes to speed the data manipulation language (DML). • By default. or adding columns to the index to improve selectivity.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a job is scheduled to run in the maintenance windows. • You can maintain the existing execution plan of SQL statements over time by using stored statistics or SQL plan baselines. • Operating system statistics provide information about the usage and performance of the main hardware components as well as the performance of the operating system itself.Page 14 Oracle University and InfoTech (Pvt. On the other hand. or nodes to a cluster. Introduction to SQL Tuning Chapter 2 .Page 15 Oracle University and InfoTech (Pvt. a system where the proportion of CPU time does not decrease significantly as load increases can scale better. Oracle and/or its affiliates. Although the proportion of CPU time to wait time always tends to decrease as load on the system increases. As a general rule. CPU time Possibly needs SQL tuning Scalable application Scalable application Needs instance/RAC tuning No gain achieved by adding CPUs/nodes Wait time CPU and Wait Time Tuning Dimensions When you tune your system.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Conversely. steep increases in wait time are a sign of contention and must be addressed for good scalability. Note: AWR reports display CPU time together with wait time in the Top 5 Timed Events section. it is important that you compare the CPU time with the wait time of your system. would provide very limited benefit under contention. All rights reserved. and would most likely benefit from adding CPUs or Real Application Clusters (RAC) instances if needed. the systems where CPU time is dominant usually need less tuning than the ones where wait time is dominant. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED CPU and Wait Time Tuning Dimensions . Copyright © 2010. Adding more CPUs to a node. you can determine how much of the response time is spent on useful work and how much on waiting for resources potentially held by other processes.CPU and Wait Time Tuning Dimensions Scalability is a system’s ability to process more workload with a proportional increase in system resource use. high CPU usage can be caused by badly-written SQL statements. By comparing CPU time with wait time. if the CPU time portion is among the top five events. • Poor connection management can cause unsatisfactory response times. • Infrequent transaction COMMITs or ROLLBACKs can cause long locks on resources. resulting in a higher number of logical input/output (I/O) for the same number of rows returned • Reduced availability because database objects take longer to maintain However. This affects performance and availability. implementation.Page 16 Oracle University and InfoTech (Pvt. • The production environment can use different execution plans than those generated in testing. • Poor schema design can cause expensive SQL that does not scale. and configuration have a significant impact on scalability. All rights reserved. The physical implementation of the application can be the weak link. • Memory-intensive applications that allocate a large amount of memory without much thought for freeing the memory can cause excessive memory fragmentation. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Scalability with Application Design. and Configuration Poor application design. as in the following examples: • Systems can move to production environments with poorly written SQL that cause high I/O.Scalability with Application Design. Implementation. Implementation. and Configuration Applications have a significant impact on scalability. Implementation. • Poor transaction design can cause locking and serialization problems.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and Configuration . Introduction to SQL Tuning Chapter 2 . Scalability with Application Design. This results in: • Poor SQL and index design. Oracle and/or its affiliates. design is not the only problem. Copyright © 2010. • Inefficient memory usage places high stress on the operating virtual memory subsystem. 6.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. 8. This has an order of magnitude impact on performance. 3. and it is not scalable. 2. This problem is common with stateless middleware in application servers. Bad connection management Bad use of cursors and the shared pool Excess of resources consuming SQL statements Use of nonstandard initialization parameters Poor database disk configuration Redo log setup problems Excessive serialization Inappropriate full table scans Large number of recursive SQL statements related to space management or parsing activity 10. 5. Oracle and/or its affiliates. Introduction to SQL Tuning Chapter 2 . Likewise. Bad use of cursors and the shared pool: Not using cursors results in repeated parses. Deployment and migration errors Common Mistakes on Customer Systems 1. If bind variables are not used.Common Mistakes on Customer Systems 1. Bad SQL: Bad SQL is SQL that uses more resources than appropriate for the application. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Common Mistakes on Customer Systems . undocumented optimizer features can cause a great deal of problems that may require considerable investigation.Page 17 Oracle University and InfoTech (Pvt. ADDM identifies high-load SQL and the SQL Tuning Advisor can be used to provide recommendations for improvement. 4. All rights reserved. Be suspicious of applications generating dynamic SQL. optimizer parameters set in the initialization parameter file can Copyright © 2010. In particular. SQL that consumes significant system resources should be investigated for potential improvement. 7. and is not scalable. Use cursors with bind variables that open the cursor and execute it many times. there may be hard parsing of all similar SQL statements. This can be a decision support system (DSS) query that runs for more than 24 hours or a query from an online application that takes more than a minute. 3. Most systems give acceptable performance using only the set of basic parameters. It has over two orders of magnitude impact on performance. 9. 2. Bad connection management: The application connects and disconnects for each database interaction. 4. Use of nonstandard initialization parameters: These might have been implemented based on poor advice or incorrect assumptions. or poor SQL optimization. Deployment and migration errors: In many cases. When migrating applications of known performance. and optimizer settings should be managed together as a group to ensure consistency of performance. the archive cannot keep up. This is not scalable and impacts user response time.5.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED override proven optimal execution plans. Long full table scans: Long full table scans for high-volume or interactive online operations could indicate poor transaction design. Small redo logs cause system checkpoints to continuously put a high load on the buffer cache and the I/O system. so this is not a problem. High amounts of recursive (SYS) SQL: Large amounts of recursive SQL executed by SYS could indicate that space management activities. by nature. Redo log setup problems: Many sites run with too small redo log files. an application uses too many resources because the schema owning the tables has not been successfully migrated from the development environment or from an older implementation. and the database waits for the archive process to catch up. 6. 8. Other sites specify the number of disks incorrectly because they configure disks by disk space and not by I/O bandwidth.Page 18 Oracle University and InfoTech (Pvt. export the schema statistics to maintain plan stability using the DBMS_STATS package. Use Automatic Segment Space Management (ASSM) and automatic undo management to solve this problem. 9. are I/O-intensive and not scalable. Use locally managed tablespaces to reduce recursive SQL due to extent allocation. schema statistics. 7. Copyright © 2010. All rights reserved. For these reasons. ADDM highlights the resulting highload SQL. Oracle and/or its affiliates. Recursive SQL executed under another user ID is probably SQL and PL/SQL. Excessive serialization: Serialization of data blocks in the buffer cache due to shortage of undo segments is particularly common in applications with large numbers of active users and a few undo segments. such as extent allocations. Getting database I/O wrong: Many sites lay out their databases poorly over the available disks. Long table scans. missing indexes. Examples of this are missing indexes or incorrect statistics. Introduction to SQL Tuning Chapter 2 . These errors can lead to suboptimal execution plans and poor interactive user performance. . If there are very few redo logs. Although these errors are not directly detected by ADDM. take place. 10. schemas. and maintenance stages. The slide lists some of the issues that affect performance and that should be tuned proactively instead of reactively. through the analysis. tuning should be part of the life cycle of an application.Page 19 Oracle University and InfoTech (Pvt. However. where the most important bottleneck is identified and fixed. Oracle and/or its affiliates.Proactive Tuning Methodology • • • • • • • Simple design Data modeling Tables and indexes Using views Writing efficient SQL Cursor sharing Using bind variables Proactive Tuning Methodology Tuning usually implies fixing a performance problem. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Proactive Tuning Methodology . production. The tuning phase is often left until the system is in production. These are discussed in more detail in the following slides. All rights reserved. coding. design.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. At that time. Introduction to SQL Tuning Chapter 2 . Copyright © 2010. tuning becomes a reactive exercise. or table design. This principle should always be kept in mind when building applications. • If there are many indexes on a table and the same columns are repeatedly indexed.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. • If SQL statements are so long and involved that it would be impossible for any optimizer to effectively optimize it in real time. there is probably a bad index design. Consider some of the following design issues: • If the table design is so complicated that nobody can fully understand it. If the design looks right.Page 20 Oracle University and InfoTech (Pvt. the table is probably designed badly. • If queries are submitted without suitable qualification (the WHERE clause) for rapid response for online users.Simplicity in Application Design • • • • Simple tables Well-written SQL Indexing only as required Retrieving only required information Simplicity in Application Design Applications are no different from any other designed and engineered product. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Simplicity in Application Design . All rights reserved. underlying transaction. it probably is right. there is probably a bad statement. there is probably a bad user interface or transaction design. Copyright © 2010. Introduction to SQL Tuning Chapter 2 . A good data model ultimately means that your queries are written more efficiently. It is then easier to perform the next step of creating tables.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Data Modeling • • • • Accurately represent business practices Focus on the most frequent and important business transactions Use modeling tools Appropriately normalize data (OLTP versus DW) Data Modeling Data modeling is important in successful relational application design. This should be done in a way that quickly and accurately represents the business practices. and indexes.Page 21 Oracle University and InfoTech (Pvt. Copyright © 2010. Introduction to SQL Tuning Chapter 2 . you have a clear picture of the keys and relationships. Apply your greatest modeling efforts to those entities affected by the most frequent business transactions. All rights reserved. Normalizing data prevents duplication. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Data Modeling . constraints. When data is normalized. Use of modeling tools can then rapidly generate schema definitions and can be useful when a fast prototype is required. Page 22 Oracle University and InfoTech (Pvt. a noncore table becomes a performance problem during prototyping and testing.Table Design • Compromise between flexibility and performance: – Principally normalize – Selectively denormalize • Use Oracle performance and management features: – – – – – • Default values Constraints Materialized views Clusters Partitioning Focus on business-critical tables Table Design Table design is largely a compromise between flexibility and performance of core transactions. however. Introduction to SQL Tuning Chapter 2 . Design should be focused on business-critical tables so that good performance can be achieved in areas that are the most used. and it should be normalized to at least third normal form. If. For noncritical tables. Copyright © 2010. Additionally. remedial design efforts should be applied immediately. the table design should be very similar to the data model. and using materialized views or partitioned tables. Use the features supplied with Oracle Database to simplify table design for performance. However. certain core transactions can require selective denormalization for performance purposes. adding derived columns and aggregate values. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Table Design . such as storing tables prejoined in clusters. All rights reserved. create check constraints and columns with default values to prevent bad data from getting into the tables. To keep the database flexible and able to accommodate unforeseen workloads. shortcuts in design can be adopted to enable a more rapid application development. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and any required join or sort columns.Index Design • Create indexes on the following: – Primary key (can be automatically created) – Unique key (can be automatically created) – Foreign keys (good candidates) • • Index data that is frequently queried (select list). Primary keys and unique keys are automatically indexed except for the DISABLE VALIDATE and DISABLE NOVALIDATE RELY constraints. This technique is particularly useful in speeding up an online application’s response times when timeconsuming I/Os are reduced. The following indexing design ideas should be considered when building a new index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The most aggressive form of this technique is to build an index-organized table (IOT). Oracle and/or its affiliates. This can be done by creating an index over all the columns of the table referenced by the query. Copyright © 2010. However. These columns are the select list columns. Appending Columns to an Index or Using Index-Organized Tables One of the easiest ways to speed up a query is to reduce the number of logical I/Os by eliminating a table scan from the execution plan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Design . This is best applied when testing the application with properly-sized data for the first time. WHERE clause columns. such as a person’s name.Page 23 Oracle University and InfoTech (Pvt. certain queries need performance improvements. As the application evolves and testing is performed on realistic sizes of data. Use SQL as a guide to index design. All rights reserved. Introduction to SQL Tuning Chapter 2 . Index Design Index design is also a largely iterative process based on the SQL that is generated by application designers. it is possible to make a sensible start by building indexes that enforce foreign key constraints (to reduce response time on joins between primary key tables and foreign key tables) and creating indexes on frequently accessed data. for which building a better index is a good solution. In many cases.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. display. The worst type of view use is creating joins on views that reference other views. and store data.Page 24 Oracle University and InfoTech (Pvt. However. A simple view definition can mask data model complexity from the programmers whose priorities are to retrieve. views usually make it difficult for the optimizer to generate the optimal execution plan. collect. Because of their inherent properties. though views provide clean programming interfaces. Views are often used to provide simple row and column-level access restrictions. which in turn reference other views. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Views . they can cause suboptimal. resource-intensive queries when nested too deeply. Introduction to SQL Tuning Chapter 2 .Using Views • • • Simplifies application design Is transparent to the developer Can cause suboptimal execution plans Using Views Views can speed up and simplify application design. developers can satisfy the query directly from the table without using a view. All rights reserved. Copyright © 2010. Oracle and/or its affiliates. Good cursor usage and management: Maintaining user connections is equally important for minimizing the parsing activity on the system. where application servers are used to multiplex database connections to users. There are two types of parse operations: • Hard parsing: A SQL statement is submitted for the first time. design efforts should ensure that database connections are pooled and not reestablished for each user request. Oracle and/or its affiliates. in a Web-based or multitiered application. security checking.SQL Execution Efficiency • • • • Good database connectivity Minimizing parsing Share cursors Using bind variables SQL Execution Efficiency An application that is designed for SQL execution efficiency must support the following characteristics: Good database connection management: Connecting to the database is an expensive operation that is not scalable. A simple system. and no match is found in the shared pool. With these types of applications. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Execution Efficiency . This process has many phases. However.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. where a user connects at application initialization. this can be difficult.Page 25 Oracle University and InfoTech (Pvt. Hard parses are the most resource-intensive and are not scalable because they perform all the operations involved in a parse. and loading shared structures into the shared pool. the number of concurrent connections to the database should be minimized as much as possible. Introduction to SQL Tuning Chapter 2 . Parsing is the process of interpreting a SQL statement and creating an execution plan for it. execution plan generation. including syntax checking. is ideal. Therefore. All rights reserved. All rights reserved.Page 26 Oracle University and InfoTech (Pvt. Application developers must also ensure that SQL statements are shared within the shared pool.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Experienced SQL programmers should be familiar with the concept of opening and reexecuting cursors. The match can be the result of previous execution by another user. To do this. which consume system resources. Because parsing should be minimized as much as possible. the SQL statement is likely to be parsed once and never reused by other users. soft parses are not ideal because they still require syntax and security checking. which is good for performance. However.Soft parsing: A SQL statement is submitted for the first time. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • . If this is not done. To ensure that SQL is shared. application developers should design their applications to parse SQL statements once and execute them many times. Copyright © 2010. Oracle and/or its affiliates. This is done through cursors. use bind variables and do not use string literals with SQL statements. Introduction to SQL Tuning Chapter 2 . The SQL statement is shared. and a match is found in the shared pool. bind variables to represent the parts of the query that change from execution to execution. Introduction to SQL Tuning Chapter 2 . • Write referenced triggers and procedures when using application development tools. Oracle and/or its affiliates. SQL scripts. including those in PL/SQL code. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Writing SQL to Share Cursors . and Oracle Call Interface (OCI) calls. • Develop rules for the use of white space (spaces. Use generic shared code: • Write and store procedures that can be shared across applications. • Develop rules for the use of uppercase and lowercase characters.” You should develop coding conventions for SQL statements in ad hoc queries. even if you use some special initialization parameters. All rights reserved. • Write library routines and procedures in other environments. Copyright © 2010.Page 27 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Use database triggers.Writing SQL to Share Cursors • Create generic code using the following: – Stored procedures and packages – Database triggers – Any other library routines and procedures • Write to format standards (improves readability): – – – – – Case White space Comments Object references Bind variables Writing SQL to Share Cursors Applications can share cursors when the code is written in the same way characterwise (which allows the system to recognize that two statements are the same and thus can be shared). such as CURSOR_SHARING discussed later in the lesson titled “Using Bind Variables. Write to format standards: • Develop format standards for all statements. returns). tabs. If possible.Develop rules for the use of comments (preferably keeping them out of the SQL statements themselves). Introduction to SQL Tuning Chapter 2 . prefix each object with a schema name. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010.Page 28 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Oracle University and InfoTech (Pvt. • Use the same names to refer to identical database objects. All rights reserved. Oracle and/or its affiliates. Performance Checklist • Set the minimal number of initialization parameters. Copyright © 2010. If there is more tuning to perform. • Validate that the SQL statements use cursors efficiently. This includes tables. indexes. this shows up when the system is under load. This first set of statistics validates or corrects any assumptions made in the design and rollout process. Ideally. procedures. Set storage options for tables and indexes in appropriate tablespaces. This happens mostly when bind variables are not used properly and the WHERE clause predicates are sent as string literals. synonyms.Page 29 Oracle University and InfoTech (Pvt. Validate connections by middleware. packages.Performance Checklist • • • • • • Set initialization parameters and storage options. Verify validity and availability of optimizer statistics. Validate migration of all required objects. Introduction to SQL Tuning Chapter 2 . triggers. sequences. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. grants. • Verify that all SQL statements are optimal and understand their resource usage. most initialization parameters should be left at default. • Validate that middleware and programs that connect to the database are efficient in their connection management and do not log on and log off repeatedly. and views. Verify cursor sharing. • As soon as the system is rolled out. functions. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Performance Checklist . Verify resource usage of SQL statements. Each SQL statement should be parsed once and then executed multiple times. establish a baseline set of statistics from the database and operating system. • Validate that all schema objects are correctly migrated from the development environment to the production database. Ensure that any modifications made in testing are made to the production system. Java objects. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Development Environments: Overview . and is the default tool for this class. including simple instructions on installing version 2. Oracle and/or its affiliates. Copyright © 2010. Note: See Appendix C titled “Using SQL Developer” for information about using SQL Developer. All rights reserved.1. SQL*Plus The SQL*Plus environment may also be used to run all SQL commands covered in this course.Page 30 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Development Environments: Overview • SQL Developer • SQL*Plus SQL Developer PL/SQL Development Environments SQL Developer This course has been developed using Oracle SQL Developer as the tool for running the SQL statements discussed in the examples in the slide and the practices. Introduction to SQL Tuning Chapter 2 . • SQL Developer is shipped with Oracle Database 11g Release 2. Appendix C Appendix C of this course provides an introduction on using the SQL Developer interface. Introduction to SQL Tuning Chapter 2 . When connected. and view optimizer plans. interacting with data using SQL and PL/SQL.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. You can connect to any target Oracle database schema using standard Oracle database authentication. simplifies the following tasks: • Browsing and managing database objects • Executing SQL statements and scripts • Editing and debugging PL/SQL statements • Creating reports You can connect to any target Oracle database schema using standard Oracle database authentication.What Is Oracle SQL Developer? • • • • Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database development tasks. Appendix C contains details on using SQL Developer SQL Developer What Is Oracle SQL Developer? Oracle SQL Developer is a free graphical tool designed to improve your productivity and simplify the development of everyday database tasks. test SQL statements. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED What Is Oracle SQL Developer? . All rights reserved. you can easily create and maintain stored procedures. Oracle and/or its affiliates. SQL Developer. you can perform operations on objects in the database.Page 31 Oracle University and InfoTech (Pvt. You will use SQL Developer in this course. and so on. the visual tool for database development. With just a few clicks. It also provides information about creating a database connection. Introduction to SQL Tuning Chapter 2 . Oracle and/or its affiliates. Note • To launch SQL*Plus in the Linux environment. remember the following: • You create subprograms by using the CREATE SQL statement.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 32 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Coding PL/SQL in SQL*Plus . • You execute subprograms by using either an anonymous PL/SQL block or the EXECUTE command. All rights reserved. Copyright © 2010. • If you use the DBMS_OUTPUT package procedures to print text to the screen. SQL*Plus is: • Shipped with the database • Accessed from an icon or the command line When coding PL/SQL subprograms using SQL*Plus.Coding PL/SQL in SQL*Plus Coding PL/SQL in SQL*Plus Oracle SQL*Plus is a command-line interface that enables you to submit SQL statements and PL/SQL blocks for execution and receive the results in an application or a command window. open a Terminal window and enter the sqlplus command. you must first execute the SET SERVEROUTPUT ON command in your session. All rights reserved. Introduction to SQL Tuning Chapter 2 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. see the SQL*Plus User's Guide and Reference.For more information about how to use SQL*Plus. Oracle and/or its affiliates.Page 33 . AWR c. ADDM d. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .Page 34 Oracle University and InfoTech (Pvt. All rights reserved. a.Quiz _________automatically identifies bottlenecks within Oracle Database and makes recommendations on the options available for fixing these bottlenecks. Snapshots Answer: c Copyright © 2010. ASH b.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Introduction to SQL Tuning Chapter 2 . Oracle and/or its affiliates. which ultimately means that your queries are written more efficiently. Oracle and/or its affiliates.Quiz Normalizing data results in a good data model. Introduction to SQL Tuning Chapter 2 . All rights reserved.Page 35 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . True b.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. False Answer: a Copyright © 2010. a. False Answer: a Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . resource-intensive queries when nested too deeply. Oracle and/or its affiliates. All rights reserved. they can cause suboptimal. True b.Page 36 Oracle University and InfoTech (Pvt.Quiz Views should be used carefully because. a.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. though they provide clean programming interfaces. Introduction to SQL Tuning Chapter 2 . Answer: b. Use cursors so that SQL statements are parsed once and executed multiple times. Use concurrent connections to the database. c Copyright © 2010. b.Page 37 Oracle University and InfoTech (Pvt. a.Quiz Identify the characteristics that must be supported by an application designed for SQL execution efficiency.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. c. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Oracle and/or its affiliates. Use bind variables. Introduction to SQL Tuning Chapter 2 . Introduction to SQL Tuning Chapter 2 . Oracle and/or its affiliates. you should have learned how to: • Describe what attributes of a SQL statement can make it perform poorly • List the Oracle tools that can be used to tune SQL • List the tuning tasks Copyright © 2010. All rights reserved.Summary In this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 38 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 2: Overview Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Practice 2: Overview This practice covers the following topics: • Rewriting queries for better performance • Rewriting applications for better performance Oracle University and InfoTech (Pvt.Page 39 . Introduction to SQL Tuning Chapter 2 . Page 40 . All rights reserved. Introduction to SQL Tuning Chapter 2 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Oracle University and InfoTech (Pvt. Page 1 Oracle University and InfoTech (Pvt. Introduction to the Optimizer Chapter 3 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 3 Copyright © 2010.) Ltd use only Introduction to the Optimizer . All rights reserved. Introduction to the Optimizer Chapter 3 .Page 2 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.) Ltd use only Introduction to the Optimizer . All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Introduction to the Optimizer Copyright © 2010. Objectives After completing this lesson. Oracle and/or its affiliates.Page 3 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . All rights reserved. you should be able to: • Describe the execution steps of a SQL statement • Discuss the need for an optimizer • Explain the various phases of optimization • Control the behavior of the optimizer Copyright © 2010. Introduction to the Optimizer Chapter 3 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. DML. • Session Control statements manage the properties of a particular user’s session. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Structured Query Language . Oracle and/or its affiliates. You can categorize SQL statements into six main sets: • Data manipulation language (DML) statements manipulate or query data in existing schema objects. This incorporation is done using the statements listed in the slide under the ESS category. and TCS within a procedural language program. • System Control statements change the properties of the Oracle Database instance. and drop schema objects. Copyright © 2010.Structured Query Language DML ESS DDL INSERT UPDATE DELETE MERGE SELECT TCS COMMIT ROLLBACK SAVEPOINT SET TRANSACTION DECLARE CONNECT OPEN CLOSE DESCRIBE WHENEVER PREPARE EXECUTE FETCH CREATE DROP ALTER RENAME TRUNCATE GRANT REVOKE AUDIT NOAUDIT COMMENT SessionCS SystemCS ALTER SESSION SET ROLE ALTER SYSTEM Structured Query Language All programs and users access data in an Oracle Database with the language SQL Oracle tools and Application programs often allow users to access the database without using SQL directly. such as PL/SQL and Oracle precompilers. Introduction to the Optimizer Chapter 3 . Oracle Corp strives to comply with industry-accepted standards and participates in SQL standards committees (ANSI and ISO). • Embedded SQL statements incorporate DDL. • Data definition language (DDL) statements define.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 4 Oracle University and InfoTech (Pvt. but then these applications must use SQL when executing user requests. • Transaction control statements (TCS) manage the changes made by DML statements and group DML statements into transactions. alter the structure of. Page 5 . All rights reserved. Oracle and/or its affiliates. While his course focuses mainly on queries.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: SELECT statements are the most used statements. Introduction to the Optimizer Chapter 3 . Copyright © 2010. it is important to note that any type of SQL statement is subject to optimization.Oracle University and InfoTech (Pvt. Note: In evaluating whether statements are similar or identical. each user must have a separate copy of the statement’s private SQL area. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Representation . Introduction to the Optimizer Chapter 3 . Oracle Database recognizes when two users execute the same SQL statement and reuses the shared SQL area for those users. which often happens when many users run the same application. Copyright © 2010.Page 6 Oracle University and InfoTech (Pvt. A shared SQL area contains all optimization information necessary to execute the statement whereas a private SQL area contains all run-time information related to a particular execution of the statement. Oracle Database saves memory by using one shared SQL area for SQL statements run multiple times. Oracle Database considers SQL statements issued directly by users and applications.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. as well as recursive SQL statements issued internally by a DDL statement. However.SQL Statement Representation Private SQL area Private SQL area Private SQL area Shared SQL area Private SQL area Private SQL area Shared SQL area SQL Statement Representation Oracle Database represents each SQL statement it runs with a shared SQL area and a private SQL area. All rights reserved. SQL Statement Implementation User process User process User process User process User process Server process Server process Private SQL area Private SQL area Client Server Server process Server process Server process Private SQL area Private SQL area Private SQL area Aggregated PGA SGA Shared SQL area Shared SQL area Library cache Buffer cache Data dictionary cache Redo log buffer Result cache Java pool Other SHARED_POOL Streams pool SQL Statement Implementation Oracle Database creates and uses memory structures for various purposes. Oracle Database can deallocate items from the pool using a modified least recently used (LRU) algorithm until there is enough free space for the new statement’s shared SQL area. memory stores program codes that are run. and private data areas for each connected user. If Oracle Database deallocates a shared SQL area. All rights reserved. Introduction to the Optimizer Chapter 3 . to store in the shared SQL area.Page 7 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle Database allocates memory from the shared pool when a new SQL statement is parsed. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Implementation . Oracle and/or its affiliates. For example. The size of this memory depends on the complexity of the statement. the associated SQL statement must be reparsed and reassigned to another shared SQL area at its next execution. If the entire shared pool has already been allocated. Copyright © 2010. data that is shared among users. 1). Copyright © 2010. All rights reserved.Page 8 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Processing: Overview . Introduction to the Optimizer Chapter 3 .SQL Statement Processing: Overview OPEN PARSE query? yes describe? no yes no DESCRIBE no more? yes DEFINE no more? yes yes reparse? no yes bind? no BIND no more? yes PARALLELIZE EXECUTE query? yes FETCH no yes execute others? no more? yes no CLOSE SQL Statement Processing: Overview The graphic in the slide shows all the steps involved in query execution and these steps can be found in Oracle® Database Concepts 11g Release 1 (11. Parse the statement. For example. 6. 2. All rights reserved. 7. Fetch rows of a query. Execute the statement. 5. SQL Statement Processing: Steps Note that not all statements require all these steps. Create a cursor. Describe query results. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Processing: Steps . Close the cursor. nonparallel DDL statements are required in only two steps: Create and Parse.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Parallelizing the statement involves deciding that it can be parallelized as opposed to actually building parallel execution structures. 9.SQL Statement Processing: Steps 1. Oracle and/or its affiliates. 4. Parallelize the statement.Page 9 Oracle University and InfoTech (Pvt. 3. Bind variables. Define query output. 8. Introduction to the Optimizer Chapter 3 . All rights reserved. precompilers allow explicit cursor declaration. This may be confusing to people unfamiliar with it. Step 1: Create a Cursor A cursor can be thought of as an association between a cursor data area in a client program and Oracle server’s data structures. Most of this can also be done using the DBMS_SQL package as well. you have a hold of the cursor. Programs must have an open cursor to process a SQL statement. Introduction to the Optimizer Chapter 3 . Copyright © 2010. The cursor contains a pointer to the current row. Oracle and/or its affiliates. so the execute phase cannot be separately identified in the trace.Page 10 Oracle University and InfoTech (Pvt. It contains information for statement processing. it is more friendly than PRO*C or OCI. When you have a hold of the handle.Step 1: Create a Cursor • • • • A cursor is a handle or name for a private SQL area. but Oracle Call Interface (OCI) programs need the flexibility to be able to process each part of query execution separately. The following slides use the DBMS_SQL package to illustrate cursor management. however. The cursor structure is independent of the SQL statement that it contains. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Step 1: Create a Cursor . The pointer moves as rows are fetched until there are no more rows left to process. A handle is similar to the handle on a mug. Therefore.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It is a unique identifier for a particular cursor that can only be obtained by one process at a time. It is slightly problematic in that it performs FETCH and EXECUTE together. It is created by a program interface call in expectation of a SQL statement. Most Oracle tools hide much of cursor handling from the user. For distributed statements. so there are fewer round-trips to the server.Step 2: Parse the Statement • • • Statement passed from the user process to the Oracle instance Parsed representation of SQL created and moved into the shared SQL area if there is no identical SQL in the shared SQL area Can be reused if identical SQL exists Step 2: Parse the Statement During parsing. Oracle and/or its affiliates. and a parsed representation of the SQL statement is loaded into a shared SQL area. All rights reserved. Introduction to the Optimizer Chapter 3 . Translation and verification involve checking if the statement already exists in the library cache.Page 11 Oracle University and InfoTech (Pvt. they must be identical in every way including case and spacing. Typically. What this means is that the PARSE is bundled with the EXECUTE. the parse phase is represented as the stage where the query plan is generated. the SQL statement is passed from the user process to the Oracle instance. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Step 2: Parse the Statement . Copyright © 2010. check for the existence of database links.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Note: When checking if statements are identical. The parse step can be deferred by the client software to reduce network traffic. All rights reserved. size. information about the columns in the employees table is required. Oracle Database performs data type conversion. Steps 3 and 4: Describe and Define Step 3: Describe The describe stage is necessary only if the characteristics of a query’s result are not known. In this case. Oracle and/or its affiliates. and data type of variables defined to receive each fetched value. The define step defines location. for example. for example.. when a query is entered interactively by a user. These variables are called define variables. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Steps 3 and 4: Describe and Define . you enter a query such as: SQL> select * from employees. and names) of a query’s result. However. Step 4: Define In the define stage. the describe stage determines the characteristics (data types. if necessary. with DBMS_SQL or OCI. it is necessary to tell the client what the output data is and which the setup areas are. Describe tells the application what select list items are required. and data type information required to store fetched values in variables. it is relevant when entering dynamic queries through an OCI application. size.Page 12 Oracle University and InfoTech (Pvt. If. you specify the location.Steps 3 and 4: Describe and Define • • The describe step provides information about the select list items. lengths. Introduction to the Optimizer Chapter 3 . These two steps are generally hidden from users in tools such as SQL*Plus. but still does not have enough information to run the statement. Parallelization involves dividing the work of a statement among a number of slave processes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Steps 5 and 6: Bind and Parallelize . creating a table with a subquery. Copyright © 2010. Oracle Database knows the meaning of the SQL statement. The process of obtaining these values is called binding variables. this plan is then implemented if sufficient resource is available. so it can complete faster.Steps 5 and 6: Bind and Parallelize • Bind any bind values: – Enables memory address to store data values – Allows shared SQL even though bind values may change • Parallelize the statement: – – – – – – – SELECT INSERT UPDATE MERGE DELETE CREATE ALTER Steps 5 and 6: Bind and Parallelize Step 5: Bind At this point. and some DDL operations. and operations on partitions. Introduction to the Optimizer Chapter 3 . MERGE. All rights reserved. Oracle and/or its affiliates.Page 13 Oracle University and InfoTech (Pvt. DELETE). Oracle Database needs values for any variables listed in the statement. such as index creation. At execution time. UPDATE. Step 6: Parallelize Oracle Database can parallelize the execution of SQL statements (such as SELECT. Parsing has already identified if a statement can be parallelized or not and has built the appropriate parallel plan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. INSERT. Parallelization causes multiple server processes to perform the work of the SQL statement. Steps 7 Through 9 • Execute: – Drives the SQL statement to produce the desired results • Fetch rows: – Into defined output variables – Query results returned in table format – Array fetch mechanism • Close the cursor. Steps 7 Through 9 At this point, Oracle Database has all the necessary information and resources, so the statement is run. If the statement is a query (without the FOR UPDATE clause) statement, no rows need to be locked because no data is changed. If the statement is an UPDATE or a DELETE statement, however, all rows that the statement affects are locked until the next COMMIT, ROLLBACK, or SAVEPOINT for the transaction. This ensures data integrity. For some statements, you can specify a number of executions to be performed. This is called array processing. Given n number of executions, the bind and define locations are assumed to be the beginning of an array of size n. In the fetch stage, rows are selected and ordered (if requested by the query), and each successive fetch retrieves another row of the result until the last row has been fetched. The final stage of processing a SQL statement is closing the cursor. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 14 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Steps 7 Through 9 SQL Statement Processing PL/SQL: Example SQL> variable c1 number SQL> execute :c1 := dbms_sql.open_cursor; SQL> SQL> 2 3 4 variable b1 varchar2 execute dbms_sql.parse (:c1 ,'select null from dual where dummy = :b1' ,dbms_sql.native); SQL> execute :b1:='Y'; SQL> exec dbms_sql.bind_variable(:c1,':b1',:b1); SQL> variable r number SQL> execute :r := dbms_sql.execute(:c1); SQL> variable r number SQL> execute :r := dbms_sql.close_cursor(:c1); SQL Statement Processing PL/SQL: Example This example summarizes the various steps discussed previously. Note: In this example, you do not show the fetch operation. It is also possible to combine both the EXECUTE and FETCH operations in EXECUTE_AND_FETCH to perform EXECUTE and FETCH together in one call. This may reduce the number of network round-trips when used against a remote database. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 15 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Processing PL/SQL: Example SQL Statement Parsing: Overview Syntactic and semantic check Privileges check Private SQL area Parse call Allocate private SQL Area Parsed representation Existing shared SQL area? No (Hard parse) Parse operation (Optimization) Allocate shared SQL area Yes (Soft parse) Execute statement Shared SQL area SQL Statement Parsing: Overview Parsing is one stage in the processing of a SQL statement. When an application issues a SQL statement, the application makes a parse call to Oracle Database. During the parse call, Oracle Database performs the following actions: • Checks the statement for syntactic and semantic validity • Determines whether the process issuing the statement has the privileges to run it • Allocates a private SQL area for the statement • Determines whether or not there is an existing shared SQL area containing the parsed representation of the statement in the library cache. If so, the user process uses this parsed representation and runs the statement immediately. If not, Oracle Database generates the parsed representation of the statement, and the user process allocates a shared SQL area for the statement in the library cache and stores its parsed representation there. Note the difference between an application making a parse call for a SQL statement and Oracle Database actually parsing the statement. • A parse call by the application associates a SQL statement with a private SQL area. After a statement has been associated with a private SQL area, it can be run repeatedly without your application making a parse call. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 16 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Parsing: Overview A parse operation by Oracle Database allocates a shared SQL area for a SQL statement. After a shared SQL area has been allocated for a statement, it can be run repeatedly without being reparsed. Both parse calls and parsing can be expensive relative to execution, so perform them as rarely as possible. Note: Although parsing a SQL statement validates that statement, parsing only identifies errors that can be found before statement execution. Thus, some errors cannot be caught by parsing. For example, errors in data conversion or errors in data (such as an attempt to enter duplicate values in a primary key) and deadlocks are all errors or situations that can be encountered and reported only during the execution stage. Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 17 Why Do You Need an Optimizer? Query to optimize SELECT * FROM emp WHERE job = 'MANAGER'; Schema information How to retrieve these rows? Possible access paths Use the index. Read each row and check. 1 Which one is faster? Statistics 2 Only 1% of employees are managers 3 Use the index I have a plan! Why Do You Need an Optimizer? The optimizer should always return the correct result as quickly as possible. The query optimizer tries to determine which execution plan is most efficient by considering available access paths and by factoring in information based on statistics for the schema objects (tables or indexes) accessed by the SQL statement. The query optimizer performs the following steps: 1. The optimizer generates a set of potential plans for the SQL statement based on available access paths. 2. The optimizer estimates the cost of each plan based on statistics in the data dictionary for the data distribution and storage characteristics of the tables, and indexes accessed by the statement. 3. The optimizer compares the costs of the plans and selects the one with the lowest cost. Note: Because of the complexity of finding the best possible execution plan for a particular query, the optimizer’s goal is to find a “good” plan that is generally called the best cost plan. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Why Do You Need an Optimizer? Why Do You Need an Optimizer? Query to optimize SELECT * FROM emp WHERE job = 'MANAGER'; Schema information How to retrieve these rows? Possible access paths Use the index. Read each row and check. 1 Which one is faster? Statistics 2 80% of employees are managers 3 Use Full Table Scan Generate a plan Why Do You Need an Optimizer? (continued) The example in the slide shows you that if statistics change, the optimizer adapts its execution plan. In this case, statistics show that 80 percent of the employees are managers. In the hypothetical case, a full table scan is probably a better solution than using the index. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 19 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Why Do You Need an Optimizer? Optimization During Hard Parse Operation Optimizer Transformer Statistics Estimator Dictionary Plan Generator Execution Plan Shared SQL area Optimization During Hard Parse Operation The optimizer creates the execution plan for a SQL statement. SQL queries submitted to the system first run through the parser, which checks syntax and analyzes semantics. The result of this phase is called a parsed representation of the statement, and is constituted by a set of query blocks. A query block is a self-contained DML against a table. A query block can be a top-level DML or a subquery. This parsed representation is then sent to the optimizer, which handles three main functionalities: Transformation, estimation, and execution plan generation. Before performing any cost calculation, the system may transform your statement into an equivalent statement and calculate the cost of the equivalent statement. Depending on the version of Oracle Database, there are transformations that cannot be done, some that are always done, and some that are done, costed, and discarded. The input to the query transformer is a parsed query, which is represented by a set of interrelated query blocks. The main objective of the query transformer is to determine if it is advantageous to change the structure of the query so that it enables generation of a better query plan. Several query transformation techniques are employed by the query transformer, such as transitivity, view merging, predicate pushing, subquery unnesting, query rewrite, star transformation, and OR expansion. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 20 Oracle University and InfoTech (Pvt.) Ltd use only Parsed representation (query blocks) CBO THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimization During Hard Parse Operation Transformer: OR Expansion Example • Original query: B*-tree Index SELECT * FROM emp WHERE job = 'CLERK' OR deptno = 10; • Equivalent transformed query: SELECT * FROM emp WHERE job = 'CLERK' UNION ALL SELECT * FROM emp WHERE deptno = 10 AND job <> 'CLERK'; Transformer: OR Expansion Example If a query contains a WHERE clause with multiple conditions combined with OR operators, the optimizer transforms it into an equivalent compound query that uses the UNION ALL set operator, if this makes the query execute more efficiently. For example, if each condition individually makes an index access path available, the optimizer can make the transformation. The optimizer selects an execution plan for the resulting statement that accesses the table multiple times using the different indexes and then puts the results together. This transformation is done if the cost estimation is better than the cost of the original statement. In the example in the slide, it is assumed that there are indexes on both the JOB and DEPTNO columns. Then, the optimizer might transform the original query into the equivalent transformed query shown in the slide. When the cost-based optimizer (CBO) decides whether to make a transformation, the optimizer compares the cost of executing the original query using a full table scan with that of executing the resulting query. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 21 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Transformer: OR Expansion Example Transformer: Subquery Unnesting Example • Original query: SELECT * FROM accounts WHERE custno IN (SELECT custno FROM customers); • Equivalent transformed query: SELECT accounts.* FROM accounts, customers WHERE accounts.custno = customers.custno; Primary or unique key Transformer: Subquery Unnesting Example To unnest a query, the optimizer may choose to transform the original query into an equivalent JOIN statement, and then optimize the JOIN statement. The optimizer may do this transformation only if the resulting JOIN statement is guaranteed to return exactly the same rows as the original statement. This transformation allows the optimizer to take advantage of the join optimizer techniques. In the example in the slide, if the CUSTNO column of the customers table is a primary key or has a UNIQUE constraint, the optimizer can transform the complex query into the shown JOIN statement that is guaranteed to return the same data. If the optimizer cannot transform a complex statement into a JOIN statement, it selects execution plans for the parent statement and the subquery as though they were separate statements. The optimizer then executes the subquery and uses the rows returned to execute the parent query. Note: Complex queries whose subqueries contain aggregate functions such as AVG cannot be transformed into JOIN statements. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 22 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Transformer: Subquery Unnesting Example Transformer: View Merging Example • Original query: Index CREATE VIEW emp_10 AS SELECT empno, ename, job, sal, comm, deptno FROM emp WHERE deptno = 10; SELECT empno FROM emp_10 WHERE empno > 7800; • Equivalent transformed query: SELECT empno FROM emp WHERE deptno = 10 AND empno > 7800; Transformer: View Merging Example To merge the view’s query into a referencing query block in the accessing statement, the optimizer replaces the name of the view with the names of its base tables in the query block and adds the condition of the view’s query’s WHERE clause to the accessing query block’s WHERE clause. This optimization applies to select-project-join views, which contain only selections, projections, and joins. That is, views that do not contain set operators, aggregate functions, DISTINCT, GROUP BY, CONNECT BY, and so on. The view in this example is of all employees who work in department 10. The query that follows the view’s definition in the slide accesses the view. The query selects the IDs greater than 7800 of employees who work in department 10. The optimizer may transform the query into the equivalent transformed query shown in the slide that accesses the view’s base table. If there are indexes on the DEPTNO or EMPNO columns, the resulting WHERE clause makes them available. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 23 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Transformer: View Merging Example Transformer: Predicate Pushing Example • Original query: Index CREATE VIEW two_emp_tables AS SELECT empno, ename, job, sal, comm, deptno FROM emp1 UNION SELECT empno, ename, job, sal, comm, deptno FROM emp2; SELECT ename FROM two_emp_tables WHERE deptno = 20; • Equivalent transformed query: SELECT ename FROM ( SELECT empno, ename, job,sal, comm, deptno FROM emp1 WHERE deptno = 20 UNION SELECT empno, ename, job,sal, comm, deptno FROM emp2 WHERE deptno = 20 ); Transformer: Predicate Pushing Example The optimizer can transform a query block that accesses a nonmergeable view by pushing the query block’s predicates inside the view’s query. In the example in the slide, the two_emp_tables view is the union of two employee tables. The view is defined with a compound query that uses the UNION set operator. The query that follows the view’s definition in the slide accesses the view. The query selects the IDs and names of all employees in either table who work in department 20. Because the view is defined as a compound query, the optimizer cannot merge the view’s query into the accessing query block. Instead, the optimizer can transform the accessing statement by pushing its predicate, the WHERE clause condition deptno = 20, into the view’s compound query. The equivalent transformed query is shown in the slide. If there is an index in the DEPTNO column of both tables, the resulting WHERE clauses make them available. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 24 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Transformer: Predicate Pushing Example Transformer: Transitivity Example • Original query: Index SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno; • Equivalent transformed query: SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno AND dept.deptno = 20; Transformer: Transitivity Example If two conditions in the WHERE clause involve a common column, the optimizer sometimes can infer a third condition, using the transitivity principle. The optimizer can then use the inferred condition to optimize the statement. The inferred condition can make available an index access path that was not made available by the original conditions. This is demonstrated with the example in the slide. The WHERE clause of the original query contains two conditions, each of which uses the EMP.DEPTNO column. Using transitivity, the optimizer infers the following condition: dept.deptno = 20 If an index exists in the DEPT.DEPTNO column, this condition makes access paths available using that index. Note: The optimizer only infers conditions that relate columns to constant expressions, rather than columns to other columns. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Introduction to the Optimizer Chapter 3 - Page 25 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Transformer: Transitivity Example ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Many different plans are possible because of the various combinations of different access paths. and cost.Cost-Based Optimizer • Piece of code: – Estimator – Plan generator • Estimator determines cost of optimization suggestions made by the plan generator: – Cost: Optimizer’s best estimate of the number of standardized I/Os made to execute a particular statement optimization • Plan generator: – – – – Tries out different statement optimization techniques Uses the estimator to cost each optimization suggestion Chooses the best optimization suggestion based on cost Generates an execution plan for best optimization Cost-Based Optimizer The combination of the estimator and plan generator code is commonly called the cost-based optimizer (CBO). Oracle and/or its affiliates.Page 26 Oracle University and InfoTech (Pvt. Introduction to the Optimizer Chapter 3 . initialization parameters. Cardinality is derived from selectivity and often the cost depends on cardinality. The optimizer uses various pieces of information to determine the best path: WHERE clause. The estimator generates three types of measures: selectivity. These measures are related to each other. All rights reserved. The main function of the plan generator is to try out different possible plans for a given query and pick the one that has the lowest cost. The end goal of the estimator is to estimate the overall cost of a given plan. join methods. and schema information. statistics. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cost-Based Optimizer . the estimator uses these to improve the degree of accuracy when computing the measures. Copyright © 2010. This number rises exponentially with the number of join items. supplied hints. The number of possible plans for a query block is proportional to the number of join items in the FROM clause. If statistics are available. cardinality. and join orders that can be used to access and process data in different ways and produce the same result. The histogram captures the distribution of different values in a column. depending on the value of the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter. If a histogram is available in the LAST_NAME column.0. Therefore. such as last_name = 'Smith' AND job_type = 'Clerk'. If no statistics are available. A selectivity of 0.Estimator: Selectivity Selectivity = • • • • Number of rows satisfying a condition Total number of rows Selectivity is the estimated proportion of a row set retrieved by a particular predicate or combination of predicates. For example.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. and a selectivity of 1. HIGH/LOW_VALUE. When statistics are available. The row set can be a base table. Thus. or the result of a join or a GROUP BY operator. even distribution is assumed.0 means that all rows are selected. Oracle and/or its affiliates. DENSITY.0 to 1. the optimizer either uses dynamic sampling or an internal default value.0: – High selectivity: Small proportion of rows – Low selectivity: Big proportion of rows Selectivity computation: – If no statistics: Use dynamic sampling – If no histograms: Assume even distribution of rows Statistic information: – DBA_TABLES and DBA_TAB_STATISTICS (NUM_ROWS) – DBA_TAB_COL_STATISTICS (NUM_DISTINCT. the estimator uses it instead of the number of distinct values. the estimator uses them to estimate selectivity.…) Estimator: Selectivity Selectivity represents a fraction of rows from a row set. such as last_name = 'Smith'. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Estimator: Selectivity .0 means that no rows are selected from a row set. All rights reserved.0 and 1. Selectivity lies in a value range from 0. The selectivity is tied to a query predicate. a view. or a combination of predicates.Page 27 Oracle University and InfoTech (Pvt. for an equality predicate (last_name = 'Smith'). the selectivity of a predicate indicates the percentage of rows from a row set that passes the predicate test. It is expressed as a value between 0. Introduction to the Optimizer Chapter 3 . so it yields better selectivity estimates. selectivity is set to the reciprocal of the number n of distinct values of LAST_NAME because the query selects rows that contain one out of n distinct values. A predicate acts as a filter that filters a certain number of rows from a row set. Page 28 . Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: It is important to have histograms in columns that contain values with large variations in the number of duplicates (data skew). Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. Introduction to the Optimizer Chapter 3 . All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. When costing a join operation. the optimizer deduces that the selectivity of the DEV_NAME='ANGEL' predicate is 1/203 (assuming there are no histograms).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. or the result of a join or GROUP BY operator. the driving row source defines how often the system probes the inner row source. filters. and that the total number of rows in the COURSES table is 1018. 6. a view. Based on this assumption. This number is then rounded off to the nearest integer. Because sort costs are dependent on the size and number of rows to be sorted. for example.926*e-03 – Cardinality = (1/203)*1018 = 5. Most of the time. All rights reserved. and also deduces the cardinality of the query to be (1/203)*1018. Oracle and/or its affiliates. – The number of rows in COURSES (original cardinality) is 1018. In the example in the slide. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Estimator: Cardinality . With nested loops join. cardinality figures are also vital for sort costing.01 (rounded off to 6) Estimator: Cardinality The cardinality of a particular operation in the execution plan of a query represents the estimated number of rows retrieved by that particular operation. based on assumed statistics. Copyright © 2010.Estimator: Cardinality Cardinality = Selectivity * Total number of rows • • • Expected number of rows retrieved by a particular operation in the execution plan Vital figure to determine join. the optimizer knows that there are 203 different values in the DEV_NAME column.Page 29 Oracle University and InfoTech (Pvt. Introduction to the Optimizer Chapter 3 . – Selectivity = 1/203 = 4. – The number of distinct values in DEV_NAME is 203. the row source can be a base table. it is important to know the cardinality of the driving row source. and sort costs Simple example: SELECT days FROM courses WHERE dev_name = 'ANGEL'. the cost is a normalized value in terms of a number of single block random reads The standard cost metric measured by the optimizer is in terms of number of single block random reads. hash. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Estimator: Cost . and cached I/O). All rights reserved. #MRds. and #CPUCycles. necessary adjustments are made while computing estimates for #SRds.Estimator: Cost • • Cost is the optimizer’s best estimate of the number of standardized I/Os it takes to execute a particular statement. This model is straightforward for serial execution. Basically. Copyright © 2010. often it is the only contribution to the cost (in cases of in-memory sort. so one cost unit corresponds to one single block random read. Oracle and/or its affiliates. Multiblock I/O cost Single block I/O cost #SRds*sreadtim + #MRds*mreadtim + Cost= CPU cost #CPUCycles/cpuspeed sreadtim #SRds: Number of single block reads #MRds: Number of multiblock reads #CPUCycles: Number of CPU Cycles Sreadtim: Single block read time Mreadtim: Multiblock read time Cpuspeed: Millions instructions per second Estimator: Cost The cost of a statement represents the optimizer’s best estimate of the number of standardized inputs/outputs (I/Os) it takes to execute that statement. Note: #CPUCycles includes CPU cost of query processing (pure CPU cost) and CPU cost of data retrieval (CPU cost of the buffer cache get).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. predicate evaluation. Cost unit is a standardized single block random read: – 1 cost unit = 1 SRds • The cost formula combines three different costs units into standard cost units. For parallel execution. Introduction to the Optimizer Chapter 3 . The formula shown in the slide combines three different cost units: • Estimated time to do all the single-block random reads • Estimated time to do all the multiblock reads • Estimated time for the CPU to process the statement into one standard cost unit The model includes CPU costing because in most cases CPU utilization is as important as I/O.Page 30 Oracle University and InfoTech (Pvt. 51 Join order aborted Final cost for query block SEL$1 (#0) All Rows Plan: Best join order: 1 +----------------------------------------------------------------+ | Id | Operation | Name | Rows | Bytes | Cost | +----------------------------------------------------------------+ | 0 | SELECT STATEMENT | | | | 7 | | 1 | HASH JOIN | | 106 | 6042 | 7 | | 2 | TABLE ACCESS FULL | DEPARTMENTS| 27 | 810 | 3 | | 3 | TABLE ACCESS FULL | EMPLOYEES | 107 | 2889 | 3 | +----------------------------------------------------------------+ Plan Generator The plan generator explores various plans for a query block by trying out different access paths. or Hash Join.24 Resp: 121. The best plan is the one shown at the end of the trace.51 Best:: JoinMethod: Hash Cost: 6. join methods.13 Resp: 41.Plan Generator select e. If the current best cost is large. It is assumed that there are no indexes in this example.51 Degree: 1 Resp: 6.24 Degree: 1 SM cost: 8. departments d where e. and join orders.00 Join order[2]: EMPLOYEES[E]#1 DEPARTMENTS[D]#0 NL Join: Cost: 121. the plan generator ends the search swiftly because further cost improvement is not significant. d. the plan generator tries harder (in other words. Oracle and/or its affiliates. the plan generator has six possibilities.01 HA cost: 6. To retrieve the rows. and for each. three different join methods. For each possibility. All rights reserved. If the current best cost is small. or six different plans to test: Two join orders. you can start to join the DEPARTMENTS table to the EMPLOYEES table. you can use three possible join mechanisms that the optimizer knows: Nested Loop.department_id = d.Page 31 Oracle University and InfoTech (Pvt. you have the cost of the corresponding plan. explores more alternate plans) to find a better plan with lower cost. join methods. As you can see from the trace.department_name from employees e. Join order[1]: DEPARTMENTS[D]#0 EMPLOYEES[E]#1 NL Join: Cost: 41. the plan generator delivers the best execution plan for your statement. Note: Access path.” Copyright © 2010. The cutoff is based on the cost of the current best plan. The plan generator uses an internal cutoff to reduce the number of plans it tries when finding the one with the lowest cost.01 HA cost: 6.13 Degree: 1 SM cost: 8.last_name. Introduction to the Optimizer Chapter 3 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.51 Card: 106. and plan are discussed in more detail in the lessons titled “Optimizer Operators” and “Interpreting Execution Plans. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Plan Generator . Finding a good initial join order is a difficult problem. The slide shows you an extract of an optimizer trace file generated for the select statement. Sort Merge.department_id. For that particular join order. Ultimately. The cutoff works well if the plan generator starts with an initial join order that produces a plan with a cost close to optimal. but are otherwise identical. FORCE DB_FILE_MULTIBLOCK_READ_COUNT PGA_AGGREGATE_TARGET STAR_TRANSFORMATION_ENABLED RESULT_CACHE_MODE: MANUAL. unless the literals affect the meaning of the statement - SIMILAR: Causes statements that may differ in some literals. This is the default. and whether parallel execution is Copyright © 2010. DB_FILE_MULTIBLOCK_READ_COUNT is one of the parameters you can use to minimize I/O during table scans or index fast full scan. Introduction to the Optimizer Chapter 3 . - EXACT: Only allows statements with identical text to share the same cursor. but are otherwise identical. or applications that use stored outlines.Controlling the Behavior of the Optimizer • • • • • • • • CURSOR_SHARING: SIMILAR. FORCE RESULT_CACHE_MAX_SIZE RESULT_CACHE_MAX_RESULT RESULT_CACHE_REMOTE_EXPIRATION Controlling the Behavior of the Optimizer These parameters control the optimizer behavior: • • CURSOR_SHARING determines what kind of SQL statements can share the same cursors: - FORCE: Forces statements that may differ in some literals. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Controlling the Behavior of the Optimizer . All rights reserved. Forcing cursor sharing among similar (but not identical) statements can have unexpected results in some decision support system (DSS) applications. to share a cursor. the multiblock read count. unless the literals affect either the meaning of the statement or the degree to which the plan is optimized. EXACT. such as the size of the segment. to share a cursor. The total number of I/Os needed to perform a full table scan or an index fast full scan depends on factors.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 32 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. It specifies the maximum number of blocks read in one I/O operation during a sequential scan. the optimizer uses a default value of 8 when costing full table scans and index fast full scans. whichever is greater. the default value of this parameter is a value that corresponds to the maximum I/O size that can be performed efficiently. the hint takes precedence over the parameter setting.Page 33 Oracle University and InfoTech (Pvt. the system sets it to 20% of the SGA or 10 MB. When increasing the value of this parameter. You can change the memory allocated to the result cache by setting the RESULT_CACHE_MAX_SIZE parameter. The system attempts to keep the amount of private memory below the target specified by this parameter by adapting the size of the work areas to private memory. you must specify. you should examine the total memory on your system that is available to the Oracle instance and subtract the SGA. STAR_TRANSFORMATION_ENABLED determines whether a cost-based query transformation is applied to star queries. You can assign the remaining memory to PGA_AGGREGATE_TARGET. Setting PGA_AGGREGATE_TARGET to a nonzero value has the effect of automatically setting the WORKAREA_SIZE_POLICY parameter to AUTO. Note that if the number of sessions is extremely large. and bitmap create) are automatically sized. if the statement contains a [NO_]RESULT_CACHE hint. DSS and data warehouse environments tend to benefit most from maximizing the value of this parameter. by using the RESULT_CACHE hint. The result cache Copyright © 2010. it automatically computes a value that is equal to the maximum I/O size that can be performed efficiently divided by the standard block size. When setting this parameter. For the FORCE setting. all results are stored in the cache. more memoryintensive operations are able to run fully in memory and a less number of them work their way over to disk. Setting PGA_AGGREGATE_TARGET to 0 automatically sets the WORKAREA_SIZE_POLICY parameter to MANUAL.” The query optimizer manages the result cache mechanism depending on the settings of the RESULT_CACHE_MODE parameter in the initialization parameter file. that a particular result is to be stored in the cache. Consequently.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the optimizer does not favor large plans if you do not set this parameter. - When set to FORCE. A nonzero value for this parameter is the default because. if this parameter is not set explicitly (or is set is 0). • • • • PGA_AGGREGATE_TARGET specifies the target aggregate PGA memory available to all server processes attached to the instance. Even though the default value may be a large value. You can use this parameter to determine whether or not the optimizer automatically sends the results of queries to the result cache. This means that SQL working areas used by memory-intensive SQL operators (such as sort. if the value of this parameter is high. hash-join. group-by. This optimization is explained in the lesson titled “Case Study: Star Transformation. Online transaction processing (OLTP) and batch environments typically have values in the range of 4 to 16 for this parameter. As of Oracle Database 10g. It would do so only if you explicitly set this parameter to a large value. . bitmap merge. unless you specify otherwise. The optimizer is more likely to select a full table scan over an index. Basically. Introduction to the Optimizer Chapter 3 . This means that SQL work areas are sized using the *_AREA_SIZE parameters. Oracle and/or its affiliates. The memory size allocated to the result cache depends on the memory size of the SGA as well as the memory management system. you indirectly increase the memory allotted to work areas. All rights reserved.Because the parameter is expressed in blocks. This value is platform-dependent and is calculated at instance startup for most platforms. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED being utilized for the operation. the multiblock read count value is decreased to avoid the buffer cache getting flooded with too many table scan buffers. The possible parameter values are MANUAL. Release 2. and FORCE: - When set to MANUAL (the default). if the remote table used by a result is modified at the remote database. The value of this parameter is rounded to the largest multiple of 32 KB that is not greater than the specified value.Page 34 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED is disabled if you set its value to 0. • Use the RESULT_CACHE_REMOTE_EXPIRATION parameter to specify the time (in number of minutes) for which a result that depends on remote database objects remains valid. Oracle and/or its affiliates. If the rounded value is 0. The default value is 5%.• Use the RESULT_CACHE_MAX_RESULT parameter to specify the maximum amount of cache memory that can be used by any single result. for example. which implies that results using remote objects should not be cached. All rights reserved. Oracle University and InfoTech (Pvt. The default value is 0. Introduction to the Optimizer Chapter 3 . Copyright © 2010. but you can specify any percentage value between 1 and 100. the feature is disabled. Setting this parameter to a nonzero value can produce stale answers. OPTIMIZER_FEATURES_ENABLED acts as an umbrella parameter for enabling a series of optimizer features based on an Oracle release number. which modifies the optimizer’s assumptions about index caching for nested loops and inlist iterators.Controlling the Behavior of the Optimizer • • • • • • • • • OPTIMIZER_INDEX_CACHING OPTIMIZER_INDEX_COST_ADJ OPTIMIZER_FEATURES_ENABLED OPTIMIZER_MODE: ALL_ROWS. which results in default optimizer behavior. For example. Use caution when using this parameter because execution plans can change in favor of index caching. Copyright © 2010. The range of values 0 to 100 for OPTIMIZER_INDEX_CACHING indicates percentage of index blocks in the buffer cache. that is. at which the optimizer evaluates index access paths at the regular cost.Page 35 Oracle University and InfoTech (Pvt. The default for this parameter is 0. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Controlling the Behavior of the Optimizer . All rights reserved. FIRST_ROWS. FIRST_ROWS_n OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES OPTIMIZER_USE_SQL_PLAN_BASELINES OPTIMIZER_DYNAMIC_SAMPLING OPTIMIZER_USE_INVISIBLE_INDEXES OPTIMIZER_USE_PENDING_STATISTICS Controlling the Behavior of the Optimizer (continued) • • • OPTIMIZER_INDEX_CACHING: This parameter controls the costing of an index probe in conjunction with a nested loop or an inlist iterator. Any other value makes the optimizer evaluate the access path at that percentage of the regular cost. OPTIMIZER_INDEX_COST_ADJ lets you tune optimizer behavior for access path selection to be more or less index friendly. to make the optimizer more or less prone to selecting an index access path over a full table scan. Introduction to the Optimizer Chapter 3 . The default for this parameter is 100 percent. The range of values is 1 to 10000. A value of 100 infers that 100% of the index blocks are likely to be found in the buffer cache and the optimizer adjusts the cost of an index probe or nested loop accordingly. a setting of 50 makes the index access path look half as expensive as normal.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. 1 by setting the parameter to 11. 100. OPTIMIZER_USE_PENDING_STATISTICS specifies whether or not the optimizer uses pending statistics when compiling SQL statements.1. and optimizes with a goal of best response time to return the first n number of rows. if you upgrade your database from release 10. but you want to keep the release 10. At a later time. as well as the generation of SQL plan baselines for such statements.Page 36 Oracle University and InfoTech (Pvt. you can do so by setting this parameter to 10. If one is found in SQL Management Base. it is not recommended to explicitly set the OPTIMIZER_FEATURES_ENABLE parameter to an earlier release. To avoid possible SQL performance regression that may result from execution plan changes. n can equal 1. - FIRST_ROWS: The optimizer uses a mix of cost and heuristics to find a best plan for fast delivery of the first few rows. OPTIMIZER_USE_SQL_PLAN_BASELINES enables or disables the use of SQL plan baselines stored in SQL Management Base. .0. you can try the enhancements introduced in releases up to and including release 11.0. 10. consider using SQL plan management instead. Using heuristics sometimes leads the query optimizer to generate a plan with a cost that is significantly larger than the cost of a plan without applying the heuristic. or 1000. This is the default value.1. The possible values are: - ALL_ROWS: The optimizer uses a cost-based approach for all SQL statements in the session regardless of the presence of statistics and optimizes with a goal of best throughput (minimum resource use to complete the entire statement).6.• • • • • • OPTIMIZER_MODE establishes the default behavior for selecting an optimization approach for either the instance or your session.0 or later. OPTIMIZER_DYNAMIC_SAMPLING controls the level of dynamic sampling performed by the optimizer. the optimizer looks for a SQL plan baseline for the SQL statement being compiled. When enabled. pending statistics.2. Note: Invisible indexes. Oracle and/or its affiliates. Introduction to the Optimizer Chapter 3 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. regardless of the presence of statistics.0.1 optimizer behavior. the default value is 0 OPTIMIZER_USE_INVISIBLE_INDEXES enables or disables the use of invisible indexes. the default value is 1 - 9.0. and dynamic sampling are discussed later in this course. Copyright © 2010.1. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED For example.1 or earlier. the optimizer costs each of the baseline plans and picks one with the lowest cost. use FIRST_ROWS_n instead.1 to release 11. - FIRST_ROWS_n: The optimizer uses a cost-based approach.0. However. All rights reserved. OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES enables or disables the automatic recognition of repeatable SQL statements. FIRST_ROWS is available for backward compatibility and plan stability. If OPTIMIZER_FEATURES_ENABLE is set to: - 10. the default value is 2 - 9. 1.1.2 11.5 10.2.2. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Features and Oracle Database Releases . All rights reserved.0.0. Copyright © 2010.0 to 10.0 10. Introduction to the Optimizer Chapter 3 .1. Oracle and/or its affiliates.2.0.Optimizer Features and Oracle Database Releases OPTIMIZER_FEATURES_ENABLED Features 9.Page 37 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.0 to 10. The table in the slide describes some of the optimizer features that are enabled depending on the value specified for the OPTIMIZER_FEATURES_ENABLED parameter.0.6 Index fast full scan Consideration of bitmap access to paths for tables with only Btree indexes Complex view merging Peeking into user-defined bind variables Index joins Dynamic sampling Query rewrite enables Skip unusable indexes Automatically compute index statistics as part of creation Cost-based query transformations Allow rewrites with multiple MVs and/or base tables Adaptive cursor sharing Use extended statistics to estimate selectivity Use native implementation for full outer joins Partition pruning using join filtering Group by placement optimization Null aware antijoins Optimizer Features and Oracle Database Releases OPTIMIZER_FEATURES_ENABLED acts as an umbrella parameter for enabling a series of optimizer features based on an Oracle release number.0 to 9. Page 38 Oracle University and InfoTech (Pvt.Quiz The _________step provides information about the select list items and is relevant when entering dynamic queries through an OCI application. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Oracle and/or its affiliates. Parallelize Answer: c Copyright © 2010. Introduction to the Optimizer Chapter 3 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Define c. All rights reserved. Parse b. a. Describe d. Introduction to the Optimizer Chapter 3 . Selecting the plan with the lowest cost d. Estimating and comparing the cost of each plan c.Quiz Which of the following steps is performed by the query optimizer? a. Oracle and/or its affiliates. Generating a set of potential plans for the SQL statement based on available access paths b. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . All of the above Answer: d Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 39 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Selectivity Answer: b Copyright © 2010.Page 40 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimization quotient d. Cost b. Cardinality c. Introduction to the Optimizer Chapter 3 . Oracle and/or its affiliates. All rights reserved.Quiz The expected number of rows retrieved by a particular operation in the execution plan is known as its: a. Oracle and/or its affiliates. you should have learned how to: • Describe the execution steps of a SQL statement • Describe the need for an optimizer • Explain the various phases of optimization • Control the behavior of the optimizer Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Summary In this lesson.Page 41 Oracle University and InfoTech (Pvt. Introduction to the Optimizer Chapter 3 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Practice 3: Overview This practice covers exploring a trace file to understand the optimizer’s decisions. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 3: Overview Copyright © 2010. Introduction to the Optimizer Chapter 3 .Page 42 . Oracle and/or its affiliates. Oracle University and InfoTech (Pvt. Interpreting Execution Plans Chapter 4 .) Ltd use only Interpreting Execution Plans .Page 1 Oracle University and InfoTech (Pvt.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 4 Copyright © 2010. All rights reserved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Interpreting Execution Plans Copyright © 2010.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 2 Oracle University and InfoTech (Pvt. Interpreting Execution Plans Chapter 4 . Oracle and/or its affiliates.) Ltd use only Interpreting Execution Plans . ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Objectives After completing this lesson. Interpreting Execution Plans Chapter 4 .Page 3 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . All rights reserved. you should be able to: • Gather execution plans • Display execution plans • Interpret execution plans Copyright © 2010. The row source tree is the core of the execution plan. It instructs the execution engine about the operations it must perform for retrieving the data required by a query most efficiently. The combination of row sources for a statement is called the execution plan. such as the cost and cardinality of each operation • Partitioning. There is a parent-child relationship between steps. and DELETE statements. such as the distribution method of join inputs Copyright © 2010. sort.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the execution plan can be displayed in a tree-like structure (text or graphical). the plan table contains information about the following: • Optimization.What Is an Execution Plan? • • • The execution plan of a SQL statement is composed of small building blocks called row sources for serial execution plans. The steps of the execution plan are not performed in the order in which they are numbered. such as filter. or aggregation In addition to the row source tree (or data flow tree for parallel operations). The EXPLAIN PLAN statement gathers execution plans chosen by the Oracle optimizer for the SELECT. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED What Is an Execution Plan? .Page 4 Oracle University and InfoTech (Pvt. What Is an Execution Plan? An execution plan is the output of the optimizer and is presented to the execution engine for implementation. All rights reserved. INSERT. such as the set of accessed partitions • Parallel execution. By using parent-child relationships. UPDATE. Oracle and/or its affiliates. It shows the following information: • An ordering of the tables referenced by the statement • An access method for each table mentioned in the statement • A join method for tables affected by join operations in the statement • Data operations. Interpreting Execution Plans Chapter 4 . Page 5 . Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. Copyright © 2010. such as nested loops join. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN results help you determine whether the optimizer selects a particular execution plan. Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. V$SQL_PLAN contains the actual plan used. Information in V$SQL_PLAN is very similar to the output of an EXPLAIN PLAN statement. Copyright © 2010. Interpreting Execution Plans Chapter 4 . Each row in V$SQL_PLAN_MONITOR corresponds to an operation of the execution plan that is monitored. All rights reserved. This command is very useful because it outlines the plan that the optimizer may use and inserts it in a table called PLAN_TABLE without executing the SQL statement.Page 6 Oracle University and InfoTech (Pvt. while EXPLAIN PLAN shows a theoretical plan that can be used if this statement was executed. Plans are recorded into DBA_HIST_SQL_PLAN or STATS$SQL_PLAN. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Where to Find Execution Plans? . The Automatic Workload Repository (AWR) infrastructure and Statspack store execution plans of top SQL statements. Oracle and/or its affiliates. V$SQL_PLAN provides a way to examine the execution plan for cursors that were recently executed.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This command is available from SQL*Plus or SQL Developer. The most well-known ones are listed in the slide: • • • • The EXPLAIN PLAN command enables you to view the execution plan that the optimizer might use to execute a SQL statement. V$SQL_PLAN_MONITOR displays plan-level monitoring statistics for each SQL statement found in V$SQL_MONITOR.Where to Find Execution Plans? • • • • • PLAN_TABLE (SQL Developer or SQL*Plus) V$SQL_PLAN (Library Cache) V$SQL_PLAN_MONITOR (11g) DBA_HIST_SQL_PLAN (AWR) STATS$SQL_PLAN (Statspack) • • • SQL management base (SQL plan baselines) SQL tuning set Trace files generated by DBMS_MONITOR • • Event 10053 trace file Process state dump trace file since 10gR2 Where to Find Execution Plans? There are many ways to retrieve execution plans inside the database. However. as well as SQL profiles. • Starting with Oracle Database 10g.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle University and InfoTech (Pvt. execution plans are included in the trace file that is generated. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. All rights reserved. It stores statement log. when you dump process state (or errorstack from a process). and SQL plan baselines.Page 7 .Plan and row source operations are dumped in trace files generated by DBMS_MONITOR. which is used to dump cost-based optimizer (CBO) computations may include a plan. Release 2. Interpreting Execution Plans Chapter 4 . • The event 10053. • The SQL management base (SMB) is a part of the data dictionary that resides in the SYSAUX tablespace. plan histories. Oracle and/or its affiliates. The easiest way to view an execution plan is to use the DBMS_XPLAN package.Page 8 Oracle University and InfoTech (Pvt. Interpreting Execution Plans Chapter 4 .DISPLAY_SQL_PLAN_BASELINE() Viewing Execution Plans If you execute the EXPLAIN PLAN SQL*Plus command.DISPLAY() • • • • • SQL*Plus Autotrace: SET AUTOTRACE ON DBMS_XPLAN. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Viewing Execution Plans .Viewing Execution Plans • The EXPLAIN PLAN command followed by: – SELECT from PLAN_TABLE – DBMS_XPLAN. you can then SELECT from the PLAN_TABLE to view the execution plan. There are several SQL*Plus scripts available to format the plan table output.DISPLAY_CURSOR() DBMS_XPLAN.DISPLAY_SQLSET() DBMS_XPLAN. The DBMS_XPLAN package supplies five table functions: • • • • • DISPLAY: To format and display the contents of a plan table DISPLAY_AWR: To format and display the contents of the execution plan of a stored SQL statement in the AWR DISPLAY_CURSOR: To format and display the contents of the execution plan of any loaded cursor DISPLAY_SQL_PLAN_BASELINE: To display one or more execution plans for the SQL statement identified by SQL handle DISPLAY_SQLSET: To format and display the contents of the execution plan of statements stored in a SQL tuning set An advantage of using the DBMS_XPLAN package table functions is that the output is formatted consistently without regard to the source.DISPLAY_AWR() DBMS_XPLAN. All rights reserved. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using EXPLAIN PLAN • First use the EXPLAIN PLAN command to explain a SQL statement. It does not execute the statement. but simply produces the plan that may be used. All rights reserved. Interpreting Execution Plans Chapter 4 . Copyright © 2010. and inserts this plan into a table.Page 9 Oracle University and InfoTech (Pvt. • Then retrieve the plan steps by querying PLAN_TABLE. you can see how the Oracle Server executes the statement. PLAN_TABLE is automatically created as a global temporary table to hold the output of an EXPLAIN PLAN statement for all users. Note: You can create your own PLAN_TABLE using the $ORACLE_HOME/rdbms/admin/utlxplan. If you examine the plan. Oracle and/or its affiliates. PLAN_TABLE is the default sample output table into which the EXPLAIN PLAN statement inserts rows describing execution plans.sql script if you want to keep the execution plan information for a long term.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.The EXPLAIN PLAN Command • • Generates an optimizer execution plan Stores the plan in PLAN_TABLE • Does not execute the statement itself The EXPLAIN PLAN Command The EXPLAIN PLAN command is used to generate the execution plan that the optimizer uses to execute a SQL statement. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN Command . The EXPLAIN PLAN Command EXPLAIN PLAN SET STATEMENT_ID = 'text' INTO your plan table FOR statement The EXPLAIN PLAN Command (continued) This command inserts a row in the plan table for each step of the execution plan.Page 10 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved. the fields in italics have the following meanings: Copyright © 2010. Interpreting Execution Plans Chapter 4 . In the syntax diagram in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN Command . Explained. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN Command: Example . Oracle and/or its affiliates.The EXPLAIN PLAN Command: Example SQL> 2 3 4 5 EXPLAIN PLAN SET STATEMENT_ID = 'demo01' FOR SELECT e.department_id. SQL> Note: The EXPLAIN PLAN command does not actually execute the statement.departments d WHERE e.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.department_id = d.employees e. The EXPLAIN PLAN Command: Example This command inserts the execution plan of the SQL statement in the plan table and adds the optional demo01 name tag for future reference.department_name FROM hr.last_name.department_id.departments d WHERE e. Copyright © 2010. hr. d. You can also use the following syntax: EXPLAIN PLAN FOR SELECT e.employees e.department_id =d. d. Interpreting Execution Plans Chapter 4 .last_name.department_name FROM hr. hr.Page 11 Oracle University and InfoTech (Pvt. All rights reserved. Copyright © 2010. On UNIX. Oracle and/or its affiliates. you are introduced only to the EXPLAIN PLAN statement. you can use the utlxplan. It is recommended that you drop and rebuild your local PLAN_TABLE table after upgrading the version of the database because the columns might change. PLAN_TABLE just shows you a plan that might not be the one chosen by the optimizer. Hierarchy is established with the ID and PARENT_ID columns. The exact name and location of this script depends on your operating system. Whatever the method to gather and display the explain plan. and outputs its result in the PLAN_TABLE table.sql SQL script to manually create a local PLAN_TABLE in your schema and use it to store the results of EXPLAIN PLAN. Now. PLAN_TABLE is organized in a tree-like structure and you can retrieve that structure by using both the ID and PARENT_ID columns with a CONNECT BY clause in a SELECT statement.PLAN_TABLE • PLAN_TABLE: – Is automatically created to hold the EXPLAIN PLAN output. PLAN_TABLE There are various available methods to gather execution plans. it is located in the $ORACLE_HOME/rdbms/admin directory. This SQL statement gathers the execution plan of a SQL statement without executing it. Interpreting Execution Plans Chapter 4 . the basic format and goal are the same. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED PLAN_TABLE .sql. – Advantage: SQL is not executed – Disadvantage: May not be the actual execution plan • • PLAN_TABLE is hierarchical. However. PLAN_TABLE is the default sample output table into which the EXPLAIN PLAN statement inserts rows describing execution plans. if you are specifying the table. PLAN_TABLE is automatically created as a global temporary table and is visible to all users. All rights reserved. – You can create your own using utlxplan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 12 Oracle University and InfoTech (Pvt. While a PLAN_TABLE table is automatically set up for each user. This can cause scripts to fail or cause TKPROF to fail. first create PLAN_TABLE manually with the utlxplan. All rights reserved.Page 13 . Interpreting Execution Plans Chapter 4 .sql script. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: If you want an output table with a different name. and then rename the table with the RENAME SQL statement. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. DISPLAY()). This parameter represents a filter predicate or predicates to restrict the set of rows selected from the table where the plan is stored. subquery or function calls.null)).'typical'. TYPICAL displays the most relevant information in the plan: operation ID. and optimizer cost. the plan displayed corresponds to the last executed explain plan. You can also use the following syntax to retrieve the same result: SELECT * FROM TABLE(dbms_xplan.sql (or utlxplp. bytes. The output is the same as shown in the slide. In this example. you can substitute the name of another plan table instead of PLAN_TABLE and demo01 represents the statement ID. The DISPLAY function of the DBMS_XPLAN package can be used to format and display the last statement stored in PLAN_TABLE.filter("ENAME"='KING') Displaying from PLAN_TABLE: Typical In the example in the slide.'demo01'. This parameter can reference any column of the table where the plan is stored and can contain any SQL construct—for example. When value is null (the default). Interpreting Execution Plans Chapter 4 . name and option. Note: Alternatively.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Explained. The last parameter for the DISPLAY function is the one corresponding to filter_preds. you can run the utlxpls. number of rows. SQL> SET LINESIZE 130 SQL> SET PAGESIZE 0 SQL> select * from table(DBMS_XPLAN. Plan hash value: 3956160932 -------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 37 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------1 .display('plan_table'. Oracle and/or its affiliates.sql for parallel queries) script (located in the ORACLE_HOME/rdbms/admin/ directory) to display the execution plan Copyright © 2010.Displaying from PLAN_TABLE: Typical SQL> EXPLAIN PLAN SET STATEMENT_ID = 'demo01' FOR SELECT * FROM emp 2 WHERE ename = 'KING'.Page 14 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Displaying from PLAN_TABLE: Typical . the EXPLAIN PLAN command inserts the execution plan of the SQL statement in PLAN_TABLE and adds the optional demo01 name tag for future reference. All rights reserved. Page 15 . This script uses the DISPLAY table function from the DBMS_XPLAN package. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED stored in PLAN_TABLE for the last statement explained. Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. Interpreting Execution Plans Chapter 4 . 'ALL')).filter("ENAME"='KING') Column Projection Information (identified by operation id): ----------------------------------------------------------1 . ALIAS. Oracle and/or its affiliates.22] Displaying from PLAN_TABLE: ALL Here you use the same EXPLAIN PLAN command example as in the previous slide. "EMP"."MGR"[NUMBER."SAL"[NUMBER. For finer control on the display output. All rights reserved. It includes information displayed with the TYPICAL level.22]. "ENAME"[VARCHAR2.22]."DEPTNO"[NUMBER."EMP". "EMP". shows the number of bytes estimated by the optimizer • COST: If relevant. Plan hash value: 3956160932 -------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 37 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------1 . if the operation is distributed."COMM"[NUMBER. shows optimizer cost information • PARTITION: If relevant.10]. "EMP".7]. shows the number of rows estimated by the optimizer • BYTES: If relevant. "EMP". Interpreting Execution Plans Chapter 4 . and information about REMOTE SQL."EMPNO"[NUMBER. shows partition pruning information • • PARALLEL: If relevant.SEL$1 / EMP@SEL$1 Predicate Information (identified by operation id): --------------------------------------------------1 .22]. shows PX information (distribution method and table queue information) PREDICATE: If relevant. the following keywords can be added to the format parameter to customize its default behavior."JOB"[VARCHAR2.null.9]. shows the predicate section Copyright © 2010. The ALL option used with the DISPLAY function allows you to output the maximum user level information.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Each keyword either represents a logical group of plan table columns (such as PARTITION) or logical additions to the base plan table output (such as PREDICATE).DISPLAY(null. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Displaying from PLAN_TABLE: ALL . "EMP".Displaying from PLAN_TABLE: ALL SQL> select * from table(DBMS_XPLAN.22].Page 16 Oracle University and InfoTech (Pvt."HIREDATE"[DATE. Format keywords must be separated by either a comma or a space: • ROWS: If relevant. with additional information such as PROJECTION. "EMP". ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Oracle University and InfoTech (Pvt.Page 17 . All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • PROJECTION: If relevant. Oracle and/or its affiliates. Interpreting Execution Plans Chapter 4 . shows the projection section Copyright © 2010. Note: Format keywords can be prefixed with the “-” sign to exclude the specified information. ALLSTATS and LAST. shows the “Query Block Name/Object Alias” section REMOTE: If relevant. All rights reserved. MEMSTATS.Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The EXPLAIN PLAN Command . Copyright © 2010. it is a table used to capture the content of the fixed view V$SQL_PLAN_STATISTICS_ALL). shows the note section of the explain plan If the target plan table also stores plan statistics columns (for example. “-PROJECTION” excludes projection information. shows the information for the distributed query (for example.The EXPLAIN PLAN Command EXPLAIN PLAN SET STATEMENT_ID = 'text' INTO your plan table FOR statement Displaying from PLAN_TABLE: ALL (continued) • • • ALIAS: If relevant. additional format keywords can be used to specify which class of statistics to display when using the DISPLAY function. These additional format keywords are IOSTATS. Interpreting Execution Plans Chapter 4 . For example. remote from serial distribution and remote SQL) NOTE: If relevant. Oracle and/or its affiliates. This output format includes all sections from the ALL format plus the outline data that represents a set of hints to reproduce that particular plan. Plan hash value: 3956160932 -------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 37 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------Outline Data ------------/*+ BEGIN_OUTLINE_DATA FULL(@"SEL$1" "EMP"@"SEL$1") OUTLINE_LEAF(@"SEL$1") ALL_ROWS DB_VERSION('11.'ADVANCED -PROJECTION -PREDICATE -ALIAS')). Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.0.Displaying from PLAN_TABLE: ADVANCED select plan_table_output from table(DBMS_XPLAN. Note: When the ADVANCED format is used with V$SQL_PLAN. This section may be useful if you want to reproduce a particular execution plan in a different environment.DISPLAY(null. All rights reserved.0. This is the same section.1.6') IGNORE_OPTIM_EMBEDDED_HINTS END_OUTLINE_DATA */ Displaying from PLAN_TABLE: ADVANCED The ADVANCED format is available only from Oracle Database 10g. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Displaying from PLAN_TABLE: ADVANCED . Copyright © 2010. Release 2 and later versions. Oracle and/or its affiliates.Page 19 Oracle University and InfoTech (Pvt. there is one more section called Peeked Binds (identified by position). which is displayed in the trace file for event 10053.6') OPTIMIZER_FEATURES_ENABLE('11.null.1. partitioning (such as the set of accessed partitions).Explain Plan Using SQL Developer Explain Plan Using SQL Developer The Explain Plan icon generates the execution plan. All rights reserved. and data operations such as filter. the plan table displays information about optimization (such as the cost and cardinality of each operation). and parallel execution (such as the distribution method of join inputs).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Explain Plan Using SQL Developer . join method for tables affected by join operations in the statement.Page 20 Oracle University and InfoTech (Pvt. it shows the ordering of the tables referenced by the statement. which you can see in the Explain tab. Interpreting Execution Plans Chapter 4 . For each operation. access method for each table mentioned in the statement. Copyright © 2010. An execution plan shows a row source tree with the hierarchy of operations that make up the statement. Oracle and/or its affiliates. In addition to the row source tree. sort. or aggregation. Because it is purely declarative.Page 21 Oracle University and InfoTech (Pvt. The PLUSTRACE role is created and granted to the DBA role by running the supplied $ORACLE_HOME/sqlplus/admin/plustrce. Copyright © 2010.3 Needs a PLAN_TABLE Needs the PLUSTRACE role to retrieve statistics from some V$ views • By default.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The report is generated after successful SQL DML (that is. produces the execution plan and statistics after running the query May not be the execution plan used by the optimizer when using bind peeking (recursive EXPLAIN PLAN) • AUTOTRACE When running SQL statements under SQL*Plus or SQL Developer. With bind variables in general.sql script. Oracle and/or its affiliates. DELETE. SELECT. UPDATE.sql script. and then have the PLUSTRACE role granted to you. AUTOTRACE is an excellent diagnostic tool for SQL statement tuning. The database administrator (DBA) privileges are required to grant the PLUSTRACE role. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED AUTOTRACE .AUTOTRACE • • • • Is a SQL*Plus and SQL Developer facility Was introduced with Oracle 7. Interpreting Execution Plans Chapter 4 . On some versions and platforms. connect as SYSDBA and run the plustrce. If this is not the case on your platform. The PLUSTRACE role contains the select privilege on three V$ views. Note: The system does not support EXPLAIN PLAN for statements performing implicit type conversion of date bind variables. All rights reserved. it is easier to use than EXPLAIN PLAN. To use this feature. you can automatically get a report on the execution plan and the statement execution statistics. the EXPLAIN PLAN output might not represent the real execution plan. and INSERT) statements. you must have a PLAN_TABLE available in your schema. It is useful for monitoring and tuning the performance of these statements. this is run by the database creation scripts. These privileges are necessary to generate AUTOTRACE statistics. The AUTOTRACE Syntax OFF ON SET AUTOTRACE TRACE[ONLY] EXPLAIN STATISTICS SHOW AUTOTRACE The AUTOTRACE Syntax You can enable AUTOTRACE in various ways using the syntax shown in the slide. execution plans and statistics are displayed by default. but does not display execution plans Note: If both the EXPLAIN and STATISTICS command options are omitted. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The AUTOTRACE Syntax . All rights reserved. but does not display statistics • STATISTICS: Displays statistics. The command options are as follows: • OFF: Disables autotracing SQL statements • ON: Enables autotracing SQL statements • TRACE or TRACE[ONLY]: Enables autotracing SQL statements and suppresses statement output • EXPLAIN: Displays execution plans.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Interpreting Execution Plans Chapter 4 . Oracle and/or its affiliates.Page 22 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. SET AUTOTRACE TRACEONLY: This is similar to SET AUTOTRACE ON. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED AUTOTRACE: Examples . if any. The following are some examples: • • • • • SET AUTOTRACE ON: The AUTOTRACE report includes both the optimizer execution plan and the SQL statement execution statistics. but it suppresses the printing of the user’s query output. the query data is still fetched. All rights reserved. but not printed. SET AUTOTRACE OFF: No AUTOTRACE report is generated.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. SET AUTOTRACE ON STATISTICS: The AUTOTRACE report shows the SQL statement execution statistics and rows. Interpreting Execution Plans Chapter 4 . SET AUTOTRACE TRACEONLY EXPLAIN: The AUTOTRACE report shows only the optimizer execution path without executing the statement. If STATISTICS is enabled.Page 23 Oracle University and InfoTech (Pvt. This is the default.AUTOTRACE: Examples • To start tracing statements using AUTOTRACE: SQL> set autotrace on • To display the execution plan only without execution: SQL> set autotrace traceonly explain • To display rows and statistics: SQL> set autotrace on statistics • To get the plan and the statistics only (suppress rows): SQL> set autotrace traceonly AUTOTRACE: Examples You can control the report by setting the AUTOTRACE system variable. AUTOTRACE: Statistics SQL> show autotrace autotrace OFF SQL> set autotrace traceonly statistics SQL> SELECT * FROM oe. The results include the following statistics: • recursive calls is the number of recursive calls generated at both the user and system level.Page 24 Oracle University and InfoTech (Pvt. Copyright © 2010.products. 288 rows selected. • • • • physical reads is the total number of data blocks read from disk. This number equals the value of “physical reads direct” plus all reads into buffer cache. Oracle Database maintains tables used for internal processing.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. When Oracle Database needs to make a change to these tables. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED AUTOTRACE: Statistics . bytes sent via SQL*Net to client is the total number of bytes sent to the client from the foreground processes. Statistics -------------------------------------------------------1334 recursive calls 0 db block gets 686 consistent gets 394 physical reads 0 redo size 103919 bytes sent via SQL*Net to client 629 bytes received via SQL*Net from client 21 SQL*Net roundtrips to/from client 22 sorts (memory) 0 sorts (disk) 288 rows processed AUTOTRACE: Statistics The statistics are recorded by the server when your statement executes and indicate the system resources required to execute your statement. which in turn generates a recursive call. • db block gets is the number of times a CURRENT block was requested. Interpreting Execution Plans Chapter 4 . • consistent gets is the number of times a consistent read was requested for a block. it internally generates an internal SQL statement. All rights reserved. bytes received via SQL*Net from client is the total number of bytes received from the client over Oracle Net. Oracle and/or its affiliates. redo size is the total amount of redo generated in bytes. regardless of whether Oracle Net is installed. Note: The statistics printed by AUTOTRACE are retrieved from V$SESSTAT.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • sorts (memory) is the number of sort operations that were performed completely in memory and did not require any disk writes. physical reads indicates reads of blocks from disk. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • . Note: db block gets indicates reads of the current block from the database.SQL*Net roundtrips to/from client is the total number of Oracle Net messages sent to and received from the client.Page 25 Oracle University and InfoTech (Pvt. The client referred to in the statistics is SQL*Plus. Oracle and/or its affiliates. These should be low compared to the number of rows retrieved. You cannot change the default format of the statistics report. All rights reserved. • sorts (disk) is the number of sort operations that required at least one disk write. • rows processed is the number of rows processed during the operation. Sorts should be performed in memory rather than on disk. consistent gets are reads of blocks that must satisfy a particular system change number (SCN). Interpreting Execution Plans Chapter 4 . Oracle Net refers to the generic process communication between SQL*Plus and the server. db block gets and consistent gets are the two statistics that are usually monitored. This information can help you to identify SQL statements that will benefit from tuning.AUTOTRACE Using SQL Developer AUTOTRACE Using SQL Developer The Autotrace pane displays trace-related information when you execute the SQL statement by clicking the Autotrace icon. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. Interpreting Execution Plans Chapter 4 . Oracle and/or its affiliates.Page 26 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED AUTOTRACE Using SQL Developer . Similarly. V$SQL_PLAN is very similar to PLAN_TABLE: – PLAN_TABLE shows a theoretical plan that can be used if this statement were to be executed.Page 27 Oracle University and InfoTech (Pvt. Note: Another useful view is V$SQL_PLAN_STATISTICS. Link to V$SQL: – ADDRESS. which provides the execution statistics of each operation in the execution plan for each cached cursor. The execution plan obtained by the EXPLAIN PLAN statement can be different from the execution plan used to execute the cursor. and CHILD_NUMBER Using the V$SQL_PLAN View This view displays the execution plan for cursors that are still in the library cache. V$SQL_PLAN contains the actual plan used. – V$SQL_PLAN contains the actual plan used. different hints can cause different cursors. with each cursor further identified by a CHILD_NUMBER. HASH_VALUE. Copyright © 2010.Using the V$SQL_PLAN View • • V$SQL_PLAN provides a way of examining the execution plan for cursors that are still in the library cache. the same statement executed by different users has different cursors associated with it if the object that is referenced is in a different schema. All rights reserved. This is because the cursor might have been compiled with different values of session parameters or bind variables. Also.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • • It contains the execution plan of every cursor in the library cache (including child). The information in this view is very similar to the information in PLAN_TABLE. the V$SQL_PLAN_STATISTICS_ALL view concatenates information from V$SQL_PLAN with execution statistics from V$SQL_PLAN_STATISTICS and V$SQL_WORKAREA. V$SQL_PLAN shows the plan for a cursor rather than for all cursors associated with a SQL statement.. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using the V$SQL_PLAN View . However. For example. Oracle and/or its affiliates. Interpreting Execution Plans Chapter 4 . The difference is that a SQL statement can have more than one cursor associated with it. The V$SQL_PLAN table can be used to see the different plans for different child cursors of the same statement. The PLAN_HASH VALUE column is a numerical representation of the SQL plan for the cursor.Page 28 Oracle University and InfoTech (Pvt. which is more unique. Copyright © 2010. The ADDRESS. All rights reserved. SQL_ID is a 64-bit hash value.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL_HASH_VALUE is a 32-bit value and is not unique enough for large repositories of AWR data. HASH_VALUE. which you retrieve in many other V$ views. It is normally represented as a character string to make it more manageable. and CHILD_NUMBER columns can be used to join with V$SQL to add the child cursor–specific information. SQL_HASH_VALUE in V$SESSION has been complemented with SQL_ID. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The V$SQL_PLAN Columns . Interpreting Execution Plans Chapter 4 .The V$SQL_PLAN Columns HASH_VALUE Hash value of the parent statement in the library cache ADDRESS Address of the handle to the parent for this cursor CHILD_NUMBER Child cursor number using this execution plan POSITION Order of processing for all operations that have the same PARENT_ID PARENT_ID ID of the next execution step that operates on ID the output of the current step Number assigned to each step in the execution plan PLAN_HASH_VALUE Numerical representation of the SQL plan for the cursor Note: This is only a partial listing of the columns. plus several others. you can easily identify whether the two plans are the same or not (rather than comparing the two plans line-by-line). The columns that are also present in PLAN_TABLE have the same values: • ADDRESS • HASH_VALUE The ADDRESS and HASH_VALUE columns can be used to join with V$SQLAREA to add the cursor-specific information. By comparing one PLAN_HASH_VALUE with another. The V$SQL_PLAN Columns The view contains many of the PLAN_TABLE columns. Oracle and/or its affiliates. Note: Since Oracle Database 10g. the bottom 32 bits of which are SQL_HASH_VALUE. All rights reserved. Interpreting Execution Plans Chapter 4 . All statistics. and elapsed time. The statistics in V$SQL_PLAN_STATISTICS are available for cursors that have been compiled with the STATISTICS_LEVEL initialization parameter set to ALL or using the GATHER_PLAN_STATISTICS hint. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 29 Oracle University and InfoTech (Pvt.The V$SQL_PLAN_STATISTICS View • V$SQL_PLAN_STATISTICS provides actual execution statistics: – STATISTICS_LEVEL set to ALL – The GATHER_PLAN_STATISTICS hint • V$SQL_PLAN_STATISTICS_ALL enables side-by-side comparisons of the optimizer estimates with the actual execution statistics. the statistics for a join operation also include the statistics for its two inputs. Copyright © 2010. For example. The V$SQL_PLAN_STATISTICS View The V$SQL_PLAN_STATISTICS view provides the actual execution statistics for every operation in the plan. The V$SQL_PLAN_STATISTICS_ALL view contains memory-usage statistics for row sources that use SQL memory (sort or hash join). This view concatenates information in V$SQL_PLAN with execution statistics from V$SQL_PLAN_STATISTICS and V$SQL_WORKAREA. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The V$SQL_PLAN_STATISTICS View . are cumulative. such as the number of output rows. except the number of output rows. Links Between Important Dynamic Performance Views V$SQLAREA V$SQL V$SQL_WORKAREA V$SQLSTATS Estimated statistics for each row source V$SQL_PLAN Execution statistics for each row source V$SQL_PLAN_STATISTICS V$SQL_PLAN_STATISTICS_ALL Links Between Important Dynamic Performance Views V$SQLAREA displays statistics on shared SQL areas and contains one row per SQL string. • • PLAN_HASH_VALUE is a numeric representation of the SQL plan for this cursor. Copyright © 2010. CHILD_NUMBER is the number of this child cursor. Oracle and/or its affiliates. • HASH_VALUE is the value of the parent statement in the library cache. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Links Between Important Dynamic Performance Views . By comparing one PLAN_HASH_VALUE with another. It provides statistics on SQL statements that are in memory. V$SQL lists statistics on shared SQL areas and contains one row for each child of the original SQL text entered: • ADDRESS represents the address of the handle to the parent for this cursor. Interpreting Execution Plans Chapter 4 . VERSION_COUNT is the number of child cursors that are present in the cache under this parent. you can easily identify if the two plans are the same or not (rather than comparing the two plans line-by-line). and ready for execution: • • SQL_ID is the SQL identifier of the parent cursor in the library cache. • SQL_ID is the SQL identifier of the parent cursor in the library cache.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. parsed.Page 30 Oracle University and InfoTech (Pvt. All rights reserved. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and has a greater data retention (the statistics may still appear in this view. for long-running queries. This makes it easy to see the impact of long-running SQL statements while they are still in progress. The column definitions for columns in V$SQLSTATS are identical to those in the V$SQL and V$SQLAREA views. The ADDRESS. V$SQL_WORKAREA displays information about work areas used by SQL cursors. with each row representing the data for a unique combination of SQL text and optimizer plan (that is. HASH_VALUE. However. they are updated every five seconds. V$SQL_WORKAREA lists all work areas needed by these child cursors.Page 31 Oracle University and InfoTech (Pvt. HASH_VALUE. The ADDRESS. Copyright © 2010. All rights reserved. CHILD_NUMBER). V$SQL_WORKAREA can be joined with V$SQLAREA on (ADDRESS. The ADDRESS and HASH_VALUE columns can be used to join with V$SQLAREA to locate the parent cursor. . HASH_VALUE) and with V$SQL on (ADDRESS. Note that V$SQLSTATS contains a subset of columns that appear in V$SQL and V$SQLAREA. Oracle and/or its affiliates. This view concatenates information in V$SQL_PLAN with execution statistics from V$SQL_PLAN_STATISTICS and V$SQL_WORKAREA. more scalable. V$SQL_PLAN_STATISTICS_ALL contains memory usage statistics for row sources that use SQL memory (sort or hash join). and CHILD_NUMBER columns can be used to join with V$SQL to locate the child cursor using this area. the V$SQLSTATS view differs from V$SQL and V$SQLAREA in that it is faster. and CHILD_NUMBER columns can be used to join with V$SQL to add the child cursor–specific information. unique combination of SQL_ID and PLAN_HASH_VALUE). Each SQL statement stored in the shared pool has one or more child cursors that are listed in the V$SQL view. Interpreting Execution Plans Chapter 4 . what percentage of work areas run using maximum memory? V$SQLSTATS displays basic performance statistics for SQL cursors. HASH_VALUE. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Statistics displayed in V$SQL are normally updated at the end of query execution. V$SQL_PLAN_STATISTICS provides execution statistics at the row source level for each child cursor. You can use this view to find answers to the following questions: • What are the top 10 work areas that require the most cache area? • For work areas allocated in the AUTO mode. However.V$SQL_PLAN contains the execution plan information for each child cursor loaded in the library cache. even after the cursor has been aged out of the shared pool). Oracle and/or its affiliates.department_id Plan hash value: 2933537672 -------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU| -------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 6 (100| | 1 | MERGE JOIN | | 106 | 2862 | 6 (17| | 2 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 432 | 2 (0| | 3 | INDEX FULL SCAN | DEPT_ID_PK | 27 | | 1 (0| |* 4 | SORT JOIN | | 107 | 1177 | 4 (25| | 5 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0| -------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------4 ."DEPARTMENT_ID") 24 rows selected.department_name FROM hr.department_id =d.last_name.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Querying V$SQL_PLAN You can query V$SQL_PLAN using the DBMS_XPLAN.last_name.employees e."DEPARTMENT_ID"="D". You can retrieve the appropriate value by querying the SQL_ID column in V$SQL or V$SQLAREA. hr.departments d WHERE e. This parameter defaults to null in which case the plan of the last cursor executed by the session is displayed.access("E".Querying V$SQL_PLAN SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN. To obtain SQL_ID.department_id =d. d.Page 32 Oracle University and InfoTech (Pvt.department_id. you could select the PREV_SQL_ID column for a specific session out of V$SESSION. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Querying V$SQL_PLAN .department_name FROM hr."DEPARTMENT_ID") filter("E". SQL_ID 47ju6102uvq5q.DISPLAY_CURSOR('47ju6102uvq5q')).last_name. d. Interpreting Execution Plans Chapter 4 .employees e. SQL_ID is the SQL_ID of the SQL statement in the cursor cache. child number 0 ------------------------------------SELECT e. Alternatively.DISPLAY_CURSOR() function to display the current or last executed statement (as shown in the example). hr. 13saxr0mmz1s3 select SQL_id.last_name. sql_text from v$SQL … 47ju6102uvq5q SELECT e."DEPARTMENT_ID"="D". d. SQL_TEXT FROM V$SQL WHERE SQL_TEXT LIKE '%SELECT e.department_name … Copyright © 2010.%' . execute the following query: SELECT e. SELECT SQL_ID. All rights reserved.departments d WHERE e. You can pass the value of SQL_ID for the statement as a parameter to obtain the execution plan for a given statement. Page 33 Oracle University and InfoTech (Pvt. and ADVANCED). In addition to the standard values (BASIC.The FORMAT parameter controls the level of detail for the plan. The LAST keyword can be specified to see only the statistics for the last execution. such as hash joins.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED CHILD_NUMBER is the child number of the cursor to display. CHILD_NUMBER can be specified only if SQL_ID is specified. Copyright © 2010. the pga_aggregate_target parameter is set to a nonzero value). the execution plan of all cursors matching the supplied SQL_ID parameter are displayed. sort or some bitmap operators. plan statistics are shown for all executions of the cursor. there are additional supported values to display run-time statistics for the cursor: • • • • IOSTATS: Assuming that the basic plan statistics are collected when SQL statements are executed (either by using the GATHER_PLAN_STATISTICS hint or by setting the statistics_level parameter to ALL). execution mode of the operator. MEMSTATS: Assuming that the Program Global Area (PGA) memory management is enabled (that is. These statistics only apply to memory-intensive operations. SERIAL. All rights reserved. this format shows I/O statistics for ALL (or only for LAST) executions of the cursor. how much memory was used. this format allows to display memory management statistics (for example. TYPICAL. Oracle and/or its affiliates. Interpreting Execution Plans Chapter 4 . . ALL. ALLSTATS: A shortcut for 'IOSTATS MEMSTATS' LAST: By default. number of bytes spilled to disk. If not supplied. and so on). The statistics collected and processed by the AWR include: • Object statistics that determine both access and usage statistics of database segments • Time-model statistics based on time usage for activities.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. displayed in the V$SYS_TIME_MODEL and V$SESS_TIME_MODEL views • Some of the system and session statistics collected in the V$SYSSTAT and V$SESSTAT views • SQL statements that produce the highest load on the system. This infrastructure is used by many components. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic Workload Repository (AWR) . buffer gets. processes. All rights reserved. The ADDM compares the differences between snapshots to determine Copyright © 2010.Page 34 Oracle University and InfoTech (Pvt. and so on • ASH statistics. Oracle and/or its affiliates. processes. based on criteria. CPU time. and maintains performance statistics for problem-detection and self-tuning purposes Statistics include: – – – – • Object statistics Time-model statistics Some system and session statistics Active Session History (ASH) statistics Automatically generates snapshots of the performance data Automatic Workload Repository (AWR) The AWR is part of the intelligent infrastructure introduced with Oracle Database 10g. Interpreting Execution Plans Chapter 4 . The data in the snapshot interval is then analyzed by ADDM. The AWR automatically collects.Automatic Workload Repository (AWR) • • Collects. representing the history of recent sessions The database automatically generates snapshots of the performance data once every hour and collects the statistics in the workload repository. and maintains systemperformance statistics for problem-detection and self-tuning purposes and stores the statistics persistently in the database. such as elapsed time. such as Automatic Database Diagnostic Monitor (ADDM) for analysis. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you can manage the frequency and retention period of SQL that is stored in the AWR. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED which SQL statements to capture based on the effect on the system load. This reduces the number of SQL statements that need to be captured over time. such as DBMS_WORKLOAD_REPOSITORY or Oracle Enterprise Manager. All rights reserved. Oracle University and InfoTech (Pvt. Interpreting Execution Plans Chapter 4 . Copyright © 2010.Page 35 .Note: By using PL/SQL packages. Page 36 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.CREATE_SNAPSHOT ('ALL'). Snapshots are automatically generated for an Oracle Database. Creating Snapshots You can manually create snapshots with the CREATE_SNAPSHOT procedure if you want to capture statistics at times different than those of the automatically generated snapshots. dbid => 3310949047). a snapshot for the instance is created immediately with the flush level specified to the default flush level of TYPICAL. Interpreting Execution Plans Chapter 4 . • Dropping snapshots: SQL> exec DBMS_WORKLOAD_REPOSITORY. To invoke these procedures.Managing AWR with PL/SQL • Creating snapshots: SQL> exec DBMS_WORKLOAD_REPOSITORY. drop.MODIFY_SNAPSHOT_SETTINGS – (retention => 43200. Oracle and/or its affiliates. high_snap_id => 32. In this example. however. a user must be granted the DBA role. dbid => 3310949047). Here is an example: Exec DBMS_WORKLOAD_REPOSITORY. Dropping Snapshots Copyright © 2010.DROP_SNAPSHOT_RANGE – (low_snap_id => 22. • Managing snapshot settings: SQL> exec DBMS_WORKLOAD_REPOSITORY. and modify the snapshots and baselines that are used by the ADDM. monitoring functions can be managed with procedures in the DBMS_WORKLOAD_REPOSITORY package. Managing AWR with PL/SQL Although the primary interface for managing the AWR is Enterprise Manager.CREATE_SNAPSHOT ('ALL'). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Managing AWR with PL/SQL . You can view this snapshot in the DBA_HIST_SNAPSHOT view. you can use DBMS_WORKLOAD_REPOSITORY procedures to manually create. interval => 30. Snapshots and baselines are sets of historical data for specific time periods that are used for performance comparisons. All rights reserved. To view a list of the snapshot IDs along with database IDs. The optional database identifier is 3310949047. the retention period is specified as 43. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED You can drop a range of snapshots using the DROP_SNAPSHOT_RANGE procedure. For example. . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the range of snapshot IDs to drop is specified from 22 to 32. as in the following example: Exec DBMS_WORKLOAD_REPOSITORY.200 minutes (30 days). and the interval between each snapshot is specified as 30 minutes. However. If you do not specify a value for dbid. ASH data that belongs to the time period specified by the snapshot range is also purged when the DROP_SNAPSHOT_RANGE procedure is called. You can check the current settings for your database instance with the DBA_HIST_WR_CONTROL view. the local database identifier is used as the default value.(low_snap_id => 22. All rights reserved. Copyright © 2010. dbid => 3310949047). note that this can affect the precision of the Oracle diagnostic tools. dbid => 3310949047). the existing value is preserved. the local database identifier is used as the default value.DROP_SNAPSHOT_RANGE . high_snap_id => 32. The optional database identifier is 3310949047. To adjust the settings.In the example.Page 37 Oracle University and InfoTech (Pvt. use the MODIFY_SNAPSHOT_SETTINGS procedure.MODIFY_SNAPSHOT_SETTINGS( -retention => 43200. check the DBA_HIST_SNAPSHOT view. interval => 30. Modifying Snapshot Settings You can adjust the interval and retention of snapshot generation for a specified database ID. The RETENTION setting specifies how long (in minutes) snapshots are stored in the workload repository. you can drop the following range of snapshots: Exec DBMS_WORKLOAD_REPOSITORY. The INTERVAL setting specifies how often (in minutes) snapshots are automatically generated. Interpreting Execution Plans Chapter 4 . If NULL is specified. In this example. If you do not specify a value for dbid. you can also view the statistics directly from the following views: V$ACTIVE_SESSION_HISTORY: This view displays active database session activity. The DBA_HIST views contain historical data stored in the database. This group of views includes: • • • DBA_HIST_ACTIVE_SESS_HISTORY displays the history of the contents of the sampled in-memory active session history for recent system activity. V$ metric views provide metric data to track the performance of the system. Copyright © 2010. Interpreting Execution Plans Chapter 4 . However. and tablespace metrics. The metric views are organized into various groups.Page 38 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. These groups are identified in the V$METRICGROUP view. service. such as event.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. session. event class. DBA_HIST_DATABASE_INSTANCE displays information about the database environment. sampled once every second.Important AWR Views • • • V$ACTIVE_SESSION_HISTORY V$ metric views DBA_HIST views: – – – – – DBA_HIST_ACTIVE_SESS_HISTORY DBA_HIST_BASELINE DBA_HIST_DATABASE_INSTANCE DBA_HIST_SNAPSHOT DBA_HIST_SQL_PLAN DBA_HIST_WR_CONTROL Important AWR Views You can view the AWR data on Oracle Enterprise Manager screens or in AWR reports. • DBA_HIST_SNAPSHOT displays information about snapshots in the system. file. DBA_HIST_BASELINE displays information about the baselines captured in the system. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Important AWR Views . system. • DBA_HIST_SQL_PLAN displays SQL execution plans. DBA_HIST_WR_CONTROL displays the settings for controlling AWR. Oracle University and InfoTech (Pvt.Page 39 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. Oracle and/or its affiliates. Interpreting Execution Plans Chapter 4 . All rights reserved. * FROM DBA_HIST_SQLTEXT ht.departments.Querying the AWR • Retrieve all execution plans stored for a particular SQL_ID. SQL_ID SQL_TEXT ------------.DISPLAY_AWR() function to display all stored plans in the AWR. SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE (DBMS_XPLAN.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.null. Interpreting Execution Plans Chapter 4 .” SELECT tf. SQL_ID is the SQL_ID of the SQL statement in the cursor cache.employees natural join hr.employees natural join hr.------------------------------------------F8tc4anpz5cdb select sql_id. table (DBMS_XPLAN. and FORMAT parameters.departments Plan hash value: 4179021502 ---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 6 (100)| | | 1 | HASH JOIN | | 11 | 968 | 6 (17)| 00:00:01 | | 2 | TABLE ACCESS FULL| DEPARTMENTS | 11 | 220 | 2 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------------- • Display all execution plans of all statements containing “JF. PLAN_TABLE_OUTPUT -----------------------------------------------------------------------------------SQL_ID 454rug2yva18w -------------------select /* example */ * from hr. All rights reserved. 'ALL' )) tf Querying the AWR You can use the DBMS_XPLAN. null. you pass in a SQL_ID as an argument. Query V$SQL_TEXT to obtain the SQL_ID: SQL> select sql_id. 2. In the example in the slide.DISPLAY_AWR('454rug2yva18w')). sql_text from v$SQL where sql_text like '%example%'.sql_text like '%JF%'. DB_ID. sql_text from v$SQL … 454rug2yva18w select /* example */ * from … Copyright © 2010. Execute the SQL statement: SQL> select /* example */ * from hr. The steps to complete this example are as follows: 1. Oracle and/or its affiliates. WHERE ht. The DISPLAY_AWR() function also takes the PLAN_HASH_VALUE.DISPLAY_AWR(ht.Page 40 Oracle University and InfoTech (Pvt.sql_id. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Querying the AWR . verify that this statement has been captured in the DBA_HIST_SQLTEXT dictionary view. Interpreting Execution Plans Chapter 4 . SQL> SELECT SQL_ID. SQL_TEXT FROM dba_hist_sqltext WHERE SQL_ID =' 454rug2yva18w'.DISPLAY_AWR () function to retrieve the execution plan: SQL>SELECT PLAN_TABLE_OUTPUT FROM TABLE (DBMS_XPLAN. Oracle and/or its affiliates.create_snapshot. SQL_ID SQL_TEXT -------------- ------------------------------- 454rug2yva18w select /* example */ * from … 4. Using the SQL_ID.Page 41 Oracle University and InfoTech (Pvt. . SQL> SELECT SQL_ID. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 3. All rights reserved. PL/SQL procedure successfully completed. Use the DBMS_XPLAN.DISPLAY_AWR('454rug2yva18w’)).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL_TEXT FROM dba_hist_sqltext WHERE SQL_ID =' 454rug2yva18w'. If the query does not return rows.no rows selected You can take a manual AWR snapshot rather than wait for the next snapshot (which occurs every hour). Then check to see if it has been captured in DBA_HIST_SQLTEXT: SQL> exec dbms_workload_repository. it indicates that the statement has not yet been loaded in the AWR. Copyright © 2010. In 10gR2. All rights reserved. the equivalent to sqrepsql. Release 2.sql file. Interpreting Execution Plans Chapter 4 . the AWR SQL report can be generated by calling the $ORACLE_HOME/rdbms/admin/awrsqrpt. For example. In 10.sql is available.display_awr('dvza55c7zu0yv')). it is possible to generate SQL reports from AWR data. You can display the plan information in AWR by using the display_awr table function in the dbms_xplan PL/SQL package. in 10gR2. Copyright © 2010. basically.sql with Statspack. the equivalent of sprepsql. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Generating SQL Reports from AWR Data .4.0.Page 42 Oracle University and InfoTech (Pvt.sql is not available in AWR. or a plain text report? Specify the number of days of snapshots to choose from Specify the Begin and End Snapshot Ids … Specify the SQL Id … Enter value for sql_id: dvza55c7zu0yv Specify the Report Name … Generating SQL Reports from AWR Data Since Oracle Database 10g.0.Generating SQL Reports from AWR Data SQL> @$ORACLE_HOME/rdbms/admin/awrsqrpt Specify the Report Type … Would you like an HTML report. this displays the plan information for a SQL_ID in AWR: select * from table(dbms_xplan. You can retrieve the appropriate value for the SQL statement of interest by querying SQL_ID in the DBA_HIST_SQLTEXT column.1. However. the equivalent to sprepsql. an entry is added to the dynamic performance V$SQL_MONITOR view. SQL monitoring is automatically started when a SQL statement runs parallel. To prevent the hinted SQL statement from being monitored. As mentioned.SQL Monitoring: Overview STATISTICS_LEVEL=TYPICAL|ALL + CONTROL_MANAGEMENT_PACK_ACCESS=DIAGNOSTIC+TUNING SQL monitoring Every second V$SQL_MONITOR V$SQL_PLAN_MONITOR DBMS_SQLTUNE. the CONTROL_MANAGEMENT_PACK_ACCESS parameter must be set to DIAGNOSTIC+TUNING (the default value) because SQL monitoring is a feature of the Oracle Database Tuning Pack. All rights reserved. including the elapsed time.Page 43 Oracle University and InfoTech (Pvt. SQL monitoring is active by default. two statement-level hints are available to force or prevent a SQL statement from being monitored. However. use the NO_MONITOR hint. You can monitor the statistics for SQL statement execution using the V$SQL_MONITOR and V$SQL_PLAN_MONITOR views. After monitoring is initiated. use the MONITOR hint. Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. CPU time. By default. This entry tracks key performance metrics collected for the execution. number of reads and writes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Monitoring: Overview . Oracle and/or its affiliates.REPORT_SQL_MONITOR V$SQL V$SQL_PLAN V$ACTIVE_SESSION_HISTORY V$SESSION_LONGOPS V$SESSION SQL Monitoring: Overview The SQL monitoring feature is enabled by default when the STATISTICS_LEVEL initialization parameter is either set to ALL or TYPICAL (the default value). or when it has consumed at least five seconds of the CPU or I/O time in a single execution. and various other wait Copyright © 2010. To force SQL monitoring. I/O wait time. Additionally. you can use the SQL monitoring report to view SQL monitoring data. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED times. V$ACTIVE_SESSION_HISTORY. The V$SQL_MONITOR and V$SQL_PLAN_MONITOR views can be used in conjunction with the following views to get additional information about the execution that is monitored: V$SQL. These statistics are refreshed in near real time as the statement executes.Page 44 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. V$SESSION_LONGOPS. The entry is eventually deleted so its space can be reclaimed as new statements are monitored.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. monitoring information is not deleted immediately. The SQL monitoring report is also available in a GUI version through Enterprise Manager and SQL Developer Copyright © 2010. V$SQL_PLAN. . All rights reserved. Interpreting Execution Plans Chapter 4 . generally once every second. but is kept in the V$SQL_MONITOR view for at least one minute. and V$SESSION Instead.After the execution ends. All rights reserved. a composite key called an execution key is generated. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Monitoring Report: Example . The Global Information section gives you some important information: • To uniquely identify two executions of the same SQL statement. By default. HTML. you print the SQL monitoring report from a second session.01 | 19 | 259K | 199K | ------------------------------------------------------------ SQL Monitoring Report: Example In this example. a text report is generated for the last execution that was monitored if no parameters are specified as shown in the example in the slide.REPORT_SQL_MONITOR function accepts several input parameters to specify the execution. Oracle and/or its affiliates. each corresponding to a column in V$SQL_MONITOR: - SQL identifier to identify the SQL statement (SQL_ID) - An internally generated identifier to ensure that this primary key is truly unique (SQL_EXEC_ID) Copyright © 2010. SQL Monitoring Report In a different session SQL Text -------------------------select count(*) from sales SQL> select count(*) from sales. After the SELECT statement is started.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Global Information Status : EXECUTING Instance ID : 1 Session ID : 125 SQL ID : fazrk33ng71km SQL Execution ID : 16777216 Plan Hash Value : 1047182207 Execution Started : 02/19/2008 21:01:18 First Refresh Time : 02/19/2008 21:01:22 Last Refresh Time : 02/19/2008 21:01:42 -----------------------------------------------------------| Elapsed | Cpu | IO | Other | Buffer | Reads | | Time(s) | Time(s) | Waits(s) | Waits(s) | Gets | | -----------------------------------------------------------| 22 | 3. or XML). The DBMS_SQLTUNE.SQL Monitoring Report: Example SQL> SQL> SQL> SQL> set long 10000000 set longchunksize 10000000 set linesize 200 select dbms_sqltune. From the report. This execution key consists of three attributes. and while it executes. you can see that the SELECT statement executes currently.Page 45 Oracle University and InfoTech (Pvt. it is assumed that you SELECT from SALES from a different session than the one used to print the SQL monitoring report. the level of detail in the report.36 | 0.report_sql_monitor from dual. Interpreting Execution Plans Chapter 4 . and the report type (TEXT. Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED - Copyright © 2010.A start execution time stamp (SQL_EXEC_START) The report also shows you some important statistics calculated so far. Interpreting Execution Plans Chapter 4 . Oracle and/or its affiliates.Page 46 . All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. In this report.Page 47 Oracle University and InfoTech (Pvt. The running operation is marked by an arrow in the Id column of the report. The Time Active(s) column shows how long the operation has been active (the delta in seconds between the first and the last active time). The Starts column shows the number of times the operation in the execution plan was executed. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Monitoring Report: Example . when the operation in the execution plan started relative to the SQL statement execution start time.00 | Cpu (4) | 74% | ================================================================================== SQL Monitoring Report: Example (continued) The report then displays the execution path currently used by your statement. so that you can focus your analysis on those parts.SQL Monitoring Report: Example SQL Plan Monitoring Details ==================================================================================== | Id | Operation | Name | Rows | Cost | Time | Start | | | | | (Estim) | | Active(s) | Active | ==================================================================================== | 0 | SELECT STATEMENT | | | 78139 | | | | 1 | SORT AGGREGATE | | 1 | | | | | -> 2 | TABLE ACCESS FULL | SALES | 53984K | 78139 | 23 | +1 | | | | | | | | | ==================================================================================== ================================================================================== Starts | Rows | Activity | Activity Detail | Progress | | (Actual) | (percent) | (sample #) | | 1 | | | | | 1 | | | | | 1 | 42081K | 100. and the Rows (Estim) column shows the estimated cardinality from the optimizer. SQL monitoring gives you the display of the current operation that executes in the plan. Activity Detail (sample#) shows the nature of that activity (such as CPU or wait event). Interpreting Execution Plans Chapter 4 . the table access full operation at Id 2 was the first to start (+1s Start Active) and ran for the first 23 seconds so far. The Rows (Actual) column indicates the number of rows produced. The Start Active column shows. The Activity (percent) and Activity Detail (sample #) columns are derived by joining the V$SQL_PLAN_MONITOR and V$ACTIVE_SESSION_HISTORY views. in seconds. Activity (percent) shows the percentage of database time consumed by each operation of the execution plan. Copyright © 2010. This enables you to detect parts of the plan that are the most time consuming. Note: Not shown by this particular report. In this report. Oracle University and InfoTech (Pvt.Page 48 . this activity consists of 4 samples. is consumed by operation Id 2 (TABLE ACCESS FULL of SALES).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. so far. the Activity Detail (sample #) column shows that most of the database time. Progress. Oracle and/or its affiliates. All rights reserved. 100%. So far. the TABLE ACCESS FULL operation is 74% complete. and only for the instrumented row sources. This column only appears in the report after a certain amount of time.The last column. Copyright © 2010. shows progress monitoring information for the operation from the V$SESSION_LONGOPS view. which are only attributed to CPU. it shows that. Interpreting Execution Plans Chapter 4 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED In this report. the Memory and Temp columns indicate the amount of memory and temporary space consumed by corresponding operation of the execution plan. the first one is executed first. Oracle and/or its affiliates. Root/Parent Executed next Level 1 From top/down Parent/Child Level 2 From left/right Child/Leaf Parent/Child Child/Leaf Child/Leaf Child/Leaf Parent/Child Level 3 Child/Leaf Level 4 Interpreting an Execution Plan Explain plan output is a representation of a tree of row sources. Note: The course focuses on serial plans and does not discusses parallel execution plans. All rights reserved. respectively. The explain plan utility indents nodes to indicate that they are the children of the parent above it. these columns correspond to the id. PARENT_ID. Next.Interpreting an Execution Plan id= 1 id= 2 id= 3 id= 4 id= 5 id= 6 id= 7 id= 8 id= 9 id=10 (pid= ) (pid=1) (pid=2) (pid=2) (pid=4) (pid=4) (pid=1) (pid=7) (pid=7) (pid=9) (pos=1) (pos=1) (pos=2) (pos=1) (pos=2) (pos=2) (pos=1) (pos=2) (pos=1) root/parent parent/child child/leaf parent/child child/leaf child/leaf parent/child child/leaf parent/child child/leaf Executed first Parent/Child Transform it into a tree. and POSITION columns. there is a parent–child relationship between steps. which have parent_id or pid with value 1. You can start from the top. Each step (line in the execution plan or node in the tree) represents a row source.Page 49 Oracle University and InfoTech (Pvt. The steps of the execution plan are not performed in the order in which they are numbered.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. with id=1. In PLAN_TABLE and V$SQL_PLAN. which is the root node in the tree. The order of the nodes under the parent indicates the order of execution of the nodes within that level. pid. you must find the operations that feed this root node. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Interpreting an Execution Plan . the leaf at the left on each level of the tree is where the execution starts. Interpreting Execution Plans Chapter 4 . Copyright © 2010. In a trace file. That is accomplished by operations. If two steps are indented at the same level. and pos fields. In the tree format. One way to read an execution plan is by converting it into a graph that has a tree structure. the important elements to retrieve the tree structure are the ID. Copyright © 2010. 6. move down to the next ID and repeat the process. Look for rows which have a PID (parent) equal to this value. Start at the top. the parent is executed next. but does not consume any. Oracle and/or its affiliates. work back up the tree. 4. 4. finding new rows with the same PID. The first thing to determine in an explain plan is which node is executed first. 5. Move down the row sources until you get to one which produces data. 2. Interpreting Execution Plans Chapter 4 . Large plans are exactly the same as smaller ones. Take the ID with the lowest number and place it at the top. Now that this parent and its children are completed. After all the IDs for a parent have been found. Start at the top. Now that this parent and its children are completed. Standard tree interpretation: 1. All rights reserved. Execute as before. work back up the tree. After the children are executed. you can read most plans easily. Move back up the plan until all row sources are exhausted.1. do the following: . 6.Page 50 Oracle University and InfoTech (Pvt. This is the start row source. The same basic rules apply. Look at the siblings of this row source. Move down the tree to the left until you reach the left node. 3. 2. the parent is executed next. Move back up the tree until all row sources are exhausted. 3. These row sources are executed next. These row sources are executed next. Place these in the tree below the Parent according to their POS values from the lowest to the highest. Execute as before. You can always collapse the plan to hide a branch of the tree which does not consume much of the resources. The method in the slide explains this. After the children are executed. ordered from left to right. If you remember the few basic rules of explain plans and with some experience. Look at the siblings of this row source. 3. but with more entries. 2. 5.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and look at the siblings of the parent row source and its parents. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED To draw plan as a tree. but sometimes with complicated plans it is difficult to do this and also difficult to follow the steps through to the end. 4. Standard explain plan interpretation: 1. and look at the siblings of the parent row source and its parents. This is executed first. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Execution Plan Interpretation: Example 1 .Page 51 Oracle University and InfoTech (Pvt.sal between losal and hisal). • 2: ID=2.deptno=emp. The query tries to find employees who have salaries outside the range of salaries in the salary grade table. Oracle and/or its affiliates.Execution Plan Interpretation: Example 1 SELECT /*+ RULE */ ename.job. See the execution order for this query. the nested loops join step. The slide shows a query with its associated execution plan and the same plan in the tree format. Interpreting Execution Plans Chapter 4 ."DEPTNO"="EMP". a full table scan of SALGRADE in ID=6 (at the same level in the tree as ID=2. therefore.access("DEPT".dname FROM emp. • 4: The ROWIDs from the index are used to lookup the other information from the DEPT table in ID=4. and from the previous slide.deptno and not exists(SELECT * FROM salgrade WHERE emp. the execution order is 3 – 5 – 4 – 2 – 6 – 1: • 3: The plan starts with a full table scan of EMP (ID=3).sal. • 5: The rows are passed back to the controlling nested loops join step (ID=2). its sibling) is executed.dept WHERE dept. which uses them to execute the lookup of rows in the PK_DEPT index in ID=5. -------------------------------------------------| Id | Operation | Name | -------------------------------------------------| 0 | SELECT STATEMENT | | |* 1 | FILTER | | | 2 | NESTED LOOPS | | | 3 | TABLE ACCESS FULL | EMP | | 4 | TABLE ACCESS BY INDEX ROWID| DEPT | |* 5 | INDEX UNIQUE SCAN | PK_DEPT | |* 6 | TABLE ACCESS FULL | SALGRADE | -------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------1 . Copyright © 2010.filter( NOT EXISTS (SELECT 0 FROM "SALGRADE" "SALGRADE" WHERE "HISAL">=:B1 AND "LOSAL"<=:B2)) 5 . The query is a SELECT statement from two tables with a subquery based on another table to check the salary grades.filter("HISAL">=:B1 AND "LOSAL"<=:B2) FILTER NESTED LOOPS 1 6 2 TABLE ACCESS FULL SALGRADE 4 3 TABLE ACCESS FULL EMP TABLE ACCESS BY ROWID DEPT 5 INDEX UNIQUE SCAN PK_DEPT Execution Plan Interpretation: Example 1 You start with an example query to illustrate how to interpret an execution plan. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Based on the example in the slide. • 6: After ID=2 has exhausted its row sources. is executed until completion. • 1: This is used to filter the rows from ID2 and ID6."DEPTNO") 6 . Oracle and/or its affiliates. the easiest way is to consider it as the order in which execution completes.Oracle University and InfoTech (Pvt. Probably. Interpreting Execution Plans Chapter 4 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note that children are executed before parents. so for the NESTED LOOPS join at ID=2. Copyright © 2010.Page 52 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. the children are notated as executed first. so although structures for joins must be set up before the child execution. the two children {ID=3 and ID=4 (together with its child)} must have completed their execution before ID=2 can be completed. • Starts indicates how many times the corresponding operation was processed. Then the system accesses the DEPT table by its unique index (PK_DEPT) to get DNAME using DEPTNO from the previous result set. SAL. JOB. To do that.dept where dept. Interpreting Execution Plans Chapter 4 . At this step (ID=2). once for each EMP rows.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. no rows selected SQL> select * from table(dbms_xplan. All rights reserved.deptno = emp.Execution Plan Interpretation: Example 1 SQL> alter session set statistics_level=ALL.sal between losal and hisal). For each row from the EMP table.null. SAL. If you observe the statistics closely. Oracle and/or its affiliates. SQL> select /*+ RULE to make sure it reproduces 100% */ ename.job. the system gets all ENAME.sal.deptno and not exists (select * from salgrade where emp. However.'TYPICAL IOSTATS LAST')).display_cursor(null. and DEPTNO. child number 0 ------------------------------------… Plan hash value: 1175760222 -------------------------------------------------------------------------------| Id | Operation | Name | Starts | A-Rows | Buffers | -------------------------------------------------------------------------------|* 1 | FILTER | | 1 | 0 | 61 | | 2 | NESTED LOOPS | | 1 | 14 | 25 | | 3 | TABLE ACCESS FULL | EMP | 1 | 14 | 7 | | 4 | TABLE ACCESS BY INDEX ROWID| DEPT | 14 | 14 | 18 | |* 5 | INDEX UNIQUE SCAN | PK_DEPT | 14 | 14 | 4 | |* 6 | TABLE ACCESS FULL | SALGRADE | 12 | 12 | 36 | -------------------------------------------------------------------------------… Execution Plan Interpretation: Example 1 (continued) The example in the slide is a plan dump from V$SQL_PLAN with STATISTICS_LEVEL set to ALL. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Execution Plan Interpretation: Example 1 . operations from ID 5 and 4 are started 14 times. the TABLE ACCESS FULL operation on the EMP table (ID=3) is started once. This operation only needs to be done 12 times in this case because Copyright © 2010. the system accesses the SALGRADE table using a FULL TABLE SCAN operation to check if the employee’s salary is outside the salary range. • Buffers corresponds to the number of consistent reads done by the row source. This report shows you some important additional information compared to the output of the EXPLAIN PLAN command: • A-Rows corresponds to the number of rows produced by the corresponding row source. for each row from ID=2. the system gets its ENAME. and DNAME. SQL_ID 274019myw3vuf. JOB.Page 53 Oracle University and InfoTech (Pvt. The system now must filter out employees who have salaries outside the range of salaries in the salary grade table. Session altered.dname from emp. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED at run time the system does the check for each distinct salary. Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Page 54 . Interpreting Execution Plans Chapter 4 .Oracle University and InfoTech (Pvt. and there are 12 distinct salaries in the EMP table. ID=4 is the first row source that does not consume any. department names. which produces data. All rights reserved. Node ID=5 has a child ID=6. which are used to lookup in the LOCATIONS table in ID=3. These row sources are executed next. which is executed before it. ID=0 2. This is another index range scan producing ROWIDs. but does not consume any. The sibling at the same level as ID=3 is ID=5. and addresses for employees whose departments are located in Seattle and who have managers. .Page 55 Oracle University and InfoTech (Pvt.city = 'Seattle'.last_name as dept_manager d. In this case.Execution Plan Interpretation: Example 2 SQL> 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 2 5 1 7 select . which are used to lookup in the DEPARTMENTS table in ID=5.employees m join hr. The position is reflected by the indentation. This is the start row source. ID=4 is executed first. and 3 consume data. Start at the top. and PID in the second column. The execution plan shows two nested loops join operations. Oracle and/or its affiliates.employee_id) natural join hr. from where /*+ USE_NL(d) use_nl(m) */ m. 2. Look at the siblings of this row source.manager_id = m. SELECT STATEMENT NESTED LOOPS NESTED LOOPS TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN TABLE ACCESS BY INDEX ROWID INDEX UNIQUE SCAN 1 LOCATIONS LOC_CITY_IX DEPARTMENTS DEPT_LOCATION_IX EMPLOYEES EMP_EMP_ID_PK 7 2 3 5 4 6 8 Execution Plan Interpretation: Example 2 This query retrieves names.department_name l.locations l l. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Execution Plan Interpretation: Example 2 . 1. Interpreting Execution Plans Chapter 4 . 3. ID 0. You follow the steps from the previous example: 1.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Move down the row sources until you get to the one.street_address hr. For formatting reasons. The index range scan produces ROWIDs.departments d on (d. the explain plan has the ID in the first column. This has a child ID=8. Finally this is brought together with the NESTED LOOPS at ID=1. walk back up the tree. which passes the results back to ID=0. the EMPLOYEES table is accessed to retrieve the last name. the result of this first join operation is the driving row source for the second nested loops join. Interpreting Execution Plans Chapter 4 . This is because you search for departments in Seattle only. The execution order is: 4 – 3 – 6 – 5 – 2 – 8 – 7 – 1 – 0 Here is the complete description of this plan: The inner nested loops is executed first using LOCATIONS as the driving table. . The result is joined with the DEPARTMENTS table. Execute as before. Oracle and/or its affiliates. the parent operation is next. The sibling of ID=2 at the same level in the plan is ID=7. Finally. All rights reserved. The second join probes the index on the EMPLOYEE_ID column of the EMPLOYEES table. The NESTED LOOPS join at ID=2 is executed next bringing together the underlying data. 7. Note that this is a unique scan because it is based on the primary key.Page 56 Oracle University and InfoTech (Pvt.5. which is executed first. using the index on the LOCATION_ID join column. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 4.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. which are used to lookup in the EMPLOYEES table in ID=7. The system can do that because it knows (from the first join) the employee ID of all managers of departments in Seattle. and look at the siblings of the parent row source and its parents. Move back up the plan until all row sources are exhausted. using an index access on the CITY column. 6. Copyright © 2010. The index unique scan produces ROWIDs. Now that this parent and its children are completed. After the children operation. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.T2 .i and a.i) from t1 a. Interpreting Execution Plans Chapter 4 . Try to find the order in which the plan is executed and deduce what is the join order (order in which the system joins tables).T3 Execution Plan Interpretation: Example 3 See the execution plan in the slide. In conclusion.i=T2.i.i=T3. The position is reflected by the indentation. t3 c where a. 0 1 2 3 4 5 6 1 2 2 4 4 SELECT STATEMENT SORT AGGREGATE HASH JOIN TABLE ACCESS FULL T3 HASH JOIN TABLE ACCESS FULL T1 TABLE ACCESS FULL T2 1 2 3 4 5 6 Join order is: T1 .Execution Plan Interpretation: Example 3 select /*+ ORDERED USE_HASH(b) SWAP_JOIN_INPUTS(c) */ max(a. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Execution Plan Interpretation: Example 3 . the system probes T3 (T1. t2 b. • The system picks a row from T2 and probes T1 (T1. • The system outputs the maximum value from the previous result set.i). the system sends it to next operation. Here is the interpretation of this plan: • The system first hashes the T3 table (Operation ID=3) into memory.Page 57 Oracle University and InfoTech (Pvt. • Then it hashes the T1 table (Operation ID=5) into memory. • Then the scan of the T2 table begins (Operation ID=6). It is important to recognize what the join order of an execution plan is. Again.i = b. • If the row survives. All rights reserved.i = c. to be able to find your plan in a 10053 event trace file. the execution order is : 3 – 5 – 6 – 4 – 2 – 1 The join order is: T1 – T2 – T3 Copyright © 2010.i). Oracle and/or its affiliates. ID is in the first column and PID in the second column. • If the row survives. Interpreting Execution Plans Chapter 4 . All rights reserved. Copyright © 2010.Note: A special hint was used to make sure T3 would be first in the plan. Oracle and/or its affiliates.Page 58 . Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED You can also use Enterprise Manager to understand execution plans. especially because it displays the Order column. Reading More Complex Execution Plans SELECT owner . Oracle and/or its affiliates. segment_type FROM dba_extents WHERE file_id = 1 AND 123213 BETWEEN block_id AND block_id + blocks -1. Your knowledge about the data dictionary enables you to understand that the two branches correspond to dictionary-managed tablespaces and locally-managed ones. you just need to expand the branch you want to investigate (where time is being spent).Page 59 Oracle University and InfoTech (Pvt. You can clearly see that this plan is a UNION ALL of two branches. this is easy to do when using the Enterprise Manager or SQL Developer graphical interface. This is illustrated at the right where you can see the same plan collapsed. Your knowledge about your database enables you to know that there are no dictionarymanaged tablespaces. you must look at the plan information and execution statistics of each row source to locate the part of the plan that consumes most resources. Copyright © 2010. All rights reserved. So. As shown. it must be on the second branch. segment_name . To get confirmation. It is so long that it is very difficult to apply the previous method to interpret it and locate the first operation. if there is a problem. Collapse using indentation and focus on operations consuming most resources. For example. Then. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Reading More Complex Execution Plans . tkprof cumulates for each parent operation the time it takes to execute itself plus the sum of all its child operation time.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you must look at the execution statistics that are generally found in V$SQL_PLAN_STATISTICS or in the tkprof reports generated from trace files. To use this method. Interpreting Execution Plans Chapter 4 . You can always collapse a plan to make it readable. Reading More Complex Execution Plans The plan at the left comes from the query (in the slide) on the data dictionary. nested loop joins through indexes may not be optimal when many rows are returned. look for the following: • The plan is such that the driving table has the best filter. Look for suspicious activity. the goal is to drive from the table that has the most selective filter. • The join order in each step means that the fewest number of rows are returned to the next step (that is. • Each table is being accessed efficiently: Consider the predicates in the SQL statement and the number of rows in the table. If the next step is a join. Oracle and/or its affiliates.Page 60 Oracle University and InfoTech (Pvt. All rights reserved. • Views are used efficiently. For example. the join order should reflect going to the best not-yet-used filters). This means that there are fewer rows passed to the next step. this means fewer rows are joined. Look at the SELECT list to see whether access to the view is necessary. • The join method is appropriate for the number of rows being returned. • There are any unintentional Cartesian products (even with small tables). which have predicates in the WHERE clause. Copyright © 2010. such as a full table scans on tables with large number of rows. When you examine the optimizer execution plan. Look for the following: – Driving table has the best filter – Fewest number of rows are returned to the next step – The join method is appropriate for the number of rows returned – Views are correctly used – Unintentional Cartesian products – Tables accessed efficiently Reviewing the Execution Plan When you tune a SQL statement in an online transaction processing (OLTP) environment. Check to see whether the access paths are optimal.Reviewing the Execution Plan • • Drive from the table that has most selective filter.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Reviewing the Execution Plan . Interpreting Execution Plans Chapter 4 . hash join) for the number of rows returned. All rights reserved.If any of these conditions are not optimal. Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. consider restructuring the SQL statement or the indexes available on the tables. or to leverage a better join method (for example. Copyright © 2010. a full table scan might be more efficient on a small table. Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Also. Oracle and/or its affiliates.Page 61 . For example. The rest of this course is intended to show you various methods to achieve this. and then later prove that it is the optimal plan through testing. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Looking Beyond Execution Plans .Looking Beyond Execution Plans • • An execution plan alone cannot tell you whether a plan is good or not. Sometimes indexes can be extremely inefficient. you should examine the statement’s actual resource consumption. Oracle and/or its affiliates. All rights reserved. an EXPLAIN PLAN output that shows that a statement uses an index does not necessarily mean that the statement runs efficiently. Interpreting Execution Plans Chapter 4 . Copyright © 2010.Page 62 Oracle University and InfoTech (Pvt. It is best to use EXPLAIN PLAN to determine an access plan. May need additional testing and tuning: – – – – – SQL Tuning Advisor SQL Access Advisor SQL Performance Analyzer SQL Monitoring Tracing Looking Beyond Execution Plans The execution plan alone cannot differentiate between well-tuned statements and those that perform poorly. When evaluating a plan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz Answer: a Copyright © 2010. False Oracle University and InfoTech (Pvt. True b.Quiz A user needs to be granted some specialized privileges to generate AUTOTRACE statistics. Oracle and/or its affiliates.Page 63 . a. Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. True b.Page 64 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a. Interpreting Execution Plans Chapter 4 . False Answer: b Copyright © 2010. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .Quiz An EXPLAIN PLAN command executes the statement and inserts the plan used by the optimizer into a table. All rights reserved. You cannot create your own PLAN_TABLE. The PLAN_TABLE is automatically created to hold the EXPLAIN PLAN output. Answer: b Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz Which of the following is not true about a PLAN_TABLE? a.Page 65 Oracle University and InfoTech (Pvt. The actual SQL command is not executed. Oracle and/or its affiliates. Interpreting Execution Plans Chapter 4 . c. The plan in the PLAN_TABLE may not be the actual execution plan. d. b. Interpreting Execution Plans Chapter 4 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. an entry is added to the _______view.Quiz After monitoring is initiated. Oracle and/or its affiliates. This entry tracks key performance metrics collected for the execution. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . V$SQL_MONITOR b. All rights reserved.Page 66 Oracle University and InfoTech (Pvt. ALL_SQL_PLAN_MONITOR Answer: b Copyright © 2010. V$PLAN_MONITOR c. a. ALL_SQL_MONITOR d. All rights reserved. you should have learned how to: • Gather execution plans • Display execution plans • Interpret execution plans Copyright © 2010. Interpreting Execution Plans Chapter 4 .Summary In this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Page 67 Oracle University and InfoTech (Pvt. Page 68 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 4: Overview Copyright © 2010. All rights reserved. Interpreting Execution Plans Chapter 4 . Oracle and/or its affiliates.Practice 4: Overview This practice covers the following topics: • Using different techniques to extract execution plans • Using SQL monitoring Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ) Ltd use only Application Tracing . All rights reserved.Page 1 Oracle University and InfoTech (Pvt. Application Tracing Chapter 5 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 5 Copyright © 2010. Oracle and/or its affiliates. Page 2 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Application Tracing Copyright © 2010. Application Tracing Chapter 5 .) Ltd use only Application Tracing .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Objectives After completing this lesson. you should be able to do the following: • Configure the SQL Trace facility to collect session statistics • Use the trcsess utility to consolidate SQL trace files • Format trace files using the tkprof utility • Interpret the output of the tkprof command Copyright © 2010. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Application Tracing Chapter 5 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .Page 3 Oracle University and InfoTech (Pvt. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED End-to-End Application Tracing Challenge .End-to-End Application Tracing Challenge Client Client Client OE Client OE • • • Clients Client Client JF/Session 6 Client C4 CRM ERP CRM CRM ERP CRM Dedicated server Dedicated server Dedicated server Shared server Shared server Shared server Trace file Trace file Trace file Trace file Trace file Trace file I want to retrieve traces from CRM service. I want to retrieve traces from client C4. End-to-End Application Tracing Challenge Oracle Database implements tracing by generating a trace file for each server process when you enable the tracing mechanism. a client is serviced by different processes from time-to-time. Tracing a specific client is usually not a problem in the dedicated server model as a single dedicated process serves a session during its lifetime. All the trace information for the session can be seen from the trace file belonging to the dedicated server serving it. Application Tracing Chapter 5 . However. Moreover. I want to retrieve traces from session 6. Oracle and/or its affiliates. what if you want to consolidate trace information for a particular service for performance or debugging purposes? This is also difficult because you have multiple clients using the same service and each generating trace files belonging to the server process serving it. Copyright © 2010. The trace pertaining to the user session is scattered across different trace files belonging to different processes. All rights reserved. This makes it difficult for you to get a complete picture of the life cycle of a session.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. in a shared server configuration.Page 4 Oracle University and InfoTech (Pvt. Page 5 Oracle University and InfoTech (Pvt. such as a high-load SQL statement. service-level thresholds. Workload problems can be identified by: • Client identifier: Specifies an end user based on the logon ID. Application Tracing Chapter 5 . DBMS_SESSION – SQL Trace and trcsess utility – tkprof End-to-End Application Tracing End-to-end application tracing simplifies the diagnosis of performance problems in multitier environments. Other tools listed in the slide are discussed later in this lesson. and priorities. In multitier environments. All rights reserved. DBMS_SERVICE. in a module • Session: Specifies a session based on a given database session identifier (SID) The primary interface for end-to-end application tracing is Enterprise Manager. such as an INSERT or an UPDATE operation. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED End-to-End Application Tracing . End-to-end application tracing also simplifies management of application workloads by tracking specific modules and actions in a service.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you can identify what a user’s session does at the database level to resolve that user's performance problems. making it difficult to track a specific client. Also. a request from an end client is routed to different database sessions by the middle tier. such as HR • Service: Specifies a group of applications with common attributes. DBMS_MONITOR. You can used end-to-end application tracing to identify the source of an excessive workload. Copyright © 2010.End-to-End Application Tracing • Simplifies the process of diagnosing performance problems in multitier environments by allowing application workloads to be seen by: – – – – – • Service Module Action Session Client End-to-end application tracing tools: – Enterprise Manager – DBMS_APPICATION_INFO. A client identifier is used to uniquely trace a specific end client through all tiers to the database server. or a single application • Module: Specifies a functional block within an application • Action: Specifies an action. Oracle and/or its affiliates. Application Tracing Chapter 5 . Also. a core dump is a binary memory dump that is port specific. core dumps.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. However. Incident dumps are placed in files separated from the normal process trace files. With Oracle Database 11g.Location for Diagnostic Traces DIAGNOSTIC_DEST Diagnostic Data Previous Location ADR Location Foreground process traces USER_DUMP_DEST $ADR_HOME/trace Background process traces BACKGROUND_DUMP_DEST $ADR_HOME/trace Alert log data BACKGROUND_DUMP_DEST $ADR_HOME/alert $ADR_HOME/trace Core dumps CORE_DUMP_DEST $ADR_HOME/cdump Incident dumps USER_DUMP_DEST BACKGROUND_DUMP_DEST $ADR_HOME/incident/incdir_n V$DIAG_INFO $ADR_HOME/trace <= Oracle Database 11g trace – critical error trace Location for Diagnostic Traces Starting with Oracle Database 11g. and a dump is a one-time output in response to an event. this location is denoted by $ADR_HOME. incident dumps. The table shown in the slide describes the different classes of trace data and dumps that reside both in Oracle Database 10g (and earlier releases) and in Oracle Database 11g. Note: The main difference between a trace and a dump is that a trace is a continuous output. In the slide. All rights reserved. The traditional …_DUMP_DEST initialization parameters are ignored. All nonincident traces are stored inside the TRACE subdirectory. You can use V$DIAG_INFO to list some important ADR locations. Its location is set by the DIAGNOSTIC_DEST initialization parameter. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Location for Diagnostic Traces .Page 6 Oracle University and InfoTech (Pvt. packages. there is no official environment variable called ADR_HOME. Release 1. Oracle and/or its affiliates. The ADR root directory is known as the ADR base. the alert log. Automatic Diagnostic Repository (ADR) is a file-based repository for database diagnostic data such as traces. such as an incident. Health Monitor reports. there is no distinction between foreground and background trace files. Starting with Oracle Database 11g. and so on. Copyright © 2010. such as when SQL tracing is turned on. Both types of files go into the $ADR_HOME/trace directory. critical error information is dumped into the corresponding process trace files instead of incident dumps. Services provide an additional dimension to performance tuning.What Is a Service? • • • • • • Is a means of grouping sessions that perform the same kind of work Provides a single-system image instead of a multipleinstances image Is a part of the regular administration tasks that provide dynamic service-to-instance allocation Is the base for high availability of connections Provides a performance-tuning dimension Is a handle for capturing trace information What Is a Service? The concept of a service was first introduced in Oracle8i as a means for the listener to perform connection load balancing between nodes and instances of a cluster. and recoverable.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 7 Oracle University and InfoTech (Pvt. A service provides a single-system image for managing competing applications that run within a single instance and across multiple instances and databases. workloads are visible and measurable. measurable. administered. Copyright © 2010. the concept. and implementation of services have been dramatically expanded. A service organizes work execution within the database to make it more manageable. disabled. Enterprise Manager. and priority relative to other services. and measured as a single entity using standard interfaces. Tuning by “service and SQL” replaces tuning by “session and SQL” in the majority of systems where sessions are anonymous and shared. a service provides a handle that permits capturing trace information by service name regardless of the session. tunable. and SRVCTL. definition. Following outages. With services. Oracle and/or its affiliates. enabled. From a tracing point of view. Application Tracing Chapter 5 .. Services provide availability. However. A service is a grouping of related tasks within the database with common functionality. a service is recovered quickly and automatically at surviving instances. All rights reserved. quality expectations. Services can be configured. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED What Is a Service? . Using Services with Client Applications ERP=(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=mynode)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=ERP))) url="jdbc:oracle:oci:@ERP" url="jdbc:oracle:thin:@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=mynode)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=ERP)))" Using Services with Client Applications A service name is used by any client connecting to the database server.Page 8 Oracle University and InfoTech (Pvt. The third example shows the thin JDBC connection description using the same TNS connect descriptor. Application Tracing Chapter 5 . Oracle and/or its affiliates. The second example shows the thick Java Database Connectivity (JDBC) connection description using the previously defined TNS connect descriptor. Applications and middle-tier connection pools select a service by using the Transparent Network Substrate (TNS) connection descriptor. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Applications can be grouped by services by simply using a different service name for each application to connect. That service name is automatically applied to the client actions. The selected service must match the service that has been created. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Services with Client Applications . The first example in the slide shows the TNS connect descriptor that can be used to access the ERP service. Tracing Services • Applications using services can be further qualified by: – MODULE – ACTION – CLIENT_IDENTIFIER • Set using the following PL/SQL packages: – DBMS_APPLICATION_INFO – DBMS_SESSION • Tracing can be done at all levels: – – – – – – CLIENT_IDENTIFIER SESSION_ID SERVICE_NAMES MODULE ACTION Combination of SERVICE_NAME, MODULE, ACTION Tracing Services An application can qualify a service by MODULE and ACTION names to identify the important transactions within the service. This enables you to locate the poorly performing transactions for categorized workloads. This is important when you monitor performance in systems using connection pools or transaction processing monitors. For these systems, the sessions are shared, which makes accountability difficult. SERVICE_NAME, MODULE, ACTION, CLIENT_IDENTIFIER, and SESSION_ID are actual columns in V$SESSION. SERVICE_NAME is set automatically at login time based on the connect descriptor, and SESSION_ID is automatically set by the database when a session is created. MODULE and ACTION names are set by the application by using the DBMS_APPLICATION_INFO PL/SQL package or special Oracle Call Interface (OCI) calls. MODULE should be set to a name that is recognizable by the user for the program that currently executes. Likewise, ACTION should be set to a specific action or task that a user performs within a module (for example, entering a new customer). CLIENT_IDENTIFIER can be set using the DBMS_SESSION.SET_IDENTIFIER procedure. The traditional method of tracing each session produces trace files with SQL commands that may contain the trace information for multiple end users or applications. Unless all database sessions are being traced, some information from the end user sessions may be missed. This results in a hit-or-miss approach to diagnose problematic SQL. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 9 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Tracing Services Note: DBA_ENABLED_TRACES displays information about enabled traces. Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED With the criteria that you provide (SERVICE_NAME, MODULE, or ACTION), specific trace information is captured in a set of trace files and combined into a single output trace file. This enables you to produce trace files that contain SQL that is relevant to a specific workload. It is also possible to do the same for CLIENT_IDs and SESSION_IDs. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 10 Use Enterprise Manager to Trace Services Use Enterprise Manager to Trace Services On the Performance page, you can click the Top Consumers link. The Top Consumers page is displayed. The Top Consumers page has several tabs for displaying your database as a single-system image. The Overview tabbed page contains four pie charts: Top Clients, Top Services, Top Modules, and Top Actions. Each chart provides a different perspective about the top resource consumers in your database. The Top Services tabbed page displays performance-related information for the services that are defined in your database. On this page, you can enable or disable tracing at the service level. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 11 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Use Enterprise Manager to Trace Services Service Tracing: Example • Trace on service, module, and action: exec DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE('AP'); exec DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE('AP', 'PAYMENTS', 'QUERY_DELINQUENT'); • Trace a particular client identifier: exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE (client_id=>'C4', waits => TRUE, binds => FALSE); Service Tracing: Example In the first code box, all sessions that log in under the AP service are traced. A trace file is created for each session that uses the service, regardless of the module and action. You can also enable tracing for specific tasks within a service. This is illustrated in the second example, where all sessions of the AP service that execute the QUERY_DELINQUENT action within the PAYMENTS module are traced. Tracing by service, module, and action enable you to focus your tuning efforts on specific SQL, rather than sifting through trace files with SQL from different programs. Only the SQL statements that are identified with this MODULE and ACTION are recorded in the trace file. With this feature, relevant wait events for a specific action can be identified. You can also start tracing for a particular client identifier as shown by the third example. In this example, C4 is the client identifier for which SQL tracing is to be enabled. The TRUE argument specifies that wait information is present in the trace file. The FALSE argument specifies that bind information is not present in the trace file. Although not shown in the slide, you can use the CLIENT_ID_TRACE_DISABLE procedure to disable tracing globally for the database for a given client identifier. To disable tracing, for the previous example, execute the following command: EXECUTE DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE(client_id => 'C4'); Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 12 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Service Tracing: Example Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: CLIENT_IDENTIFIER can be set using the DBMS_SESSION.SET_IDENTIFIER procedure. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 13 Session Level Tracing: Example • For all sessions in the database: EXEC dbms_monitor.DATABASE_TRACE_ENABLE(TRUE,TRUE); EXEC dbms_monitor.DATABASE_TRACE_DISABLE(); • For a particular session: EXEC dbms_monitor.SESSION_TRACE_ENABLE(session_id=> 27, serial_num=>60, waits=>TRUE, binds=>FALSE); EXEC dbms_monitor.SESSION_TRACE_DISABLE(session_id =>27, serial_num=>60); Session Level Tracing: Example You can use tracing to debug performance problems. Trace-enabling procedures have been implemented as part of the DBMS_MONITOR package. These procedures enable tracing globally for a database. You can use the DATABASE_TRACE_ENABLE procedure to enable session level SQL tracing instance-wide. The procedure has the following parameters: • WAITS: Specifies whether wait information is to be traced • BINDS: Specifies whether bind information is to be traced • INSTANCE_NAME: Specifies the instance for which tracing is to be enabled. Omitting INSTANCE_NAME means that the session-level tracing is enabled for the whole database. Use the DATABASE_TRACE_DISABLE procedure to disable SQL tracing for the whole database or a specific instance. Similarly, you can use the SESSION_TRACE_ENABLE procedure to enable tracing for a given database session identifier on the local instance. The SID and SERIAL# information can be found from V$SESSION. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 14 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Session Level Tracing: Example Note: SQL Trace involves some overhead, so you usually do not want to enable SQL Trace at the instance level. Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Use the SESSION_TRACE_DISABLE procedure to disable the trace for a given database session identifier and serial number. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 15 Trace Your Own Session • Enabling trace: EXEC DBMS_SESSION.SESSION_TRACE_ENABLE(waits => TRUE, binds => FALSE); • Disabling trace: EXEC DBMS_SESSION.SESSION_TRACE_DISABLE(); • Easily identifying your trace files: alter session set tracefile_identifier='mytraceid'; Trace Your Own Session Although the DBMS_MONITOR package can be invoked only by a user with the DBA role, any user can enable SQL tracing for his or her own session by using the DBMS_SESSION package. The SESSION_TRACE_ENABLE procedure can be invoked by any user to enable session level SQL tracing for his or her own session. An example is shown in the slide. You can then use the DBMS_SESSION.SESSION_TRACE_DISABLE procedure to stop dumping to your trace file. The TRACEFILE_IDENTIFIER initialization parameter specifies a custom identifier that becomes part of the Oracle trace file name. You can use such a custom identifier to identify a trace file simply from its name and without opening it or view its contents. Each time this parameter is dynamically modified at the session level, the next trace dump written to a trace file will have the new parameter value embedded in its name. This parameter can only be used to change the name of the foreground process trace file; the background processes continue to have their trace files named in the regular format. For foreground processes, the TRACEID column of the V$PROCESS view contains the current value of this parameter. When this parameter value is set, the trace file name has the following format: sid_ora_pid_traceid.trc. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 16 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Trace Your Own Session The trcsess Utility Clients Client Client Client CRM ERP CRM Dedicated server Dedicated server Dedicated server Shared server Shared server Shared server Trace file Trace file Trace file Trace file Trace file Trace file CRM CRM TRCSESS trcsess Trace file for CRM service ERP tkprof Trace file for one client Report file The trcsess Utility The trcsess utility consolidates trace output from selected trace files on the basis of several criteria: session ID, client identifier, service name, action name, and module name. After trcsess merges the trace information into a single output file, the output file can be processed by tkprof. When using the DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE procedure, tracing information is present in multiple trace files and you must use the trcsess tool to collect it into a single file. The trcsess utility is useful for consolidating the tracing of a particular session or service for performance or debugging purposes. Tracing a specific session is usually not a problem in the dedicated server model because a single dedicated process serves a session during its lifetime. All the trace information for the session can be seen from the trace file belonging to the dedicated server that serves it. However, tracing a service might become a complex task even in the dedicated server model. Moreover, in a shared-server configuration, a user session is serviced by different processes from time-to-time. The trace pertaining to the user session is scattered across different trace files belonging to different processes. This makes it difficult to get a complete picture of the life cycle of a session. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 17 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The trcsess Utility Invoking the trcsess Utility trcsess [output=output_file_name] [session=session_id] [clientid=client_identifier] [service=service_name] [action=action_name] [module=module_name] [<trace file names>] Trace file Trace file Trace file TRCSESS Consolidated trace file Invoking the trcsess Utility The syntax for the trcsess utility is shown in the slide, where: • output specifies the file where the output is generated. If this option is not specified, standard output is used for the output. • session consolidates the trace information for the session specified. The session identifier is a combination of session index and session serial number, such as 21.2371. You can locate these values in the V$SESSION view. • clientid consolidates the trace information for the given client identifier. • service consolidates the trace information for the given service name. • action consolidates the trace information for the given action name. • module consolidates the trace information for the given module name. • <trace file names> is a list of all the trace file names, separated by spaces, in which trcsess should look for trace information. The wildcard character “*” can be used to specify the trace file names. If trace files are not specified, all the files in the current directory are taken as input to trcsess. You can find trace files in ADR. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Invoking the trcsess Utility Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: One of the session, clientid, service, action, or module options must be specified. If there is more than one option specified, the trace files, which satisfy all the criteria specified are consolidated into the output file. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 19 The trcsess Utility: Example exec dbms_session.set_identifier('HR session'); Second session First session exec dbms_session.set_identifier('HR session'); exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE( client_id=>'HR session', waits => FALSE, binds => FALSE); Third session select * from employees; … select * from departments; … exec DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE( client_id => 'HR session'); trcsess output=mytrace.trc clientid='HR session' $ORACLE_BASE/diag/rdbms/orcl/orcl/trace/*.trc The trcsess Utility: Example The example in the slide illustrates a possible use of the trcsess utility. The example assumes that you have three different sessions: Two sessions that are traced (left and right), and one session (center) that enables or disables tracing and concatenates trace information from the previous two sessions. The first and second session set their client identifier to the ‘HR session’ value. This is done using the DBMS_SESSION package. Then, the third session enables tracing for these two sessions using the DBMS_MONITOR package. At that point, two new trace files are generated in ADR; one for each session that is identified with the ‘HR session’ client identifier. Each traced session now executes its SQL statements. Every statement generates trace information in its own trace file in ADR. Then, the third session stops trace generation using the DBMS_MONITOR package, and consolidates trace information for the ‘HR session’ client identifier in the mytrace.trc file. The example assumes that all trace files are generated in the $ORACLE_BASE/diag/rdbms/orcl/orcl/trace directory, which is the default in most cases. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 20 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The trcsess Utility: Example SQL Trace File Contents • • • • • • • • • • Parse, execute, and fetch counts CPU and elapsed times Physical reads and logical reads Number of rows processed Misses on the library cache Username under which each parse occurred Each commit and rollback Wait event and bind data for each SQL statement Row operations showing the actual execution plan of each SQL statement Number of consistent reads, physical reads, physical writes, and time elapsed for each operation on a row SQL Trace File Contents As seen already, a SQL trace file provides performance information on individual SQL statements. It generates the following statistics for each statement: • Parse, execute, and fetch counts • CPU and elapsed times • Physical reads and logical reads • Number of rows processed • Misses on the library cache • Username under which each parse occurred • Each commit and rollback • Wait event data for each SQL statement, and a summary for each trace file If the cursor for the SQL statement is closed, SQL Trace also provides row source information that includes: • Row operations showing the actual execution plan of each SQL statement • Number of rows, number of consistent reads, number of physical reads, number of physical writes, and time elapsed for each operation. This is possible only when the STATISTICS_LEVEL initialization parameter is set to ALL. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 21 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Trace File Contents Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Using the SQL Trace facility can have a severe performance impact and may result in increased system overhead, excessive CPU usage, and inadequate disk space. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 22 SQL Trace File Contents: Example *** [ Unix process pid: 15911 ] *** 2010-07-29 13:43:11.327 *** 2010-07-29 13:43:11.327 *** 2010-07-29 13:43:11.327 *** 2010-07-29 13:43:11.327 … ==================== PARSING IN CURSOR #2 len=23 dep=0 uid=85 oct=3 lid=85 tim=1280410994003145 hv=40 69246757 ad='4cd57ac0' sqlid='f34thrbt8rjt5' select * from employees END OF STMT PARSE #2:c=3000,e=2264,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=1445457117, tim=1280410994003139 EXEC #2:c=0,e=36,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1445457117, tim=1280410994003312 FETCH #2:c=0,e=215,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,plh=1445457117, tim=1280410994003628 FETCH #2:c=0,e=89,p=0,cr=5,cu=0,mis=0,r=15,dep=0,og=1,plh=1445457117, tim=1280410994004232 … FETCH #2:c=0,e=60,p=0,cr=1,cu=0,mis=0,r=1,dep=0,og=1,plh=1445457117, tim=1280410994107857 STAT #2 id=1 cnt=107 pid=0 pos=1 obj=73933 op='TABLE ACCESS FULL EMPLOYEES (cr=15 pr=0 pw=0 time=0 us cost=3 size=7383 card=107)' XCTEND rlbk=0, rd_only=1, tim=1280410994108875 ===================== SQL Trace File Contents: Example There are multiple types of trace files that can be generated by the Oracle Database. The one that is referred to in this lesson is generally called a SQL trace file. The slide shows you a sample output from the mytrace.trc SQL trace file generated by the previous example. In this type of trace file, you can find (for each statement that was traced) the statement itself, with some corresponding cursor details. You can see statistic details for each phase of the statement’s execution: PARSE, EXEC, and FETCH. As you can see, you can have multiple FETCH for one EXEC depending on the number of rows returned by your query. Last part of the trace is the execution plan with some cumulated statistics for each row source. Depending on the way you enabled tracing, you can also obtain information about wait events and bind variables in the generated trace files. Generally, you do not try to interpret the trace file itself. This is because you do not get an overall idea of what your sessions did. For example, one session could have executed the same statement multiple times at different moments. The corresponding traces are then scattered across the entire trace file, which makes them hard to find. Instead, you use another tool, such as tkprof to interpret the contents of the raw trace information. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 23 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Trace File Contents: Example Formatting SQL Trace Files: Overview Use the tkprof utility to format your SQL trace files: • • Sort raw trace file to exhibit top SQL statements Filter dictionary statements Trace file Trace file Trace file Trace file Trace Trace file Trace file Trace file Trace file file trcsess Consolidated trace file tkprof Concatenated trace file Report file Formatting SQL Trace Files: Overview The tkprof utility parses SQL trace files to produce more readable output. Remember that all the information in tkprof is available from the raw trace file. There is a huge number of sort options that you can invoke with tkprof at the command prompt. A useful starting point is the fchela sort option, which orders the output by elapsed time fetching. The resultant file contains the most time-consuming SQL statement at the start of the file. Another useful parameter is SYS=NO. This can be used to prevent SQL statements run as the SYS user from being displayed. This can make the output file much shorter and easier to manage. After a number of SQL trace files have been generated, you can perform any of the following: • Run tkprof on each individual trace file, producing a number of formatted output files, one for each session. • Concatenate the trace files, and then run tkprof on the result to produce a formatted output file for the entire instance. • Run the trcsess command-line utility to consolidate tracing information from several trace files, then run tkprof on the result. tkprof does not report COMMITs and ROLLBACKs that are recorded in the trace file. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 24 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Formatting SQL Trace Files: Overview Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Set the TIMED_STATISTICS parameter to TRUE when tracing sessions because no time-based comparisons can be made without this. TRUE is the default value with Oracle Database 11g. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 25 Invoking the tkprof Utility tkprof inputfile outputfile [waits=yes|no] [sort=option] [print=n] [aggregate=yes|no] [insert=sqlscriptfile] [sys=yes|no] [table=schema.table] [explain=user/password] [record=statementfile] [width=n] Invoking the tkprof Utility When you enter the tkprof command without any arguments, it generates a usage message together with a description of all tkprof options. The various arguments are shown in the slide: • inputfile: Specifies the SQL trace input file • outputfile: Specifies the file to which tkprof writes its formatted output • waits: Specifies whether to record the summary for any wait events found in the trace file. Values are YES or NO. The default is YES. • sorts: Sorts traced SQL statements in the descending order of specified sort option before listing them into the output file. If more than one option is specified, the output is sorted in the descending order by the sum of the values specified in the sort options. If you omit this parameter, tkprof lists statements into the output file in the order of first use. • print: Lists only the first integer sorted SQL statements from the output file. If you omit this parameter, tkprof lists all traced SQL statements. This parameter does not affect the optional SQL script. The SQL script always generates insert data for all traced SQL statements. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 26 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Invoking the tkprof Utility aggregate: If set to NO, tkprof does not aggregate multiple users of the same SQL text. • insert: Creates a SQL script to store the trace file statistics in the database. tkprof creates this script with the name you specify for sqlscriptfile. This script creates a table and inserts a row of statistics for each traced SQL statement into the table. • sys: Enables and disables the listing of SQL statements issued by the SYS user, or recursive SQL statements, into the output file. The default value of YES causes tkprof to list these statements. The value of NO causes tkprof to omit them. This parameter does not affect the optional SQL script. The SQL script always inserts statistics for all traced SQL statements, including recursive SQL statements. • table: Specifies the schema and name of the table into which tkprof temporarily places execution plans before writing them to the output file. If the specified table already exists, tkprof deletes all rows in the table, uses it for the EXPLAIN PLAN statement (which writes more rows into the table), and then deletes those rows. If this table does not exist, tkprof creates it, uses it, and then drops it. The specified user must be able to issue INSERT, SELECT, and DELETE statements against the table. If the table does not already exist, the user must also be able to issue the CREATE TABLE and DROP TABLE statements. This option allows multiple individuals to run tkprof concurrently with the same user in the EXPLAIN value. These individuals can specify different TABLE values and avoid destructively interfering with each other’s processing on the temporary plan table. If you use the EXPLAIN parameter without the TABLE parameter, tkprof uses the PROF$PLAN_TABLE table in the schema of the user specified by the EXPLAIN parameter. If you use the TABLE parameter without the EXPLAIN parameter, tkprof ignores the TABLE parameter. If no plan table exists, tkprof creates the PROF$PLAN_TABLE table and then drops it at the end. • explain: Determines the execution plan for each SQL statement in the trace file and writes these execution plans to the output file. tkprof determines execution plans by issuing the EXPLAIN PLAN statement after connecting to the system with the user and password specified in this parameter. The specified user must have CREATE SESSION system privileges. tkprof takes longer to process a large trace file if the EXPLAIN option is used. • record: Creates a SQL script with the specified file name statementfile with all the nonrecursive SQL statements in the trace file. This can be used to replay the user events from the trace file. • width: An integer that controls the output line width of some tkprof output, such as the explain plan. This parameter is useful for post-processing of tkprof output. The input and output files are the only required arguments. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Application Tracing Chapter 5 - Page 27 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED tkprof Sorting Options . Application Tracing Chapter 5 .tkprof Sorting Options Sort Option Description prscnt Number of times parse was called prscpu CPU time parsing prsela Elapsed time parsing prsdsk Number of disk reads during parse prsqry Number of buffers for consistent read during parse prscu Number of buffers for current read during parse prsmis Number of misses in the library cache during parse execnt Number of executes that were called execpu CPU time spent executing exeela Elapsed time executing exedsk Number of disk reads during execute exeqry Number of buffers for consistent read during execute execu Number of buffers for current read during execute tkprof Sorting Options The table lists all the sort options you can use with the sort argument of tkprof.Page 28 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Page 29 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED tkprof Sorting Options .tkprof Sorting Options Sort Option Description exerow Number of rows processed during execute exemis Number of library cache misses during execute fchcnt Number of times fetch was called fchcpu CPU time spent fetching fchela Elapsed time fetching fchdsk Number of disk reads during fetch fchqry Number of buffers for consistent read during fetch fchcu Number of buffers for current read during fetch fchrow Number of rows fetched userid User ID of user that parsed the cursor Copyright © 2010. Application Tracing Chapter 5 . All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. such as access rights. Hard parses can be expensive. columns. and other referenced objects. All rights reserved. and DELETE statements. and DELETE statements. PARSE This step translates the SQL statement into an execution plan and includes checks for proper security authorization and checks for the existence of tables. A soft parse is mostly expensive in terms of library cache activity 25 Copyright © 2010. A hard parse refers to the development of the execution plan (including optimization). this step modifies the data (including sorts when needed). Note: The PARSE value includes both hard and soft parses. UPDATE. Application Tracing Chapter 5 . it is subsequently stored in the library cache.) FETCH Retrieves the rows returned by a query (Fetches are performed only for the SELECT statements.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The step for each row that contains statistics is identified by the value of the call column. but the database finds it in the library cache and only needs to verify things. FETCH This step retrieves rows returned by a query and sorts them when needed. this step identifies the selected rows. particularly due to the optimization.Page 30 Oracle University and InfoTech (Pvt.) Output of the tkprof Command The tkprof output file lists the statistics for a SQL statement by the SQL processing step. For the SELECT statements.Output of the tkprof Command • • Text of the SQL statement Trace statistics (for statement and recursive calls) separated into three SQL processing steps: PARSE Translates the SQL statement into an execution plan EXECUTE Executes the statement (This step modifies the data for the INSERT. EXECUTE This step is the actual execution of the statement by the Oracle server. Fetches are performed only for the SELECT statements. A soft parse means that a SQL statement is sent for parsing to the database. Oracle and/or its affiliates. UPDATE. For the INSERT. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Output of the tkprof Command . All rights reserved. tkprof aggregates identical statement executions into one summary table.--- Parse 1 Execute 1 Fetch 2 ------.) • CPU: Total CPU time in seconds for all parse.------ cpu elapsed disk query current rows -------.09 0. Oracle and/or its affiliates. execute.Output of the tkprof Command There are seven categories of trace statistics: Count Number of times the procedure was executed CPU Number of seconds to process Elapsed Total number of seconds to execute Disk Number of physical blocks read Query Number of logical buffers read for consistent read Current Number of logical buffers read in current mode Rows Number of rows processed by the fetch or execute Output of the tkprof Command (continued) The output is explained on the following page. tkprof displays the following statistics for each statement: • Count: Number of times a statement was parsed.03 0.Page 31 Oracle University and InfoTech (Pvt. or fetch calls Copyright © 2010.06 0 0 0 0.---------. or fetch calls • Elapsed: Total elapsed time in seconds for all parse.06 0.46 0 0 0 -------.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.83 1 3 0 0 0 1 --1 Next to the CALL column.---------.---------- total 4 0.---------.---------. or fetched (Check this column for values greater than 1 before interpreting the statistics in the other columns. Unless the AGGREGATE = NO option is used. execute.------ 0. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Output of the tkprof Command . Application Tracing Chapter 5 .---------. Sample output is as follows: call count ------.30 1 3 0 0.---------. executed.---------.00 0. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • . For the UPDATE.) Rows: Total number of rows processed by the SQL statement (This total does not include rows processed by subqueries of the SQL statement. the number of rows returned appears for the fetch step. or fetch calls Query: Total number of buffers retrieved in consistent mode for all parse. For SELECT statements.• • • Disk: Total number of data blocks physically read from the data files on disk for all parse.Page 32 Oracle University and InfoTech (Pvt. execute. All rights reserved. However. Oracle and/or its affiliates. or fetch calls (Buffers are usually retrieved in consistent mode for queries. • QUERY is equivalent to consistent gets from v$sysstat or AUTOTRACE. segment header blocks are always retrieved in current mode. and INSERT statements. DELETE.) Current: Total number of buffers retrieved in current mode (Buffers typically are retrieved in current mode for data manipulation language statements. • CURRENT is equivalent to db block gets from v$sysstat or AUTOTRACE. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the number of rows processed appears for the execute step. execute. Application Tracing Chapter 5 .) Note • DISK is equivalent to physical reads from v$sysstat or AUTOTRACE. tkprof marks them clearly as recursive SQL statements in the output file. the Oracle server makes recursive calls to allocate the space dynamically. If recursive calls occur while the SQL Trace facility is enabled. the Oracle server must occasionally issue additional statements.Output of the tkprof Command The tkprof output also includes the following: • • • • • • Recursive SQL statements Library cache misses Parsing user ID Execution plan Optimizer mode or hint Row source operation . Such statements are called recursive SQL statements. For example. Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.. Recursive calls are also generated when data dictionary information is not available in the data dictionary cache and must be retrieved from disk. if you insert a row in a table that does not have enough space to hold that row.Page 33 Oracle University and InfoTech (Pvt. These statistics appear on separate lines following the tabular statistics. Misses in library cache during parse: 1 Optimizer mode: ALL_ROWS Parsing user id: 85 Rows ------5 5 … Row Source Operation --------------------------------------------------TABLE ACCESS BY INDEX ROWID EMPLOYEES (cr=4 pr=1 pw=0 time=0 us … INDEX RANGE SCAN EMP_NAME_IX (cr=2 pr=1 pw=0 time=80 us cost=1 … Output of the tkprof Command (continued) Recursive Calls To execute a SQL statement issued by a user. Note that the statistics for recursive SQL statements are always included in the listing for the SQL statement that caused the recursive call. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Output of the tkprof Command .. You can suppress the listing of recursive calls in the output file by setting the SYS=NO command-line parameter. Oracle and/or its affiliates. Library Cache Misses tkprof also lists the number of library cache misses resulting from parse and execute steps for each SQL statement. Application Tracing Chapter 5 . tkprof uses the EXPLAIN PLAN command to generate the execution plan of each SQL statement traced. such as physical reads and writes. Optimizer Mode or Hint This indicates the optimizer hint that is used during the execution of the statement. Oracle and/or its affiliates. r = physical reads. All rights reserved. time = time (in microseconds).Page 34 Oracle University and InfoTech (Pvt. Copyright © 2010. If the row source operation does not appear in the trace file. If there is no hint. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Row Source Operations . tkprof also displays the number of rows processed by each step of the execution plan. an index has been created or dropped since tracing the statements.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. for example. This could make a difference if. Parsing User ID This is the ID of the last user to parse the statement. w = physical writes. Execution Plan If you specify the EXPLAIN parameter on the tkprof command line. Note: Be aware that the execution plan is generated at the time that the tkprof command is run and not at the time the trace file was produced. This is included only if the cursor has been closed during tracing. Row Source Operation The row source operation shows the data sources for execution of the SQL statement. Application Tracing Chapter 5 . cr = consistent reads.These provide the number of rows processed for each operation executed on the rows and additional row source information. it shows the optimizer mode that is used. you may then want to view the output of the EXPLAIN PLAN. Copyright © 2010..-------.. timed_statistics is not set..00 0.02 0. The statement is executed through a full table scan of the CUSTOMERS table.00 0 0 0 0.10 72 1459 0 ------.--------.tkprof Output with No Index: Example .-------. The statement must be optimized.--------0.-------.-----total 4 cpu elapsed disk query current ------. Application Tracing Chapter 5 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED tkprof Output with No Index: Example .-------. Oracle and/or its affiliates.00 0. All rights reserved. as you can see in the row source operation of the output.12 second of CPU fetch time.10 72 1459 0 rows --------0 0 1 --------1 Misses in library cache during parse: 1 Optimizer mode: ALL_ROWS Parsing user id: 88 Rows ------1 77 cost=405 Row Source Operation --------------------------------------------------SORT AGGREGATE (cr=1459 pr=72 pw=0 time=0 us) TABLE ACCESS FULL CUSTOMERS (cr=1459 pr=72 pw=0 time=4104 us size=1260 card=90) . tkprof Output with No Index: Example The example in the slide shows that the aggregation of results across several executions (rows) is being fetched from the CUSTOMERS table.02 0.--------0. select max(cust_credit_limit) from customers where cust_city ='Paris' call count ------.Page 35 Oracle University and InfoTech (Pvt.-----Parse 1 Execute 1 Fetch 2 ------. Note: If CPU or elapsed values are 0.00 0 0 0 0.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It requires 0.--------.. All rights reserved.--------..00 0 0 0 0. Copyright © 2010.-----total 4 cpu elapsed disk query current -------. Unused indexes should be removed.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and DELETE commands because references to rows must be added.00 0.Page 36 Oracle University and InfoTech (Pvt.tkprof Output with Index: Example .--------. because this example reexecutes the same statement. Additionally. you can use index monitoring to identify and remove any indexes that are not used. You can achieve significant improvements in performance by indexing sensibly.---------0.00 1 77 0 rows ---------0 0 1 ---------1 Misses in library cache during parse: 1 Optimizer mode: ALL_ROWS Parsing user id: 88 Rows ------1 77 77 Row Source Operation --------------------------------------------------SORT AGGREGATE (cr=77 pr=1 pw=0 time=0 us) TABLE ACCESS BY INDEX ROWID CUSTOMERS (cr=77 pr=1 pw=0 time=760 us cost=85 size=1260 card=90) INDEX RANGE SCAN CUST_CUST_CITY_IDX (cr=2 pr=1 pw=0 time=152 us cost=1 size=0 card=90)(object id 78183) tkprof Output with Index: Example The results shown in the slide indicate that CPU time was reduced to 0. Oracle and/or its affiliates.--------..00 0 0 0 0. UPDATE. Application Tracing Chapter 5 .---------.--------.00 1 77 0 -------. Note: Indexes should not be built unless required. most of the data blocks are already in memory. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED tkprof Output with Index: Example .00 0.01 second when an index was created on the CUST_CITY column. However.---------. Identify areas for potential improvement using the SQL Trace facility. changed. These results may have been achieved because the statement uses the index to retrieve the data.-----Parse 1 Execute 1 Fetch 2 ------.00 0.---------0. Indexes do slow down the processing of the INSERT. instead of processing all the application SQL through EXPLAIN PLAN.00 0. or removed. select max(cust_credit_limit) from customers where cust_city ='Paris' call count ------. trcsess output=mytrace. tkprof *mytrace*.t rc d. Application Tracing Chapter 5 . Oracle and/or its affiliates. All rights reserved. alter session set tracefile_identifier='mytraceid'. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .txt SYS=NO Answer: b Copyright © 2010. EXEC DBMS_SESSION. c.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 37 Oracle University and InfoTech (Pvt. binds => FALSE).SESSION_TRACE_ENABLE(waits => TRUE.Quiz Which command would you use to create a trace file of your SQL*Plus session in a dedicated server environment? a.trc clientid='HR session' $ORACLE_BASE/diag/rdbms/orcl/orcl/trace/*.trc mytrace. b. trcsess b. d. c Copyright © 2010. tkprof c.Quiz The _____ utility formats the trace file into a readable format. Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Application Tracing Chapter 5 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz SQL Developer SQL*Plus Autotrace Answer: b. All rights reserved. a.Page 38 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 39 Oracle University and InfoTech (Pvt. you will use ________ to identify which trace files need to be combined to get an overall trace of the application. trcsess b. a. d. Oracle and/or its affiliates. Application Tracing Chapter 5 . SQL Developer DBMS_APPLICATION_INFO Answer: d Copyright © 2010. All rights reserved.Quiz In an environment with an applications server that uses a connection pool. tkprof c. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary . All rights reserved.Summary In this lesson. Application Tracing Chapter 5 .Page 40 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you should have learned how to: • Configure the SQL Trace facility to collect session statistics • Use the trcsess utility to consolidate SQL trace files • Format trace files using the tkprof utility • Interpret the output of the tkprof command Copyright © 2010. Practice 5: Overview This practice covers the following topics: • Creating a service • Tracing your application using services • Interpreting trace information using trcsess and tkprof Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 5: Overview . Application Tracing Chapter 5 .Page 41 Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt.Page 42 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Application Tracing Chapter 5 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010. All rights reserved. Optimizer Operators Chapter 6 .) Ltd use only Optimizer Operators .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Page 1 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 6 Copyright © 2010. All rights reserved. THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Operators Chapter 6 . Oracle and/or its affiliates. All rights reserved.Page 2 Oracle University and InfoTech (Pvt.) Ltd use only Optimizer Operators . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Operators Copyright © 2010. Page 3 Oracle University and InfoTech (Pvt. Copyright © 2010.Objectives After completing this lesson. you should be able to: • Describe the SQL operators for tables and indexes • List the possible access paths Objectives This lesson helps you understand the execution plans that use operators related to table and index access methods. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . Optimizer Operators Chapter 6 . Oracle and/or its affiliates. Page 4 Oracle University and InfoTech (Pvt. Row Source Operations A row source is a set of rows returned by a step in the execution plan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Row Source Operations . Online transaction processing (OLTP) applications.Row Source Operations • Unary operations – Access Path • Binary operations – Joins • N-ary operations Ams row source is a set of rows returned by a step in the execution plan . such as joins • N-ary operations: Operations that act on several inputs. The row source can be a table. which consist of short-running SQL statements with high selectivity. while full scans are more efficient when accessing a large portion of the table. Decision support systems (DSS). All rights reserved. Optimizer Operators Chapter 6 . on the other hand. such as an access path • Binary operations: Operations that act on two inputs. You can classify row sources as follows: • Unary operations: Operations that act on only one input. Copyright © 2010. tend to use partitioned tables and perform full scans of the relevant partitions. Oracle and/or its affiliates. view. such as a relational operator Access paths are ways in which data is retrieved from the database. index access paths should be used for statements that retrieve a small subset of table rows. are often characterized by the use of index access paths. In general. or result of a join or grouping operation.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In general. Oracle and/or its affiliates. Index Scan (Unique) 5. The special types of access paths are discussed later in this course.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Main Structures and Access Paths Structures Access Paths 1. All rows with the same key value are stored together on disk. Index Scan (Fast Full) Indexes 8. There are special types of table access paths including clusters. For example. Combining Bitmap Indexes Main Structures and Access Paths Any row can be located and retrieved with one of the methods mentioned in the slide.Page 5 Oracle University and InfoTech (Pvt. which have not been mentioned in the slide. A cluster is a group of tables that share the same data blocks because they share common columns and are often used together. When you cluster the EMP and DEPT tables. Index Scan (Range) 6. index-organized tables. Clusters are an optional method of storing table data. and partitions. Optimizer Operators Chapter 6 . Hash clusters are single-table clusters in which rows with the same hash-key values are stored together. Copyright © 2010. Using Bitmap Indexes 11. the optimizer gives each alternative (execution plan) a cost. A mathematical hash function is used to select the location of a row within the cluster. The one with the lower cost is elected. To decide on the alternative. All rights reserved. index access paths should be used for statements that retrieve a small subset of table rows. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Main Structures and Access Paths . Rowid Scan 3. Index Scan (Skip) 9. Index Scan (Index Join) 10. the EMP and DEPT table share the DEPTNO column. Sample Table Scan 4. Full Table Scan Tables 2. Oracle physically stores all rows for each department from both the EMP and DEPT tables in the same data blocks. Index Scan (Full) 7. while full scans are more efficient when accessing a large portion of the table. it reads blocks that are physically adjacent to each other. Copyright © 2010. Since Oracle V7. B B B B B Is faster than index range scans for large amount of data Full Table Scan A full table scan sequentially reads all rows from a table and filters out those that do not meet the selection criteria. During a full table scan.. Note: In Oracle 6. All rights reserved. or space that was formatted to receive data.. The high-water mark indicates the amount of used space. Each row is examined to determine whether it satisfies the statement’s WHERE clause using the applicable filter conditions specified in the query. This means that performance benefits can be reaped by utilizing input/output (I/O) calls that read multiple blocks at the same time.Full Table Scan • Performs multiblock reads (here DB_FILE_MULTIBLOCK_READ_COUNT = 4) • • • Reads all formatted blocks below the high-water mark HWM May filter rows B B B B . Because a full table scan reads all the formatted blocks in a table. The filter to be applied returns only rows where EMP. blocks read by FTS are allowed to occupy only a small percentage of the buffer cache.Page 6 Oracle University and InfoTech (Pvt.ENAME='King'.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. You can see the filter conditions in the “Predicate Information” section of the explain plan. a full table scan (FTS) could flood the buffer cache because there was no difference in the way blocks were handled between FTS and other reads. all formatted blocks in the table that are under the high-water mark are scanned even if all the rows have been deleted from the table. FTS are read into the PGA with direct reads bypassing the buffer cache in most cases. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Full Table Scan . Oracle and/or its affiliates. Each block is read only once. Optimizer Operators Chapter 6 . The size of the read call can range from a single block to any number of blocks up to the DB_FILE_MULTIBLOCK_READ_COUNT init parameter. Currently. it uses a full table scan (unless a ROWID filter or a cluster access path is available). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Full Table Scans: Use Cases . All rights reserved.Full Table Scans: Use Cases • • • • • No suitable index Low selectivity filters (or no filters) Small table High degree of parallelism Full table scan hint: FULL (<table name>) Full Table Scans: Use Cases The optimizer uses a full table scan in any of the following cases: • Lack of index: If the query is unable to use any existing indexes. either do not permit mixed-case data in the search columns or create a function-based index. For example. it may use a full table scan even though indexes might be available. regardless of the fraction of tables being accessed or indexes present. the optimizer cannot use the index and instead uses a full table scan. such as UPPER(last_name) on the search column. if there is a function used on the indexed column in the query. • Small table: If a table contains less than DB_FILE_MULTIBLOCK_READ_COUNT blocks under the high-water mark.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a full table scan might be cheaper than an index range scan. Optimizer Operators Chapter 6 . Examine the DEGREE column in ALL_TABLES for the table to determine the degree of parallelism. Oracle and/or its affiliates.Page 7 Oracle University and InfoTech (Pvt. If you need to use the index for case-independent searches. • Large amount of data (low selectivity): If the optimizer thinks that the query accesses enough blocks in the table. • High degree of parallelism: A high degree of parallelism for a table skews the optimizer towards full table scans over range scans. Copyright © 2010. Page 8 . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Full table scan hints: Use the FULL(table alias) hint to instruct the optimizer to use a full table scan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. All rights reserved. Optimizer Operators Chapter 6 . Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED ROWID Scan . and also after export and import. however. The table access might be required for columns in the statement that are not present in the index. The rowid. Note: Due to row migration. Oracle and/or its affiliates. a rowid can sometimes point to an address different from the actual row location. Optimizer Operators Chapter 6 . To access a table by rowid. Accessing data based on position is not recommended because rows can move around due to row migration and chaining. Rowids are the system’s internal representation of where data is stored. either from the statement’s WHERE clause or through an index scan of one or more of the table’s indexes. Copyright © 2010.).) Ltd use only select * from scott.emp where rowid='AAAQ+LAAEAAAAAfAAJ'. All rights reserved. A table access by rowid does not need to follow every index scan. table access by rowid might not occur. If the index contains all the columns needed for the statement. The system then locates each selected row in the table based on its rowid. Locating a row by specifying its rowid is the fastest way to retrieve a single row because the exact location of the row in the database is specified.Page 9 Oracle University and InfoTech (Pvt. the system first obtains the rowids of the selected rows. an update to a row may cause the row to be placed in another block with a pointer in the original block. For example. B B B Row migration ROWID Scan The rowid of a row specifies the data file and data block containing the row and the location of the row in that block. still has only the address of the original block. Block 6959–Row 2 THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Mostly. the optimizer uses rowids after retrieving them from an index (See the “Index Scans” slides. resulting in more than one block being accessed to locate a row.ROWID Scan -----------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost | -----------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 37 | 1| | 1 | TABLE ACCESS BY USER ROWID| EMP | 1 | 37 | 1| ------------------------------------------------------------------ B B . The sample value must be in the [0.999999] range. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Sample Table Scans . 99. Optimizer Operators Chapter 6 . Oracle and/or its affiliates.000001 . It does not mean that the database retrieves exactly sample_percent of the rows of table. This percentage indicates the probability of each row. such as a statement involving joins and views. The sample percent is a number specifying the percentage of the total row or block count to be included in the sample.Page 10 Oracle University and InfoTech (Pvt. the system reads a specified percentage of the table’s blocks and examines each row in the sampled blocks to determine whether it satisfies the statement’s WHERE clause. • • SAMPLE option: To perform a sample table scan when sampling by rows. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. To perform a sample table scan when sampling by blocks with the SAMPLE BLOCK clause. the system reads a specified percentage of rows in the table and examines each of these rows to determine whether it satisfies the statement’s WHERE clause. the system reads a specified percentage of table blocks. --------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------------| 0 | SELECT STATEMENT | | 4 | 99 | 2 (0)| | 1 | TABLE ACCESS SAMPLE| EMP | 4 | 99 | 2 (0)| --------------------------------------------------------------------- B B B B B Sample Table Scans A sample table scan retrieves a random sample of data from a simple table or a complex SELECT statement.Sample Table Scans SELECT * FROM emp SAMPLE BLOCK (10) SEED (1). being selected as part of the sample. To perform a sample table scan when sampling by rows with the SAMPLE clause. This access path is used when a statement’s FROM clause includes the SAMPLE clause or the SAMPLE BLOCK clause. SAMPLE BLOCK option: To perform a sample table scan when sampling by blocks. the system reads a specified percentage of rows in the table. or each cluster of rows in the case of block sampling. Oracle University and InfoTech (Pvt. Block samples are less costly. more blocks need to be accessed given a particular sample size. use the FULL or INDEX_FFS hint. In row sampling. the resulting sample changes from one execution to the next.Page 11 . Oracle and/or its affiliates. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. more so with smaller samples. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. If you want to guarantee block sampling for a particular table or index. Optimizer Operators Chapter 6 . Note: Block sampling is possible only during full table scans or index fast full scans. If a more efficient execution path exists. seed_value must be an integer between 0 and 4294967295. but the results are usually more accurate. If you omit this clause. Oracle Database does not perform block sampling. but may be inaccurate.SEED seed_value: Specify this clause to instruct the database to attempt to return the same sample from one execution to the next. reverse key. However. You can create and drop indexes at any time. The system automatically maintains indexes when the related data changes. the “B” in “B*-tree” does not stand for “binary. Just as the index of a book helps you locate information fast.Indexes: Overview Indexes • Storage techniques: – B*-tree indexes: The default and the most common — — — Normal Function based: Precomputed value of a function or expression Index-organized table (IOT) – Bitmap indexes – Cluster indexes: Defined specifically for cluster • Index attributes: – Key compression – Reverse key – Ascending. Similar in construct to a binary tree.” Copyright © 2010. If you drop an index. The Oracle Database may use an index to access data that is required by a SQL statement. they require storage space. is an index that you create on multiple columns (up to 32) in a table. all applications continue to work. by key. A composite index. Optimizer Operators Chapter 6 . B*-tree indexes can be normal. to an individual row or range of rows. For standard indexes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Indexes: Overview . or it may use indexes to enforce integrity constraints. Indexes can be unique or nonunique. descending • Domain indexes: Specific to an application or cartridge Indexes: Overview An index is an optional database object that is logically and physically independent of the table data.” but rather for “balanced. However. an Oracle Database index provides a faster access path to table data. Oracle and/or its affiliates. descending. access to previously indexed data might be slower.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. B*-tree indexes provide fast access. Being independent structures. or function based. normally requiring few reads to find the correct row. All rights reserved. the database uses B*-tree indexes that are balanced to equalize access times. • B*-tree indexes: They are by far the most common indexes.Page 12 Oracle University and InfoTech (Pvt. Columns in a composite index can appear in any order and need not be adjacent in the table. also called a concatenated index. • Reverse key indexes: These are B*-tree indexes whereby the bytes in the key are reversed. 987502. • Application domain indexes: These are indexes you build with packages and store. Copyright © 2010. 205789.Descending indexes: Descending indexes allow for data to be sorted from “big to small” (descending) instead of from “small to big” (ascending) in the index structure. 987501. and not the column data itself. This is not compression in the same manner that ZIP files are compressed. 105789. the sequence generates values such as 987500. and the optimizer decides whether or not to use your index based on that information.Page 13 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . • Bitmap indexes: In a normal B*-tree. Queries using bitmap join indexes can be sped up using bit-wise operations. only equality predicates can benefit from these indexes. You tell the optimizer how selective your index is and how costly it is to execute. and so on. the database logically indexes 005789. While rows of data in a heap organized table are stored in an unorganized fashion (data goes wherever there is available space). 987501. All rights reserved. this is an optional compression that removes redundancies from concatenated (multicolumn) indexes. a single index entry uses a bitmap to point to many rows simultaneously. With bitmap indexes. You can gather statistics on this virtual column. Reverse key indexes can be used to obtain a more even distribution of index entries throughout an index that is populated with increasing values. this can reduce contention for particular blocks that may otherwise be targets for contention. A bitmap join index can be used to avoid actual joins of tables. • Index key compression: The basic concept behind a compressed key index is that every entry is broken into two—a prefix and a suffix component. • Index-organized tables: These are tables stored in a B*-tree structure. The prefix is built on the leading columns of the concatenated index and has many repeating values. and so on. They are appropriate for repetitive data (data with few distinct values relative to the total number of rows in the table) that is mostly read-only. For example. by performing restrictions in advance. However. or to greatly reduce the volume of data that must be joined. there is a one-to-one relationship between an index entry and a row. With a reverse key index. Bitmap indexes should never be considered in an OLTP database for concurrency-related issues. that is.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • . The suffix is built on the trailing columns in the index key and is the unique component of the index entry within the prefix. You can consider them as indexes on a virtual (derived or hidden) column. rather. In other words. • Function-based indexes: These are B*-tree or bitmap indexes that store the computed result of a function on a row’s column or columns. either in the database or even outside the database. and 987502. it is a column that is not physically stored in the table. an index entry points to a row. Because these reverse keys are now likely to be placed in different locations. if you use a sequence to generate a primary key. Oracle and/or its affiliates. data in an IOT is stored and sorted by a primary key. instead of 987500. IOTs behave like regular tables as far as your application is concerned. • Bitmap join indexes: A bitmap join index is a bitmap index for the join of two or more tables. Depending on the number of entries. Oracle and/or its affiliates. It is possible to reorganize indexes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Normal B*-tree Indexes . Note: The maximum size of a single index entry is approximately one-half of the data block size. All rights reserved.Normal B*-tree Indexes Index entry Root Branch Index entry header Key column length Leaf Key column value rowid Table data retrieved by using rowid Normal B*-tree Indexes Each B*-tree index has a root block as a starting point. and they grow from the top down.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The system can directly access rows after it has retrieved the address (the ROWID) from the index leaf blocks.Page 14 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . Indexes are always balanced. the balancing algorithm can cause the B*-tree height to increase unnecessarily. Copyright © 2010. In certain situations. The internal structure of a B*-tree index allows rapid access to the indexed values. there are multiple branch blocks that can have multiple leaf blocks. This is done by the ALTER INDEX … REBUILD | COALESCE command. The leaf blocks contain all values of the index plus ROWIDs that point to the rows in the associated data segment. Previous and next block pointers connect the leaf blocks so that they can be traversed from left to right (and vice versa). Oracle and/or its affiliates. To perform an index scan. The index contains not only the indexed value. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Scans . if the statement accesses other columns in addition to the indexed columns.Page 15 Oracle University and InfoTech (Pvt. but also the rowids of rows in the table that have the value. Copyright © 2010.Index Scans Types of index scans: • Unique • Min/Max • Range (Descending) • Skip • Full and fast full • Index join B-Tree index IX_EMP B : block Table EMP B B B B B B Index Scans An index scan can be one of the following types: A row is retrieved by traversing the index. the system searches the index for the indexed column values accessed by the statement. If the statement accesses only columns of the index. An index scan retrieves data from an index based on the value of one or more columns in the index. rather than from the table. using the indexed column values specified by the statement’s WHERE clause. the system reads the indexed column values directly from the index. Note: The graphic shows a case where four rows are retrieved from the table using their rowids obtained by the index scan. Therefore. the system can find the rows in the table by using either a table access by rowid or a cluster scan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Optimizer Operators Chapter 6 . You can look for access conditions in the “Predicate Information” section of the execution plan (The execution plan is dealt with in detail in the lesson titled “Interpreting Execution Plans. All rights reserved.Page 16 Oracle University and InfoTech (Pvt. Note: Filter conditions filter rows after the fetch operation and output the filtered rows. Key values and ROWIDs are obtained from the index. a single ROWID. Copyright © 2010. Oracle and/or its affiliates.Index Unique Scan index UNIQUE Scan PK_EMP create unique index PK_EMP on EMP(empno) select * from emp where empno = 9999. Optimizer Operators Chapter 6 . and table rows are obtained using ROWIDs. Here the system accesses only matching rows for which EMPNO=9999. This access path is used when all the columns of a unique (B*-tree) index are specified with equality conditions. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Unique Scan .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Index Unique Scan An index unique scan returns.”). The system performs a unique scan if a statement contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row is accessed. at most. All rights reserved. It can be bounded (on both sides) or unbounded (on one or both sides). select /*+ INDEX(EMP I_DEPTNO) */ * from emp where deptno = 10 and sal > 1000.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Range Scan . Range scans can avoid sorting when index columns constitute the ORDER BY/GROUP BY clause and the indexed columns are NOT NULL as otherwise they are not considered. Data is returned in the ascending order of index columns. Copyright © 2010. Wildcard searches (col1 like '%ASD') should not be in a leading position. The optimizer uses index range scan descending when an order by descending clause can be satisfied by an index. Index Range Scan An index range scan is a common operation for accessing selective data. Oracle and/or its affiliates. Range scans can use unique or nonunique indexes. An index range scan descending is identical to an index range scan. Optimizer Operators Chapter 6 . such as col1 = :b1. Multiple rows with identical values are sorted in the ascending order by ROWID. col1 > :b1. except that the data is returned in the descending order. as this does not result in a range scan. and any combination of the preceding conditions. The optimizer uses a range scan when it finds one or more leading columns of an index specified in conditions (the WHERE clause).Index Range Scan Index Range SCAN I_DEPTNO create index I_DEPTNO on EMP(deptno). col1 < :b1.Page 17 Oracle University and InfoTech (Pvt. SAL >1000 filter from these fetched rows to output the final result. It gets their ROWIDs.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED In the example in the slide. Oracle and/or its affiliates. the system accesses rows for which EMP.Oracle University and InfoTech (Pvt. fetches other columns from the EMP table. All rights reserved. Copyright © 2010. applies the EMP.Page 18 . and finally. Optimizer Operators Chapter 6 . using index I_DEPTNO.DEPTNO=10. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Index Range Scan: Descending Index Range SCAN IDX select * from emp where deptno>20 order by deptno desc. All rights reserved. the system is also able to scan indexes in the reverse order as illustrated by the graphic in the slide. You can see the DESCENDING operation row source for ID 2 in the execution plan that materialized this type of index scans. The example retrieves rows from the EMP table by descending order on the DEPTNO column. which are described in the previous slide. Oracle and/or its affiliates. Note: By default an index range scan is done in the ascending order. Index Range Scan: Descending In addition to index range scans in ascending order. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Range Scan: Descending .Page 19 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . Descending Index Range Scan Index Range SCAN IX_D drop index I_Deptno. this scan is used when ordering data in a descending order to return the most recent data first. Usually. Note: The system treats descending indexes as function-based indexes. Copyright © 2010. The optimizer uses descending index range scan when an order by descending clause can be satisfied by a descending index. create index IX_D on EMP(deptno desc).Page 20 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . The INDEX_DESC(table_alias index_name) hint can be used to force this access path if possible. Descending Index Range Scan A descending index range scan is identical to an index range scan. All rights reserved. Descending indexes allow for data to be sorted from “big to small” (descending) instead of “small to big” (ascending) in the index structure. The columns marked DESC are stored in a special descending order in the index structure that is reversed again using the SYS_OP_UNDESCEND function. except that the data is returned in descending order. select * from emp where deptno <30. or when seeking a value less than a specified value as in the example in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Descending Index Range Scan . Oracle and/or its affiliates. in a WHERE or ORDER BY clause. or columns in expressions. such as the UPPER function.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Defining a function-based index on the transformed column or expression allows that data to be returned using the index when that function or expression is used in a WHERE clause or an ORDER BY clause. such as shown in the slide. function-based indexes defined with the UPPER(column_name) or LOWER(column_name) keywords allow non-case-sensitive searches. Optimizer Operators Chapter 6 . With a function-based index. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Range Scan: Function-Based . These indexes include columns that are either transformed by a function.Index Range Scan: Function-Based Index Range SCAN IX_FBI create index IX_FBI on EMP(UPPER(ename)). a function-based index can be beneficial when frequently-executed SQL statements include transformed columns. For example. select * from emp where upper(ENAME) like 'A%'. such as col1 + col2. Index Range Scan: Function-Based A function-based index can be stored as B*-tree or bitmap structures. you can store computation-intensive expressions in the index. This allows the system to bypass computing the value of the expression when processing SELECT and DELETE statements. Copyright © 2010.Page 21 Oracle University and InfoTech (Pvt. or included in an expression. Oracle and/or its affiliates. Therefore. A full scan can be used to eliminate a sort operation because the data is ordered by the index key. All rights reserved. A full scan is also available when there is no predicate. Oracle and/or its affiliates. select * from emp where sal > 1000 and deptno is not null order by deptno.Index Full Scan create index I_DEPTNO on EMP(deptno). Note: An index full scan reads index using single-block input/output (I/O) (unlike a fast full index scan).Page 22 Oracle University and InfoTech (Pvt. The predicate does not need to be an index driver (leading column). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Full Scan .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. if both the conditions are met: • All the columns in the table referenced in the query are included in the index. index Full Scan I_DEPTNO Index Full Scan A full scan is available if a predicate references one of the columns in the index. • At least one of the index columns is not null. Copyright © 2010. Optimizer Operators Chapter 6 . Page 23 Oracle University and InfoTech (Pvt. Note: Index fast full scans are used against an index when it is rebuilt offline. L discard */ deptno from emp Index Fast Full Scan Index fast full scans are an alternative to full table scans when the index contains all the columns that are needed for the query and at least one column in the index key has a NOT NULL constraint.. In this case. A fast full scan is faster than a normal full index scan because it can use multiblock I/O just as a table scan. This operation reads the entire index using multiblock reads (unlike a full index scan). All rights reserved. It can be used for the min/avg/sum aggregate functions. the optimizer must know that all table rows are represented in the index. Fast full index scans cannot be performed against bitmap indexes. Copyright © 2010. It cannot be used to eliminate a sort operation because the data is not ordered by the index key. at least one NOT NULL column.Index Fast Full Scan LEGEND: db_file_multiblock_read_count = 4 multiblock read SH R L discard L SH=segment header R=root block B=branch block L=leaf block multiblock read L B L L discard select /*+ INDEX_FFS(EMP I_DEPTNO) where deptno is not null. A fast full scan accesses the data in the index itself without accessing the table. Oracle and/or its affiliates. Optimizer Operators Chapter 6 . You can specify fast full index scans with the OPTIMIZER_FEATURES_ENABLE initialization parameter or the INDEX_FFS hint as shown in the slide example. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Fast Full Scan .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. B .. Scanning index blocks is often faster than scanning table data blocks. and starts to scan it because it could contain A25 (that is. Optimizer Operators Chapter 6 . so it can be skipped. Returning to [B1].Index Skip Scan SELECT * FROM employees WHERE age BETWEEN 20 AND 29 Min M10 B1 Min F16 F20 F26 F30 Index on (GENDER. Oracle and/or its affiliates. The server identifies that this is not possible because the first entry is F10. the system identifies a first entry to be F16. the server finds that the next two entries have a common prefix of F2. goes to the left leaf [L1]. AGE) R B2 M10 M16 M20 M26 M30 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24 F25 F26 F27 F28 F29 F30 F31 F32 F33 F34 F35 M10 M11 M12 M13 M14 M15 M16 M17 M18 M19 M20 M21 M22 M23 M24 M25 M26 M27 M28 M29 M30 M31 M32 M33 M34 M35 L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 Index Skip Scan Index skip scans improve index scans by skipping blocks that could never contain keys matching the filter column values. Backtracking to the first branch block [B1]. This identifies possible subtrees to scan. where the “gender” is before “F” in the alphabet). Copyright © 2010. Skip scanning can happen when the initial (leading) column of the composite index is not specified in a query. Because the server is certain that it is not possible to find a 25 between F16 and F20. the second leaf block [L2] can be skipped. The system knows that these subtrees are ordered by age. The system starts from the root of the index [R] and proceeds to the left branch block [B1].) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This example illustrates how skip scanning is processed to answer the query in the slide. the server identifies that the next subtree (F16) does not need to be scanned because the next entry in [B1] is F20. It is thus not possible to find an entry such as A25 in this leaf. From there. Suppose that there is a concatenated index on the GENDER and AGE columns in the EMPLOYEES table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Skip Scan . All rights reserved.Page 24 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED So the third and fourth leaf blocks [L3–L4] are scanned and some values are retrieved. All rights reserved. the system determines that it is no longer possible to find an F2x entry. Thus. it is not necessary to scan that subtree [L5]. Oracle and/or its affiliates. Optimizer Operators Chapter 6 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 25 . Copyright © 2010. By looking at the fourth entry in the first branch block [B1]. Note that out of a total of 10 leaf blocks.The same process continues with the right part of this index. only five are scanned. Oracle University and InfoTech (Pvt. Index Skip Scan: Example Index on (DEPTNO. Optimizer Operators Chapter 6 . As you can see. The number of logical subindexes is determined by the number of distinct values in the initial column. Oracle and/or its affiliates. This leading column only has some discrete values. Copyright © 2010. it is three index structures: • where deptno = 10 • where deptno = 20 • where deptno = 30 The output is ordered by DEPTNO. Index Skip Scan: Example The example in the slide finds employees who have salary less than 1500 using an index skip scan. that is. The system pretends that the index is really three little index structures hidden inside one big one. All rights reserved.SAL). Skip scanning lets a composite index be split logically into smaller subindexes. In the example. select /*+ index_ss(EMP IX_SS) */ * from emp where SAL < 1500.Page 26 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. 10. SAL) create index IX_SS on EMP(DEPTNO. Note: Skip scanning is advantageous if there are few distinct values in the leading column of the composite index and many distinct values in the nonleading key of the index. 20 and 30. It is assumed that there is a concatenated index on the DEPTNO and SAL columns. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Skip Scan: Example . the query does not have a predicate on the DEPTNO leading column. The example in the slide assumes that you have two separate indexes on the ENAME and SAL columns of the EMP table. If an index join is used. but is built using index accesses followed by a join operation on rowid. An index join cannot be used to eliminate a sort operation. Note: You can specify an index join with the INDEX_JOIN hint as shown in the example.Index Join Scan alter table emp modify (SAL not null. ENAME not null). Copyright © 2010. Index Join Scan An index join is a hash join of several indexes that together contain all the table columns that are referenced in the query. The index join is not a real join operation (note that the example is a single table query). Oracle and/or its affiliates. create index I_ENAME on EMP(ename). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Join Scan . Optimizer Operators Chapter 6 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 27 Oracle University and InfoTech (Pvt. create index I_SAL on EMP(sal). no table access is needed because all the relevant column values can be retrieved from the indexes. All rights reserved. retrieves all COL1 values. Copyright © 2010. Note: The index could also be used by forcing the column to return only NOT NULL values using the COL1 IS NOT NULL predicate. with the second query. there is no need to read the table itself. One index is built on top of each column. col2 number not null).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Because you only search for COL1 values. the effect of the predicate against COL1 is to eliminate nulls from the data returned from the column. The first query. B*-tree Indexes and Nulls It is a common mistake to forget about nulls when dealing with B*-tree indexes. Hinting the index on COL1 (nullind1) to force index utilization makes no difference because COL1 is nullable. In the slide example. which cannot have null values. and COL2. All rights reserved. you create a table containing a nullable column called COL1. Oracle and/or its affiliates. This allows the index to be used.Page 28 Oracle University and InfoTech (Pvt.B*-tree Indexes and Nulls create table nulltest ( col1 number. Optimizer Operators Chapter 6 . create index notnullind2 on nulltest (col2). Single-column B*-tree indexes do not store null values and so indexes on nullable columns cannot be used to drive queries unless there is something that eliminates the null values from the query. However. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED B*-tree Indexes and Nulls . Because COL1 is nullable. The third query can directly use the index because the corresponding column is declared NOT NULL at table-creation time. the index cannot be used without a predicate. create index nullind1 on nulltest (col1). so the rows are not represented in the index. SELECT COUNT(*) FROM person. The SSN column is defined as allowing null values. Any rows with null for SSN are not represented in the index. SELECT /*+ INDEX(person) */ COUNT(*) FROM person. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Indexes: Considering Nullable Columns . Copyright © 2010. after the unique index is dropped in the place of designating a primary key. In the example in the slide. This is one reason why it is better to create a primary key rather than a unique index. All rights reserved. SELECT STATEMENT | SORT AGGREGATE | TABLE ACCESS FULL| PERSON PERSON DROP INDEX person_ssn_ix. for example.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Single-column B*-tree indexes never store null values. Column Null? SSN FNAME LNAME N Y N PERSON ALTER TABLE person ADD CONSTRAINT pk_ssn PRIMARY KEY (ssn). In the slide. so the count across the index is not necessarily accurate.Using Indexes: Considering Nullable Columns Column Null? SSN FNAME LNAME Y Y N CREATE UNIQUE INDEX person_ssn_ix ON person(ssn). and thus. SELECT STATEMENT | SORT AGGREGATE | INDEX FAST FULL SCAN| PK_SSN Using Indexes: Considering Nullable Columns Some queries look as if they should use an index to compute a simple count of rows in the table. Oracle and/or its affiliates. the index is used to compute the row count. A primary key column cannot contain null values. But the index to be used must not be built on a column that can contain null values. do not contribute to the COUNT being computed. and creating a unique index on it does nothing to change that. there is a unique index on the SSN column of the PERSON table. This index is not used when executing the count query in the slide.Page 29 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . This is typically more efficient that scanning the table. Note: The PRIMARY KEY constraints combine a NOT NULL constraint and a unique constraint in a single declaration. All rights reserved. Optimizer Operators Chapter 6 . Index-organized tables provide fast key-based access to table data for queries involving exact match and range searches.Index-Organized Tables Indexed access on table Accessing index-organized table ROWID Non-key columns Key column Row header Index-Organized Tables An index-organized table (IOT) is a table physically stored in a concatenated index structure. The remaining non-key columns are stored in the index structure. The primary key can be a composite key. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index-Organized Tables . which is called an overflow area. storage requirements are reduced because key columns are not duplicated in the table and index. An IOT contains: • Primary key values • Other (non-key) column values for the row The B*-tree structure. Also. The leaf blocks in this structure contain the rows instead of the ROWIDs. Oracle and/or its affiliates. Copyright © 2010. Changes to the table data result only in updating the index structure. Because large rows of an IOT can destroy the dense and efficient storage of the B*-tree structure.Page 30 Oracle University and InfoTech (Pvt. You can create additional indexes on IOTs.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. IOTs are particularly useful when you use applications that must retrieve data based on a primary key and have only a few. you can store part of the row in another segment. which is based on the primary key of the table. This means that the rows in the IOT are always maintained in the order of the primary key. is organized in the same way as an index. relatively short non-key columns. The key values (for the table and the B*-tree index) are stored in the same segment. Optimizer Operators Chapter 6 . Copyright © 2010.Page 31 .Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: The descriptions mentioned here are correct if no overflow segments exist. Overflow segments should be used with long rows. All rights reserved. select * from iotemp where empno=9999. You can retrieve the link between the table name and the segment from USER_INDEXES with these columns: INDEX_NAME. All rights reserved.2) not null. They use the same access paths that you saw for normal indexes. Note: SYS_IOT_TOP_75664 is the system-generated name of the segment used to store the IOT structure.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. mgr number(4). deptno number(2)) organization index. Copyright © 2010.2).Page 32 Oracle University and InfoTech (Pvt. INDEX_TYPE. TABLE_NAME. ename varchar2(10) not null. select * from iotemp where sal>1000. sal number(7. Oracle and/or its affiliates.Index-Organized Table Scans create table iotemp ( empno number(4) primary key. comm number(7. The major difference from a heap-organized table is that there is no need to access both an index and a table to retrieve indexed data. Optimizer Operators Chapter 6 . Index-Organized Table Scans Index-organized tables are just like indexes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index-Organized Table Scans . job varchar2(9). hiredate date. 3. Start ROWID 12. reduced storage requirements compared to other indexing techniques. Each bitmap header stores start and end ROWIDs. an index entry points to a row. All rights reserved.8. with bitmap indexes. you lock many rows in the table at the same time. Copyright © 2010. dramatic performance gains even on hardware with a relatively small number of CPUs or a small amount of memory. 12. otherwise it is 0.0.0. a single index entry uses a bitmap to point to many rows simultaneously.3. Bitmap indexes are widely used in data warehousing environments. 12.3. Based on these values. Oracle and/or its affiliates. and efficient maintenance during parallel DML and loads.8. <Yellow. If a bitmap index involves more than one column.3. there is a bitmap for every possible combination.3.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. A bitmap index is organized as a B*-tree index but. there is a one-to-one relationship between an index entry and a row. the system uses an internal algorithm to map bitmaps onto ROWIDs. 10. <Green.0. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Indexes . 10. 100010000 000101000 010000000 001000000 End ROWID 010000000 000000000 001100000 100000000 010010100> 100100000> 000001001> 001000010> Bitmap Bitmap Indexes In a B*-tree. This is possible because the system knows the maximum possible number of rows that can be stored in a system block. These environments typically have large amounts of data and ad hoc queries. For such applications. The contents of that position in the bitmap for a particular value indicate whether that row has that value in the bitmap columns. Each position in a bitmap maps to a potential row in the table even if that row does not exist. 10.3.Page 33 Oracle University and InfoTech (Pvt.8. The value stored is 1 if the row values match the bitmap condition. Optimizer Operators Chapter 6 .3.Bitmap Indexes Table File 3 Block 10 Index Block 11 Block 12 <Blue. <Red. but no concurrent data manipulation language (DML) transactions because when locking a bitmap.8. bitmap indexing provides reduced response time for large classes of ad hoc queries.0.3. Key 10. 12. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Unlike most other types of indexes. they are split in multiple leaves if the number of rows increases. Although bitmaps are compressed internally. Optimizer Operators Chapter 6 .Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Indexing of nulls can be useful for some types of SQL statements. bitmap indexes include rows that have NULL values. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. such as queries with the aggregate function COUNT. The IS NOT NULL predicate can also benefit from bitmap indexes.Page 34 . Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Index Access: Examples .access("COUNTRY"='FR') SELECT * FROM PERF_TEAM WHERE country>'FR'. the query might simply use the bitmap itself and count the number of 1s (not needing the actual rows). In some cases (such as a query counting the number of rows with COUNTRY FR).Bitmap Index Access: Examples SELECT * FROM PERF_TEAM WHERE country='FR'. All rights reserved.access("COUNTRY">'FR') filter("COUNTRY">'FR') Bitmap Index Access: Examples The examples in the slide illustrate two possible access paths for bitmap indexes—BITMAP INDEX SINGLE VALUE and BITMAP INDEX RANGE SCAN—depending on the type of predicate you use in the queries. --------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | --------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 45 | | 1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 | 45 | | 2 | BITMAP CONVERSION TO ROWIDS| | | | | 3 | BITMAP INDEX SINGLE VALUE | IX_B2 | | | --------------------------------------------------------------------Predicate: 3 . Optimizer Operators Chapter 6 . ---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 3 | | 1 | | | 1 | 3 | | 2 | BITMAP CONVERSION COUNT | | 1 | 3 | | 3 | BITMAP INDEX RANGE SCAN| IX_B2 | | | SORT AGGREGATE Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.” Positions with a “1” are converted into ROWIDs and have their corresponding rows returned for the query.Page 35 2 (0)| | 2 (0)| | Oracle University and InfoTech (Pvt. The first query scans the bitmap for country “FR” for positions containing a “1. --------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | --------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 45 | | 1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 | 45 | | 2 | BITMAP CONVERSION TO ROWIDS| | | | | 3 | BITMAP INDEX RANGE SCAN | IX_B2 | | | --------------------------------------------------------------------Predicate: 3 . This is illustrated in the following example: SELECT count(*) FROM PERF_TEAM WHERE country>'FR'. All rights reserved.Predicate: 3 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED ---------------------------------------------------------------------------- Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 36 . Optimizer Operators Chapter 6 . Oracle and/or its affiliates.access("COUNTRY">'FR') filter("COUNTRY">'FR') Oracle University and InfoTech (Pvt. This improves response time. As the bitmaps from bitmap indexes can be combined quickly. Rows that satisfy some. FR 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 OR DE 0 1 0 0 0 0 0 0 0 0 0 0 F 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 AND M 1 1 1 0 1 1 0 1 0 1 1 1 SELECT * FROM EMEA_PERF_TEAM T WHERE country='FR' and gender='M'. All rights reserved. Due to fast bit-and. bit-minus. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and bit-or operations.Page 37 Oracle University and InfoTech (Pvt.Combining Bitmap Indexes: Examples SELECT * FROM PERF_TEAM WHERE country in('FR'. Combining Bitmap Indexes: Examples Bitmap indexes are the most effective for queries that contain multiple conditions in the WHERE clause. but not all. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Combining Bitmap Indexes: Examples . often dramatically. Optimizer Operators Chapter 6 . it is usually best to use single-column bitmap indexes.'DE'). bitmap indexes are efficient when: • Using IN (value_list) • Predicates are combined with AND or OR Copyright © 2010. conditions are filtered out before the table itself is accessed. Optimizer Operators Chapter 6 .access("COUNTRY"='FR') Combining Bitmap Index Access Paths Bitmap indexes can be used efficiently when a query combines several possible values for a column or when two separately-indexed columns are used. All rights reserved.Combining Bitmap Index Access Paths SELECT * FROM PERF_TEAM WHERE country in ('FR'. the more benefit you get from bitmap indexing.access("GENDER"='M') 5 .'DE'). The more complex the compound WHERE clauses become. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Combining Bitmap Index Access Paths . a WHERE clause might reference several separately indexed columns as in the examples shown in the slide. --------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | | 0 | SELECT STATEMENT | | 1 | 45 | | 1 | INLIST ITERATOR | | | | | 2 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 | 45 | | 3 | BITMAP CONVERSION TO ROWIDS| | | | | 4 | BITMAP INDEX SINGLE VALUE | IX_B2 | | | Predicate: 4 . --------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | | 0 | SELECT STATEMENT | | 1 | 45 | | 1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 | 45 | | 2 | BITMAP CONVERSION TO ROWIDS| | | | | 3 | BITMAP AND | | | | | 4 | BITMAP INDEX SINGLE VALUE| IX_B1 | | | | 5 | BITMAP INDEX SINGLE VALUE| IX_B2 | | | Predicate: 4 . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If both the COUNTRY and GENDER columns have bitmap indexes. a bit-and operation on the two bitmaps quickly locates the rows that you look for.Page 38 Oracle University and InfoTech (Pvt. Copyright © 2010.access("COUNTRY"='DE' OR "COUNTRY"='FR') SELECT * FROM PERF_TEAM WHERE country='FR' and gender='M'. In some cases. BITMAP KEY ITERATION takes each row from a table row source and finds the corresponding bitmap from a bitmap index. Oracle and/or its affiliates. The bitmap minus operation can then be performed as a BITMAP AND operation using this negated bitmap. This set of bitmaps is then merged into one bitmap in a BITMAP MERGE operation. and zeros to ones.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 39 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 . After the bitmap comparison has been done. BITMAP MERGE merges several bitmaps resulting from a range scan into one bitmap. BITMAP MINUS is a dual operator that takes the second bitmap operation and negates it by changing ones to zeros. The following operations have not been explained so far: • • • • BITMAP CONVERSION FROM ROWID: B*-tree index converted by the optimizer into BITMAP (cost is lower than other methods) to make these efficient bitmaps comparison operations available. the resultant bitmap is converted back into ROWIDs (BITMAP CONVERSION TO ROWIDS) to perform the data lookup. This would typically be the case with the following combination of predicates: C1=2 and C2<>6. All rights reserved. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Operations .Bitmap Operations • BITMAP CONVERSION: – TO ROWIDS – FROM ROWIDS – COUNT • BITMAP INDEX: – SINGLE VALUE – RANGE SCAN – FULL SCAN • • • • BITMAP BITMAP BITMAP BITMAP MERGE AND/OR MINUS KEY ITERATION Bitmap Operations The slide summarizes all the possible bitmap operations. Bitmap Join Index 1.2. Each bit in a bitmap corresponds to one row in the SALES table.8000. The key of this index is the CUST_CITY column of the CUSTOMERS table. 10.2.3.cust_id = s.8000. The middle part of the graphic shows you a theoretical implementation of this bitmap join index. Customers 10.3.3 Sales CREATE BITMAP INDEX cust_sales_bji ON sales(c. 10. 10. They represent the join condition between the two tables.3. you can create a bitmap join index. 1.3. 000101000100000…> <Marseille. 1. see the lesson titled “Case Study: Star Transformation.cust_id. A bitmap join index is a bitmap index for the join of two or more tables.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Join Index .3 <Rognes.” Here.3.3. 100010010010100…> <Aix-en-Provence. Each entry or key in the index represents a possible city found in the CUSTOMERS table. A bitmap join index is a spaceefficient way of reducing the volume of data that must be joined by performing the join in advance. customers c WHERE c. although not required. The FROM and WHERE clause in the CREATE statement allow the system to make the link between the two tables. The CUST_ID column is the primary key of CUSTOMERS. For a row source example. you see that the first row in the SALES table corresponds to a product sold to a Rognes customer. Note: Bitmap join indexes are much more efficient in storage than materialized join views.Page 40 Oracle University and InfoTech (Pvt. All rights reserved. In the first key in the slide (Rognes).2.2. you create a new bitmap join index named cust_sales_bji on the SALES table. Optimizer Operators Chapter 6 .cust_city) FROM sales s. 010000001000001…> Bitmap Join Index In addition to a bitmap index on a single table. while the second bit is not a product Copyright © 2010. 1. Oracle and/or its affiliates. but is also a foreign key inside SALES. This example assumes that there is an enforced primary key constraint on CUSTOMERS to ensure that what is stored in the bitmap reflects the reality of the data in the tables. A bitmap is then associated to one particular key.8000.8000. Oracle University and InfoTech (Pvt.Page 41 . Optimizer Operators Chapter 6 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Oracle and/or its affiliates. By storing the result of a join. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED sold to a Rognes customer. the join can be avoided completely for SQL statements using a bitmap join index. you can include up to 32 columns in the index. and you decided to create a composite index. Copyright © 2010. model). unless it is a bitmap composite index. In the illustration in the slide. select * from cars where make = 'CITROËN' and model = '2CV'. Composite indexes can provide additional advantages over single-column indexes: • Improved selectivity: Sometimes two or more columns or expressions. It is not required that the columns in the index are adjacent.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. in which case the limit is 30. If some keys are used in WHERE clauses more frequently. or the leading portion of the columns in the index. Optimizer Operators Chapter 6 .Composite Indexes MAKE MODEL CARS Index columns create index cars_make_model_idx on cars(make. This is possible since index skip scans and fast full scans were implemented. And. All rights reserved. A composite index is mainly useful when you often have a WHERE clause that references all. each with poor selectivity. the MAKE and MODEL columns are concatenated together to form the index.Page 42 Oracle University and InfoTech (Pvt. • Reduced I/O: If all columns selected by a query are in a composite index. the system can return these values from the index without accessing the table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Composite Indexes . Note: It is also possible for the optimizer to use a concatenated index even though your query does not reference a leading part of that index. can be combined to form a composite index with higher selectivity. Oracle and/or its affiliates. be sure to create the index so that the more frequently selected keys constitute a leading portion for allowing the statements that use only these keys to use the index. ----------------------------------------------------------------| Id | Operation | Name | ----------------------------------------------------------------| 0 | SELECT STATEMENT | | | 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS | |* 2 | INDEX RANGE SCAN | CARS_MAKE_MODEL_IDX | ----------------------------------------------------------------- Composite Indexes A composite index is also referred to as a concatenated index because it concatenates column values together to form the index key value. Unlike unusable indexes. Copyright © 2010. Making an index invisible is an alternative to making it unusable or dropping it. The default value for this parameter is FALSE. Optimizer Operators Chapter 6 . an invisible index is maintained during DML statements. you can perform the following actions: • Test the removal of an index before dropping it.Invisible Index: Overview Use index. Update table. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Update table. • Use temporary index structures for certain operations or modules of an application without affecting the overall application. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Invisible Index: Overview .Page 43 Oracle University and InfoTech (Pvt. Invisible Index: Overview An invisible index is an index that is ignored by the optimizer unless you explicitly set the OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at the session or system level. Optimizer view point INVISIBLE VISIBLE Index Index OPTIMIZER_USE_INVISIBLE_INDEXES=FALSE Data view point Update index. Do not use index. Update index. Using invisible indexes. Oracle and/or its affiliates. it is assumed that OPTIMIZER_USE_INVISIBLE_INDEXES is set to FALSE.Page 44 Oracle University and InfoTech (Pvt. Note: For all the statements given in the slide. Invisible Indexes: Examples When an index is invisible. Optimizer Operators Chapter 6 . • Optimizer considers this index: ALTER INDEX ind1 VISIBLE. and then determine whether to make the index visible. • Create an index as invisible initially: CREATE INDEX IND1 ON TAB1(COL1) INVISIBLE. the optimizer selects plans that do not use the index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Invisible Indexes: Examples . You can query the VISIBILITY column of the *_INDEXES data dictionary views to determine whether the index is VISIBLE or INVISIBLE. Copyright © 2010. perform testing.Invisible Indexes: Examples • Index is altered as not visible to the optimizer: ALTER INDEX ind1 INVISIBLE. All rights reserved. If there is no discernible drop in performance. You can also create an index initially as invisible. you can drop the index. • Optimizer does not consider this index: SELECT /*+ index(TAB1 IND1) */ COL1 FROM TAB1 WHERE …. Columns suitable for indexing: Some columns are strong candidates for indexing: - Values are relatively unique in the column. index the columns used for joins. Index the correct tables and columns. Specify the tablespace for each index. Optimizer Operators Chapter 6 . Drop indexes that are no longer required. - There is a small range of values (good for bitmap indexes). - The column contains many nulls. • Index the correct tables and columns: Use the following guidelines for determining when to create an index: • • - Create an index if you frequently want to retrieve less than 15% of the rows in a large table. All rights reserved. • Consider costs and benefits of coalescing or rebuilding indexes. but queries often select all rows having a value. Oracle and/or its affiliates. Limit the number of indexes for each table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Guidelines for Managing Indexes .Guidelines for Managing Indexes • • • • • • • • Create indexes after inserting table data. Consider creating indexes with NOLOGGING. It is more efficient to create an index for a table after inserting or loading the data. Consider cost before disabling or dropping constraints. - To improve performance on joins of multiple tables. Order index columns for performance. • Guidelines for Managing Indexes • Create indexes after inserting table data: Data is often inserted or loaded into a table using either the SQL*Loader or an import utility. - There is a wide range of values (good for regular indexes). Columns not suitable for indexing: Copyright © 2010. Consider parallelizing index creation. - Small tables do not require indexes.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 45 Oracle University and InfoTech (Pvt. If the associated index for a UNIQUE key or PRIMARY KEY constraint is extremely large.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. just as you can parallelize table creation. specify the most frequently used columns first. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED - . However. such as tablespace backup. there is a trade-off between the speed of retrieving data from a table and the speed of updating the table. • Order index columns for performance: The order of columns in the CREATE INDEX statement can affect query performance. - The LONG and LONG RAW columns cannot be indexed. - Virtual columns: You can create unique or nonunique indexes on virtual columns. you can rebuild or coalesce the index. • Limit the number of indexes for each table: A table can have any number of indexes. it can be more convenient to perform database maintenance. All rights reserved. and select the one that works best for your situation. But before you perform either task. • Consider cost before disabling or dropping constraints: Because unique and primary keys have associated indexes. you should factor in the cost of dropping and creating indexes when considering whether to disable or drop a UNIQUE or PRIMARY KEY constraint. you can save time by leaving the constraint enabled rather than dropping and re-creating the large index. In general. • Consider costs and benefits of coalescing or rebuilding indexes: Improper sizing or increased growth can produce index fragmentation. However. an index created with an INITIAL value of 5M and a parallel degree of 12 consumes at least 60 MB of storage during index creation. Because indexes created using NOLOGGING are not archived. Oracle and/or its affiliates. the more overhead is incurred as the table is modified. • Drop indexes that are no longer required. • Specify the tablespace for each index: If you use the same tablespace for a table and its index.Page 46 Oracle University and InfoTech (Pvt. Copyright © 2010. weigh the costs and benefits of each option. This speeds up index creation. • Consider creating indexes with NOLOGGING: You can create an index and generate minimal redo log records by specifying NOLOGGING in the CREATE INDEX statement. perform a backup after you create the index. Thus. the more indexes there are. • Consider parallelizing index creation: You can parallelize index creation.There are many nulls in the column and you do not search on the not null values. Note that NOLOGGING is the default in a NOARCHIVELOG database. You also have the option of explicitly specifying that you want to keep or drop the index when dropping or disabling a UNIQUE or PRIMARY KEY constraint. To eliminate or reduce fragmentation. Optimizer Operators Chapter 6 . the following does not use the index on SSN: SELECT * FROM person WHERE SSN = 123456789 Copyright © 2010. the index cannot be used. • The column can contain null. the index is not used.Investigating Index Usage An index may not be used for one of many reasons: • There are functions being applied to the predicate. Function-based indexes were covered under “Index Range Scan: Function-Based” earlier in this lesson. Oracle and/or its affiliates. or because it should not use the index. • Statistics are old. you can create a function-based index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Functions If you apply a function to the indexed column in the WHERE clause. Data Type Mismatch If there is a data type mismatch between the indexed column and the compared value. For example. For example. if the SSN column is of the VARCHAR2 type. This can be because the optimizer is unaware of some information. • There is a data type mismatch.10*salary > 10000 If you want an index to be used in this case. • Using the index would actually be slower than not using it. Investigating Index Usage You may often run a SQL statement expecting a particular index to be used. the following statement does not use an index on the salary column: SELECT * FROM employees WHERE 1. All rights reserved. Optimizer Operators Chapter 6 . This is due to the implicit data type conversion. the index is based on column values without the effect of the function. and it is not.Page 47 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Investigating Index Usage . it may prevent the use of an index on that column. Oracle University and InfoTech (Pvt. Null Columns If a column can contain nulls. Optimizer Operators Chapter 6 .The statistics are considered by the optimizer when deciding whether to use an index. Slower Index Sometimes the use of an index is not efficient.Page 48 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. This is covered later in this lesson. All rights reserved. This is covered later in this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Old Statistics Copyright © 2010. they may influence the optimizer to make poor decisions about indexes. If they are outdated. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a. False Answer: a Copyright © 2010. Oracle and/or its affiliates. Optimizer Operators Chapter 6 .Page 49 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . True b.Quiz A full table scan sequentially reads all rows from a table and filters out those that do not meet the selection criteria. All rights reserved. Quiz Assuming that the column email has an index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a. All rights reserved. the following query will result in an index range scan: Select employee_name from employees where email like '%A'. Oracle and/or its affiliates.Page 50 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Optimizer Operators Chapter 6 . False Answer: b Copyright © 2010. True b. Limit the number of indexes for each table d.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Do not specify the tablespace for each index Answer: c Copyright © 2010. Do not order index columns c. All rights reserved.Page 51 Oracle University and InfoTech (Pvt.Quiz To get optimum result from indexes: a. Create indexes before inserting table data b. Oracle and/or its affiliates. Optimizer Operators Chapter 6 . Oracle and/or its affiliates. you should have learned to: • Describe the SQL operators for tables and indexes • List the possible access paths Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary Copyright © 2010.Summary In this lesson.Page 52 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Optimizer Operators Chapter 6 . • Case 1 through case 13 Oracle University and InfoTech (Pvt. Optimizer Operators Chapter 6 .Page 53 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 6: Overview Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Practice 6: Overview This practice covers using different access paths for better optimization. All rights reserved. Optimizer Operators Chapter 6 . All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010.Page 54 . Page 1 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) Ltd use only Optimizer: Join Operators . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 7 Copyright © 2010. All rights reserved. Optimizer: Join Operators Chapter 7 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Page 2 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer: Join Operators Copyright © 2010. Optimizer: Join Operators Chapter 7 .) Ltd use only Optimizer: Join Operators .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. Copyright © 2010. Optimizer: Join Operators Chapter 7 .Page 3 Oracle University and InfoTech (Pvt. you should be able to: • Describe the SQL operators for joins • List the possible access paths Objectives This lesson helps you understand the execution plans related to join operations. Oracle and/or its affiliates. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .Objectives After completing this lesson. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer: Join Operators Chapter 7 . which define how the objects are related • Join methods: – Nested loops – Sort-merge join – Hash join SELECT e.Join Methods A join: • Defines the relationship between two row sources • Is a method of combining data from two data sources • Is controlled by join predicates.job = 'ANALYST' OR e.d. Hash joins generally perform better than sort-merge joins. or even the result set of a join tree consisting of many different objects.empno = 9999). Join predicate Nonjoin predicate Join Methods A row source is a set of data that can be accessed in a query. All rights reserved. probing the hash table to find the joined rows.Page 4 Oracle University and InfoTech (Pvt. a nonmergeable view.empno = 9999.ename. A join operation combines the output from two row sources (such as tables or views) and returns one resulting row source (data set). • Hash join: Used for joining large data sets. The optimizer uses the smaller of two tables or data sources to build a hash table on the join key in memory. It can be a table. Oracle and/or its affiliates.dname FROM dept d JOIN emp e USING (deptno) WHERE e. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Join Methods . A join predicate is a predicate in the WHERE clause that combines the columns of two of the tables in the join. Join predicate Nonjoin predicate SELECT e.dname FROM emp e. This method is best used when the Copyright © 2010.deptno AND (e. A nonjoin predicate is a predicate in the WHERE clause that references only one table. d.ename. On the other hand. an index.job = 'ANALYST' OR e. dept d WHERE e.deptno = d. The optimizer supports different join methods such as the following: • Nested loop join: Useful when small subsets of data are being joined and if the join condition is an efficient way of accessing the second table • Sort-merge join: Can be used to join rows from two independent sources. It then scans the larger table. sort-merge joins can perform better than hash joins if one or two row sources are already sorted. The cost is then limited to a single read pass over the data for the two tables. Oracle University and InfoTech (Pvt. Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED smaller table fits in the available memory. The ANSI syntax is the first example.Note: The slide shows you the same query using both the American National Standards Institute (ANSI) and non-ANSI join syntax. Oracle and/or its affiliates. Optimizer: Join Operators Chapter 7 .Page 5 . it can be used to access the inner table by rowid.filter("E". e. so the order of tables in the execution plan is important. --------------------------------------------------------------------| Id | Operation | Name | Rows |Cost | --------------------------------------------------------------------| 0 | SELECT STATEMENT | | 2 | 4 | | 1 | NESTED LOOPS | | 2 | 4 | | 2 | TABLE ACCESS FULL | EMP | 2 | 2 | | 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 1 | | 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | --------------------------------------------------------------------2 ."DEPTNO") Nested Loops Join In the general form of the nested loops join. Each row returned drives a lookup in inner row source. Copyright © 2010.deptno. d. unless a composite index combining both the join and the nonjoin predicate is used. You drive from the outer loop to the inner loop.Page 6 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. one of the two tables is defined as the outer table. or the driving table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Nested Loops Join . If an index is available. All rights reserved. you should use other join methods when two independent row sources are joined. all rows in the inner table that satisfy the join predicate (matching rows) are retrieved. or the right-hand side.deptno and ename like 'A%'. For each row in the outer (driving) table that matches the single table predicates. with a good driving condition between the two tables. dept d where e.access("E". Oracle and/or its affiliates.deptno = d."ENAME" LIKE 'A%') 4 . Any nonjoin predicates on the inner table are considered after this initial retrieval. d. Optimizer: Join Operators Chapter 7 .dname from emp e. The code to emulate a nested loop join might look as follows: for r1 in (select rows from EMP that match single table predicate) loop for r2 in (select rows from DEPT that match current row from EMP) loop output values from current row of EMP and current row of DEPT end loop end loop The optimizer uses nested loop joins when joining small number of rows."DEPTNO"="D". Joining rows are then returned. The other table is called the inner table.Nested Loops Join NL • • • Driving row source is scanned. Therefore. TAF TAR Driving For each IS Inner select ename.deptno. dname from emp e.deptno and ename like 'A%'. All rights reserved. It does not affect the join order. d. e. Oracle and/or its affiliates.Nested Loops Join: Prefetching NL TAF TAR Driving IRS Inner select ename.Page 7 Oracle University and InfoTech (Pvt.access("E"."DEPTNO"="D".deptno. accessing a large number of rows using a poorly clustered B*tree index could be expensive. When an index access path is chosen and the query cannot be satisfied by the index alone. the system delays data blocks reads until multiple rows specified by the underlying index are ready to be accessed and then retrieves multiple data blocks simultaneously."ENAME" LIKE 'A%') 4 . With data block prefetching. or parallelization scheme. of index access with table lookup by batching rowid lookups into parallel block reads. The idea is to improve I/O utilization. which involves reading an array of blocks which are pointed at by an array of qualifying ROWIDs. --------------------------------------------------------------------| 0 | SELECT STATEMENT | | 2 | 84 | 5 | 1 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 22 | 1 | 2 | NESTED LOOPS | | 2 | 84 | 5 |* 3 | TABLE ACCESS FULL | EMP | 2 | 40 | 3 |* 4 | INDEX RANGE SCAN | IDEPT | 1 | | 0 --------------------------------------------------------------------3 . access method.deptno. Without data block prefetching.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. therefore response time. This change to the plan output is not considered a different execution plan. Copyright © 2010.deptno = d. Each row accessed by the index would likely be in a separate data block and thus would require a separate I/O operation. d.filter("E". This ROWID to data row access (table lookup) is improved using data block prefetching. join method. the data rows indicated by the ROWID also must be fetched. This optimization is only available when the inner access path is index range scan and not if the inner access path is index unique scan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Nested Loops Join: Prefetching . Optimizer: Join Operators Chapter 7 . rather than reading a single data block at a time. The prefetching mechanism is used by table lookup."DEPTNO") Nested Loops Join: Prefetching Oracle 9iR2 introduced a mechanism called nested loops prefetching. dept d where e. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This produces a set of ROWIDs that you can use to look up the corresponding rows from the table with the index. e. Instead of going to the table for each ROWID produced by the first NESTED LOOPS join. the system first performs a NESTED LOOPS join between the other table and the index. With this NESTED LOOPS implementation. All rights reserved.deptno = d. Driving NL TAF TAR IRS Inner Nested Loops Join: 11g Implementation Oracle Database 11g introduces a new way of performing joins with NESTED LOOPS operators.deptno and ename like 'A%'. the system batches up the ROWIDs and performs a second NESTED LOOPS join between the ROWIDs and the table. d.deptno. Oracle and/or its affiliates. Optimizer: Join Operators Chapter 7 . Copyright © 2010.deptno. d.Page 8 Oracle University and InfoTech (Pvt.dname from emp e. dept d where e.Nested Loops Join: 11g Implementation NL select ename. This ROWID batching technique improves performance as the system only reads each block in the inner table once. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Nested Loops Join: 11g Implementation . deptno = d. d. especially if sorting cannot be performed in memory. For each row in the first data set. The merge operation combines the two sorted row sources to retrieve every pair of rows that contain matching values for the columns used in the join predicate.deptno.Page 9 Oracle University and InfoTech (Pvt. Get the second data set.Sort Merge Join • • First and second row sources are sorted by the same sort key. for example). select /*+ USE_MERGE(d e) NO_INDEX(d) */ ename. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Sort Merge Join . The optimizer can select a sort merge join over a hash join for joining large amounts of data if any of the following conditions are true: Copyright © 2010. A sort merge join is executed as follows: 1. there is no concept of a driving table. Sorting could make this join technique expensive. Optimizer: Join Operators Chapter 7 . Get the first data set. using any access and filter predicates.deptno and ename > 'A' Merged MJ SJ SJ TAF IRS Sorted Independent Sort Merge Join In a sort merge join.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Sorted Sorted rows from both tables are merged. 3. When you perform a merge join.deptno. and sort it on the join columns. If one row source has already been sorted in a previous operation (there is an index on the join column. 2. and sort it on the join columns. dept d where e. All rights reserved. dname from emp e. using any access and filter predicates. the sort merge operation skips the sort on that row source. find the start point in the second data set and scan until you find a row that does not join. e. you must fetch all rows from the two row sources before to return the first row to the next operation. All rights reserved. • Sorts already required by previous operations. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010.The join condition between two tables is not an equijoin. <=. Note: Sort merge joins are useful when the join condition between two tables is an inequality condition (but not a nonequality). Optimizer: Join Operators Chapter 7 . Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. or >=. such as <.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 10 . >. d. The system then checks to see if there are any rows in that bucket. and uses the same hash function on the join column or columns to calculate the number of the relevant hash bucket. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hash Join . and are most useful when joining large amount of data. you must fetch all rows from the smallest row source to return the first row to next operation. Note: Hash joins are performed only for equijoins.deptno = d. using whatever access mechanism is most appropriate for acquiring the rows. A hash bucket is little more than a location that acts as the starting point for a linked list of rows from the build table. Optimizer: Join Operators Chapter 7 . So. when you perform a hash join. Copyright © 2010.deptno and ename like 'A%’ IS Probe Hash Join To perform a hash join between two row sources. The second row source is hashed and checked against the hash table.deptno.Hash Join HJ • The smallest row source is used to build a hash table. Oracle and/or its affiliates. e. dept d where e.deptno. A row belongs to a hash bucket if the bucket number matches the result that the system gets by applying an internal hashing function to the join column or columns of the row.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If there are no rows in the relevant bucket. the system reads the first data set and builds an array of hash buckets in memory. the system can immediately discard the row from the probe table. The system starts to read the second set of rows. If there are some rows in the relevant bucket. the system does an exact check on the join column or columns to see if there is a proper match.Page 11 Oracle University and InfoTech (Pvt. All rights reserved. This is known as probing the hash table. Any rows that survive the exact check can immediately be reported (or passed on to the next step in the execution plan). • Driving TAF TAR Build hash table in memory select /*+ USE_HASH(e d) */ ename. dname from emp e. the first row source is read and then for every row. it is perfectly acceptable to have one with single-row row source (guaranteed by a unique index.deptno. e.Page 12 Oracle University and InfoTech (Pvt. all the rows are returned from the other row source. Optimizer: Join Operators Chapter 7 .deptno. for example) joined to some other table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cartesian Join . Cartesian Join A Cartesian join is used when one or more of the tables does not have any join conditions to any other tables in the statement.Cartesian Join select ename. A Cartesian join can be seen as a nested loop with no elimination. Copyright © 2010. d. Note: Cartesian join is generally not desirable. Oracle and/or its affiliates. dept d where ename like 'A%'. The optimizer joins every row from one data source with every row from the other data source. dname from emp e. All rights reserved. creating the Cartesian product of the two sets. However.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. then stop search. Join operation types include the following : – – – – Join (Equijoin/Natural – Nonequijoin) Outer join (Full. Antijoin and semijoin are considered to be join types. Copyright © 2010. Find one match in the inner table. Left. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Join Types . even though the SQL constructs that cause them are subqueries. and Right) Semi join: EXISTS subquery Anti join: NOT IN subquery – Star join (Optimization) Join Types Join operation types include the following: • Join (equijoin and nonequijoin): Returns rows that match predicate join • Outer join: Returns rows that match predicate join and row when no match is found • Semi join: Returns rows that match the EXISTS subquery. Stop as soon as one match is found. • Star join: This is not a join type.Page 13 Oracle University and InfoTech (Pvt. • Anti join: Returns rows with no match in the NOT IN subquery.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Join Types • • A join operation combines the output from two row sources and returns one resulting row source. Optimizer: Join Operators Chapter 7 . Antijoin and semijoin are internal optimizations algorithms used to flatten subquery constructs in such a way that they can be resolved in a join-like way. but just a name for an implementation of a performance optimization to better handle the fact and dimension model. Oracle and/or its affiliates. Optimizer: Join Operators Chapter 7 .sal = s.sal.hisal.grade FROM emp e . Copyright © 2010. e. s. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Equijoins and Nonequijoins . Oracle and/or its affiliates. When a join condition relates two tables by an operator other than equality. Equijoins and Nonequijoins The join condition determines whether a join is an equijoin or a nonequijoin. use equijoins whenever possible. An example each of an equijoin and a nonequijoin are shown in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.sal between s.grade FROM emp e . Nonequijoins are less frequently used. s. To improve SQL efficiency.Page 14 Oracle University and InfoTech (Pvt. a hash join is not possible.ename.ename. Note: If you have a nonequijoin.Equijoins and Nonequijoins SELECT e. All rights reserved.hisal. Equijoins are the most commonly used. Statements that perform equijoins on untransformed column values are the easiest to tune. Nonequijoin Equijoin SELECT e.salgrade s WHERE e.sal.salgrade s WHERE e.hisal and s. An equijoin is a join with a join condition containing an equality operator. e. it is a nonequijoin. SELECT /*+ USE_NL(e d) */ d.empno. Consider EMP and DEPT. 2 3 Outer Joins The simple join is the most commonly used within the system.dname.deptno. the optimizer uses MERGE OUTER JOIN.empno. If rows are found.ename FROM emp e. look for all matching rows in EMP. this particular department would not appear in the output because there is no row that matches the join condition for that department. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Outer Joins . the hash table is read over once again. There may be a department that has no employees.deptno(+)=d. Merge Outer joins: By default. SELECT d.empno.d. After the inner table is exhausted.deptno.dname. By using the outer join. the missing department can be displayed. and then reads the Copyright © 2010.deptno. dept d WHERE e.deptno. and the right/inner table is used to probe the hash table. output DEPT values with these EMP values.e.e. If EMP and DEPT are joined together. the row is output and the entry in the hash table is marked as matched to a row. and any rows that are not marked as matched are output with null values for the EMP columns. The system hashes the table whose rows are not being preserved. it is placed against the table that has the missing join information.dname. DEPT EMP 10 20 20 10 30 20 40 30 10 1 SELECT /*+ USE_HASH(e d) */ d.d. but have much more specialized uses. In other words.e. The outer join operator is placed on the deficient side of the query. Other joins open up extra functionality.Outer Joins An outer join returns a row even if no match is found. dept d WHERE e. 2. output DEPT values with null values for the EMP columns. Outer join with nested loops: The left/driving table is always the table whose rows are being preserved (DEPT in the example).e. Hash Outer joins: The left/outer table whose rows are being preserved is used to build the hash table. Oracle and/or its affiliates.ename FROM emp e. For each row from DEPT. If none is found.e. All rights reserved.deptno(+)=d.deptno.deptno(+)=d. 3.d.e.deptno. Optimizer: Join Operators Chapter 7 .ename FROM emp e.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. 1. When a match is found. dept d WHERE e.Page 15 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. and right outer joins (not shown in the slide). Optimizer: Join Operators Chapter 7 . probing the hash table to see whether there was a row to join to. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED table whose rows are being preserved. Copyright © 2010.Note: You can also use the ANSI syntax for full. Oracle University and InfoTech (Pvt. left.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 16 . deptno=dept. Semijoins Semijoins return a result when you hit the first joining record. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 17 Oracle University and InfoTech (Pvt. A semijoin is an internal way of transforming an EXISTS subquery into a join. DEPT EMP 10 20 20 10 30 20 40 30 10 SELECT deptno. The optimizer may choose nested-loop. All rights reserved. dname FROM dept WHERE EXISTS (SELECT 1 FROM emp WHERE emp. However. or hash joins methods to perform semijoins as well. you cannot see this occur anywhere. In the above diagram. Optimizer: Join Operators Chapter 7 . Semijoins return rows that match an EXISTS subquery without duplicating rows from the left side of the predicate when multiple rows on the right side satisfy the criteria of the subquery.deptno). When the subquery is not unnested. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Semijoins . then returning it. This prevents scanning huge numbers of duplicate rows in a table when all you are interested in is if there are any matches. a similar result could be achieved by using a FILTER operation and scanning a row source until a match is found. Note: A semijoin can always use a Merge join.Semijoins Semijoins look only for the first match. Copyright © 2010. for each DEPT record. only the first matching EMP record is returned as a join result. EMP DEPT Antijoins Antijoins return rows that fail to match (NOT IN) the subquery at the right side. dname FROM dept WHERE deptno IS NOT NULL AND deptno NOT IN (SELECT /*+ HASH_AJ */ deptno FROM emp WHERE deptno IS NOT NULL).Page 18 Oracle University and InfoTech (Pvt. Although antijoins are mostly transparent to the user. Oracle and/or its affiliates. an antijoin can select a list of departments which do not have any employee. Copyright © 2010.Antijoins Reverse of what would have been returned by a join SELECT deptno. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Antijoins . Optimizer: Join Operators Chapter 7 . All rights reserved. if the HASH_AJ or NL_AJ hints are used and various required conditions are met. it is useful to know that these join types exist and could help explain unexpected performance changes between releases. The optimizer uses a merge antijoin algorithm for NOT IN subqueries by default.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. dname FROM dept WHERE deptno not in (SELECT deptno FROM emp). DEPT EMP 10 20 20 10 30 20 40 30 10 SELECT deptno. For example. the NOT IN uncorrelated subquery can be changed. However. All rights reserved. Cartesian c. Hash b.Page 19 Oracle University and InfoTech (Pvt.Quiz The _______ join is used when one or more of the tables do not have any join conditions to any other tables in the statement. a.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer: Join Operators Chapter 7 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Outer Answer: b Copyright © 2010. Non-equi d. Oracle and/or its affiliates. Cartesian c. a. All rights reserved. Outer Answer: d Copyright © 2010.Quiz The _______ join returns a row even if no match is found. Semi d. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Oracle and/or its affiliates.Page 20 Oracle University and InfoTech (Pvt. Hash b. Optimizer: Join Operators Chapter 7 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Optimizer: Join Operators Chapter 7 .Page 21 Oracle University and InfoTech (Pvt. Outer Answer: c Copyright © 2010. Oracle and/or its affiliates.Quiz The _______ join looks only for the first match. Cartesian c.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . a. Hash b. Semi d. Smallest c. Biggest b. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Optimizer: Join Operators Chapter 7 . All rights reserved. a. Sorted d.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz In a hash join. the _______row source is used to build a hash table. Unsorted Answer: b Copyright © 2010.Page 22 Oracle University and InfoTech (Pvt. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Optimizer: Join Operators Chapter 7 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary Copyright © 2010. you should have learned to: • Describe the SQL operators for joins • List the possible access paths Oracle University and InfoTech (Pvt.Summary In this lesson.Page 23 . Page 24 . All rights reserved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 7: Overview Copyright © 2010.Practice 7: Overview This practice covers the following topics: • Using different access paths for better optimization • Using the result cache Oracle University and InfoTech (Pvt. Optimizer: Join Operators Chapter 7 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Other Optimizer Operators Chapter 8 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 8 Copyright © 2010.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.) Ltd use only Other Optimizer Operators .Page 1 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Other Optimizer Operators Copyright © 2010. Other Optimizer Operators Chapter 8 .) Ltd use only Other Optimizer Operators .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 2 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved. Objectives After completing this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .Page 3 Oracle University and InfoTech (Pvt. Other Optimizer Operators Chapter 8 . you should be able to: • Describe SQL operators for: – – – – – • Clusters In-List Sorts Filters Set Operations Result Cache operators Objectives This lesson helps you understand the execution plans that use common operators of other access methods. All rights reserved. Copyright © 2010. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. To find or store a row in a hash cluster. Other Optimizer Operators Chapter 8 . All rights reserved. the resulting hash value corresponds to a data block in the cluster. Such an index contains an entry for each cluster key value. When you cluster the ORDERS and ORDER_ITEMS tables.Page 4 Oracle University and InfoTech (Pvt.. Oracle and/or its affiliates. To locate a row in a cluster.. the system applies the hash function to the row’s cluster key value. the system physically stores all rows for each order from both the ORDERS and ORDER_ITEMS tables in the same data blocks. Cluster index: A cluster index is an index defined specifically for a cluster. For example. Hash clusters: Hashing is an optional way of storing table data to improve the performance of data retrieval. A cluster is a group of tables that share the same data blocks because they share common columns and are often used together. which the system then reads or writes on behalf of the issued statement. The system physically stores the rows of a table in a hash cluster and retrieves them according to the results of a hash function. The key of a hash cluster (just as the key of an index cluster) can be a single column or composite key.Clusters ORD_NO PROD ----------101 A4102 102 A2091 102 G7830 102 N9587 101 A5675 101 W0824 QTY -----20 11 20 26 19 10 ORD_NO -----101 102 . the system accesses a given row with a minimum of two I/Os. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Clusters . you create a hash cluster and load tables into the cluster. Copyright © 2010. - ORD_DT CUST_CD ----------05-JAN-97 R01 07-JAN-97 N45 Unclustered ORDERS and ORDER_ITEMS tables Cluster Key (ORD_NO) 101 ORD_DT CUST_CD 05-JAN-97 R01 PROD QTY A4102 20 A5675 19 W0824 10 102 ORD_DT CUST_CD 07-JAN-97 N45 PROD QTY A2091 11 G7830 20 N9587 26 Clustered ORDERS and ORDER_ITEMS tables Clusters Clusters are an optional method for storing table data.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Therefore. To use hashing. the ORDERS and ORDER_ITEMS table share the ORDER_ID column. which points to the data block associated with that cluster key value. the cluster index is used to find the cluster key value. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Hash clusters are a better choice than using an indexed table or index cluster when a table is queried frequently with equality queries. Oracle and/or its affiliates. All rights reserved.Page 5 . Other Optimizer Operators Chapter 8 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Oracle University and InfoTech (Pvt. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Hash clusters allow row data from one or more tables that share a cluster key value to be stored in same block. Index clusters are useful when the size of the tables is not known in advance (For example: Creating a new table rather than converting an existing one whose size is stable) because a cluster bucket is only created after a cluster key value is used. Oracle and/or its affiliates. Note that full table scans do not perform well on a table in a multiple table cluster as it has more blocks than the table would have if created as a heap table. You can locate these rows using a system-provided or userprovided hashing function or using the cluster key value assuming that this is already evenly distributed making the access to a row faster than using index clusters. All rights reserved. Copyright © 2010.Page 6 Oracle University and InfoTech (Pvt. Other Optimizer Operators Chapter 8 . Table rows with the same cluster key values hash into the same cluster buckets and can be stored in the same block or small group of blocks. You can locate these rows using a cluster index. They are also useful for all filter operations or searches. which has one entry per cluster key value and not for each row. The rows with the same key are in a small group of blocks. This means that in an index cluster the clustering factor is very good and provides clustering for data from multiple tables sharing the same join key. Therefore. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED When Are Clusters Useful? . the index is smaller and less costly to access for finding multiple rows. The smaller index and smaller group of blocks reduce the cost of access by reducing block visits to the buffer cache.When Are Clusters Useful? • Index cluster: – Tables always joined on the same keys – The size of the table is not known – In any type of searches • Hash cluster: – Tables always joined on the same keys – Storage for all cluster keys allocated initially – In either equality (=) or nonequality (<>) searches When Are Clusters Useful? • Index clusters allow row data from one or more tables that share a cluster key value to be stored in same block. All rights reserved. billing. full scans are more expensive for the same reason. Oracle and/or its affiliates. full scans.Page 7 Oracle University and InfoTech (Pvt. • Sorted hash clusters are designed to reduce costs of accessing ordered data by using a hashing algorithm on the hash key. Other Optimizer Operators Chapter 8 . Hash clusters allocate all the storage for all the hash buckets when the cluster is created.When Are Clusters Useful? • Single-table hash cluster: – Fastest way to access a large table with an equality search • Sorted hash cluster: – Only used for equality search – Avoid sorts on batch reporting – Avoid overhead probe on the branch blocks of an IOT When Are Clusters Useful? (continued) • This means that in a hash cluster the clustering factor is also very good and a row may be accessed by its key with one block visit only and without needing an index. eliminating the need for a sort to process the order by clause. thereby. Like index clusters if they contain multiple tables. cost as much as they would in a heap table. but are optimized in the block structures for access to a single table. All the rows that match on a particular hash key (For example: Account number) are stored in the cluster in the order of the sort key or keys (For example: Phone calls). Copyright © 2010. They also do not perform well other than on equality searches or nonequality searches. so they may waste space. These clusters are very good for batch reporting.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. thereby providing the fastest possible access to a row other than by using a rowid filter. • Single-table hash clusters are similar to a hash cluster. Accessing the first row matching the hash key may be less costly than using an IOT for a large table because it saves the cost of a B*-tree probe. if they happen. and so on. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED When Are Clusters Useful? . As they only have one table. To perform a hash scan. In this case. Note: You see examples of how to create clusters in the labs for this lesson.Page 8 Oracle University and InfoTech (Pvt. the system first obtains the ROWID of one of the selected rows by scanning the cluster index. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cluster Access Path: Examples . all rows with the same cluster key value are stored in the same data block. all rows that have the same cluster key value. In an indexed cluster. Other Optimizer Operators Chapter 8 . To perform a cluster scan. The system then scans the data blocks containing rows with that hash value. In the top one. all rows with the same hash value are stored in the same data block. The system then locates the rows based on this ROWID. The second one assumes that a cluster index was used to cluster both the EMP and DEPT tables. the system first obtains the hash value by applying a hash function to a cluster key value specified by the statement. Copyright © 2010. In a hash cluster. based on a hash value.Cluster Access Path: Examples Cluster Access Path: Examples The example in the slide shows you two different cluster access paths. from a table stored in an indexed cluster. a hash scan is used to locate rows in a hash cluster. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a cluster scan is used to retrieve. ORDER BY: For these operators HASH operator: – GROUP BY: For this operator – UNIQUE: Equivalent to SORT UNIQUE • If you want ordered results. Copyright © 2010. Operations such as COUNT and MIN are shown as SORT AGGREGATE. HASH GROUP BY hashes a set of rows into groups for a query with a GROUP BY clause. SORT GROUP BY is used when aggregates are computed for different groups in the data.Page 9 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Sorting Operators . if the rows need to be sorted by the join key. The sort is required to separate the rows into different groups. Oracle and/or its affiliates. SORT UNIQUE sorts output rows to remove duplicates. All rights reserved. always use ORDER BY. It occurs if a user specifies a DISTINCT clause or if an operation requires unique values for the next step. Sorting Operators Sort operations result when users specify an operation that requires a sort.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It retrieves a single row that is the result of applying a group function to a group of selected rows. Commonly encountered operations include the following: • • • • • • SORT AGGREGATE does not involve a sort.Sorting Operators • SORT operator: – – – – • AGGREGATE: Single row from group function UNIQUE: To eliminate duplicates JOIN: Precedes a merge join GROUP BY. Other Optimizer Operators Chapter 8 . SORT ORDER BY is required when the statement specifies an ORDER BY that cannot be satisfied by one of the indexes. SORT JOIN happens during a sort-merge join. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. GROUP BY. Note: Several SQL operators cause implicit sorts (or hashes since Oracle Database 10g.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. MINUS. If you want to have rows ordered. such as DISTINCT. use the ORDER BY clause. It occurs if a user specifies a DISTINCT clause or if an operation requires unique values for the next step.HASH UNIQUE hashes a set of rows to eliminate duplicates. This is similar to SORT UNIQUE. UNION. All rights reserved. do not rely on these SQL operators to return ordered rows. However. Release 2). Oracle and/or its affiliates. Oracle University and InfoTech (Pvt.Page 10 . and INTERSECT. Other Optimizer Operators Chapter 8 . The system simply buffers the data. Buffer Sort Operator The BUFFER SORT operator uses a temporary table or a sort area in memory to store intermediate data. dept WHERE ename like 'A%'. dept. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Buffer Sort Operator . in the User Global Area (UGA) or Program Global Area (PGA). but without sorting the data. Copyright © 2010. All rights reserved.deptno.”) So BUFFER SORT uses the buffering mechanism of a traditional sort. • A buffer sort uses UGA. including the swap to disk when not enough sort area memory is available. The difference between a temporary table and a buffer sort is as follows: • A temporary table uses System Global Area (SGA).deptno.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. (See “Cartesian Join.Page 11 Oracle University and InfoTech (Pvt. The BUFFER SORT operator is needed if there is an operation that needs all the input data before it can start. emp. but it does not do the sort itself. Other Optimizer Operators Chapter 8 . However.Buffer Sort Operator SELECT ename. the data is not necessarily sorted. The whole sort mechanism is reused. to avoid multiple table scans against real data blocks. dname FROM emp. which is scanned for each value in the list. and the optimizer finds a selective index for that column. 7902).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. because it is more efficient. this is significantly more efficient. it is a function that uses an index. Also. except for one additional step. and every value is executed separately. If there are multiple OR clauses using the same index. the optimizer selects this operation rather than CONCATENATION or UNION ALL. Oracle and/or its affiliates. Inlist Iterator It is used when a query contains an IN clause with values or multiple equality predicates on the same column linked with ORs. The execution plan is identical to the result of a statement with an equality clause instead of IN. An alternative handling is UNION ALL of each value or a FILTER of the values against all the rows. 7900. you iterate the index probe over two values: 1 and 2. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Inlist Iterator .Inlist Iterator Every value executed separately deptno=1 deptno=2 SELECT * FROM emp WHERE empno IN (7876. In the example in the slide. The INLIST ITERATOR operator iterates over the enumerated value list. You can view this operator as a FOR LOOP statement in PL/SQL.Page 12 Oracle University and InfoTech (Pvt. The optimizer uses an INLIST ITERATOR when an IN clause is specified with values. Copyright © 2010. The extra step occurs when INLIST ITERATOR feeds the equality clause with unique values from the list. All rights reserved. Other Optimizer Operators Chapter 8 . Views can also be left as standalone views and selected from directly as in the case of a table. Basically. If a number of conditions are met. select v.*. An inline view is basically a query within the FROM clause of your statement. Other Optimizer Operators Chapter 8 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Sometimes a view cannot be merged and must be executed independently in a separate query block. When views are referenced in a query.View Operator create view V as select /*+ NO_MERGE */ DEPTNO. In this case. select * from V.d. you can see the VIEW operator. sum(sal) SUM_SAL from emp group by deptno) v. The VIEW keyword indicates that the view is executed as a separate query block. When a view is not merged. Copyright © 2010. The second example in the slide shows a nonmergeable inline view.deptno=d. this operator collects all rows from a query block before they can be processed by higher operations in the plan. Oracle and/or its affiliates. dept d where v. All rows from the view are returned. they can be merged into the main query. and the next operation can be done. A view simply gives a name to this set of data.Page 13 Oracle University and InfoTech (Pvt. The view operation is executed separately. For example.dname from (select DEPTNO.deptno. you can also see the VIEW operator in the explain plan. sal from emp . This means that the view text is rewritten as a join with the other tables in the query. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED View Operator . the system can handle them in two ways. All rights reserved. Predicates can also be pushed into or pulled out of the views as long as certain conditions are met. View Operator Each query produces a variable set of data in the form of a table. views containing GROUP BY functions cannot be merged. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Count Stop Key Operator . and there are no indexes. Copyright © 2010. If the value appears very frequently in the table.Page 14 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. It terminates the current operation when the count is reached. Count Stop Key Operator COUNT STOPKEY limits the number of rows returned. the system has to read most of the table’s blocks before reaching the count.Count Stop Key Operator SELECT count(*) FROM (SELECT /*+ NO_MERGE */ * FROM emp WHERE empno ='1' and rownum < 10). the count is reached quickly. The limitation is expressed by the ROWNUM expression in the WHERE clause. If the value is very infrequent. Note: The cost of this operator depends on the number of occurrences of the values you try to retrieve. Other Optimizer Operators Chapter 8 . Min/Max and First Row Operators SELECT MIN(quantity_on_hand) FROM INVENTORIES WHERE quantity_on_hand < 500. it is assumed that there is an index on the quantity_on_hand column. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Min/Max and First Row Operators . All rights reserved. Other Optimizer Operators Chapter 8 . Oracle and/or its affiliates. Min/Max and First Row Operators FIRST ROW retrieves only the first row selected by a query. In the example in the slide. This is an optimization introduced in Oracle 8i and it works with the index range scan and the index full scan. Copyright © 2010.Page 15 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It stops accessing the data after the first value is returned. Other N-Array Operations • • • • • FILTER CONCATENATION UNION ALL/UNION INTERSECT MINUS Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Other N-Array Operations Copyright © 2010.Page 16 . All rights reserved. Other Optimizer Operators Chapter 8 . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. sum(sal) SUM_SAL FROM emp GROUP BY deptno HAVING sum(sal) > 9000. after DEPT rows are fetched. EMP is accessed by its index on DEPTNO. FILTER applies to the groups that are created by the GROUP BY operation. This operation is done as many times as the number of rows in DEPT. for each row. 2 FILTER Operations A FILTER operation is any operation that discards rows returned by another step. Other Optimizer Operators Chapter 8 . and for each row from DEPT. In the example 2. but is not involved in retrieving the rows itself.FILTER Operations • • • Accepts a set of rows Eliminates some of them Returns the rest SELECT deptno. FILTER is almost used in the same way as NESTED LOOPS. The FILTER discards rows for the inner query returned at least one row (select 1 from emp e where e.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In the example 1. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED FILTER Operations . 1 SELECT deptno.deptno). dname FROM dept d WHERE NOT EXISTS (select 1 from emp e where e. The FILTER operation is applied. including subqueries and single table predicates. All sorts of operations can be filters. Oracle and/or its affiliates. Copyright © 2010. DEPT is accessed once.deptno) is TRUE.Page 17 Oracle University and InfoTech (Pvt.deptno=d.deptno=d. Other Optimizer Operators Chapter 8 .DEPTNO = 1 AND NOT row in Branch 1 The LNNVL function is generated by the OR clause to process this negation. it appends a negation of the previous components (LNNVL): CONCATENATION . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Concatenation Operation .Concatenation Operation SELECT * FROM emp WHERE deptno=1 or sal=2. Copyright © 2010.filter(LNNVL("SAL"=2)) 5 . However. Oracle and/or its affiliates.BRANCH 1 . OR does not return duplicate rows.BRANCH 2 .access("DEPTNO"=1) Concatenation Operation CONCATENATION concatenates the rows returned by two or more row sets.SAL=2 .Page 18 Oracle University and InfoTech (Pvt. if the predicate is NULL or FALSE. so for each component after the first. The LNNVL() function returns TRUE. -------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | -------------------------------------------------------------------| 0 | SELECT STATEMENT | | 8 | 696 | | 1 | CONCATENATION | | | | | 2 | TABLE ACCESS BY INDEX ROWID| EMP | 4 | 348 | | 3 | INDEX RANGE SCAN | I_SAL | 2 | | | 4 | TABLE ACCESS BY INDEX ROWID| EMP | 4 | 348 | | 5 | INDEX RANGE SCAN | I_DEPTNO | 2 | | -------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------3 . It is used with OR expansions.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This works like UNION ALL and does not remove duplicate rows. Note: The explain plan in the slide is from Oracle Database 11g Release 1. All rights reserved.access("SAL"=2) 4 . So filter (LNNVL(SAL=2)) returns all rows for which SAL != 2 or SAL is NULL. INTERSECT. ALL preserves duplicates and DISTINCT removes them. Similar to INTERSECT processing. Other Optimizer Operators Chapter 8 . MINUS . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED UNION [ALL]. eliminating duplicates. it is match-and-exclude. Subrow sources are executed or optimized individually. The expensive sort operation is not necessary. but not in the second. MINUS SQL handles duplicate rows with an ALL or DISTINCT modifier in different places in the language.UNION [ALL]. All rights reserved. MINUS UNION UNION ALL SORT UNIQUE UNION-ALL INDEX FULL SCAN INDEX FAST FULL SCAN INTERSECT INTERSECTION SORT UNIQUE NOSORT INDEX FULL SCAN SORT UNIQUE INDEX FAST FULL SCAN MINUS MINUS SORT UNIQUE NOSORT INDEX FULL SCAN SORT UNIQUE INDEX FAST FULL SCAN 1 2 3 3 4 1 5 1 2 1 2 2 3 3 4 3 3 4 3 3 4 5 5 5 UNION [ALL]. However. MINUS: Operation accepting two sets of rows and returning rows appearing in the first set. instead of matchand-return. UNION ALL: Operation accepting two sets of rows and returning the union of the sets. Subrow sources are executed or optimized individually. This is very similar to sort-merge-join processing: Full rows are sorted and matched. eliminating duplicates.Page 19 Oracle University and InfoTech (Pvt. UNION: Operation accepting two sets of rows and returning the union of the sets. Subrow sources are executed or optimized individually. and not eliminating duplicates. INTERSECT.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. eliminating duplicates. Rows retrieved are concatenated and sorted to eliminate duplicate rows. Here is a quick description of the possible SQL set operations: • • • • INTERSECTION: Operation accepting two sets of rows and returning the intersection of the sets. Oracle and/or its affiliates. INTERSECT. Use UNION ALL if you know you do not have to deal with duplicates. This introduces the ResultCache operator into the execution plan for the query. the query is executed.Result Cache Operator SELECT /*+ RESULT_CACHE */ deptno. If it does not yet exist in the cache. you must explicitly specify the RESULT_CACHE hint in your query. This is mostly the case for data warehousing applications. Oracle and/or its affiliates. and you do not want to store the result of a query in the result cache. The query results stored in this cache become invalid when data in the database objects that are accessed by the query is modified. A dedicated memory buffer stored in the shared pool can be used for storing and retrieving the cached results. When you execute the query. All rights reserved. Although the SQL query cache can be used for any query. you must then use the NO_RESULT_CACHE hint in your query.Page 20 Oracle University and InfoTech (Pvt. AVG(sal) FROM emp GROUP BY deptno. and is also stored in the result cache memory. If it exists.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the result is returned as output. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Result Cache Operator . Other Optimizer Operators Chapter 8 . If you want to use the query result cache and the RESULT_CACHE_MODE initialization parameter is set to MANUAL. the ResultCache operator looks up the result cache memory to check whether the result for the query already exists in the cache. If the RESULT_CACHE_MODE initialization parameter is set to FORCE. Copyright © 2010. good candidate statements are the ones that need to access a very high number of rows to return only a fraction of them. the result is retrieved directly out of the cache. Result Cache Operator The SQL query result cache enables explicit caching of query result sets and query fragments in database memory. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz Hash clusters are a better choice than using an indexed table or index cluster when a table is queried frequently with equality queries. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .Page 21 Oracle University and InfoTech (Pvt. True b. False Answer: a Copyright © 2010. All rights reserved. Other Optimizer Operators Chapter 8 . a. Oracle and/or its affiliates. Other Optimizer Operators Chapter 8 . d. Buffer Sort Operator Inlist Min/Max N-Array Oracle University and InfoTech (Pvt. a.Quiz The ________ operator uses a temporary table to store intermediate data.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. c. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz Answer: a Copyright © 2010.Page 22 . b. Page 23 . All rights reserved. c. Other Optimizer Operators Chapter 8 . d. b. Buffer Sort Operator Inlist Min/Max N-Array Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz Answer: b Copyright © 2010. 7900. Oracle and/or its affiliates. a. 7902).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz The following query uses the ________ operator: SELECT * FROM emp WHERE empno IN (7876. True b. a.Page 24 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Oracle and/or its affiliates. Other Optimizer Operators Chapter 8 .Quiz A FILTER operation retrieves rows returned by another statement. False Answer: b Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Other Optimizer Operators Chapter 8 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. you should have learned to: • Describe SQL operators for: – – – – – • Clusters In-List Sorts Filters Set Operations Result Cache operators Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Summary In this lesson.Page 25 Oracle University and InfoTech (Pvt. All rights reserved. Other Optimizer Operators Chapter 8 . Oracle and/or its affiliates.Page 26 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 8: Overview .Practice 8: Overview This practice covers the following topics: • Using different access paths for better optimization – Case 14 to case 16 • Using the result cache Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Page 1 Oracle University and InfoTech (Pvt. Case Study: Star Transformation Chapter 9 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 9 Copyright © 2010. All rights reserved. Oracle and/or its affiliates.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.) Ltd use only Case Study: Star Transformation . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Case Study: Star Transformation .Case Study: Star Transformation Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 2 Oracle University and InfoTech (Pvt. Case Study: Star Transformation Chapter 9 . Page 3 Oracle University and InfoTech (Pvt. Case Study: Star Transformation Chapter 9 .Objectives After completing this lesson. you should be able to: • Define a star schema • Show a star query plan without transformation • Define the star transformation requirements • Show a star query plan after transformation Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . All rights reserved. and the cust_id. the sales fact table contains the quantity_sold. A star schema is characterized by one or more very large fact tables that contain the primary information in the data warehouse and a number of much smaller dimension tables (or lookup tables). and promotions. The cost-based optimizer (CBO) recognizes star queries and generates efficient execution plans for them. in the Sales History schema. All rights reserved. The products dimension table.The Star Schema Model Dimension/Lookup table PRODUCTS TIMES Fact table Keys PROD_ID PROD_NAME PROD_DESC PROD_ID TIME_ID CHANNEL_ID TIME_ID DAY_NAME CALENDAR_YEAR SALES CUSTOMERS AMOUNT_SOLD QUANTITY_SOLD CHANNELS CUST_ID CHANNEL_ID CUST_GENDER CUST_CITY Measures CHANNEL_DESC CHANNEL_CLASS Fact >> Dimension The Star Schema Model The star schema is the simplest data warehouse schema. The dimension tables are customers. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The Star Schema Model . Each dimension table is joined to the fact table using a primary key to foreign key join. Case Study: Star Transformation Chapter 9 . times. channel_id. prod_id. For example. each of which contains information about the entries for a particular attribute in the fact table. but the dimension tables are not joined to each other. with points radiating from a central table. and cost measures. Note: It is easy to generalize this model to include more than one fact table. contains information about each product number that appears in the fact table.Page 4 Oracle University and InfoTech (Pvt. Copyright © 2010. time_id. channels.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. products. Oracle and/or its affiliates. A star query is a join between a fact table and a number of dimension tables. A typical fact table contains keys and measures. and promo_id keys. amount. for example. The center of the star consists of one or more fact tables and the points of the star are the dimension tables. It is called a star schema because the entity-relationship diagram of this schema resembles a star. Note: It is suggested that you select a star schema over a snowflake schema unless you have a clear reason to choice the snowflake schema. All rights reserved. the dimension data has been grouped into multiple tables instead of one large table. That is. a product_category table.Page 5 Oracle University and InfoTech (Pvt. Case Study: Star Transformation Chapter 9 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. While this saves space. Copyright © 2010.The Snowflake Schema Model PRODUCTS TIMES SALES CUSTOMERS CHANNELS COUNTRIES The Snowflake Schema Model The snowflake schema is a more complex data warehouse model than a star schema. Oracle and/or its affiliates. and is a type of star schema. It is called a snowflake schema because the diagram of the schema resembles a snowflake. Snowflake schemas normalize dimensions to eliminate redundancy. and a product_manufacturer table in a snowflake schema or. you can normalize the customers table using the countries table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The Snowflake Schema Model . as shown in the slide. a product dimension table in a star schema might be normalized into a products table. For example. The result is more complex queries and reduced query performance. it increases the number of dimension tables and requires more foreign key joins. it is supposed that the sales table of the Sales History schema has bitmap indexes on the time_id.cust_city.channel_id = ch. SUM(s.Star Query: Example SELECT ch.time_id = t. and cust_id columns. Copyright © 2010.channels ch 0 1 1 0 WHERE s. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Query: Example .channel_desc IN ('Internet'. channel_id.channel_class. t. Case Study: Star Transformation Chapter 9 .calendar_quarter_desc IN ('1999-Q1'. Oracle and/or its affiliates.Page 6 Oracle University and InfoTech (Pvt.cust_state_province = 'CA' AND ch. c.cust_id = c.'1999-Q2') GROUP BY ch. All rights reserved.time_id AND 0 1 1 0 s. Star Query: Example Consider the star query in the slide.'Catalog') AND t.channel_class.channel_id AND c. For the star transformation to operate. c.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.cust_id AND 0 1 1 0 s.cust_city.calendar_quarter_desc.amount_sold) sales_amount FROM sales s. t.calendar_quarter_desc.times t.customers c. filter("C"."CALENDAR_QUARTER_DESC"='1999-Q1' OR "T"."TIME_ID") 5 ."CUST_ID") 7 . Copyright © 2010."CUST_STATE_PROVINCE"='CA') Execution Plan Without Star Transformation Before you see the benefits of a star transformation.filter("T".access("S".access("S". Oracle and/or its affiliates."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_ID") 3 . This results in a very large number of rows that can only be trimmed down by the other parent joins in the execution plan. The fundamental issue with the plan in the slide is that the query always starts joining the SALES table to a dimension table. Case Study: Star Transformation Chapter 9 . you should review how a join on a star schema is processed without their benefits.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Execution Plan Without Star Transformation Hash Join CHANNELS Hash Join TIMES CUSTOMERS Hash Join SALES ------------------------------------------| Id | Operation | Name | ------------------------------------------| 0 | SELECT STATEMENT | | | 1 | HASH GROUP BY | | |* 2 | HASH JOIN | | |* 3 | TABLE ACCESS FULL | CHANNELS | |* 4 | HASH JOIN | | |* 5 | TABLE ACCESS FULL | TIMES | |* 6 | HASH JOIN | | |* 7 | TABLE ACCESS FULL| CUSTOMERS | | 8 | TABLE ACCESS FULL| SALES | ------------------------------------------Predicate Information (by operation id): -------------------------------------------2 .filter("CH"."TIME_ID"="T". All rights reserved."CALENDAR_QUARTER_DESC"='1999-Q2') 6 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Execution Plan Without Star Transformation ."CUST_ID"="C"."CHANNEL_DESC"='Internet') 4 .access("S"."CHANNEL_ID"="CH".Page 7 Oracle University and InfoTech (Pvt. Star Transformation • • Create bitmap indexes on fact tables foreign keys. Star Transformation To get the best possible performance for star queries. Case Study: Star Transformation Chapter 9 . The system’s CBO automatically selects star transformation where appropriate. The end user never needs to know any of the details about the star transformation. The optimizer creates an execution plan that processes a star query using two basic phases: • The first phase retrieves exactly the necessary rows from the fact table (the result set). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation . Copyright © 2010. • • • Requires at least two dimensions and one fact table Gather statistics on all corresponding objects. • The STAR_TRANSFORMATION_ENABLED initialization parameter should be set to TRUE. the majority of the star queries that run in the data warehouse use a query execution strategy known as star transformation.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. it is important to follow some basic guidelines: • A bitmap index should be built on each of the foreign key columns of the fact table or tables. When a data warehouse satisfies these conditions. identify interesting fact rows using bitmap indexes based on dimensional filters. Because this retrieval utilizes bitmap indexes. Star transformation provides very efficient query performance for star queries. All rights reserved. Carried out in two phases: – First. This enables an important optimizer feature for star queries. Oracle and/or its affiliates. – Join them to the dimension tables. It is set to FALSE by default for backwards compatibility.Page 8 Oracle University and InfoTech (Pvt. Star transformation is a powerful optimization technique that relies on implicitly rewriting (or transforming) the SQL of the original star query. it is very efficient. Set STAR_TRANSFORMATION_ENABLED to TRUE. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. Oracle and/or its affiliates.Page 9 .The second phase joins this result set to the dimension tables. Oracle University and InfoTech (Pvt. All rights reserved. Note: At least three tables are used in the query (two dimensions and one fact). Case Study: Star Transformation Chapter 9 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This operation is called a semijoin. Oracle and/or its affiliates. Queries containing antijoined tables are not transformed. Queries referring to remote fact tables are not transformed. Queries referring to unmerged nonpartitioned views are not transformed.Page 10 Oracle University and InfoTech (Pvt. • Antijoined tables • Tables that are already used as a dimension table in a subquery • Tables that are really unmerged views. • Remote fact tables.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Star Transformation: Considerations • • • • Queries containing bind variables are not transformed. remote dimension tables are allowed in the subqueries that are generated. All rights reserved. Case Study: Star Transformation Chapter 9 . which are not view partitions Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation: Considerations . There must be a bitmap index on a fact table column for the optimizer to generate a subquery for it. However. Star Transformation: Considerations Star transformation is not supported for tables with any of the following characteristics: • Queries with a table hint that is incompatible with a bitmap access path • Queries that contain bind variables • Tables with too few bitmap indexes. In the first phase. the system uses the filters against the dimensional tables to retrieve the dimensional primary keys. Copyright © 2010. Case Study: Star Transformation Chapter 9 . That is. Then the system uses those primary keys to probe the bitmap indexes on the foreign key columns of the fact table to identify and retrieve only the necessary rows from the fact table. which match those filters.'Catalog')).Page 11 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation: Rewrite Example .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the system retrieves the result set from the sales table by using essentially the rewritten query in the slide.'1999-Q2')) AND 0 1 1 0 cust_id IN (SELECT cust_id FROM customers WHERE cust_state_province = 'CA') 0 1 1 0 AND channel_id IN(SELECT channel_id FROM channels WHERE channel_desc IN ('Internet'. All rights reserved.amount_sold FROM sales s 0 1 1 0 WHERE time_id IN (SELECT time_id FROM times WHERE calendar_quarter_desc IN('1999-Q1'. Star Transformation: Rewrite Example The system processes the query seen earlier in two phases.Star Transformation: Rewrite Example Phase 1 SELECT s. Note: The SQL in the slide is a theoretical SQL statement that represents what goes on in phase I. The system recommends that you enable automatic sizing of SQL working areas by setting PGA_AGGREGATE_TARGET instead. like in t. BITMAP KEY ITERATION makes each key coming from its left input a lookup key for the index on its right input.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. it probes the corresponding fact bitmap index and fetches the corresponding bitmap. and for each corresponding row. Note that its left input supplies join keys from the dimension table in this case.'1999-Q2') from the example in the previous slide. BITMAP_MERGE_AREA_SIZE is retained for backward compatibility. Case Study: Star Transformation Chapter 9 . This merge operation produces one bitmap that can be described as representing the rows of the fact table that join with the rows of interest from the dimension table. and returns all bitmaps fetched by that index. The last step in this tree merges all fetched bitmaps from the previous steps. Based on the corresponding dimension filter predicates. The system does not recommend using the BITMAP_MERGE_AREA_SIZE parameter unless the instance is configured with the shared server option. BITMAP KEY ITERATION Dimension Table Access Fact Table Bitmap Access Retrieving Fact Rows from One Dimension The slide shows retrieval of fact table rows using only one dimension table. Note: BITMAP_MERGE_AREA_SIZE plays an important role in tuning the performance of this operation when using the shared server mode. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Retrieving Fact Rows from One Dimension . the system scans the dimension table.Page 12 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved.Retrieving Fact Rows from One Dimension Phase 1 BITMAP MERGE One bitmap is Produced.calendar_quarter_desc IN ('1999-Q1'. Case Study: Star Transformation Chapter 9 . the system must intersect all generated bitmaps. only fact bitmap indexes and dimension tables were used. Note: Until now. This is achieved by performing a very efficient BITMAP AND operation on all the bitmaps generated for each dimension. Oracle and/or its affiliates. the steps mentioned in the previous slide are repeated for each dimension table. the system must convert the generated bitmap to a rowids set. To identify all rows from the fact table that are of interest. This is to eliminate fact rows that join with one dimension.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. To further access the fact table. BITMAP AND MERGE 1 … MERGE … MERGE n Retrieving Fact Rows from All Dimensions During the first phase. The resulting bitmap can be described as representing the rows from the fact table that are known to join with all the qualified dimension rows.Retrieving Fact Rows from All Dimensions Intermediate Result Set (IRS) Phase 1 BITMAP Conversion To Rowids Multiple bitmaps are ANDed together. All rights reserved. but not with all of them. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Retrieving Fact Rows from All Dimensions . So each BITMAP MERGE in the plan generates a bitmap for a single dimension table.Page 13 Oracle University and InfoTech (Pvt. Copyright © 2010. In this phase. with the dimension tables data used to group the rows and pertaining to the query’s select list. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Joining the Intermediate Result Set with Dimensions . Copyright © 2010. Case Study: Star Transformation Chapter 9 .Page 14 Oracle University and InfoTech (Pvt. the system enters phase 2 of the star transformation algorithm.Joining the Intermediate Result Set with Dimensions Phase 2 Hash Join Dimension n Table Access Hash Join Dimension i Table Access Hash Join Dimension 1 Table Access Fact Table Access From IRS Joining the Intermediate Result Set with Dimensions After the result set is determined. Note that the graphic in the slide shows that a hash join is performed between the fact table and its dimensions. it is needed to join the sales data.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Although a hash join is statistically the most-used technique to join rows in a star query. this might not be always true. Oracle and/or its affiliates. as this is evaluated by the CBO. corresponding to the result set. Copyright © 2010. only the channels and times dimensions are shown. All rights reserved. Oracle and/or its affiliates.Star Transformation Plan: Example 1 SORT GROUP BY HASH JOIN HASH JOIN TABLE ACCESS BY INDEX ROWID SALES BITMAP CONVERSION TO ROWIDS BITMAP AND BITMAP MERGE BITMAP KEY ITERATION BUFFER SORT TABLE ACCESS FULL CHANNELS BITMAP INDEX RANGE SCAN SALES_CHANNELS_BX BITMAP MERGE BITMAP KEY ITERATION BUFFER SORT TABLE ACCESS FULL TIMES BITMAP INDEX RANGE SCAN SALES_TIMES_BX … TABLE ACCESS FULL CHANNELS TABLE ACCESS FULL TIMES Star Transformation Plan: Example 1 This is a possible plan to answer the query shown in the “Execution Plan Without Star Transformation” section. Note that for formatting purposes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation Plan: Example 1 .Page 15 Oracle University and InfoTech (Pvt. It is easy to generalize the case for n dimensions. Note: It is supposed that sales is not partitioned.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Case Study: Star Transformation Chapter 9 . Page 16 Oracle University and InfoTech (Pvt. the system might decide to create a temporary table and use it instead of accessing the same dimension table twice. The name of those temporary tables is system-generated and varies.Star Transformation: Further Optimization • • • • In a star transformation execution plan. Note: Temporary tables are not used by star transformation under the following conditions: • The database is in read-only mode. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation: Further Optimization ."CUST_STATE_PROVINCE"='CA') Star Transformation: Further Optimization When you look at the previous execution plan. you see that each dimension table is accessed twice—once during the first phase. Oracle and/or its affiliates. the system might decide to create temporary tables containing information needed for both phases. consisting of the result set for both the predicate and the join columns on the dimension table. • The star query is part of a transaction that is in serializable mode. • STAR_TRANSFORMATION_ENABLED is set to TEMP_DISABLE. dimension tables are accessed twice. and there is no fast access path to them for solving the problem. the TIMES and CHANNELS tables are very small. All rights reserved. you see an extract from an execution plan using temporary tables for the CUSTOMERS table.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If the cost is lower. Temporary table’s creation in the plan: LOAD AS SELECT SYS_TEMP_0FD9D6720_BEBDC TABLE ACCESS FULL CUSTOMERS … filter("C". and once when joining the fact rows to each dimension table during the second phase. This decision is made if the cost for creating a temporary table. Case Study: Star Transformation Chapter 9 . In the previous execution plan example. is cheaper than accessing the dimension table twice. In such cases. The creation of these temporary tables and the data insertion are shown in the execution plan. once for each phase. This might be a performance issue if the dimension tables are big. and accessing them using a full table scan has a very small cost. This might be a performance issue in the case of big dimension tables and low selectivity. Copyright © 2010. where the system determines the necessary fact table rows. In the slide. time_id = times. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Using Bitmap Join Indexes • • • Volume of data to be joined is reduced Can be used to eliminate bitwise operations More efficient in storage than MJVs CREATE BITMAP INDEX sales_q_bjx ON sales(times. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Bitmap Join Indexes . which are necessary in the star transformation with existing bitmap indexes. Finally. Oracle and/or its affiliates. Using Bitmap Join Indexes The volume of data that must be joined can be reduced if the join indexes used have already been precalculated.Page 17 Oracle University and InfoTech (Pvt. times WHERE sales. Case Study: Star Transformation Chapter 9 . All rights reserved. which do not compress rowids of the fact tables. which contain multiple dimension tables can eliminate bitwise operations.calendar_quarter_desc) FROM sales. Assume that you have created the additional index structure mentioned in the slide. the join indexes. bitmap join indexes are much more efficient in storage than materialized join views (MJVs). Note: Since the SALES table is partitioned the bitmap join index will also be partitioned therefore the LOCAL keyword is required.time_id LOCAL. In addition. calendar_quarter_desc IN ('1999-Q1'. The only difference is that the system uses the join index instead of a single-table bitmap index to access the times data in the first phase of the star query. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Case Study: Star Transformation Chapter 9 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation Plan: Example 2 .calendar_quarter_desc can be satisfied with the sales_q_bjx bitmap join index.Page 18 Oracle University and InfoTech (Pvt. Note that access to the join index is done twice because the corresponding query’s predicate is t. This is because the join predicate information on times. Oracle and/or its affiliates.'1999-Q2') Copyright © 2010.Star Transformation Plan: Example 2 SORT GROUP BY HASH JOIN HASH JOIN TABLE ACCESS BY INDEX ROWID SALES BITMAP CONVERSION TO ROWIDS BITMAP AND BITMAP MERGE BITMAP KEY ITERATION BUFFER SORT TABLE ACCESS FULL CHANNELS BITMAP INDEX RANGE SCAN SALES_CHANNELS_BX BITMAP OR BITMAP INDEX SINGLE VALUE SALES_Q_BJX BITMAP INDEX SINGLE VALUE SALES_Q_BJX TABLE ACCESS FULL CHANNELS TABLE ACCESS FULL TIMES Star Transformation Plan: Example 2 The processing of the same star query using the bitmap join index is similar to the previous example. The difference between this plan as compared to the previous one is that the inner part of the bitmap index scan for the times dimension has no subselect in the rewritten query for phase 1. if there is one. Even if the hint is given. • The FACT hint is used in the context of the star transformation to indicate to the transformation that the hinted table should be considered as a fact table and all other tables regardless of their size are considered as dimensions. and the best plan for the untransformed query is used. instead of the best plan for the transformed query. The FACT and NO_FACT hints are useful for star queries containing more than one fact table. regardless of the hint. • The NO_FACT hint is used in the context of the star transformation to indicate to the transformation that the hinted table should not be considered as a fact table. the optimizer could make a cost-based decision to use the best plan generated without the transformation.Page 19 Oracle University and InfoTech (Pvt. Without the hint. there is no guarantee that the transformation takes place. there is no transformed query. Oracle and/or its affiliates. Star Transformation Hints • The STAR_TRANSFORMATION hint makes the optimizer use the best plan in which the transformation has been used. The optimizer only generates the subqueries if it seems reasonable to do so. Case Study: Star Transformation Chapter 9 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Star Transformation Hints • • • • The STAR_TRANSFORMATION hint: Use best plan containing a star transformation. The NO_FACT (<table_name>) hint: The hinted table should not be considered as the fact table in the context of a star transformation. If no subqueries are generated. All rights reserved. Copyright © 2010. Note: The FACT and NO_FACT hints might be useful only in case there are more than one fact table accessed in the star query. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Star Transformation Hints . The FACT(<table_name>) hint: The hinted table should be considered as the fact table in the context of a star transformation. All rights reserved. a bitmap join index precomputes the join and stores it as a database object.pk = f. Case Study: Star Transformation Chapter 9 . SELECT sum(f. f WHERE d.facts) FROM d. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Join Indexes: Join Model 1 . D. A bitmap join index can be used in the SELECT statement in the slide to avoid the join operation. Oracle and/or its affiliates. PK a primary key.c1 = 1.pk = f.fk. Similar to the materialized join view.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. F represents the fact table. d WHERE d.fk AND d. and FK a foreign key.Page 20 Oracle University and InfoTech (Pvt. Bitmap Join Indexes: Join Model 1 In the following three slides. Note: C1 is the indexed column in the dimension table.c1) FROM f.Bitmap Join Indexes: Join Model 1 c1 pk fk d f CREATE BITMAP INDEX bji ON f(d. Copyright © 2010. The difference is that a materialized join view materializes the join into a table while a bitmap join index materializes the join into a bitmap index. the dimension table. in this case.f where d. can also be used to answer the following select statement: select sum(f. Bitmap Join Indexes: Join Model 2 The model in the slide is an extension of model 1.fk and d. Copyright © 2010.c2 = 1. Oracle and/or its affiliates.c1=1 This is due to the fact that D. d WHERE d. All rights reserved.fk AND d.facts) from d.pk=f.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.d. requiring a concatenated bitmap join index to represent it.fk. Case Study: Star Transformation Chapter 9 .pk = f. Note that BJX.c1. f WHERE d.c1 = 1 AND d.facts) FROM d. SELECT sum(f.Bitmap Join Indexes: Join Model 2 c1 c2 pk fk d f CREATE BITMAP INDEX bjx ON f(d.Page 21 Oracle University and InfoTech (Pvt.c2) FROM f.C1 is the leading part of the BJX.pk = f. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Join Indexes: Join Model 2 . All rights reserved.pk = f. two dimension tables are used.fk1 AND d2.Page 22 Oracle University and InfoTech (Pvt. Bitmap Join Indexes: Join Model 3 This model also requires the concatenated bitmap join index shown in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. d1.pk = f. SELECT sum(f. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Join Indexes: Join Model 3 . d2 WHERE d1.sales) FROM d1. d2 WHERE d1.fk2 AND d1.Bitmap Join Indexes: Join Model 3 c1 pk fk1 d1 fk2 pk f c1 d2 CREATE BITMAP INDEX bjx ON f(d1. Copyright © 2010.pk = f. Case Study: Star Transformation Chapter 9 .c1 = 1 AND d2.c1 = 2. Oracle and/or its affiliates.c1.c1) FROM f.pk = f.fk1 AND d2.d2. f.fk2. In this case. c2 AND d2.pk = d2. SELECT sum(f.sales) FROM d1.fk AND d1. Oracle and/or its affiliates. Copyright © 2010.pk = d2.Bitmap Join Indexes: Join Model 4 c1 pk c2 d1 pk fk d2 f CREATE BITMAP INDEX bjx ON f(d1.Page 23 Oracle University and InfoTech (Pvt. It can be expressed by a bitmap join index. d2 WHERE d1. The bitmap join index can be either single or concatenated depending on the number of columns in the dimension tables to be indexed. d1.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.pk = f. d2.pk = f. f WHERE d1. A bitmap join index on D1. Case Study: Star Transformation Chapter 9 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bitmap Join Indexes: Join Model 4 .fk.C1 with a join between D1 and D2 and a join between D2 and F can be created as shown in the slide with BJX.c1 = 1. All rights reserved.c2 AND d2. Bitmap Join Indexes: Join Model 4 The slide shows a snowflake model that involves joins between two or more dimension tables.c1) FROM f. All rights reserved. Always use a temporary table c. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Always use a star transformation b.Quiz If the star_transformation_enabled parameter is set to true. Oracle and/or its affiliates.Page 24 Oracle University and InfoTech (Pvt. Always use a hash join Answer: c Copyright © 2010. the optimizer will: a. Never use a temporary table e.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Always consider a star transformation d. Case Study: Star Transformation Chapter 9 . At least one bind variable e. Case Study: Star Transformation Chapter 9 .Quiz Which two of the following properties of the schema structure are required by the optimizer to consider using the star transformation? a. Oracle and/or its affiliates. At least one bitmap join index c.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. At least two dimension tables d.Page 25 Oracle University and InfoTech (Pvt. c Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . At least one histogram on a join column Answer: a. At least one fact table b. All rights reserved.Quiz Assuming that the START_TRANSFORMATION_ENABLED parameter is set to TRUE. The cost is lower than a tradition access path c. the star transformation is chosen by the optimizer when: a. All the conditions are met b. Case Study: Star Transformation Chapter 9 . Oracle and/or its affiliates.Page 26 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . The bitmap join indexes eliminate additional joins d.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Temporary tables can be used to reduce table accesses Answer: b Copyright © 2010. Case Study: Star Transformation Chapter 9 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Page 27 Oracle University and InfoTech (Pvt.Summary In this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you should have learned how to: • Define a star schema • Show a star query plan without transformation • Define the star transformation requirements • Show a star query plan after transformation Copyright © 2010. All rights reserved. Oracle and/or its affiliates. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Case Study: Star Transformation Chapter 9 .Practice 9: Overview This practice covers using the star transformation technique to optimize your query. All rights reserved.Page 28 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 9: Overview Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 10 Copyright © 2010.Page 1 Oracle University and InfoTech (Pvt. Optimizer Statistics Chapter 10 . Oracle and/or its affiliates.) Ltd use only Optimizer Statistics .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Statistics Copyright © 2010. Oracle and/or its affiliates. All rights reserved.) Ltd use only Optimizer Statistics .Page 2 Oracle University and InfoTech (Pvt.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Statistics Chapter 10 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Objectives After completing this lesson. Oracle and/or its affiliates. Optimizer Statistics Chapter 10 . All rights reserved.Page 3 Oracle University and InfoTech (Pvt. you should be able to do the following: • Gather optimizer statistics • Gather system statistics • Set statistic preferences • Use dynamic sampling • Manipulate optimizer statistics Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Statistics . join order. All rights reserved. Oracle and/or its affiliates. or you can maintain the optimizer statistics manually using the DBMS_STATS package. and join method CPU and input/output (I/O) costs Refreshing optimizer statistics whenever they are stale is as important as gathering them: – Automatically gathered by the system – Manually gathered by the user with DBMS_STATS Optimizer Statistics Optimizer statistics describe details about the database and the objects in the database. statistics must be regularly updated so that they accurately describe these database objects. Optimizer Statistics Chapter 10 . Copyright © 2010. Statistics are maintained automatically by Oracle Database. Because the objects in a database change constantly.Page 4 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Optimizer Statistics • • Describe the database and the objects in the database Information used by the query optimizer to estimate: – – – – • Selectivity of predicates Cost of each execution plan Access method. These statistics are used by the query optimizer to select the best execution plan for each SQL statement. Copyright © 2010.Types of Optimizer Statistics • Table statistics: • – Number of rows – Number of blocks – Average row length • Index Statistics: – – – – • B*-tree level Distinct keys Number of leaf blocks Clustering factor Column statistics – Basic: Number of distinct values. index statistics are automatically gathered when the index is created or rebuilt. min. max – Histograms (data distribution when the column data is skewed) – Extended statistics System statistics – I/O performance and utilization – CPU performance and utilization Types of Optimizer Statistics Most of the optimizer statistics are listed in the slide.Page 5 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Types of Optimizer Statistics . All rights reserved. Starting with Oracle Database 10g. Oracle and/or its affiliates. average length. which are created for query optimization and are stored in the data dictionary. number of nulls.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Statistics Chapter 10 . Note: The statistics mentioned in this slide are optimizer statistics. These statistics should not be confused with performance statistics visible through V$ views. STALE_STATS This tells you if statistics are valid on the corresponding table. and CHAIN_CNT that are not used by the optimizer. Row count is especially important if the table is the driving table of a nested loops join. as it defines how many times the inner table is probed. BLOCKS This is the number of used data blocks. Oracle and/or its affiliates. If these are required the ANALYZE command must be used. and not gathered by the DBMS_STATS procedures. Optimizer Statistics Chapter 10 . AVE_ROW_LEN. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Table Statistics (DBA_TAB_STATISTICS) .Page 6 Oracle University and InfoTech (Pvt. Note: There are three other statistics: EMPTY_BLOCKS.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Block count in combination with DB_FILE_MULTIBLOCK_READ_COUNT gives the base table access cost. All rights reserved. AVG_ROW_LEN This is the average length of a row in the table in bytes.Table Statistics (DBA_TAB_STATISTICS) • Table statistics are used to determine: – Table access cost – Join cardinality – Join order • Some of the table statistics gathered are: – – – – Row count (NUM_ROWS) Block count (BLOCKS) Exact Average row length (AVG_ROW_LEN) Statistics status (STALE_STATS) Table Statistics (DBA_TAB_STATISTICS) NUM_ROWS This is the basis for cardinality computations. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Statistics (DBA_IND_STATISTICS) . Optimizer Statistics Chapter 10 . BLEVEL This is used to calculate the cost of leaf block lookups. the table is very randomly ordered.Index Statistics (DBA_IND_STATISTICS) • Used to decide: – Full table scan versus index scan • Statistics gathered are: – B*-tree level (BLEVEL) Exact – Leaf block count (LEAF_BLOCKS) – Clustering factor (CLUSTERING_FACTOR) – Distinct keys (DISTINCT_KEYS) – Average number of leaf blocks in which each distinct value in the index appears (AVG_LEAF_BLOCKS_PER_KEY) – Average number of data blocks in the table pointed to by a distinct value in the index (AVG_DATA_BLOCKS_PER_KEY) – Number of rows in the index (NUM_ROWS) Index Statistics (DBA_IND_STATISTICS) In general. in case there is no predicate and all the columns referenced in the query are present in an index. In this case. Copyright © 2010. Oracle and/or its affiliates. the index entries in a single leaf block tend to point to the rows in the same data blocks. it is unlikely that the index entries in the same leaf block point to rows in the same data blocks. However. In this case. LEAF_BLOCKS This is used to calculate the cost of a full index scan.Page 7 Oracle University and InfoTech (Pvt. If the value is near the number of blocks. A depth of "0" indicates that the root block and leaf block are the same. If the value is near the number of rows. the table is very well ordered. the optimizer requires a predicate on the prefix of the index columns. All rights reserved. to select an index access. It indicates the depth of the index from its root block to its leaf blocks. the optimizer considers using a full index scan versus a full table scan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. CLUSTERING_FACTOR This measures the order of the rows in the table based on the values of the index. Optimizer Statistics Chapter 10 . For indexes that enforce UNIQUE and PRIMARY KEY constraints. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED STALE_STATS .This tells you if the statistics are valid in the corresponding index. Oracle and/or its affiliates. rounded to the nearest integer. This statistic is the average number of data blocks that contain rows that contain a given value for the indexed columns. All rights reserved. AVG_DATA_BLOCKS_PER_KEY This is the average number of data blocks in the table that are pointed to by a distinct value in the index rounded to the nearest integer. DISTINCT_KEYS This is the number of distinct indexed values. NUM_ROWS It is the number of rows in the index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. For indexes that enforce UNIQUE and PRIMARY KEY constraints. AVG_LEAF_BLOCKS_PER_KEY This is the average number of leaf blocks in which each distinct value in the index appears. this value is always 1 (one). Copyright © 2010. this value is the same as the number of rows in the table.Page 8 Oracle University and InfoTech (Pvt. not rows. All rights reserved. the rows accessed and blocks accessed are the same. In real-life scenarios. This is called the index clustering factor. A clustering factor indicates that the individual rows are concentrated within fewer blocks in the table. Therefore. An attempt to reorganize the table to improve the clustering factor for one index can cause degradation of the clustering factor of the other index. it appears that the clustering factor plays an important role in determining the cost of an index range scan simply because the number of leaf blocks and the height of the B*-tree are relatively small compared to the clustering factor and table’s selectivity. Note: If you have more than one index on a table. A high clustering factor indicates that the individual rows are scattered more randomly across the blocks in the table. a high clustering factor means that it costs more to use an index range scan to fetch rows by ROWID because more blocks in the table need to be visited to return the data. Consequently. the index selectivity.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. or they could be spread out over a larger number of blocks. the clustering factor for one index might be small while at the same time the clustering factor for another index might be large. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Index Clustering Factor . The cost formula of an index range scan uses the level of the B*-tree. each index entry selected points to a block in the table. If each entry points to a different block. the number of leaf blocks. Therefore.Page 9 Oracle University and InfoTech (Pvt.CLUSTERING_FACTOR A B C Number of blocks (3) Low clustering factor: Favor the index range scan path A A A B B B C C C Block 1 Block 2 Block 3 Only need to read one block to retrieve all As Index Clustering Factor The system performs input/output (I/O) by blocks. and the clustering factor. Copyright © 2010. Optimizer Statistics Chapter 10 . Oracle and/or its affiliates. the desired number of rows could be clustered together in a few blocks. When an index range scan is used. the optimizer’s decision to use full table scans is influenced by the percentage of blocks accessed.Index Clustering Factor Must read all blocks to retrieve all As Block 1 Block 2 Block 3 A B C A B C A B C Number of rows (9) C B A High clustering factor: Favor alternative paths DBA_IND_STATISTICS. the smallest possible value for the clustering factor is the number of blocks. Assume the following situation: There is a table with 9 rows. All rights reserved. Copyright © 2010. and the table only has three data blocks. Based on this algorithm. the c1 column currently stores the values A. Optimizer Statistics Chapter 10 . Oracle and/or its affiliates.Page 10 Oracle University and InfoTech (Pvt. • Case 1: If the same rows in the table are arranged so that the index values are scattered across the table blocks (rather than collocated). and C. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The clustering factor is computed and stored in the CLUSTERING_FACTOR column of the DBA_INDEXES view when you gather statistics on the index. You read the index from left to right. Note: For bitmap indexes. and the highest possible value is the number of rows. The way it is computed is relatively easy. there is a nonunique index on col1 for table.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and for each indexed entry. you add one to the clustering factor if the corresponding row is located in a different block than the one from the previous row. the index clustering factor is high. • Case 2: The index clustering factor is low for the rows as they are collocated in the same block for the same value. . B. the clustering factor is not applicable and is not used.The example in the slide shows how the clustering factor can affect cost. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Column Statistics (DBA_TAB_COL_STATISTICS) . NUM_BUCKETS is the number of buckets in histogram for the column.Column Statistics (DBA_TAB_COL_STATISTICS) • • • • • • • Count of distinct values of the column (NUM_DISTINCT) Low value (LOW_VALUE) Exact High value (HIGH_VALUE) Exact Number of nulls (NUM_NULLS) Selectivity estimate for nonpopular values (DENSITY) Number of histogram buckets (NUM_BUCKETS) Type of histogram (HISTOGRAM) Column Statistics (DBA_TAB_COL_STATISTICS) NUM_DISTINCT is used in selectivity calculations. Oracle and/or its affiliates. Optimizer Statistics Chapter 10 . These values are used to determine range selectivity. DENSITY is only relevant for histograms. HEIGHT BALANCED Copyright © 2010. The calculation depends on the histogram type. It is used as the selectivity estimate for nonpopular values. NUM_NULLS helps with selectivity of nullable columns and the IS NULL and IS NOT NULL predicates. It can be thought of as the probability of finding one particular value in this column. HISTOGRAM indicates the existence or type of the histogram: NONE. for example. 1/Number of Distinct Values LOW_VALUE and HIGH_VALUE: The cost-based optimizer (CBO) assumes uniform distribution of values between low and high values for all data types. FREQUENCY.Page 11 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Histograms: – Store additional column distribution information – Give better selectivity estimates in the case of nonuniform distributions • • With unlimited resources you could store each different value and the number of rows for that value. the system creates a frequency histogram. Oracle Database uses two different types of histogram representations depending on the number of distinct values found in the corresponding column. If a histogram is available on a column.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Having histograms on columns that contain skewed data or values with large variations in the number of duplicates help the query optimizer generate good selectivity estimates and make better decisions regarding index usage. join orders. All rights reserved. Histograms A histogram captures the distribution of different values in a column. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Histograms . and DBA_SUBPART_HISTOGRAMS Note: Gathering histogram statistics is the most resource-consuming operation in gathering statistics. so it yields better selectivity estimates. the system creates a height-balanced histogram. and the number of histogram buckets is not specified. When you have a data set with less than 254 distinct values. Without histograms. When creating histograms. DBA_PART_HISTOGRAMS. Optimizer Statistics Chapter 10 . This becomes unmanageable for a large number of distinct values and a different approach is used: – Frequency histogram (#distinct values ≤ #buckets) – Height-balanced histogram (#buckets < #distinct values) • They are stored in DBA_TAB_HISTOGRAMS.Page 12 Oracle University and InfoTech (Pvt. the estimator uses it instead of the number of distinct values. and join methods. Oracle and/or its affiliates.Histograms • • The optimizer assumes uniform distributions. You can find information about histograms in these dictionary views: DBA_TAB_HISTOGRAMS. a uniform distribution is assumed. this may lead to suboptimal access plans in the case of data skew. Copyright © 2010. If the number of distinct values is greater than the required number of histogram buckets. 27. 10. 27. and 49.293 occurrences. 10. Copyright © 2010. 5. 3. 39. 39. 49 Number of rows: 40001 Frequency Histograms For the example in the slide. only the ENDPOINT_VALUE and ENDPOINT_NUMBER columns are stored in the data dictionary. assume that you have a column that is populated with 40. 16.Frequency Histograms 10 buckets. The actual number of row counts is shown by the curve in the slide for clarity. in DBA_TAB_HISTOGRAMS. The actual row counts are derived from the endpoint values if needed. 3. 32. 7. the ENDPOINT_VALUE column stores the column value and the ENDPOINT_NUMBER column stores the cumulative row count including that column value. 32.Page 13 Oracle University and InfoTech (Pvt. 10 distinct values Cumulative cardinality 40000 ENDPOINT # rows for column value 30000 NUMBER 20000 10000 0 1 3 5 7 10 16 27 32 39 49 ENDPOINT VALUE: Column value Distinct values: 1. All rights reserved. 7. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you can store each different value and record exact cardinality statistics. You only have ten distinct values: 1. because this can avoid some calculation for range scans. Optimizer Statistics Chapter 10 . 5. In this case.001 numbers. When the requested number of buckets equals (or is greater than) the number of distinct values. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Frequency Histograms . 16. Value 10 is the most popular value with 16. the system automatically creates a frequency histogram with 9 buckets. You can view this information in the USER_TAB_COL_STATISTICS view.Page 14 Oracle University and InfoTech (Pvt. in this case. To view the histogram itself.Viewing Frequency Histograms BEGIN DBMS_STATS. the warehouse_id is 1 and there are 36 rows with warehouse_id = 1. METHOD_OPT => 'FOR COLUMNS SIZE 20 warehouse_id').-------------36 1 213 2 261 3 … Viewing Frequency Histograms The example in the slide shows you how to view a frequency histogram.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Statistics Chapter 10 . You can see both ENDPOINT_NUMBER that corresponds to the cumulative frequency of the corresponding ENDPOINT_VALUE. Oracle and/or its affiliates. histogram FROM USER_TAB_COL_STATISTICS WHERE table_name = 'INVENTORIES' AND column_name = 'WAREHOUSE_ID'.gather_table_STATS (OWNNAME=>'OE'. Copyright © 2010. END. All rights reserved. COLUMN_NAME NUM_DISTINCT NUM_BUCKETS HISTOGRAM -----------. TABNAME=>'INVENTORIES'. which represents. you can query the USER_HISTOGRAMS view.-----------.----------. SELECT column_name. num_distinct. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Viewing Frequency Histograms . ENDPOINT_NUMBER ENDPOINT_VALUE --------------.--------WAREHOUSE_ID 9 9 FREQUENCY SELECT endpoint_number. and the number of requested buckets is 20. endpoint_value FROM USER_HISTOGRAMS WHERE table_name = 'INVENTORIES' and column_name = 'WAREHOUSE_ID' ORDER BY endpoint_number. Note: The DBMS_STATS package is dealt with later in the lesson. There are 177 rows with warehouse_id = 2 so the sum of rows so far (36+177) is the cumulative frequency of 213. Because the number of distinct values in the WAREHOUSE_ID column of the INVENTORIES table is 9. num_buckets. the actual value of the column data. In this case. 27. Oracle and/or its affiliates. 49 Number of rows: 40001 Height-Balanced Histograms In a height-balanced histogram. 7. 10. the ordered column values are divided into bands so that each band contains approximately the same number of rows.Height-Balanced Histograms 5 buckets.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. value 10 is a popular value because it spans multiple endpoint values. 32. 16. The histogram tells you values of the endpoints of each band. 7. 39.001 numbers. the histogram does not actually store duplicated buckets. Based on this assumption. assume that you have a column that is populated with 40. the number of rows per bucket is one-fifth of the total number of rows. So you are sure that value 10 is a popular value.293 occurrences. 27. value 10 appears between 8000 and 24000 times. that is 8000. and 49. and range predicates. All rights reserved. This type of histogram is good for equality predicates on popular value. The number of rows per bucket is not recorded because this can be derived from the total number of values and the fact that all the buckets contain an equal number of values. 16. 5. bucket 2 (with endpoint value 10) would not be recorded in DBA_TAB_HISTOGRAMS for that reason. 5. 39. There will be 8. In this example. In the example in the slide. 32. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Height-Balanced Histograms . ENDPOINT_NUMBER records the bucket number and ENDPOINT_VALUE records the column value that corresponds to this endpoint. When the number of buckets is less than the number of distinct values. 3.000 values in each band.Page 15 Oracle University and InfoTech (Pvt. You only have ten distinct values: 1. In the example. Value 10 is the most popular value with 16. Copyright © 2010. Optimizer Statistics Chapter 10 . To save space. 3. 10. In the example in the slide. 10 distinct values (8000 rows per bucket) Popular value 1 7 10 10 32 49 0 1 2 3 4 5 ENDPOINT VALUE Same number of rows per bucket ENDPOINT NUMBER: Bucket number Distinct values: 1. endpoint_value FROM USER_HISTOGRAMS WHERE table_name = 'INVENTORIES' and column_name = 'QUANTITY_ON_HAND' ORDER BY endpoint_number. Oracle and/or its affiliates. METHOD_OPT => 'FOR COLUMNS SIZE 10 quantity_on_hand'). COLUMN_NAME NUM_DISTINCT NUM_BUCKETS HISTOGRAM -----------------------------. Because the number of distinct values in the QUANTITY_ON_HAND column of the INVENTORIES table is 237. and the number of requested buckets is 10. num_buckets. Optimizer Statistics Chapter 10 . TABNAME=>'INVENTORIES'.gather_table_STATS(OWNNAME =>'OE'. Note: The DBMS_STATS package is dealt with later in the lesson. ENDPOINT_NUMBER ENDPOINT_VALUE --------------. You can view this information in the USER_TAB_COL_STATISTICS view. To view the histogram itself. histogram FROM USER_TAB_COL_STATISTICS WHERE table_name = 'INVENTORIES' AND column_name = 'QUANTITY_ON_HAND'. You can see that the ENDPOINT_NUMBER corresponds to the bucket number. you can query the USER_HISTOGRAMS view.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Viewing Height-Balanced Histograms .--------------QUANTITY_ON_HAND 237 10 HEIGHT BALANCED SELECT endpoint_number. All rights reserved.Viewing Height-Balanced Histograms BEGIN DBMS_STATS.-----------. END. and ENDPOINT_VALUE corresponds to values of the endpoints end. num_distinct. Copyright © 2010.Page 16 Oracle University and InfoTech (Pvt. SELECT column_name.----------.-------------0 0 1 27 2 42 3 57 … Viewing Height-Balanced Histograms The example in the slide shows you how to view a height-balanced histogram. the system automatically creates a heightbalanced histogram with 10 buckets. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Histogram Considerations .# distinct values). Do not use histograms unless they substantially improve performance. Optimizer Statistics Chapter 10 . you must recompute its histogram frequently. there is no need to create histograms for columns which do not appear in a WHERE clause of a SQL statement. Oracle and/or its affiliates. However. Histograms can affect performance and should be used only when they substantially improve query plans. Histograms are not useful for: – Columns which do not appear in the WHERE or JOIN clauses – Columns with uniform distributions – Equality predicates with unique columns • • The maximum number of buckets is the least (254. For uniformly distributed data. for columns declared as UNIQUE. histograms are useless because the selectivity is obvious.Histogram Considerations • • Histograms are useful when you have a high degree of skew in the column distribution. Note: Character columns have some exceptional behavior as histogram data is stored only for the first 32 bytes of any string. which can be lower depending on the actual number of distinct column values. The data in the column can change as long as the distribution remains constant.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the maximum number of buckets is 254. the optimizer can make fairly accurate guesses about the cost of executing a particular statement without the use of histograms. there is no need to create histograms for columns with uniform distribution. Copyright © 2010.Page 17 Oracle University and InfoTech (Pvt. Similarly. Also. If the data distribution of a column changes frequently. Histograms are useful when you have a high degree of data skew in the columns for which you want to create histograms. Histogram Considerations Histograms are useful only when they reflect the current data distribution of a given column. In addition. All rights reserved. To circumvent this issue.model)') from dual.create_extended_stats('jfv'. exec dbms_stats.MODEL) Multicolumn Statistics: Overview With Oracle Database 10g. As a result. However. then the optimizer uses the number of distinct keys (NDK) in the index for estimating selectivity.'(make. Oracle Database 11g allows you to collect. as 1/NDK.'vehicle'. • When DYNAMIC_SAMPLING is set to level 4. Optimizer Statistics Chapter 10 . Oracle and/or its affiliates. the query optimizer uses dynamic sampling to estimate the selectivity of complex predicates involving several columns from the same table. the query optimizer takes into account the correlation between columns when computing the selectivity of multiple predicates in the following limited cases: • If all the columns of a conjunctive predicate match all the columns of a concatenated index key.'vehicle'. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Multicolumn Statistics: Overview .Multicolumn Statistics: Overview VEHICLE MAKE MODEL 1 S(MAKE Λ MODEL)=S(MAKE)xS(MODEL) select dbms_stats. frequency histograms.model) size 3').) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.gather_table_stats('jfv'.method_opt=>'for all columns size 1 for columns (make. It estimates the selectivity of a conjunctive predicate by multiplying the selectivity of individual predicates. the sample is likely to be statistically inaccurate and may cause more harm than good. and the predicates are equalities used in equijoins.Page 18 Oracle University and InfoTech (Pvt. store. and use the following statistics to capture functional dependency between two or more columns (also called groups of columns): number of distinct values. and density. This approach results in underestimation of the selectivity if there is a correlation between the columns. number of nulls. the sample size is very small and increases parsing time. Copyright © 2010. 2 3 SYS_STUF3GLKIOP5F4B0BTTCFTMX0W DBA_STAT_EXTENSIONS VEHICLE MAKE MODEL 4 S(MAKE Λ MODEL)=S(MAKE. In all other cases. the optimizer assumes that the values of columns used in a complex predicate are independent of each other. All rights reserved. 'vehicle'. Note • The CREATE_EXTENDED_STATS function returns a virtual hidden column name.show_extended_stats_name('jfv'. in that MODEL determines MAKE. consider a VEHICLE table in which you store information about cars. The MAKE and MODEL columns are highly correlated. All rights reserved.model) ') from dual • After you create the statistics extensions. and both columns should be considered by the optimizer as highly correlated.'(make. you can retrieve them by using the ALL|DBA|USER_STAT_EXTENSIONS views. and then compute the statistics for all columns (including the ones for the correlated groups that you created).) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Based on the example in the slide. This is a strong dependency.Page 19 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED For example. the name can be determined by using the following SQL: select dbms_stats. Copyright © 2010. such as SYS_STUW_5RHLX443AN1ZCLPE_GLE4. Oracle and/or its affiliates. . Optimizer Statistics Chapter 10 .The optimizer only uses multicolumn statistics with equality predicates. You can signal that correlation to the optimizer by using the CREATE_EXTENDED_STATS function as shown in the example in the slide. Optimizer Statistics Chapter 10 . When computing selectivity on predicates of the form function(Column) = constant.Page 20 Oracle University and InfoTech (Pvt. this feature relies on the virtual column infrastructure to create statistics on expressions of columns. All rights reserved. these solutions are either limited to a certain class of functions or work only for expressions used to create function-based indexes. the optimizer assumes a static selectivity value of 1 percent. you can use a more general solution that includes arbitrary user-defined functions and does not depend on the presence of function-based indexes. The query optimizer has been extended to better handle such predicates in limited cases where functions preserve the data distribution characteristics of the column and thus allow the optimizer to use the columns statistics. An example of such a function is TO_NUMBER.method_opt=>'for all columns size 1 for columns (upper(model)) size 3'). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Expression Statistics: Overview .Expression Statistics: Overview CREATE INDEX upperidx ON VEHICLE(upper(MODEL)) VEHICLE VEHICLE Still possible MODEL MODEL Recommended S(upper( MODEL))=0.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.01 DBA_STAT_EXTENSIONS VEHICLE select dbms_stats. By using expression statistics in Oracle Database 11g. However.'vehicle'. This approach almost never has the correct selectivity and it may cause the optimizer to produce suboptimal plans. the optimizer collects statistics on virtual columns created to support function-based indexes. Expression Statistics: Overview Predicates involving expressions on columns are a significant issue for the query optimizer. As shown in the example in the slide. Further enhancements have been made to evaluate built-in functions during query optimization to derive better selectivity using dynamic sampling. MODEL SYS_STU3FOQ$BDH0S_14NGXFJ3TQ50 exec dbms_stats. Copyright © 2010.'(upper(model))') from dual.create_extended_stats( 'jfv'.'vehicle'. Oracle and/or its affiliates. Finally.gather_table_stats('jfv'. Gathering System Statistics • • • • System statistics enable the CBO to use CPU and I/O characteristics. All new SQL statements are parsed using new statistics. Unlike the table. All rights reserved. Gathering system statistics equals analyzing system activity for a specified period of time: Procedures: – DBMS_STATS. Oracle and/or its affiliates. For example. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Gathering System Statistics . System statistics must be gathered on a regular basis. System CPU and I/O characteristics depend on many factors and do not stay constant all the time. the system does not invalidate already parsed SQL statements when system statistics get updated. database applications can process online transaction processing (OLTP) transactions during the day and run OLAP reports at night. This allows the optimizer to generate relevant costs with respect to the available system resource plans. For each candidate plan. Optimizer Statistics Chapter 10 . or column statistics.GET_SYSTEM_STATS to verify system statistics. System statistics are gathered in a user-defined time frame with the DBMS_STATS.SET_SYSTEM_STATS.GATHER_SYSTEM_STATS routine.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It is important to know the system characteristics to select the most efficient plan with optimal proportion between I/O and CPU cost.GATHER_SYSTEM_STATS – DBMS_STATS. You can also set system statistics values explicitly using DBMS_STATS. Copyright © 2010.Page 21 Oracle University and InfoTech (Pvt. It is highly recommended that you gather system statistics. you can capture statistics in the interval of time when the system has the most common workload. this does not invalidate cached plans. and utilization. it analyzes system activity in a specified period of time. the optimizer computes estimates for I/O and CPU costs. Using system statistics management routines. When the system generates system statistics. Use DBMS_STATS. index.GET_SYSTEM_STATS • GATHERING_MODE: – NOWORKLOAD|INTERVAL – START|STOP Gathering System Statistics System statistics allow the optimizer to consider a system’s I/O and CPU performance.SET_SYSTEM_STATS – DBMS_STATS. You can gather statistics for both states and activate appropriate OLTP or OLAP statistics when needed. after which system statistics are created or updated in the dictionary or a staging table. This mode captures characteristics of the I/O system. All rights reserved. Release 2. the system automatically gathers essential parts of system statistics at startup. Optimizer Statistics Chapter 10 . This mode is suitable for all workloads. START | STOP: Captures system activity during specified start and stop times.Page 22 Oracle University and InfoTech (Pvt. INTERVAL: Captures system activity during a specified interval. This works in combination with the interval parameter that specifies the amount of time for the capture. During this period the system estimates the average read seek time and transfer speed for the I/O system. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED When you use the GATHER_SYSTEM_STATS procedure. Note: Since Oracle Database 10g. Gathering may take a few minutes and depends on the size of the database. You should provide an interval value in minutes. You can use GATHER_SYSTEM_STATS (gathering_mode=>'STOP') to stop gathering earlier than scheduled. and refreshes the dictionary or a staging table with statistics for the elapsed period.• • • NOWORKLOAD: This is the default. you should specify the GATHERING_MODE parameter: . It is recommended that you run GATHER_SYSTEM_STATS ('noworkload') after you create the database and tablespaces.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. Oracle and/or its affiliates. statid => 'OLTP').GATHER_SYSTEM_STATS( interval => 120. gathering ends after 120 minutes and is stored in the mystats table. the default value of job_queue_processes is 1000. such as SQL> alter system set job_queue_processes = 1. stattab => 'mystats'.GATHER_SYSTEM_STATS( interval => 120. system statistics are collected during the night..IMPORT_SYSTEM_STATS( Next nights stattab => 'mystats'. Copyright © 2010. Next days EXECUTE DBMS_STATS. EXECUTE DBMS_STATS. system statistics must be collected during the day. Before invoking the GATHER_SYSTEM_STATS procedure with the INTERVAL parameter specified. Note that it is possible to automate this process by submitting a job to update the dictionary with appropriate statistics. Gathering ends after 120 minutes and is stored in the mystats table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Gathering System Statistics: Example . you can switch between the statistics gathered. you must activate job processes using a command. Then. In this example. Oracle and/or its affiliates. stattab => 'mystats'.Gathering System Statistics: Example First day EXECUTE DBMS_STATS. You can also invoke the same procedure with different arguments to enable manual gathering instead of using jobs. During the day.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Statistics Chapter 10 .Page 23 Oracle University and InfoTech (Pvt. the syntax in the slide is used to gather system statistics. Gathering System Statistics: Example The example in the slide shows database applications processing OLTP transactions during the day and running reports at night. If appropriate. First night EXECUTE DBMS_STATS. statid => 'OLAP'). another job imports the online analytical processing (OLAP) statistics for the nighttime run. First. a job may import the OLTP statistics for the daytime run.IMPORT_SYSTEM_STATS( stattab => 'mystats'. statid => 'OLTP'). Note: In Oracle Database 11g Release 2. and during the night. All rights reserved. Generally. statid => 'OLAP'). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Gathering System Statistics: Example . you must start the system statistics collection. First. Copyright © 2010.GATHER_SYSTEM_STATS procedure. The example collects system statistics and stores them directly in the data dictionary. another parameter of this procedure can be used as shown in the slide.GATHER_SYSTEM_STATS( 2 gathering_mode => 'START'). Oracle and/or its affiliates. End the collection of system statistics: SQL> EXECUTE DBMS_STATS. To collect system statistics manually. All rights reserved. Optimizer Statistics Chapter 10 .Gathering System Statistics: Example • Start manual system statistics collection in the data dictionary: SQL> EXECUTE DBMS_STATS.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Gathering System Statistics: Example (continued) The example in the previous slide shows how to collect system statistics with jobs by using the internal parameter of the DBMS_STATS. • • Generate the workload.Page 24 Oracle University and InfoTech (Pvt.GATHER_SYSTEM_STATS( 2 gathering_mode => 'STOP'). and then you can end the collection process at any time after you are certain that a representative workload has been generated on the instance. These are discussed in more detail in the subsequent slides. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Mechanisms for Gathering Statistics .Page 25 Oracle University and InfoTech (Pvt. Note: When the system encounters a table with missing statistics. All rights reserved. for certain types of tables. the optimizer uses default values for its statistics. In those cases and also when dynamic sampling has been disabled. It is recommended that you use automatic statistics gathering for objects. Copyright © 2010.Mechanisms for Gathering Statistics • Automatic statistics gathering – gather_stats_prog automated task • Manual statistics gathering – DBMS_STATS package • • Dynamic sampling When statistics are missing: Selectivity: Equality 1% Inequality 5% Other predicates 5% Table row length 20 # of index leaf blocks 25 # of distinct values 100 Table cardinality 100 Remote table cardinality 2000 Mechanisms for Gathering Statistics Oracle Database provides several mechanisms to gather statistics. These include remote tables and external tables. it does not perform dynamic sampling. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. However. Optimizer Statistics Chapter 10 . it dynamically gathers the necessary statistics needed by the optimizer. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Statistic Preferences: Overview . The only way to get around this issue was to manually specify the sample size in your own script.Page 26 Oracle University and InfoTech (Pvt. the feature that automatically tries to determine adequate sample size (ESTIMATE_PERCENT=AUTO_SAMPLE_SIZE) does not work well against columns that contain data with very high frequency skews. Oracle and/or its affiliates. Release 1 to reduce the burden of maintaining optimizer statistics.set_table_prefs('SH'. One reason was the lack of object-level control. Optimizer Statistics Chapter 10 . For example. delete. schema. Global preferences are used for tables that do not have preferences. there were cases where you had to disable it and run your own scripts instead. Whenever you found a small subset of objects for which the default gather statistics options did not work well.'13'). and import those preferences at the table. and global levels.'SALES'. You can use the DBMS_STATS package to manage the gathering statistics options shown in the slide. You can set. you had to lock the statistics and analyze them separately by using your own options. However. The Statistic Preferences feature in Oracle Database 11g introduces flexibility so that you can rely more on the automated statistics-gathering feature to maintain the optimizer statistics when some objects require settings that are different from the database default. This feature allows you to associate the statistics-gathering options that override the default behavior of the GATHER_*_STATS procedures and the automated Optimizer Statistics Gathering task at the object or schema level. Copyright © 2010.Statistic Preferences: Overview Optimizer statistics gathering task Statement level Table level DBA_TAB_STAT_PREFS Schema level Database level Global level CASCADE DEGREE ESTIMATE_PERCENT METHOD_OPT GRANULARITY NO_INVALIDATE INCREMENTAL PUBLISH STALE_PERCENT set_global_prefs set_database_prefs set_schema_prefs set_table_prefs gather_*_stats exec dbms_stats. get.'STALE_PERCENT'. export. database.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Statistic Preferences: Overview The automated statistics-gathering feature was introduced in Oracle Database 10g. All rights reserved. GRANULARITY is the granularity of statistics to collect for partitioned tables. METHOD_OPT is a SQL string used to collect histogram statistics. Index statistics gathering is not parallelized. . It is important to note that you can change default values for the above parameters using the DBMS_STATS.In the graphic in the slide. The default value is FOR ALL COLUMNS SIZE AUTO. STALE_PERCENT is used to determine the threshold level at which an object is considered to have stale statistics. This is the recommended default. The AUTO_DEGREE value determines the degree of parallelism automatically.AUTO_INVALIDATE to have the system decide when to invalidate dependent cursors. which means use the table default value specified by the DEGREE clause in the CREATE TABLE or ALTER TABLE statement. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED whereas database preferences are used to set preferences on all tables. the last three highlighted options are new in Oracle Database 11g. PUBLISH is used to decide whether to publish the statistics to the dictionary or to store them in a pending area before. Release 1: • • • • • • • • • CASCADE gathers statistics on the indexes as well.SET_GLOBAL_PREFS procedure. The example changes the 10 percent default to 13 percent for SH. depending on the size of the object.SALES only. ESTIMATE_PERCENT is the estimated percentage of rows used to compute statistics (Null means all rows): The valid range is [0. Use DBMS_STATS. DEGREE determines the degree of parallelism used to compute statistics.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.AUTO_SAMPLE_SIZE to have the system determine the appropriate sample size for good statistics. Oracle and/or its affiliates. It does not invalidate the dependent cursors if set to TRUE. This is either 1 (serial execution) or DEFAULT_DEGREE (the system default value based on the number of CPUs and initialization parameters). Use the constant DBMS_STATS.Page 27 Oracle University and InfoTech (Pvt. The preference values that are specified in various ways take precedence from the outer circles to the inner ones (as shown in the slide). Note: You can describe all the effective statistics preference settings for all relevant tables by using the DBA_TAB_STAT_PREFS view. The default for degree is null.DEFAULT_DEGREE to specify the default value based on the initialization parameters. The procedure invalidates the dependent cursors immediately if set to FALSE. All rights reserved.000001. The value is a percentage of rows modified since the last statistics gathering. NO_INVALIDATE controls the invalidation of dependent cursors of the tables for which statistics are being gathered. This is the default. Copyright © 2010. Use the constant DBMS_STATS. INCREMENTAL is used to gather global statistics on partitioned tables in an incremental way. Optimizer Statistics Chapter 10 .100]. Some cases may require manual statistics gathering. Oracle and/or its affiliates. – Remember that STATISTICS_LEVEL should be set to TYPICAL or ALL for automatic statistics collection to work properly. Because data manipulation is not allowed against external tables. Optimizer Statistics Chapter 10 . but you can change this interval to suit your business needs. There are typically two types of such objects: • Volatile tables that are modified significantly during the course of the day • Objects that are the target of large bulk loads that add 10% or more to the object’s total size between statistics-gathering intervals For external tables. When to Gather Statistics Manually The automatic statistics gathering mechanism gather statistics on schema objects in the database for which statistics are absent or stale. Copyright © 2010. it is sufficient to analyze external tables when the corresponding file changes. The default gathering interval is nightly. the statistics on tables that are significantly modified during the day may become stale. For example.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED When to Gather Statistics Manually . – External tables.Page 28 Oracle University and InfoTech (Pvt. All rights reserved. It is important to determine when and how often to gather new statistics. These statistics are not automatically gathered. so the ESTIMATE_PERCENT option should be explicitly set to null. fixed objects – New objects: Gather statistics right after object creation. system statistics. You can do so by changing the characteristics of your maintenance windows. statistics are only collected manually using GATHER_TABLE_STATS. such as the dynamic performance tables. Other areas in which statistics need to be manually gathered are the system statistics and fixed objects.When to Gather Statistics Manually • Rely mostly on automatic statistics collection: – Change the frequency of automatic statistics collection to meet your needs. • Gather statistics manually for: – Objects that are volatile – Objects modified in batch operations: Gather statistics as part of the batch operation. Sampling on external tables is not supported. You may continue to use the ANALYZE statement for other purposes not related to the optimizer statistics collection: • To use the VALIDATE or LIST CHAINED ROWS clauses • To collect information on free list blocks Copyright © 2010. The DBMS_STATS package collects a broader. and delete statistics. columns. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Manual Statistics Gathering . You can use the DBMS_STATS package to gather. lock. You can gather statistics on indexes. number of blocks currently containing data.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. or database • • Gather statistics either serially or in parallel Gather/Set system statistics (currently not possible in EM) Manual Statistics Gathering Both Enterprise Manager and the DBMS_STATS package enable you to manually generate and manage statistics for the optimizer. and gathers statistics more efficiently. the table statistics gathered by DBMS_STATS include the number of rows. All rights reserved. view. export. it does not gather other statistics. schema. modify. but not the number of chained rows. Oracle and/or its affiliates.Manual Statistics Gathering You can use Enterprise Manager and the DBMS_STATS package to: • Generate and manage statistics for use by the optimizer: – – – – • Gather/Modify View/Name Export/Import Delete/Lock Gather statistics on: – Indexes.Page 29 Oracle University and InfoTech (Pvt. partitions – Object. Note: Do not use the COMPUTE and ESTIMATE clauses of the ANALYZE statement to collect optimizer statistics. Optimizer Statistics Chapter 10 . You can also use this package to identify or name gathered statistics. more accurate set of statistics. and database level. and partitions at various granularity: object. import. For example. These clauses are supported solely for backward compatibility and may be removed in a future release. and average row length. tables. tables. DBMS_STATS gathers only statistics needed for optimization. average free space. or number of unused data blocks. columns. schema. Determine the degree of parallelism. Determine if histograms should be used. Oracle and/or its affiliates. All rights reserved.Manual Statistics Collection: Factors • • • • • • Monitor objects for DMLs.Page 30 Oracle University and InfoTech (Pvt. Optimizer Statistics Chapter 10 . Determine the cascading effects on indexes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Manual Statistics Collection: Factors . you must pay special attention to the following factors: • Monitoring objects for mass data manipulation language (DML) operations and gathering statistics if necessary • Determining the correct sample sizes • Determining the degree of parallelism to speed up queries on large objects • Determining if histograms should be created on columns with skewed data • Determining whether changes on objects cascade to any dependent indexes Copyright © 2010. Procedures to use in DBMS_STATS: – – – – – – GATHER_INDEX_STATS GATHER_TABLE_STATS GATHER_SCHEMA_STATS GATHER_DICTIONARY_STATS GATHER_DATABASE_STATS GATHER_SYSTEM_STATS Manual Statistics Collection: Factors When you manually gather optimizer statistics.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Determine the correct sample sizes. partition .table .column spec . false -. It uses some of the options discussed in the previous slides.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. true ).AUTO_CASCADE').'customers' -. subpartition. Copyright © 2010.'default' -. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Managing Statistics Collection: Example . -.degree of parallelism . null -. Note: Granularity of statistics to collect is pertinent only if the table is partitioned. 4 -. This parameter determines at which level statistics should be gathered.cascade to indexes dbms_stats. This can be at the partition.'NULL'). Setting Parameter Defaults You can use the SET_PARAM procedure in DBMS_STATS to set default values for parameters of all DBMS_STATS procedures.gather_table_stats ('sh' -. Managing Statistics Collection: Example The first example uses the DBMS_STATS package to gather statistics on the CUSTOMERS table of the SH schema.granularity .sample size(%) . dbms_stats.set_param('ESTIMATE_PERCENT'. dbms_stats.set_param('DEGREE'.'for all columns' -. The second example in the slide shows this usage.set_param('CASCADE'.'5'). Optimizer Statistics Chapter 10 .Page 31 Oracle University and InfoTech (Pvt. 20 -.Managing Statistics Collection: Example dbms_stats.schema . You can also use the GET_PARAM function to get the current default value of a parameter.block sample? . or table level. 'DBMS_STATS. Page 32 Oracle University and InfoTech (Pvt. The DYNAMIC_SAMPLING and DYNAMIC_SAMPLING_EST_CDN hints can be used to further control dynamic sampling. For example. the CBO provides a value through a simple algorithm that can lead to a suboptimal execution plan.Optimizer Dynamic Sampling: Overview • Dynamic sampling can be done for tables and indexes: – Without statistics – Whose statistics cannot be trusted • Used to determine more accurate statistics when estimating: – Table cardinality – Predicate selectivity • Feature controlled by: – – – – The OPTIMIZER_DYNAMIC_SAMPLING parameter The OPTIMIZER_FEATURES_ENABLE parameter The DYNAMIC_SAMPLING hint The DYNAMIC_SAMPLING_EST_CDN hint Optimizer Dynamic Sampling: Overview Dynamic sampling improves server performance by determining more accurate selectivity and cardinality estimates that allow the optimizer to produce better performing plans. Note: The OPTIMIZER_FEATURES_ENABLE initialization parameter turns off dynamic sampling if set to a version prior to 9. although it is recommended that you collect statistics on all of your tables for use by the CBO.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Dynamic Sampling: Overview . In these cases.2. All rights reserved. Copyright © 2010. You can use dynamic sampling to: • Estimate single-table predicate selectivities when collected statistics cannot be used or are likely to lead to significant errors in estimation • Estimate table cardinality for tables and relevant indexes without statistics or for tables whose statistics are too outdated to be reliable You control dynamic sampling with the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter. Optimizer Statistics Chapter 10 . Oracle and/or its affiliates. you may not gather statistics for your temporary tables and working tables used for intermediate data manipulation. a recursive SQL statement is issued to scan a small random sample of the table’s blocks. If a query benefits from dynamic sampling: – A recursive SQL statement is executed to sample data – The number of blocks sampled depends on the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter • • During dynamic sampling. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Dynamic Sampling at Work . Depending on the value of the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter. Optimizer Statistics Chapter 10 . Copyright © 2010. For a query that normally completes quickly (in less than a few seconds).Optimizer Dynamic Sampling at Work • • Sampling is done at compile time. Use dynamic sampling when: – Sampling time is a small fraction of the execution time – Query is executed many times – You believe a better plan can be found Optimizer Dynamic Sampling at Work The primary performance attribute is compile time. you do not want to incur the cost of dynamic sampling. However.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Note: Dynamic sampling can be applied to a subset of a single table’s predicates and combined with standard selectivity estimates of predicates for which dynamic sampling is not done. Oracle and/or its affiliates. predicates are applied to the sample to determine selectivity. and to apply the relevant single table predicates to estimate predicate selectivities. dynamic sampling can be beneficial under any of the following conditions: • A better plan can be found using dynamic sampling. • The query is executed many times. a certain number of blocks is read by the dynamic sampling query.Page 33 Oracle University and InfoTech (Pvt. The system determines at compile time whether a query would benefit from dynamic sampling. If so. • The sampling time is a small fraction of total execution time for the query. A value of "1" means dynamic sampling is performed on all unanalyzed tables if the following criteria are met: • There is at least one unanalyzed table in the query. All rights reserved. Optimizer Statistics Chapter 10 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The default value is "2" if OPTIMIZER_FEATURES_ENABLE is set to 10. The number of blocks sampled is two times the default number of dynamic sampling blocks (32). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED OPTIMIZER_DYNAMIC_SAMPLING .0 or higher. This default number is 32. • This unanalyzed table has more blocks than the default number of blocks that would be used for dynamic sampling of this table.OPTIMIZER_DYNAMIC_SAMPLING • • • • Dynamic session or system parameter Can be set to a value from "0" to "10" "0" turns off dynamic sampling "1" samples all unanalyzed tables. in terms of both the type of tables sampled (analyzed or unanalyzed) and the amount of I/O spent on sampling. if an unanalyzed table: – Is joined to another table or appears in a subquery or nonmergeable view – Has no indexes – Has more than 32 blocks • • • "2" samples all unanalyzed tables The higher the value the more aggressive application of sampling Dynamic sampling is repeatable if no update activity OPTIMIZER_DYNAMIC_SAMPLING You control dynamic sampling with the OPTIMIZER_DYNAMIC_SAMPLING parameter. the system applies dynamic sampling to all unanalyzed tables.0. which can be set to a value from "0" to "10. Oracle and/or its affiliates. • This unanalyzed table has no indexes. Copyright © 2010. • This unanalyzed table is joined to another table or appears in a subquery or nonmergeable view. At this level.Page 34 Oracle University and InfoTech (Pvt." A value of "0" means dynamic sampling is not done. Increasing the value of the parameter results in more aggressive application of dynamic sampling. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: Dynamic sampling is repeatable if no rows have been inserted.Page 35 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. deleted. Oracle and/or its affiliates. Copyright © 2010. All rights reserved.Oracle University and InfoTech (Pvt. or updated in the table being sampled since the previous sample operation. Optimizer Statistics Chapter 10 . DBMS_STATS. You can use the UNLOCK_TABLE_STATS procedure to unlock the statistics on a specified table. you can lock statistics on a specified table with the LOCK_TABLE_STATS procedure of the DBMS_STATS package. This includes table statistics. Note: When you lock the statistics on a table. column statistics. and dependent index statistics. histograms.'ORDERS'). BEGIN DBMS_STATS. RESTORE_*_STATS.Locking Statistics • • Prevents automatic gathering Is mainly used for volatile tables: – Lock without statistics implies dynamic sampling. The FORCE argument is found in the following DBMS_STATS procedures: DELETE_*_STATS. Locking Statistics Starting with Oracle Database 10g. BEGIN DBMS_STATS. and SET_*_STATS. Optimizer Statistics Chapter 10 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.LOCK_TABLE_STATS('OE'. You can also lock statistics at the schema level by using the LOCK_SCHEMA_STATS procedure. – Lock with statistics for representative values. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Locking Statistics .LOCK_TABLE_STATS('OE'.Page 36 Oracle University and InfoTech (Pvt. You can query the STATTYPE_LOCKED column in the {USER | ALL | DBA}_TAB_STATISTICS view to determine whether the statistics on the table are locked. All rights reserved.'ORDERS'). END. Oracle and/or its affiliates. END. IMPORT_*_STATS. DBMS_STATS.DELETE_TABLE_STATS('OE'. SELECT stattype_locked FROM dba_tab_statistics. You can set the value of the FORCE parameter to TRUE to overwrite the statistics even if they are locked. You can also lock statistics on a volatile table at a point when it is fully populated so that the table statistics are more representative of the table population.'ORDERS'). You can lock statistics on a table without statistics or set them to NULL using the DELETE_*_STATS procedures to prevent automatic statistics collection so that you can use dynamic sampling on a volatile table with no statistic.'ORDERS').GATHER_TABLE_STATS('OE'. • The FORCE argument overrides statistics locking. all the dependent statistics are considered locked. Copyright © 2010. AS_OF_TIMESTAMP=>'15-JUL-10 09. You can restore statistics using RESTORE procedures of DBMS_STATS package. The default value is 31 days.28. END.RESTORE_TABLE_STATS( OWNNAME=>'OE'. TABNAME=>'INVENTORIES'. Note: the ANALYZE command does not store old statistics.Page 37 Oracle University and InfoTech (Pvt. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Restoring Statistics . which means that you would be able to restore the optimizer statistics to any time in last 31 days. or USER) contain a history of table statistics modifications. These procedures use a time stamp as an argument and restore statistics as of that time stamp. Oracle and/or its affiliates.597526000 AM -05:00').Restoring Statistics • Past Statistics may be restored with DBMS_STATS. • Statistics are automatically stored – With the timestamp in DBA_TAB_STATS_HISTORY – When collected with DBMS_STATS procedures • Statistics are purged – When STATISTICS_LEVEL is set to TYPICAL or ALL automatically – After 31 days or time defined by DBMS_STATS.01. DBA. The views *_TAB_STATS_HISTORY views (ALL.RESTORE_*_STATS procedures BEGIN DBMS_STATS. This is useful when newly collected statistics lead to suboptimal execution plans and the administrator wants to revert to the previous set of statistics. You can configure retention using the DBMS_STATS .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010.ALTER_STATS_HISTORY_RETENTION procedure.ALTER_STATS_HISTORY_RETENTION Restoring Statistics Old versions of statistics are saved automatically whenever statistics in dictionary are modified with the DBMS_STATS procedures. For the example in the slide the timestamp was determined by: select stats_update_time from dba_tab_stats_history where table_name = 'INVENTORIES' The database purges old statistics automatically at regular intervals based on the statistics history retention setting and the time of the recent analysis of the system. There are dictionary views that can be used to determine the time stamp for restoration of statistics. Optimizer Statistics Chapter 10 . To move statistics from one database to another. Oracle and/or its affiliates. All rights reserved. • IMPORT_*_STATS moves the statistics to data dictionary. The optimizer does not use statistics stored in a user-owned table. enabling you to create multiple versions of statistics for the same schema. Copyright © 2010. After table creation.EXPORT_*_STATS procedures.Export and Import Statistics Use DBMS_STATS procedures: • CREATE_STAT_TABLE creates the statistics table. • EXPORT_*_STATS moves the statistics to the statistics table. then copy the statistics table to the second database. To have the optimizer use the statistics in a user-owned tables. The only statistics used by the optimizer are the statistics stored in the data dictionary. • Use Data Pump to move the statistics table.IMPORT_*_STATS procedures. You can also copy statistics from one database to another database. and finally import the statistics into the second database. you can export statistics from the data dictionary into the statistics table using the DBMS_STATS. expdp impdp EXPORT_*_STATS IMPORT_*_STATS Export and Import Statistics You can export and import statistics from the data dictionary to user-owned tables.Page 38 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Export and Import Statistics . The procedure DBMS_STATS. you first need to create a table for holding the statistics. Optimizer Statistics Chapter 10 . Before exporting statistics.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you must first export the statistics on the first database. You may want to do this to copy the statistics from a production database to a scaled-down test database.CREATE_STAT_TABLE creates the statistics table. you must import those statistics into the data dictionary using the statistics import procedures. using the Data Pump Export and Import utilities or other mechanisms. You can then import statistics using the DBMS_STATS. Statistics gathered during parse phase e. c Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . the optimizer uses: a. Fixed values d.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Dynamic sampling c.Page 39 Oracle University and InfoTech (Pvt. Optimizer Statistics Chapter 10 . Random values Answer: b. Rule-based optimization b. Oracle and/or its affiliates. All rights reserved.Quiz When there are no statistics for an object being used in a SQL statement. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Volatile tables e.Page 40 Oracle University and InfoTech (Pvt. The automatic statistics gathering (AGS) task does not gather statistics on everything. Which objects require you to gather statistics manually? a. Fixed objects d.Quiz The optimizer depends on accurate statistics to produce the best execution plans. Oracle and/or its affiliates. c. Data dictionary c. External tables b. All rights reserved. e Copyright © 2010. Optimizer Statistics Chapter 10 . System statistics Answer: a.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. b. Lock statistics.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Delete statistics. All rights reserved. Gather statistics when the table is at its largest. d. b. Oracle and/or its affiliates. Optimizer Statistics Chapter 10 . e.Quiz There is a very volatile table in the database. Allow the DYNAMIC_SAMPLING parameter to default. What steps are part of the procedure to force dynamic sampling? a. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . f Copyright © 2010. Set DYNAMIC_SAMPLING=0. The size of the table changes by more than 50% daily. c. Set DYNAMIC_SAMPLING=9. Answer: a.Page 41 Oracle University and InfoTech (Pvt. f. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary . All rights reserved.Page 42 Oracle University and InfoTech (Pvt. Optimizer Statistics Chapter 10 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you should have learned how to: • Collect optimizer statistics • Collect system statistics • Set statistic preferences • Use dynamic sampling • Manipulate optimizer statistics Copyright © 2010. Oracle and/or its affiliates.Summary In this lesson. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 10: Overview Copyright © 2010. Optimizer Statistics Chapter 10 . All rights reserved.Practice 10: Overview This practice covers the following topics: • Using system statistics • Using automatic statistics gathering Oracle University and InfoTech (Pvt.Page 43 . Oracle and/or its affiliates. Oracle and/or its affiliates.Oracle University and InfoTech (Pvt. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010.Page 44 . Optimizer Statistics Chapter 10 . THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 1 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 11 Copyright © 2010. Oracle and/or its affiliates. Using Bind Variables Chapter 11 .) Ltd use only Using Bind Variables . Oracle and/or its affiliates.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Bind Variables Copyright © 2010. Using Bind Variables Chapter 11 .Page 2 Oracle University and InfoTech (Pvt.) Ltd use only Using Bind Variables . All rights reserved. Using Bind Variables Chapter 11 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you should be able to: • List the benefits of using bind variables • Use bind peeking • Use adaptive cursor sharing Copyright © 2010.Page 3 Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates.Objectives After completing this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . If the cursor is shared using a bind variable rather than a literal. there might be very few JOBS that have a MIN_SALARY greater than 18000. Using Bind Variables Chapter 11 . This may also cause the library cache to fill up quickly because of all the different statements stored in it. SELECT * FROM jobs WHERE min_salary > 18000.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. this is not very economic because you could have shared cursors for the first and last queries in this example. depending on the literal value provided. SELECT * FROM jobs WHERE min_salary > 7500. but the second query uses a different one. with one execution plan. the first and third queries use the same execution plan. the optimizer must perform all the steps for processing a new SQL statement. you are not taking advantage of cursor sharing.Page 4 Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates. Copyright © 2010. For example. From a performance perspective. As you can see. it is good to have separate cursors. there will be many versions of almost identical SQL stored in the library cache.Cursor Sharing and Different Literal Values SELECT * FROM jobs WHERE min_salary > 12000. When coded this way. This difference in data distribution could justify the addition of an index so that different plans can be used depending on the value provided in the query. However. This is illustrated in the slide. there will be one shared cursor. However. where MIN_SALARY is greater than 12000. For each different SQL statement. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cursor Sharing and Different Literal Values . Alternatively. Cursor sharing Library cache Cursor Sharing and Different Literal Values If your SQL statements use literal values for the WHERE clause conditions. different execution plans might be generated by the optimizer. there might be several JOBS. Using Bind Variables Chapter 11 .Page 5 . V$SQL. Copyright © 2010.Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: In the case of the example in the slide.PLAN_HASH_VALUE is identical for the first and third query. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. 18000 SELECT * FROM jobs WHERE min_salary > :min_sal. All rights reserved. issue a single statement that uses a bind variable. the given value is substituted for the variable. instead of issuing different statements for each literal. However. At execution time. the bind variable is called min_sal.Cursor Sharing and Bind Variables 12000 SELECT * FROM jobs WHERE min_salary > :min_sal. Cursor sharing Library cache Cursor Sharing and Bind Variables If. Copyright © 2010. This is because the optimizer recognizes that the statement is already parsed and decides to reuse the same execution plan even though you specified different bind values the next time you execute the same statement. On the other hand.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In the example in the slide. this is not the best situation because you get best performance two times out of three. this is very economic because you just need one shared cursor in the library cache to execute all the three statements. Instead of issuing three different statements. from a performance perspective. then that extra parse activity is eliminated (in theory). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cursor Sharing and Bind Variables . Using Bind Variables Chapter 11 . the same execution plan is used.Page 6 Oracle University and InfoTech (Pvt. you use a bind variable. 17500 SELECT * FROM jobs WHERE min_salary > :min_sal. It is to be compared with the MIN_SALARY column of the JOBS table. SQL> select count(*) from employees where job_id = :job_id.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the first count is selected while SA_REP is assigned to the variable. Any references to that variable from then on use the value you assigned.Bind Variables in SQL*Plus SQL> variable job_id varchar2(10) SQL> exec :job_id := 'SA_REP'. SQL> select count(*) from employees where job_id = :job_id. AD_VP is assigned to the variable. and the resulting count is 2. Oracle and/or its affiliates. Then. In SQL*Plus. In the example in the slide. you can assign values to the variable by executing an assignment statement with the EXEC[UTE] command. COUNT(*) ---------2 Bind Variables in SQL*Plus Bind variables can be used in SQL*Plus sessions. Using Bind Variables Chapter 11 . Then.Page 7 Oracle University and InfoTech (Pvt. use the VARIABLE command to define a bind variable. Copyright © 2010. COUNT(*) ---------30 SQL> exec :job_id := 'AD_VP'. PL/SQL procedure successfully completed. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bind Variables in SQL*Plus . All rights reserved. PL/SQL procedure successfully completed. The result is 30. several fields are generated. you can specify that a SQL statement should use bind variables. All rights reserved. The order in which variables are referred to defines which variable gets which value. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bind Variables in Enterprise Manager . Refer to these values in the SQL statement using variable names that begin with a colon. You can do this by selecting the "Use bind variables for execution" check box. When you select that. Using Bind Variables Chapter 11 .Page 8 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and so on. you may need to change the value list to match that order. Oracle and/or its affiliates. Copyright © 2010. If you change the order in which variables are referenced in the statement.Bind Variables in Enterprise Manager Bind Variables in Enterprise Manager On the SQL Worksheet page of Enterprise Manager (see the SQL Worksheet link in the Related Links region of the Database Home page). The first variable referred to gets the first value. where you can enter bind variable values. the second variable gets the second value. Copyright © 2010.Page 9 Oracle University and InfoTech (Pvt.Bind Variables in SQL Developer Bind Variables in SQL Developer On the SQL Worksheet pane of SQL. Select each bind variable in turn to enter a value for that variable. Using Bind Variables Chapter 11 . the Enter Binds dialog appears where you can enter bind variable values. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bind Variables in SQL Developer .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you can specify that a SQL statement that uses bind variables. Oracle and/or its affiliates. All rights reserved. When you execute the statement . Refer to these values in the SQL statement using variable names that begin with a colon. the optimizer evaluates the value for each bind variable. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bind Variable Peeking . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. to ensure that a good plan that would suit those values is selected. Release 2. as a SQL statement is parsed. it is reused when you execute the same statement regardless of the bind values used. The optimizer does this by peeking at the value in the bind variable. those literal values can be used by the optimizer to decide on the best plan. Oracle Database 11g changes this behavior. the system needs to be able to see the value of the bind variables. That means. After the execution is determined the first time you parsed the query. the optimizer still needs to select the best plan based on the values of the conditions in the query.Page 10 Oracle University and InfoTech (Pvt.Bind Variable Peeking SELECT * FROM jobs WHERE min_salary > :min_sal 12000 First time you execute Plan A min_sal=12000 Next time you execute SELECT * FROM jobs WHERE min_salary > :min_sal 18000 Bind Variable Peeking When literals are used in a query. and uses that as input in determining the best plan. but cannot see those values readily in the SQL text. All rights reserved. When the SQL statement is hard parsed. when bind variables are used. However. Copyright © 2010. Using Bind Variables Chapter 11 . This feature was introduced in Oracle9i Database. plan A may be good for the first and third values of min_sal. bind variable peeking can cause the optimizer to select the suboptimal plan. it is possible that they cause a suboptimal plan to be chosen for some of the bind variable values. An example is where the selectivity of a particular index varies extremely depending on the column value. Suppose there are very few MIN_SALARY values that are above 18000. Therefore. in that case. To execute with a slower plan is not worth the savings gained in parse time. even though subsequent executions provide different bind values. This occurs because the first value of the bind variable is used to determine the plan for all subsequent executions of the query. where the parsing of the query is a very small percentage of the work done when submitting a query. So bind variables are beneficial in that they cause more cursor sharing to happen. the same plan is used. As shown in the slide. All rights reserved. This is a good reason for not using bind variables for decision support system (DSS) environments. But. The parsing may take fractions of a second. and plan A is a full table scan. For high selectivity. Copyright © 2010. but the execution may take minutes or hours. For low selectivity. as in this case. but it may not be the best for the second one. Oracle and/or its affiliates. an index range scan may be more appropriate. and thus reduce parsing of SQL. It is probable that a full table scan is not a good plan for the second execution. It is possible that a different plan would be better for executions that have different bind variable values.Page 11 Oracle University and InfoTech (Pvt. a full table scan may be faster. Using Bind Variables Chapter 11 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Bind Variable Peeking .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Bind Variable Peeking SELECT * FROM jobs WHERE min_salary > :min_sal 1 min_sal=12000 2 min_sal=18000 3 min_sal=7500 Plan A One plan not always appropriate for all bind values Bind Variable Peeking (continued) Under some conditions. All rights reserved. Using Bind Variables Chapter 11 . That is why Oracle9i extends this feature by sharing only statements considered as similar. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Cursor Sharing Enhancements . Oracle9i extends this feature by limiting it to similar statements only. For example. Similar: Regardless of the literal value. It would. only when the optimizer has the guarantee that the execution plan is independent of the literal value used. consider a query. same execution plan SQL> SELECT * FROM employees 2 WHERE employee_id = 153. • Not similar: Possible different execution plans for different literal values SQL> SELECT * FROM employees 2 WHERE department_id = 50. For example. this feature should be tested against your applications before you decide to enable it or not. be safe for the optimizer to generate only one plan for different occurrences of the same statement executed with different literal values. Because only one execution plan is used instead of potential different ones. Cursor Sharing Enhancements Oracle8i introduced the possibility of sharing SQL statements that differ only in literal values. instead of forcing it. Rather than developing an execution plan each time the same statement—with a different literal value—is executed. the optimizer generates a common execution plan used for all subsequent executions of the statement. where EMPLOYEE_ID is the primary key: SQL> SELECT * FROM employees WHERE employee_id = 153.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 12 Oracle University and InfoTech (Pvt. assume that the same EMPLOYEES table has a wide range of values in its DEPARTMENT_ID column. On the other hand. See the two queries: SQL> SELECT * FROM employees WHERE department_id = 50. The substitution of any value would produce the same execution plan. department 50 could contain over one third of all employees and department 70 could contain just one or two.Cursor Sharing Enhancements • • • Oracle8i introduced the possibility of sharing SQL statements that differ only in literal values. Oracle and/or its affiliates. That is. therefore. Page 13 . In this case.Using only one execution plan for sharing the same cursor would not be safe if you have histogram statistics (and there is skew in the data) on the DEPARTMENT_ID column. All rights reserved. depending on which statement was executed first. or it could use a simple index range scan. Copyright © 2010. the execution plan could contain a full table (or fast full index) scan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL> SELECT * FROM employees WHERE department_id = 70.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using Bind Variables Chapter 11 . Oracle and/or its affiliates. Oracle University and InfoTech (Pvt. The CURSOR_SHARING Parameter • The CURSOR_SHARING parameter values: – FORCE – EXACT (default) – SIMILAR • CURSOR_SHARING can be changed using: – ALTER SYSTEM – ALTER SESSION – Initialization parameter files • The CURSOR_SHARING_EXACT hint The CURSOR_SHARING Parameter The value of the CURSOR_SHARING initialization parameter determines how the optimizer processes statements with bind variables: • EXACT: Literal replacement disabled completely • FORCE: Causes sharing for all literals • SIMILAR: Causes sharing for safe literals only In earlier releases. you could select only the EXACT or the FORCE option. Setting the value to SIMILAR causes the optimizer to examine the statement to ensure that replacement occurs only for safe literals.Page 14 Oracle University and InfoTech (Pvt. The CURSOR_SHARING_EXACT hint causes the system to execute the SQL statement without any attempt to replace literals by bind variables. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The CURSOR_SHARING Parameter . Using Bind Variables Chapter 11 . The value of CURSOR_SHARING in the initialization file can be overridden with an ALTER SYSTEM SET CURSOR_SHARING or an ALTER SESSION SET CURSOR_SHARING command. Oracle and/or its affiliates. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It can then use information about the nature of any available index (unique or nonunique) and statistics collected on the index or underlying table. including histograms. you end up with only one child cursor instead of three.Forcing Cursor Sharing: Example SQL> alter session set cursor_sharing = FORCE. Using Bind Variables Chapter 11 . SELECT * FROM jobs WHERE min_salary > 18000. As a result. Oracle and/or its affiliates. SELECT * FROM jobs WHERE min_salary > 7500. SELECT * FROM jobs WHERE min_salary > 12000. Note: Adaptive cursor sharing may also apply. SELECT * FROM jobs WHERE min_salary > :"SYS_B_0" System-generated bind variable Forcing Cursor Sharing: Example Because you forced cursor sharing with the ALTER SESSION command. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Forcing Cursor Sharing: Example . Copyright © 2010. All rights reserved. and might generate a second child cursor in this case. all your queries that differ only with literal values are automatically rewritten to use the same system-generated bind variable called SYS_B_0 in the example in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 15 Oracle University and InfoTech (Pvt. Oracle9i also introduced the concept of bind peeking. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Adaptive Cursor Sharing: Overview . because the purpose of using bind variables is to share cursors in memory. Using Bind Variables Chapter 11 . cursor sharing and SQL optimization are conflicting goals. a compromise must be found regarding the number of child cursors that need to be generated. To address this issue as much as possible. Oracle and/or its affiliates. However. Copyright © 2010. Oracle9i Database was the first attempt to introduce a compromise solution by allowing similar SQL statements using different literal values to be shared. it is assumed that cursor sharing is intended and that different invocations of the statement are supposed to use the same execution plan. To benefit from bind peeking.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Adaptive Cursor Sharing: Overview Adaptive cursor sharing: • Allows for intelligent cursor sharing for statements that use bind variables • Is used to compromise between cursor sharing and optimization • Has the following benefits: – Automatically detects when different executions would benefit from different execution plans – Limits the number of generated child cursors to a minimum – Provides an automated mechanism that cannot be turned off Adaptive Cursor Sharing: Overview Bind variables were designed to allow the Oracle Database to share a single cursor for multiple SQL statements to reduce the amount of shared memory used to parse SQL statements. but generate multiple plans per SQL statement with bind variables if the benefit of using multiple execution plans outweighs the parse time and memory usage overhead. However. All rights reserved. bind peeking is of no use in generating good execution plans. while increasing memory and CPU overhead caused by excessive hard parses.Page 16 Oracle University and InfoTech (Pvt. For statements using bind variables. This feature is a more sophisticated strategy designed to not share the cursor blindly. If different invocations of the statement would significantly benefit from different execution plans. Writing a SQL statement with literals provides more information for the optimizer and naturally leads to better execution plans. Oracle Database 11g introduces adaptive cursor sharing. In the slide example.0. 2 HJ 0.004 . At the end of execution. Using Bind Variables Chapter 11 . On the next soft parse of this query. Because selectivity is used as part of bind-aware cursor Copyright © 2010. some information is stored about the predicate containing the bind variables. including the predicate selectivity.003 0.18 :1=A & :2=B S(:1)=0. The system then observes the pattern of the statistics over all the previous runs (see V$SQL_CS_… views in the slide that follows) and decides whether or not to mark the cursor as bind aware.15 Merged selectivity cubes P a r s e 0. GB GB HJ HJ HJ P a r s e 0.18 ∧ S(:2)=0. No need for new plan GB GB HJ HJ HJ HJ Cubes merged :1=C & :2=D S(:1)=0.0025).18.15 ∧ S(:2)=0. After the cursor is executed. Suppose the selectivity of the predicate with the new set of bind values is now (0.28 Need new plan HJ 3 0.0025 HJ .3 ∧ S(:2)=0. as usual. then the cursor is marked as a bind-sensitive cursor. and a histogram is used to compute selectivity of the predicate containing the bind variable.28 ∧ S(:2)=0. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Adaptive Cursor Sharing: Architecture .0. In addition.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If bind peeking takes place. Oracle and/or its affiliates.Page 17 Oracle University and InfoTech (Pvt. The cursor starts its life with a hard parse.003 Second selectivity cube H a r d 0. the bind values and the execution statistics of the cursor are stored in that cursor. During the next execution of the statement when a new set of bind values is used. the system performs a usual soft parse.15.009 . the predicate selectivity that would be stored is a cube centered around (0. and finds the matching cursor for execution.Adaptive Cursor Sharing: Architecture Bind-sensitive cursor System observes statement for a while.009 Adaptive Cursor Sharing: Architecture When you use adaptive cursor sharing. 2. Because of the initial hard parse.004 :1=E & :2=F S(:1)=0.0025 H a r d No need for new plan Same selectivity cube S o f t HJ 4 11 SELECT * FROM emp WHERE sal = :1 and dept = :2 . All rights reserved. execution statistics are compared with the ones currently stored in the cursor.003). P a r s e GB HJ 0. an initial execution plan is determined using the peeked binds. if the cursor is now bind aware. Bind-aware cursor Initial selectivity cube Initial plan GB 0. the following steps take place in the scenario illustrated in the slide: 1.3 :1=G & :2=H S(:1)=0. bind-aware cursor matching is used. and because the selectivity is within an existing cube.3. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED matching. Oracle and/or its affiliates.0. All rights reserved. On the next soft parse of this query. On the next soft parse of this query. no child cursor match is found. Copyright © 2010. Because the plan is the same as the first child cursor. suppose that the selectivity of the predicate with the new set of bind values is now (0. After the new child cursor executes. So the system does a hard parse. the statement uses the existing child cursor’s execution plan to run. the new selectivity cube is stored as part of the new child cursor. The next time there is a soft parse. both cubes are merged into a new bigger cube. both child cursors are merged. the system does a hard parse. the child cursor matches. Because that selectivity is not within an existing cube.004). the system stores the bind values and execution statistics in the cursor. Using Bind Variables Chapter 11 . suppose the selectivity of the predicate with the new set of bind values is now (0.0. . Suppose that this time. Because that selectivity is not within one of the existing cubes. That is.Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and one of the child cursors is deleted. which generates a new child cursor with a second execution plan in that case.009). the hard parse generates the same execution plan as the first one. 4. In addition. if the selectivity falls within the new cube.28.3. if any. Using Bind Variables Chapter 11 . New columns have been added to V$SQL: • • • • IS_BIND_SENSITIVE: Indicates if a cursor is bind sensitive. information about what goes on is exposed through V$ views so that you can diagnose problems. All rights reserved. However. value YES | NO. without any user input. Oracle and/or its affiliates. V$SQL_CS_HISTOGRAM Shows the distribution of the execution count across the execution history histogram V$SQL_CS_SELECTIVITY Shows the selectivity cubes stored for every predicate containing a bind variable and whose selectivity is used in the cursor sharing checks V$SQL_CS_STATISTICS Shows execution statistics of a cursor using different bind sets Adaptive Cursor Sharing: Views These views determine whether a query is bind aware or not. and is handled automatically. V$SQL_CS_SELECTIVITY: Shows the selectivity cubes or ranges stored in a cursor for every predicate containing a bind variable and whose selectivity is used in the cursor sharing checks. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Adaptive Cursor Sharing: Views . A query for which the optimizer peeked at bind variable values when computing predicate selectivities and where a change in a bind variable value may lead to a different plan is called bind sensitive. IS_BIND_AWARE: Indicates if a cursor is bind aware. value YES | NO.Adaptive Cursor Sharing: Views The following views provide information about adaptive cursor sharing usage: V$SQL Two new columns show whether a cursor is bind sensitive or bind aware.Page 19 Oracle University and InfoTech (Pvt. A cursor in the cursor cache that has been marked to use bind-aware cursor sharing is called bind aware.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. V$SQL_CS_HISTOGRAM: Shows the distribution of the execution count across a threebucket execution history histogram. It contains the text of the predicates and the selectivity range low and high values. V$SQL_CS_STATISTICS: Adaptive cursor sharing monitors execution of a query and collects information about it for a while. Oracle University and InfoTech (Pvt. buffer gets. For a sample of executions.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Using Bind Variables Chapter 11 .Page 20 . and CPU time. The PEEKED column has the value YES if the bind set was used to build the cursor. This view summarizes the information that it collects to make this decision. and uses this information to decide whether to switch to using bind-aware cursor sharing for the query. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. and NO otherwise. Oracle and/or its affiliates. it keeps track of the rows processed. This means the best plan is used for the query. at which point it changes the plan based on the bound value. if literals were used instead of a bind variable.Adaptive Cursor Sharing: Example SQL> variable job varchar2(6) SQL> exec :job := 'AD_ASST' SQL> select count(*). Using Bind Variables Chapter 11 . Copyright © 2010. If the table has over a million rows in it. whereas the SA_REP value occurs in approximately a third of the rows. After several executions of this query using a bind variable. it is more efficient to read the entire table. showing that there are many thousands times more occurrences of SA_REP than AD_ASST. the system considers the query bind aware. Oracle and/or its affiliates. There are histogram statistics on the JOB_ID column.Page 21 Oracle University and InfoTech (Pvt. the execution plans are different for each of these values’ queries. they use the same plan. based on the bind variable value. In this case. even though there exist different and better plans for each of these values. So. using a bind variable causes the same execution plan to be used for both of the values. as it is.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. at first. But. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Adaptive Cursor Sharing: Example . The SA_REP query results in a full table scan because so many of the rows have that value. All rights reserved. Selectivity Plan A 'SA_REP' Plan B 'AD_ASST' Adaptive Cursor Sharing: Example Consider the data in the slide. the query optimizer would see that the AD_ASST value occurs in less than 1% of the rows. max(salary) from emp where job_id=:job. The AD_ASST query results in an index range scan because there are so few rows with that value. The setting of this parameter determines whether literals are replaced by the system-generated bind variables. all the plans for a single SQL statement are marked as SQL baseline plans by default. So even though adaptive cursor sharing has come up with a new plan based on a new set of bind values. • SQL Plan Management (SPM): – If OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES is set to TRUE. only the first generated plan is used. If another plan is found for that same SQL statement (which maybe the case with adaptive cursor sharing). Thus reverting to10g behavior. set this parameter to FALSE. it is added to the SQL statements plan history and marked for verification. – As a workaround. and run your application until all plans are loaded in the cursor cache. statements containing literals may be rewritten using bind variables.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. only the plan generated based on the first set of bind values is used by all subsequent executions of the statement. Using Bind Variables Chapter 11 . If they are.Interacting with Adaptive Cursor Sharing • CURSOR_SHARING: – If CURSOR_SHARING <> EXACT. One possible workaround is to run the system for some time with automatic plan capture set to False.Page 22 Oracle University and InfoTech (Pvt. All rights reserved. SPM does not let it be used until the plan has been verified. – If statements are rewritten. It will not be used immediately. and after the cursor cache has been populated with all the plans a SQL statement with bind has. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Interacting with Adaptive Cursor Sharing . the first plan captured for a SQL statement with bind variables is marked as the corresponding SQL plan baseline. load the entire plan directly from the cursor cache into the corresponding SQL plan baseline. Refer to the lesson titled "SQL Plan Management" for more information. adaptive cursor sharing behaves just as it would if the user supplied binds to begin with. By doing this. Interacting with Adaptive Cursor Sharing • Adaptive cursor sharing is independent of the CURSOR_SHARING parameter. Oracle and/or its affiliates. • When using the SPM automatic plan capture. adaptive cursor sharing may apply to them. – Manually load the cursor cache into the corresponding plan baseline. Copyright © 2010. Histograms are used if available. All rights reserved. Histograms are not used. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . f. f Copyright © 2010.Quiz Which three statements are true about applications that are coded with literals in the SQL statements rather than bind variables? a. No parsing is required for literal values. c. Oracle and/or its affiliates. Less shared pool space is required for cursors. d.Page 23 Oracle University and InfoTech (Pvt. More shared pool space is required for cursors. Answer: a. b. Every different literal value requires parsing.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. e. Using Bind Variables Chapter 11 . c. All rights reserved. Oracle and/or its affiliates. True f.Page 24 Oracle University and InfoTech (Pvt. Exact d. Literal e. Similar b. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Force c. a. False Answer: c Copyright © 2010.Quiz The CURSOR_SHARING parameter should be set to ________ for systems with large tables and long-running queries such as a data warehouse. Using Bind Variables Chapter 11 . Page 25 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz Answer: b Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. True b. Using Bind Variables Chapter 11 .Quiz Adaptive Cursor Sharing can be turned off by setting the CURSOR_SHARING parameter to FALSE. Oracle and/or its affiliates. False Oracle University and InfoTech (Pvt. a. Oracle and/or its affiliates. you should have learned how to: • List the benefits of using bind variables • Use bind peeking • Use adaptive cursor sharing Copyright © 2010. Using Bind Variables Chapter 11 .Page 26 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary . All rights reserved.Summary In this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using Bind Variables Chapter 11 .Page 27 . Oracle and/or its affiliates. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 11: Overview Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Practice 11: Overview This practice covers the following topics: • Using adaptive cursor sharing and bind peeking • Using the CURSOR_SHARING initialization parameter Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010. All rights reserved.Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using Bind Variables Chapter 11 .Page 28 . ) Ltd use only SQL Tuning Advisor . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 12 Copyright © 2010.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Tuning Advisor Chapter 12 .Page 1 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. All rights reserved. SQL Tuning Advisor Chapter 12 . Oracle and/or its affiliates. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Advisor Copyright © 2010.Page 2 Oracle University and InfoTech (Pvt.) Ltd use only SQL Tuning Advisor .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 3 Oracle University and InfoTech (Pvt. you should be able to do the following: • Describe statement profiling • Use SQL Tuning Advisor Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Objectives After completing this lesson. SQL Tuning Advisor Chapter 12 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . When called under tuning mode. This automatic process replaces manual SQL tuning. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Tuning SQL Statements Automatically . but a series of actions. the optimizer operates with very strict time constraints. along with their rationale and expected benefit (for producing a significantly superior plan). SQL Tuning Advisor exposes the features of SQL tuning to the user. The tuning performed by ATO is called system SQL tuning. Under the tuning mode. and time-consuming function. during which it must find a good execution plan. the optimizer is referred to as Automatic Tuning Optimizer (ATO). In the normal mode. the optimizer performs additional analysis to check whether the execution plan produced under the normal mode can be further improved. the optimizer compiles SQL and generates an execution plan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. SQL Tuning Advisor Chapter 12 . Optimizer modes: – Normal mode – Tuning mode or Automatic Tuning Optimizer (ATO) • • SQL Tuning Advisor is used to access tuning mode. Tuning SQL Statements Automatically Tuning SQL statements automatically is the capability of the query optimizer to automate the entire SQL tuning process. usually a fraction of a second. All rights reserved. • In the tuning mode.Page 4 Oracle University and InfoTech (Pvt. The output of the query optimizer in the tuning mode is not an execution plan. Oracle and/or its affiliates.Tuning SQL Statements Automatically • • Tuning SQL statements automatically eases the entire SQL tuning process and replaces manual SQL tuning. repetitive. You should use tuning mode only for high-load SQL statements. the optimizer can take several minutes to tune a single statement. The normal mode of the optimizer generates a reasonable execution plan for the vast majority of SQL statements. The enhanced query optimizer has two modes: • In the normal mode. ATO is meant to be used for complex and high-load SQL statements that have a nontrivial impact on the entire system. which is a complex. Oracle and/or its affiliates.Page 5 Oracle University and InfoTech (Pvt. Also. you can now let the Oracle optimizer tune the SQL code for you. is designed to replace the manual tuning of SQL statements. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Application Tuning Challenges . You accept the advice. I/O.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the task of identifying high-load SQL statements has been automated by Automatic Database Diagnostic Monitor (ADDM). Copyright © 2010. and temporary space) are good candidates for SQL Tuning Advisor. introduced with Oracle Database 10g. With the introduction of SQL Tuning Advisor. a rationale for the advice. the SQL tuning activity is a continuous task because the SQL workload can change relatively often when new application modules are deployed. SQL statements that consume high resources (such as CPU. All rights reserved. Even though the number of high-load SQL statements that are identified by ADDM may represent a very small percentage of the total SQL workload. thereby tuning the SQL statements. and the actual command to implement the advice. the task of tuning them is still highly complex and requires a high level of expertise. estimated performance benefits. Starting with Oracle Database 10g. but also one of the most difficult tasks to accomplish.Application Tuning Challenges How can I tune my high-load SQL? I can do it for you! SQL workload ADDM High-load SQL SQL Tuning Advisor Application Tuning Challenges The process of identifying high-load SQL statements and tuning them is very challenging even for an expert. SQL tuning is not only one of the most critical aspects of managing the performance of a database server. SQL Tuning Advisor. The advisor receives one or more SQL statements as input and then provides advice on how to optimize the execution plan. SQL Tuning Advisor Chapter 12 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Advisor: Overview . It builds a SQL Profile using the auxiliary information and makes a recommendation to create it. under normal mode.Page 6 Oracle University and InfoTech (Pvt. When a SQL Profile is created. • SQL Structure Analysis: Here.SQL Tuning Advisor: Overview Statistics Check optimization mode Detect stale or missing statistics. Copyright © 2010. to generate a well-tuned plan. All rights reserved. SQL Analysis optimization mode SQL Tuning Advisor Automatic Tuning Optimizer Restructure SQL. ATO tries to identify SQL statements that lend themselves to bad plans. SQL Tuning Advisor Chapter 12 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. It also collects auxiliary information in the form of customized optimizer settings. Run Access Advisor. Plan Tuning optimization mode Plan tuning (SQL Profile). • Access Path Analysis: ATO explores whether a new index can be used to significantly improve access to each table in the query. such as first rows and all rows. It calls Automatic Tuning Optimizer (ATO) to perform the following four specific types of analysis: • Statistics Analysis: ATO checks each query object for missing or stale statistics and makes a recommendation to gather relevant statistics. Comprehensive SQL tuning SQL Tuning Advisor: Overview SQL Tuning Advisor is primarily the driver of the tuning process. It also collects auxiliary information to supply missing statistics or correct stale statistics in case recommendations are not implemented. • SQL Profiling: ATO verifies its own estimates and collects auxiliary information to remove estimation errors. based on the past execution history of the SQL statement. and makes relevant suggestions to restructure them. The suggested restructuring can be syntactic as well as semantic changes to the SQL code. and when appropriate makes recommendations to create such indexes. the profile enables the query optimizer. Access Analysis optimization mode Add missing index. Oracle and/or its affiliates. Stale or Missing Object Statistics The query optimizer relies on object statistics to generate execution plans.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ATO checks each query object for missing or stale statistics and produces two types of outputs: • Auxiliary information in the form of statistics for objects with no statistics. you may be hesitant to accept this recommendation immediately because of the impact it could have on other queries in the system. ATO uses dynamic sampling and generates: – Auxiliary object statistics to compensate for missing or stale object statistics – Recommendations to gather object statistics where appropriate: EXEC DBMS_STATS. tabname=>'CUSTOMERS'.GATHER_TABLE_STATS( ownname=>'SH'.AUTO_SAMPLE_SIZE).Stale or Missing Object Statistics • • • Object statistics are key inputs to the optimizer. If these statistics are stale or missing. the optimizer does not have the necessary information it needs and can generate suboptimal execution plans. ATO verifies object statistics for each query object. Oracle and/or its affiliates. Copyright © 2010. you gather statistics when recommended and then rerun Automatic Tuning Optimizer. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Stale or Missing Object Statistics . However.Page 7 Oracle University and InfoTech (Pvt. All rights reserved. SQL Tuning Advisor Chapter 12 . estimate_percent=>DBMS_STATS. and statistic adjustment factor for objects with stale statistics • Recommendations to gather relevant statistics for objects with stale or no statistics For optimal results. a correction factor is applied. During SQL Profiling. All rights reserved. ATO performs verification steps to validate its own estimates.SQL Statement Profiling • • Statement statistics are key inputs to the optimizer. Oracle and/or its affiliates. Copyright © 2010. The validation consists of taking a sample of data and applying appropriate predicates to the sample. Another method of estimate validation involves the execution of a fragment of the SQL statement. The partial execution method is more efficient than the sampling method when the respective predicates provide efficient access paths. SQL Tuning Advisor Chapter 12 . selectivity. it generates a user recommendation to create a SQL Profile. if the execution history indicates that a SQL statement is only partially executed the majority of times. For example. The new estimate is compared to the regular estimate. SQL Statement Profiling The main verification step during SQL Profiling is the verification of the query optimizer’s own estimates of cost. ATO verifies statement statistics such as: – Predicate selectivity – Optimizer settings (FIRST_ROWS versus ALL_ROWS) • Automatic Tuning Optimizer uses: – Dynamic sampling – Partial execution of the statement – Past execution history statistics of the statement • ATO builds a profile if statistics were generated: exec :profile_name := dbms_sqltune.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and cardinality for the statement that is tuned. ATO also uses the past execution history of the SQL statement to determine correct settings. ATO can recommend the acceptance of the generated SQL Profile to activate it. and if the difference is large enough.accept_sql_profile( task_name =>'my_sql_tuning_task'). ATO picks the appropriate estimate validation method. ATO uses the FIRST_ROWS optimization as opposed to ALL_ROWS. When a SQL Profile is built. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Statement Profiling . In this mode.Page 8 Oracle University and InfoTech (Pvt. ATO builds a SQL Profile if it has generated auxiliary information either during Statistics Analysis or during SQL Profiling. the query optimizer (under normal mode) uses the SQL Profile to build a well-tuned plan. to produce a well-tuned plan for the corresponding SQL statement. the SQL Profile is stored in the data dictionary.Plan Tuning Flow and SQL Profile Creation Submit Optimizer Create (Tuning mode) SQL Tuning Advisor SQL Profile Use No application code change Optimizer Output (Normal mode) Database users Well-tuned plan Plan Tuning Flow and SQL Profile Creation A SQL Profile is a collection of auxiliary information that is built during automatic tuning of a SQL statement. The process consists of two separate phases. in normal mode. After it is created. you can accept it. If a SQL Profile is built. All rights reserved. every time the corresponding SQL statement is compiled in normal mode. When it is accepted. They are system SQL tuning phase and regular optimization phase. a SQL Profile is used in conjunction with the existing statistics by the query optimizer. Thus. The use of the SQL Profile remains completely transparent to the end user and does not require changes to the application source code.Page 9 Oracle University and InfoTech (Pvt. the SQL profile information is not exposed through regular dictionary views.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a SQL Profile is to a SQL statement what statistics are to a table or index. The slide shows the process flow of the creation and use of a SQL Profile. In the next phase. A SQL Profile is stored persistently in the data dictionary. possibly with a SQL Profile. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Plan Tuning Flow and SQL Profile Creation . SQL Tuning Advisor invokes ATO to generate tuning recommendations. Copyright © 2010. After creation of a SQL Profile. During the system SQL tuning phase. However. when an end user issues the same SQL statement. you select a SQL statement for system tuning and run SQL Tuning Advisor by using either Database Control or the command-line interface. SQL Tuning Advisor Chapter 12 . the query optimizer uses the SQL Profile to produce a well-tuned plan. and periodic collection of database statistics. the performance of the corresponding SQL statement may become noticeably worse.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. However. ADDM again captures the statement as high-load SQL. All rights reserved. when a SQL Profile becomes outdated. growth in the size of tables.SQL Tuning Loop Workload Generate profiles High load SQL Tuning Advisor ADDM SQL Tuning Loop The auxiliary information contained in a SQL Profile is stored in such a way that it stays relevant after database changes. In such cases. In such a situation. a SQL Profile may not adapt to massive changes in the database or changes that have accumulated over a long period of time. the corresponding SQL statement may start showing up as high-load or top SQL. a new SQL Profile needs to be built to replace the old one. For example. If that happens. SQL Tuning Advisor Chapter 12 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Loop .Page 10 Oracle University and InfoTech (Pvt. the corresponding plan is not frozen (as when outlines are used). Copyright © 2010. Oracle and/or its affiliates. Therefore. thus becoming again a target for system SQL Tuning. In such a case. when a profile is created. you can decide to re-create a new profile for that statement. such as addition or removal of indexes. it provides a quick solution to the performance problem associated with a single SQL statement. Because ATO does not perform an analysis of how its index recommendations are going to affect the entire SQL workload. Access Path Analysis ATO also provides advice on indexes. The Access Path Analysis can make the following recommendations: • Create new indexes if they provide significantly superior performance. Copyright © 2010. Oracle and/or its affiliates. SQL Tuning Advisor Chapter 12 . Any index recommendations generated by ATO are specific to the SQL statement being tuned.Access Path Analysis SQL Tuning Advisor Significant performance gain SQL Access Advisor Workload Comprehensive index analysis Indexes Indexes CREATE INDEX JFV.TEST("C").Page 11 Oracle University and InfoTech (Pvt. Effective indexing is a well-known tuning technique that can significantly improve the performance of SQL statements by reducing the need for full table scans. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Access Path Analysis . The Access Advisor collects advice given on each statement of a SQL workload and consolidates it into global advice for the entire SQL workload. • Run SQL Access Advisor to perform a comprehensive index analysis based on application workload.IDX$_00002 on JFV. Therefore. All rights reserved. it recommends running the Access Advisor on the SQL statement along with a representative SQL workload.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. but not precisely equivalent to the original query. ATO does not automatically rewrite the query. Therefore. There are certain syntax variations that are known to have a negative impact on performance. the NOT EXISTS and NOT IN constructors are similar. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Structure Analysis .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and providing recommendations for an alternative statement where possible. but not exactly the same. you have to decide whether the recommendation is valid. For this reason. The recommendation may be very similar. identifying less efficient coding techniques.SQL Structure Analysis Poorly written SQL statement How can I rewrite it? Restructured SQL statement SQL constructs Type mismatch and indexes SQL Tuning Advisor Design mistakes SQL Structure Analysis The goal of the SQL Structure Analysis is to help you identify poorly written SQL statements as well as to advise you on how to restructure them. ATO evaluates statements against a set of rules. but gives advice instead. All rights reserved. SQL Tuning Advisor Chapter 12 . The following categories of problems are detected by the SQL Structure Analysis: • Use of SQL constructors such as NOT IN instead of NOT EXISTS.Page 12 Oracle University and InfoTech (Pvt. In this mode. or UNION instead of UNION ALL • Use of predicates involving indexed columns with data-type mismatch that prevents the use of the index • Design mistakes (such as Cartesian products) Copyright © 2010. Oracle and/or its affiliates. For example. SQL Tuning Advisor: Usage Model Automatic selection AWR ADDM High-load SQL Sources AWR Manual Selection SQL Tuning Advisor Cursor cache Filter Custom SQL Tuning Advisor: Usage Model SQL Tuning Advisor takes one or more SQL statements as input. Copyright © 2010. • Custom workload: A user can create a custom workload consisting of statements of interest to the user. Oracle and/or its affiliates. The input can come from different sources: • High-load SQL statements identified by ADDM • SQL statements that are currently in cursor cache • SQL statements from Automatic Workload Repository (AWR): A user can select any set of SQL statements captured by AWR. For such statements.Page 13 Oracle University and InfoTech (Pvt. SQL Tuning Advisor Chapter 12 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Advisor: Usage Model . An STS stores multiple SQL statements along with their execution information: • Execution context: Parsing schema name and bind values • Execution statistics: Average elapsed time and execution count Note: Another STS can be a possible source for STS creation. All rights reserved. For a multistatement input. a user can create a custom workload and tune it using the advisor. SQL statements from cursor cache. an object called SQL Tuning Set (STS) is provided. This can be done using snapshots or baselines. and custom workload can be filtered and ranked before they are input to SQL Tuning Advisor. AWR. These may be statements that are not in cursor cache and are not high-load to be captured by ADDM or AWR.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. On this page. Copyright © 2010. All rights reserved. click the SQL Tuning Advisor link. Oracle and/or its affiliates. click the SQL Advisors link. click the Advisor Central link located in the Related Links section to open the Advisor Central page.Page 14 Oracle University and InfoTech (Pvt. On the Advisor Central page. On the SQL Advisors page.Database Control and SQL Tuning Advisor Database Control and SQL Tuning Advisor The easiest way to access the SQL Tuning Advisor from Enterprise Manager is on the Advisor Central page. On the Home page. you find links to various other pages. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Database Control and SQL Tuning Advisor . This takes you to the Schedule SQL Tuning Advisor page. You click the Top Activity link to open the Top Activity page. SQL Tuning Advisor Chapter 12 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010.Page 15 Oracle University and InfoTech (Pvt. or by selecting customized SQL statements. or an STS: • Tuning ADDM-identified SQL statements: The ADDM Finding Details page shows high-load SQL statements identified by ADDM. SQL Tuning Advisor Chapter 12 . • Tuning top SQL statements: Another SQL source is the list of top SQL statements. Each of these high-load SQL statements is known to consume a significant proportion of one or more system resources. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Running SQL Tuning Advisor: Example . You can use this page to launch SQL Tuning Advisor on a selected high-load SQL statement. The user can select one or more top SQL statements identified by their SQL IDs. • Tuning a SQL Tuning Set: It is also possible to look at various STSs created by different users.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This is shown in the slide. You can identify the list of top SQL statements by looking at their cumulative execution statistics based on a selected time window.Running SQL Tuning Advisor: Example Running SQL Tuning Advisor: Example You can use Database Control to identify the high-load or top SQL statements. Oracle and/or its affiliates. All rights reserved. and then click Schedule SQL Tuning Advisor. by selecting SQL statements from a range of snapshots created by AWR. There are several locations in Database Control from where SQL Tuning Advisor can be launched with the identified SQL statement or statements. An STS could have been created from a list of top SQL statements. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Schedule SQL Tuning Advisor . the Recommendations page appears. Another useful option is to run the tuning task immediately or schedule it to be run at a later time. and invokes the optimizer under SQL Profiling mode to build a SQL Profile if applicable.Schedule SQL Tuning Advisor Schedule SQL Tuning Advisor When SQL Tuning Advisor is launched. Enterprise Manager automatically creates a tuning task. the user can change the automatic defaults pertaining to a tuning task.Page 16 Oracle University and InfoTech (Pvt. and SQL structure analysis. SQL Tuning Advisor performs all of the Limited scope actions. All rights reserved. SQL Tuning Advisor Chapter 12 . Oracle and/or its affiliates. No SQL Profile recommendation is generated with Limited scope. provided the user has the appropriate ADVISOR privilege to do so. When the task is submitted. you can also specify a time limit for the tuning task. On this page. With the Comprehensive option. When the task is complete. access path analysis. One of the important options is to select the scope of the tuning task. Copyright © 2010. which by default is 30 minutes. the Processing page appears. SQL Tuning Advisor produces recommendations based on statistics check.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If you select the Comprehensive option. If you select the Limited option. Enterprise Manager shows the tuning task with automatic defaults on the Schedule SQL Tuning Advisor page. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a SQL Profile has been created.Page 17 Oracle University and InfoTech (Pvt. Click the eyeglass icon to view the Compare Explain Plan page. SQL Tuning Advisor Chapter 12 . you can implement it if you want. Oracle and/or its affiliates. after you view the new plan. For each recommendation.Implementing Recommendations Implementing Recommendations On the recommendations page. as shown. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Implementing Recommendations . All rights reserved. Copyright © 2010. you can view the various recommendations. Page 18 Oracle University and InfoTech (Pvt. the recommendation can ignored or deleted. Copyright © 2010. You can see the benefits graphically and in a table. SQL Tuning Advisor Chapter 12 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Compare Explain Plan . in this case a SQL profile. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Notice the Cost values for the SQL statement in the original and new explain plans. If the difference is not enough or the explain is not acceptable.Compare Explain Plan Compare Explain Plan The Compare explain Plan page give you the opportunity to view the projected benefits of implementing the recommendation. Copyright © 2010.Page 19 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All of the above Answer: a. SQL Tuning Advisor focuses on one SQL statement at a time. d SQL Tuning Advisor in comprehensive mode recommends all except deleting indexes. b. Additional Indexes c. SQL Tuning Advisor Chapter 12 . An entire workload must be considered to determine if deleting an index will help performance. Oracle and/or its affiliates. SQL Profiles b.Quiz SQL Tuning Advisor will recommend: a. All rights reserved. Rewriting SQL Statements e. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Deleting Indexes d. True b. Oracle and/or its affiliates.Page 20 Oracle University and InfoTech (Pvt. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . SQL Tuning Advisor Chapter 12 .Quiz The SQL Profile will force the best execution plan even when the data in the table changes. a.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. False Answer: b Copyright © 2010. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary Copyright © 2010.Summary In this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Tuning Advisor Chapter 12 . All rights reserved. you should have learned the following: • Statement profiling • SQL Tuning Advisor Oracle University and InfoTech (Pvt.Page 21 . ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Tuning Advisor Chapter 12 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 12: Overview Copyright © 2010.Practice 12: Overview This practice covers using ADDM and SQL Tuning Advisor to tune your SQL statements Oracle University and InfoTech (Pvt. All rights reserved.Page 22 . Using SQL Access Advisor Chapter 13 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 13 Copyright © 2010.Page 1 Oracle University and InfoTech (Pvt.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.) Ltd use only Using SQL Access Advisor . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using SQL Access Advisor Copyright © 2010. Oracle and/or its affiliates.) Ltd use only Using SQL Access Advisor . Using SQL Access Advisor Chapter 13 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 2 Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates. you should be able to use SQL Access Advisor. Using SQL Access Advisor Chapter 13 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Objectives After completing this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives Copyright © 2010. All rights reserved. Oracle University and InfoTech (Pvt.Page 3 . SQL Access Advisor identifies and helps resolve performance problems relating to the execution of SQL statements by recommending which indexes. indexes. or retain. there have been many papers and scripts written as well as high-end tools developed to address the matter. materialized views. It then recommends the access structures for faster execution path. or the Advisor can derive a hypothetical workload from the schema. SQL Access Advisor takes an actual workload as input.SQL Access Advisor: Overview What partitions. SQL Access Advisor has been introduced to address this critical need. drop. with the development of partitioning and materialized view technology. In addition. Oracle and/or its affiliates. It can be run from Database Control or from the command line by using PL/SQL procedures. It provides the following advantages: • Does not require you to have expert knowledge • Bases decision making on rules that actually reside in the cost-based optimizer (CBO) • Is synchronized with the optimizer and Oracle database enhancements • Is a single advisor covering all aspects of SQL access methods Copyright © 2010. deciding on access structures has become even more complex. As a result. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Overview . and MVs do I need to optimize my entire workload? Solution SQL Access Advisor No expertise required Workload Component of CBO Provides implementation script SQL Access Advisor: Overview Defining appropriate access structures to optimize SQL queries has always been a concern for the developer. materialized view logs. As part of the manageability improvements in Oracle Database 10g and 11g. Using SQL Access Advisor Chapter 13 . All rights reserved.Page 4 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. or partitions to create. Using SQL Access Advisor Chapter 13 . Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Provides simple.Page 5 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. user-friendly GUI wizards • Generates scripts for implementation of recommendations Oracle University and InfoTech (Pvt. based on optimizer cost.Page 6 Oracle University and InfoTech (Pvt. and materialized views Copyright © 2010. or combinations of all three • Considers storage for creation and maintenance costs • Does not generate drop recommendations for partial workloads • Optimizes materialized views for maximum query rewrite usage and fast refresh • Recommends materialized view logs for fast refresh • Recommends partitioning for tables. This option is interesting when your system is being initially designed. to take the current content of V$SQL • Hypothetical. partition solutions. materialized view solutions. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Usage Model . to generate a likely workload from your dimensional model. a user can specify that the advisor should look at only the 30 most resource-intensive statements in the workload.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. indexes. For the given workload. Using SQL Access Advisor Chapter 13 . from the workload repository SQL Access Advisor also provides powerful workload filters that you can use to target the tuning. For example. the advisor then does the following: • Simultaneously considers index solutions.SQL Access Advisor: Usage Model SQL Access Advisor SQL cache Workload Hypothetical STS Filter Options Indexes Materialized views Materialized Partitioned views log objects SQL Access Advisor: Usage Model SQL Access Advisor takes as input a workload that can be derived from multiple sources: • SQL cache. • SQL Tuning Sets. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • Copyright © 2010. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Page 7 .Combines similar indexes into a single index • Generates recommendations that support multiple workload queries Oracle University and InfoTech (Pvt. Using SQL Access Advisor Chapter 13 . indexes. Using SQL Access Advisor Chapter 13 . As shown in the table. partitioning recommendations can be generated only for single-column interval and Copyright © 2010. Oracle and/or its affiliates. YES YES Modify an existing materialized view log to add new columns or clauses. In addition. YES NO Add a new materialized view log. SQL Access Advisor addresses all aspects of tuning partitions. YES YES Possible Recommendations SQL Access Advisor carefully considers the overall impact of recommendations and makes recommendations by using only the known workload and supplied information. It assumes that the workload contains a complete and representative set of application SQL statements. When comprehensive workload analysis is chosen. YES YES Drop an unused index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Access Advisor forms a better set of global tuning adjustments. and materialized view logs.000 rows.Page 8 Oracle University and InfoTech (Pvt. Two workload analysis methods are available: • Comprehensive: With this approach. Thus. materialized views. Note: Partition recommendations can work on only those tables that have at least 10. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Possible Recommendations . YES NO Modify an existing index by adding columns at the end. YES YES Partition an existing unpartitioned table or index. Partitioning recommendations can be generated only on these types of columns. advice is sought for improving the performance of a portion of an application environment. YES NO Modify an existing index by changing the index type. All rights reserved. but the effect may be a longer analysis time. YES YES Add a new (partitioned) materialized view.Possible Recommendations Recommendation Comprehensive Limited Add new (partitioned) index on table or materialized view. YES YES Drop an unused materialized view (log). and workloads that have some predicates and joins on columns of the NUMBER or DATE type. the chosen workload approach determines the type of recommendations made by the advisor. • Limited: Unlike the comprehensive workload approach. a limited workload approach assumes that the workload contains only problematic SQL statements. Using SQL Access Advisor Chapter 13 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED hash partitions. Interval partitioning recommendations can be output as range syntax.Page 9 . All rights reserved. but interval is the default. Copyright © 2010. Hash partitioning is done to leverage only partitionwise joins.Oracle University and InfoTech (Pvt. For general information about using SQL Access Advisor. Oracle and/or its affiliates. On the SQL Access Advisor: Initial Options page. Copyright © 2010. Note: The SQL Access Advisor may be interrupted while generating recommendations. you can select a template or task from which to populate default options before starting the wizard. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor Session: Initial Options . All rights reserved.SQL Access Advisor Session: Initial Options SQL Access Advisor Session: Initial Options The next few slides describe a typical SQL Access Advisor session. thereby allowing the results to be reviewed.Page 10 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The SQL Access Advisor consists of several steps during which you supply the SQL statements to tune and the types of access methods you want to use. You can access the SQL Access Advisor by clicking the Advisor Central link on the Database Home page or through individual alerts or performance pages that may include a link to facilitate solving a performance problem. Using SQL Access Advisor Chapter 13 . see the "Overview of the SQL Access Advisor" section in the lesson titled "SQL Access Advisor" of the Oracle Data Warehousing Guide. Page 11 Oracle University and InfoTech (Pvt. Using SQL Access Advisor Chapter 13 . or an existing template to inherit SQL Access Advisor’s options. You can view the various options defined by a task or a template by selecting the corresponding object and clicking View Options. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the SQLACCESS_EMTASK template is used. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor Session: Initial Options . you can select an existing task. All rights reserved. By default.SQL Access Advisor Session: Initial Options SQL Access Advisor Session: Initial Options (continued) If you select the "Inherit Options from a Task or Template" option on the Initial Options page. • Use an existing SQL Tuning Set: You also have the possibility of creating and using a SQL Tuning Set that holds your statements. Filter options are: • Resource Consumption: Number of statements ordered by Optimizer Cost. Executions • Users • Tables • SQL Text • Module IDs • Actions Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This is very useful to initially design your schema. Disk Reads.SQL Access Advisor: Workload Source SQL Access Advisor: Workload Source You can select your workload source from three different sources: • Current and Recent SQL Activity: This source corresponds to SQL statements that are still cached in your System Global Area (SGA).Page 12 Oracle University and InfoTech (Pvt. you can further filter your workload source. Using the Filter Options section. • Hypothetical Workload: This option provides a schema that allows the advisor to search for dimension tables and produce a workload. Elapsed Time. CPU Time. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Workload Source . Buffer Gets. Oracle and/or its affiliates. All rights reserved. Using SQL Access Advisor Chapter 13 . and default storage locations. you can select whether to limit the SQL Access Advisor to recommendations based on a single access method. In Comprehensive Mode. All rights reserved.SQL Access Advisor: Recommendation Options SQL Access Advisor: Recommendation Options On the Recommendations Options page. tuning options. Copyright © 2010. These modes affect the quality of recommendations as well as the length of time required for processing. You can use the Advisor Mode section to run the advisor in one of the two modes. If none of the three possible ones are chosen.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In Limited Mode. Using SQL Access Advisor Chapter 13 . You can select the type of structures to be recommended by the advisor.Page 13 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Recommendation Options . limiting the candidate recommendations by working on the highest-cost statements only. the advisor evaluates existing structures instead of trying to recommend new ones. the advisor performs quickly. Oracle and/or its affiliates. Note: You can click Advanced Options to show or hide options that allow you to set space restrictions. the Advisor searches a large pool of candidates resulting in recommendations of the highest quality. Using SQL Access Advisor Chapter 13 . Oracle and/or its affiliates. The possible options are shown in the screenshots in the slide.SQL Access Advisor: Schedule and Review SQL Access Advisor: Schedule and Review You can then schedule and submit your new analysis by specifying various parameters to the scheduler. Copyright © 2010. All rights reserved.Page 14 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Schedule and Review .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using SQL Access Advisor Chapter 13 . on which you can see an overview of the Access Advisor findings. The page shows you charts and statistics that provide overall workload performance and potential for improving query execution time for the recommendations. You can use the page to show statement counts and recommendation action counts. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Results .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Copyright © 2010. All rights reserved. you can retrieve the task details for your analysis. By selecting the task name in the Results section of the Advisor Central page.Page 15 Oracle University and InfoTech (Pvt.SQL Access Advisor: Results SQL Access Advisor: Results From the Advisor Central page. you can access the Results for Task Summary page. click one of the three other tabs on the page. This page also gives you all journal entries that were logged during the task execution. On the Recommendations page. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Access Advisor: Results and Implementation . you can view the full text of an action by clicking the link in the Action field for the specified action. Using SQL Access Advisor Chapter 13 . When you complete any changes. SQL Statements. Oracle and/or its affiliates. For each of them. to modify the tablespace name of the statement. Recommendations. Copyright © 2010.SQL Access Advisor: Results and Implementation SQL Access Advisor: Results and Implementation To see other aspects of the results for the Access Advisor task. you can drill down to each of the recommendations. optionally.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you are taken to the Recommendations page that displays all actions for the specified recommendation and.Page 16 Oracle University and InfoTech (Pvt. you see important information in the Select Recommendations for Implementation table. click OK to apply the changes. or Details. The SQL Statements page (not shown here) gives you a chart and a corresponding table that lists SQL statements initially ordered by the largest cost improvement. You can view the SQL for all actions in the recommendation by clicking Show SQL. On the Recommendations page. All rights reserved. The top SQL statement is improved the most by implementing its associated recommendation. If you click the ID for a particular recommendation. The Details page shows you the workload and task options that were used when the task was created. You can then select one or more recommendations and schedule their implementation. All rights reserved.Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED You can also schedule the implementation of the recommendations by clicking the Schedule Implementation button.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using SQL Access Advisor Chapter 13 . Oracle and/or its affiliates. Copyright © 2010.Page 17 . d Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . Partial d. All rights reserved. a. Oracle and/or its affiliates. Limited Answer: a. Complete c.Quiz Identify two available workload analysis methods. Comprehensive b. Using SQL Access Advisor Chapter 13 .Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates.Page 19 Oracle University and InfoTech (Pvt. False Answer: b Copyright © 2010. a. All rights reserved. Using SQL Access Advisor Chapter 13 . True b. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz SQL Access Advisor identifies but cannot help resolve performance problems relating to the execution of SQL statements. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Oracle University and InfoTech (Pvt.Page 20 . All rights reserved. Using SQL Access Advisor Chapter 13 .Summary In this lesson. you should have learned to use SQL Access Advisor. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary Copyright © 2010. Using SQL Access Advisor Chapter 13 .Practice 13: Overview This practice covers the following topics using SQL Access Advisor to change your schema Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 13: Overview Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 21 . Oracle and/or its affiliates. Oracle and/or its affiliates.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010.Oracle University and InfoTech (Pvt.Page 22 . Using SQL Access Advisor Chapter 13 . All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 14 Copyright © 2010. All rights reserved.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 1 Oracle University and InfoTech (Pvt. Automating SQL Tuning Chapter 14 .) Ltd use only Automating SQL Tuning . Oracle and/or its affiliates. All rights reserved. Oracle and/or its affiliates.Page 2 Oracle University and InfoTech (Pvt.) Ltd use only Automating SQL Tuning . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automating SQL Tuning Copyright © 2010.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Automating SQL Tuning Chapter 14 . ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Automating SQL Tuning Chapter 14 . Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives Copyright © 2010.Page 3 . All rights reserved. Oracle and/or its affiliates. you should be able to use Automatic SQL Tuning.Objectives After completing this lesson. In addition to SQL Tuning Advisor.SQL Tuning Loop 1 Workload Automatic High load SQL Tuning Advisor 4 Generate SQL profiles. Oracle and/or its affiliates. However. 2 ADDM 3 Run SQL Tuning Advisor. major issues still remain: Although it is true that ADDM identifies some SQL that should be tuned. It also targets the (more common) problem in which a SQL statement performs poorly because the optimizer generated a poor execution plan due to lack of accurate and relevant data statistics. In all cases. Copyright © 2010.Page 4 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Tuning Loop .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. SQL Tuning Loop Oracle Database 10g introduced SQL Tuning Advisor to help application developers improve the performance of SQL statements. Automating SQL Tuning Chapter 14 . but it leaves the responsibility of implementing the recommendations to the user. users must manually look at ADDM reports and run SQL Tuning Advisor on the reports for tuning. the advisor makes specific suggestions for speeding up SQL performance. This is done by ADDM. in which SQL statements have not been designed in the most efficient fashion. which automatically identifies high-load SQL statements that are good candidates for tuning. Oracle Database 10g has an automated process to identify high-load SQL statements in your system. The advisor targets the problem of poorly written SQL. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning . Automatic SQL Tuning targets for automatic tuning.Automatic SQL Tuning AWR 2 Top SQL Auto matic Workload 3 1 SQL Tuning Reports 4 Automatic SQL Tuning Oracle Database 11g further automates the SQL Tuning process by identifying problematic SQL statements.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Automating SQL Tuning Chapter 14 . the new profile is carefully tested. You then have the option of checking the tuned SQL statements to validate or remove the automatic SQL profiles that were generated. SQL profiles are created for them if needed. As a result. and implementing the resulting SQL profile recommendations to tune the statement without requiring user intervention. you can request a report about these automatic tuning activities.Page 5 Oracle University and InfoTech (Pvt. However. Automatic SQL Tuning uses the AUTOTASK framework through a task called "Automatic SQL Tuning" that runs every night by default. Oracle and/or its affiliates. A brief description of the automated SQL tuning process in Oracle Database 11g is as follows: • Step 1: Based on the AWR Top SQL identification (SQLs that were top in four different time periods: the past week. running SQL Tuning Advisor on them. All rights reserved. Copyright © 2010. • Steps 2 and 3: While the Automatic SQL Tuning task executes during the maintenance window. before making any decision. any hour in the past week. any day in the past week. or single response time). • Step 4: At any point in time. the previously identified SQL statements are automatically tuned by invoking SQL Tuning Advisor. 2. Look for a SQL profile and. but only SQL profiles can be implemented automatically (when the ACCEPT_SQL_PROFILES task parameter is set to TRUE). but are not implemented.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Tune the statement by using SQL Tuning Advisor. only the recommendation to create a SQL profile is reported in the automatic SQL tuning reports. These are left for you to review and implement manually. Y Replace profile. the tuning process carries out each of the following steps: 1. refresh stale statistics. or restructure SQL statements) are generated as part of the SQL tuning process. Here is a short description of the automatic tuning process in general: Tuning is performed on a per-statement basis. there is no need to consider the effect of such recommendations on the workload as a whole. as appropriate. Existing profile? Indexes N 3X benefit? Y Accept profile N Y 3X benefit? Stale stats N Ignore new profile. all the recommendation types are considered and reported. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic Tuning Process .Automatic Tuning Process Not considered for auto implementation Considered for auto implementation New SQL profile Restructure SQL. Automating SQL Tuning Chapter 14 .Page 6 Oracle University and InfoTech (Pvt. if it is found. the performance improvement factor has to be at least three before a SQL profile is implemented. All rights reserved. In Oracle Database 11g. verify that the base optimizer statistics are current for it. For each statement (in the order of importance). perform the following: Copyright © 2010. Because only SQL profiles can be implemented. GATHER_STATS_JOB Automatic Tuning Process During the tuning process. Otherwise. As we have already mentioned. If a SQL profile is recommended. Oracle and/or its affiliates. the Automatic SQL Tuning process implements only SQL profile recommendations automatically. Other recommendations (to create new indexes. In this way. . 3. Oracle and/or its affiliates. So the requirement is that there is a three-time improvement in the sum of CPU and I/O time.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. even with contention in CPU or I/O. make this information available to GATHER_STATS_JOB. When a SQL profile is generated and it causes the optimizer to pick a different execution plan for the statement. with neither statistic becoming worse.B. SQL profiles are not accepted when there is degradation in either statistic. Automatic implementation of tuning recommendations is limited to SQL profiles because they have fewer risks. Copyright © 2010. All rights reserved. Automating SQL Tuning Chapter 14 .Page 7 Oracle University and InfoTech (Pvt. Although the benefit thresholds here apply to the sum of CPU and input/output (I/O) time. the statement runs faster than it would without the profile. Note: All SQL profiles are created in the standard EXACT mode. the advisor must decide whether to implement the SQL profile. If stale or missing statistics are found. It is easy for you to reverse the implementation. It makes its decision according to the flowchart in the slide. They are matched and tracked according to the current value of the CURSOR_SHARING parameter. You are responsible for setting CURSOR_SHARING appropriately for your workload. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED A. Test the new SQL profile by executing the statement with and without it. Oracle and/or its affiliates.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK'. 'MAX_AUTO_SQL_PROFILES'.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. 'ACCEPT_SQL_PROFILES'.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK'. you can control when the Automatic SQL Tuning task runs and the CPU resources that it is allowed to use. 10002). All rights reserved. The last three parameters in this example are supported only for the Automatic SQL Tuning task. dbms_sqltune. You can also use parameters such as LOCAL_TIME_LIMIT or TIME_LIMIT. An important example is to disable testexecute mode (to save time) and to use only execution plan costs to decide about the performance. 'MAX_SQL_PROFILES_PER_EXEC'. 1400).Page 8 Oracle University and InfoTech (Pvt. Copyright © 2010.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK'.Automatic SQL Tuning Controls • Autotask configuration: – On/off switch – Maintenance windows running tuning task – CPU resource consumption of tuning task • Task parameters: – – – – – SQL profile implementation automatic/manual switch Global time limit for tuning task Per-SQL time limit for tuning task Test-execute mode disabled to save time Maximum number of SQL profiles automatically implemented per execution as well as overall – Task execution expiration period Automatic SQL Tuning Controls Here is a PL/SQL control example for the Automatic SQL Tuning task: dbms_sqltune. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning Controls . The TEST_EXECUTE parameter setting determines whether the advisor actually executes the statement and how much time it uses to execute. 'TRUE'). which are valid parameters for the traditional SQL tuning tasks. In addition. 'LOCAL_TIME_LIMIT'. dbms_sqltune. 50). Automating SQL Tuning Chapter 14 . dbms_sqltune.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK'. click the Automated Maintenance Tasks link in the Tasks section. To open this page. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning Task . on your Database Control Home page. Oracle and/or its affiliates. Copyright © 2010. Note: The execution time shown in this example is very small. When you click either the Automatic SQL Tuning link or the latest execution icon (the green area on the timeline).Page 9 Oracle University and InfoTech (Pvt. Automating SQL Tuning Chapter 14 . Automatic SQL Tuning is implemented as an automated maintenance task that is called Automatic SQL Tuning. you see the predefined tasks.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the Automatic SQL Tuning Result Summary page opens. All rights reserved. You can see some high-level information about the last runs of the Automatic SQL Tuning task on the Automated Maintenance Tasks page. On the Server tabbed page that opens. click the Server tab. On the Automated Maintenance Tasks page. You then access each task by clicking the corresponding link to get more information about the task itself (illustrated in the slide).Automatic SQL Tuning Task Automatic SQL Tuning Task As already stated. You see the Automated Maintenance Tasks Configuration page.Page 10 Oracle University and InfoTech (Pvt. on which you see the various maintenance windows that are delivered with Oracle Database 11g. click the Configure button on the line corresponding to Automatic SQL Tuning in the Task Settings section.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Configuring Automatic SQL Tuning Configuring Automatic SQL Tuning You can configure various Automatic SQL Tuning parameters by using the Automatic SQL Tuning Settings page. By default. By default. turn off the AWR snapshots by using DBMS_WORKLOAD_REPOSITORY. Note: If you set STATISTICS_LEVEL to BASIC. Automating SQL Tuning Chapter 14 . Automatic SQL Tuning executes on all predefined maintenance windows in MAINTENANCE_WINDOW_GROUP. To navigate to that page. Copyright © 2010. "Automatic Implementation of SQL Profiles" is not selected. To navigate to the Automatic SQL Tuning Settings page. you also stop Automatic SQL Tuning. you can specify the parameters shown in the slide. click the Configure button on the Automated Maintenance Tasks page. On this page. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Configuring Automatic SQL Tuning . On the Automatic SQL Tuning Settings page. You can disable it for specific days in the week. All rights reserved. Oracle and/or its affiliates. you can also edit each Window to change its characteristics. or if AWR retention is less than seven days. You can do so by clicking Edit Window Group. The first chart in the Overall Task Statistics section shows you the breakdown by finding types for the designated period of time. Users can request it for any time period over the past month. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning: Result Summary . Similarly. the advisor keeps historical information about all the recommendations if you want to implement them later. you can clearly see that only SQL profiles can be implemented. you can see the Tuned SQL DB Time Benefit chart. not all of them were automatically implemented for the reasons that have already been explained. Copyright © 2010. You then generate the report by clicking View Report. An example is given in the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Customized is used. it shows you the latest run. which shows you the before-and-after DB Time for implemented profiles and other recommendations. Oracle and/or its affiliates.Page 11 Oracle University and InfoTech (Pvt. recommendations for index creation and other types are not implemented. In the example. the Automatic SQL Tuning Result Summary page contains various summary graphs so that you can control the Automatic SQL Tuning task. Automating SQL Tuning Chapter 14 . On the “Breakdown by Finding Type” chart. All rights reserved. However.Automatic SQL Tuning: Result Summary Automatic SQL Tuning: Result Summary In addition. because that is the amount of time for which the advisor persists its tuning history. You can control the period of time for which you want the report to be generated by selecting a value from the Time Period list. Although many more profiles were recommended. In the Profile Effect Statistics section. You can select All to cover all executions of the task so far. Copyright © 2010. Oracle and/or its affiliates.Automatic SQL Tuning: Result Details Automatic SQL Tuning: Result Details On the Automatic SQL Tuning Result Details page. you can see that a three-time benefit (for example.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Automating SQL Tuning Chapter 14 . According to this formula. you can either drill down to the SQL statement itself by clicking its corresponding SQL ID link. and the date of the recommendation.Page 12 Oracle University and InfoTech (Pvt. 98% is a 50-times benefit. whether a particular recommendation was automatically implemented. or you can select one of the SQL statements and click the View Recommendations button to have more details about the recommendation for that statement.time_new)/(time_old). the type of recommendation that was done by SQL Tuning Advisor. the verified benefit percentage. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning: Result Details . Note: The benefit percentage shown for each recommendation is calculated using the formula benefit% = (time_old . time_old = 100. you can also see important information for each automatically tuned SQL statement. So the system implements any profiles with benefits over 66%. From this page. time_new = 33) corresponds to 66%. including its SQL text and SQL ID. With this formula. Oracle and/or its affiliates. In the slide.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Automatic SQL Tuning Result Details: Drilldown Automatic SQL Tuning Result Details: Drilldown On the “Recommendations for SQL ID” page. By clicking the SQL Test link. where you see the tuning history as well as the plan control associated with your SQL statement. All rights reserved. you see that the statement was tuned by Automatic SQL Tuning and that the associated profile was automatically implemented. Copyright © 2010. you access the SQL Details page.Page 13 Oracle University and InfoTech (Pvt. you can see the corresponding recommendations and implement them manually. Automating SQL Tuning Chapter 14 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning Result Details: Drilldown . it is not practical to test-execute and. Oracle and/or its affiliates. SQL statements that do not repeat within a week are also not considered. these limitations apply to Automatic SQL Tuning only. It does not consider the following types of SQL: • Ad hoc or rarely repeated SQL statements: If a SQL statement is not executed multiple times in the same form.Automatic SQL Tuning Considerations • SQL not considered for Automatic SQL Tuning: – – – – – • Ad hoc or rarely repeated SQL Parallel queries Long-running queries after profiling Recursive SQL statements DML and DDL These statements can still be manually tuned by using SQL Tuning Advisor. • Parallel queries • Long-running queries (after creating a profile): If a query takes too long to run after being SQL profiled. and then would terminate test-execution without waiting for the old plan to finish. Copyright © 2010. it is ignored by the advisor. thereby switching the order of execution. All rights reserved. Automating SQL Tuning Chapter 14 . The advisor would execute the old plan just long enough to determine that it is worse than the new one. such as INSERT SELECT or CREATE TABLE AS SELECT With the exception of truly ad hoc SQL. • Recursive SQL statements • DMLs. Automatic SQL Tuning Considerations Automatic SQL Tuning does not seek to solve every SQL performance issue occurring on a system. Note that this does not mean that the advisor ignores all long-running queries. Such statements can still be tuned by manually running SQL Tuning Advisor. If the advisor can find a SQL profile that causes a query that once took hours to run in minutes. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Automatic SQL Tuning Considerations . it could be accepted because test-execution is still possible. the advisor ignores it.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. therefore.Page 14 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . an Automatic SQL Tuning task will run by default. If any SQL statements remain. or when the window closes d. Automatically implements any recommended SQL profiles Answer: a. or single response time).Page 15 Oracle University and InfoTech (Pvt. Works on each statement for a maximum of 30 minutes c. The time limit is 1200 seconds per statement by default (20 minutes). any hour in the past week. All rights reserved. c The default settings for Automatic SQL Tuning task is to tune the top SQL statements after prioritizing them based on the AWR Top SQL identification (SQLs that were top in four different time periods: the past week. but you can change the configuration so that they are automatically implemented. Oracle and/or its affiliates. Stops when all SQL statements are tuned. Automating SQL Tuning Chapter 14 . any day in the past week. Any SQL profiles that are generated are not implemented by default. Copyright © 2010. Prioritizes and tunes the SQL statements with the top resource consumption b.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Quiz In the maintenance window. Which two actions will this task perform by default? a. they must wait for the next maintenance window. Work must stop when the window closes. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Automating SQL Tuning Chapter 14 . you should have learned to use Automatic SQL Tuning Oracle University and InfoTech (Pvt.Page 16 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary Copyright © 2010. All rights reserved.Summary In this lesson. Oracle and/or its affiliates. Practice 14: Overview This practice covers using Automatic SQL Tuning to tune your statements Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 14: Overview Copyright © 2010. All rights reserved. Automating SQL Tuning Chapter 14 .Page 17 . Oracle and/or its affiliates. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Oracle University and InfoTech (Pvt. All rights reserved. Automating SQL Tuning Chapter 14 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010. Oracle and/or its affiliates.Page 18 . All rights reserved. Oracle and/or its affiliates.) Ltd use only SQL Plan Management . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 15 Copyright © 2010. SQL Plan Management Chapter 15 .Page 1 Oracle University and InfoTech (Pvt.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ) Ltd use only SQL Plan Management . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Plan Management Copyright © 2010. SQL Plan Management Chapter 15 .Page 2 Oracle University and InfoTech (Pvt. All rights reserved.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . All rights reserved.Page 3 Oracle University and InfoTech (Pvt. SQL Plan Management Chapter 15 .Objectives After completing this lesson. you should be able to: • Manage SQL performance through changes • Set up SQL Plan Management • Set up various SQL Plan Management scenarios Copyright © 2010. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Maintaining SQL Performance . SQL Outlines have been used in past versions. Copyright © 2010. there will not be drastic changes in performance even as the statistics change or as the database version changes. They are still available for backward compatibility. This in effect freezes the SQL plan. without having to change the code. It is not always the same statements. All rights reserved. Optimizer statistics change for many reasons. it will not be considered by the optimizer. The challenge is to maintain the SQL performance levels in spite of the changes. SQL Plan Management Chapter 15 . The performance of these statements must be tuned.Maintaining SQL Performance Maintaining performance may require using SQL plan baselines Maintaining SQL Performance Any number of factors that influence the optimizer can change over time. Managing the changes to SQL performance despite the changes to statistics is the task of the DBA. Until a new plan is verified to produce better performance than the current plan. but Outlines are deprecated in favor of SQL Plan Management. Some SQL statements on any system will stand out as high-resource consumers. Oracle and/or its affiliates. SQL plan baselines are the key objects that SQL Plan Management uses to prevent unverified change to SQL execution plans.Page 4 Oracle University and InfoTech (Pvt. When SQL Plan Management is active. SQL profiles provide the means to control the performance of these statements. • Plan changes are automatically verified. and SQL profile creation. during the execution of a SQL statement. As described later in this lesson. – Only comparable or better plans are subsequently used. All rights reserved. system settings. SQL profiles created by the SQL Tuning Advisor allow the optimizer to collect and store additional statistics that will guide the choice of a plan. optimizer statistics. the Tuning Advisor can be invoked to produce a new profile. only a plan that is part of the SQL plan baseline can be used. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Plan Management: Overview . Various scenarios are covered later in this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Plan Management Chapter 15 . The oldest is the use of hints in the SQL code to force a specific access path. if the plan becomes inefficient. So. Both of these techniques focused on making the plan static.SQL Plan Management: Overview • • SQL Plan Management is automatically controlled SQL plan evolution. a newly generated SQL plan can join a SQL plan baseline only if it has been proven that doing so will not result in performance regression.Page 5 Oracle University and InfoTech (Pvt. A SQL plan change can occur due to a variety of reasons such as optimizer version. SQL Plan Management automatically controls SQL plan evolution by maintaining what is called “SQL plan baselines. • The plan baseline can be seeded for critical SQL with SQL tuning set (STS) from SQL Performance Analyzer. Various plan control techniques are available in the Oracle Database to address performance regressions due to plan changes. Copyright © 2010. SQL Plan Management: Overview Potential performance risk occurs when the SQL execution plan changes for a SQL statement. Stored outlines allowed the hints to be stored separate from the code and modified. Optimizer automatically manages SQL plan baselines. Oracle and/or its affiliates. SQL plan baselines can be automatically loaded or can be seeded using SQL tuning sets. – Only known and verified plans are used. optimizer parameters. schema definitions.” With this feature enabled. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The main benefit of the SQL Plan Management feature is performance stability of the system by avoiding plan regressions. In addition. SQL Plan Management Chapter 15 . Oracle and/or its affiliates. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Oracle University and InfoTech (Pvt. All rights reserved. it saves the DBA time that is often spent in identifying and analyzing SQL performance regressions and finding workable solutions.Page 6 . For those statements. it automatically implements actions such as making a successfully verified plan an accepted Copyright © 2010. After a SQL statement is recognized as repeatable. However. All rights reserved.SQL Plan Baseline: Architecture SYSAUX SQL management base Statement log Plan history Repeatable SQL statement SQL profile Plan baseline … GB GB HJ HJ HJ HJ GB HJ HJ Automatic SQL tuning task Plan verification before integration to baseline SQL Plan Baseline: Architecture The SQL Plan Management (SPM) feature introduces necessary infrastructure and services in support of plan maintenance and performance verification of new plans. a new plan generated by the optimizer is not normally used until it has been verified not to cause a performance regression.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The DBA may also add plans to the SQL plan baseline by manual seeding a set of SQL statements. A SQL statement is recognized as repeatable when it is parsed or executed again after it has been logged. A plan history contains different plans generated by the optimizer for a SQL statement over time. SQL Plan Management Chapter 15 . various plans generated by the optimizer are maintained as a plan history containing relevant information (such as SQL text.Page 7 Oracle University and InfoTech (Pvt. The optimizer recognizes a repeatable SQL statement by maintaining a statement log. For SQL statements that are executed more than once. the optimizer maintains a history of plans for individual SQL statements. and compilation environment) that is used by the optimizer to reproduce an execution plan. For example. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Plan Baseline: Architecture . Plan verification is done by default as part of the Automatic SQL Tuning task running as an automated task in a maintenance window. outline. only some of the plans in the plan history may be accepted for use. An Automatic SQL Tuning task targets only high-load SQL statements. bind variables. Oracle and/or its affiliates. The very first plan generated for a SQL statement is obviously acceptable for use. and plan baselines are stored in the SQL management base (SMB). . it forms the original plan baseline. Note: With Oracle Database 11g. which also contains SQL profiles. You can configure the SMB to change the plan retention policy and set space size limits. A set of acceptable plans constitutes a SQL plan baseline. Any new plans subsequently found by the optimizer are part of the plan history but not part of the plan baseline initially. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED plan. if the database instance is up but the SYSAUX tablespace is OFFLINE.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 8 Oracle University and InfoTech (Pvt. SQL Plan Management Chapter 15 . Oracle and/or its affiliates. Copyright © 2010. periodic purging of unused plans). plan history. The SMB has automatic space management (for example. therefore. the optimizer is unable to access SQL management objects.The statement log. The SMB is part of the database dictionary and is stored in the SYSAUX tablespace. All rights reserved. This can affect performance on some of the SQL workload. Oracle and/or its affiliates. DBMS_SPM. SQL Plan Management Chapter 15 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. For a SQL statement to be loaded into a SQL plan baseline from an STS.Loading SQL Plan Baselines OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE dbms_spm Plan history Plan history GB 1 HJ HJ GB HJ HJ GB 2 HJ HJ B GB HJ 3 A HJ Plan history Cursor cache GB 4 HJ HJ Staging table DBA Loading SQL Plan Baselines There are two ways to load SQL plan baselines. you can load SQL plans into a SQL plan baseline directly from the cursor cache ( A ) using LOAD_PLANS_FROM_CURSOR_CACHE or from an existing SQL tuning set (STS) (B) using LOAD_PLANS_FROM_SQLSET.*_STGTAB_BASELINE procedures to create a staging table. the plan for the SQL statement needs to be stored in the STS. To move baselines between databases. This is illustrated in the graphic on the left in the slide. This parameter is set to FALSE by default. use the DBMS_SPM. All rights reserved.ALTER_SQL_PLAN_BASELINE enables you to enable and disable a baseline plan and change other plan attributes. Setting it to TRUE turns on automatic recognition of repeatable SQL statements and automatic creation of plan history for such statements. • On the fly capture: Uses automatic plan capture by setting the OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES initialization parameter to TRUE. With procedures from this package. which enables you to manually manage SQL plan baselines. • Bulk loading: Uses the DBMS_SPM package. where the first generated SQL plan is automatically integrated into the original SQL plan baseline when it becomes a repeating SQL statement. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Loading SQL Plan Baselines . and to Copyright © 2010.Page 9 Oracle University and InfoTech (Pvt. Page 10 .Oracle University and InfoTech (Pvt. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED export and import baseline plans from a staging table. SQL Plan Management Chapter 15 . The staging table can be moved between databases using the Data Pump Export and Import utilities.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. Page 11 Oracle University and InfoTech (Pvt. • By running SQL Tuning Advisor: SQL plan baselines can be evolved by manually or automatically tuning SQL statements using SQL Tuning Advisor. The example specifies a specific plan in the history to be tested. The function also allows verification without accepting the plan.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Evolving SQL Plan Baselines Plan history Automatic SQL Tuning GB HJ HJ DBA GB HJ HJ >? SQL Tuning Advisor Evolving SQL Plan Baselines When the optimizer finds a new plan for a SQL statement. Verification means a nonaccepted plan does not cause a performance regression (either manually or automatically). the plan is added to the plan history as a nonaccepted plan. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Evolving SQL Plan Baselines . There are two ways to evolve SQL plan baselines: • By using the DBMS_SPM. it makes a recommendation to accept a SQL profile. Copyright © 2010. All rights reserved. Oracle and/or its affiliates. When the SQL profile is accepted. The plan will not be accepted into the SQL plan baseline until it is verified for performance relative to the SQL plan baseline performance. When SQL Tuning Advisor finds a tuned plan and verifies its performance to be better than a plan chosen from the corresponding SQL plan baseline. The function returns a report that tells you whether some of the existing history plans were moved to the plan baseline. the tuned plan is added to the corresponding SQL plan baseline. The verification of a nonaccepted plan consists of comparing its performance to the performance of one plan selected from the SQL plan baseline and ensuring that it delivers better performance.EVOLVE_SQL_PLAN_BASELINE function: An example is shown in the slide. SQL Plan Management Chapter 15 . enabled.ALTER_SQL_PLAN_BASELINE() is used to change its status. or automatically evolved by Automatic SQL Tuning (AUTOSQLTUNE). SQL_PLAN_6zsn… AUTO-CAPTURE YES NO NO YES 8.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ENABLED and ACCEPTED: Both the ENABLED and ACCEPTED attributes must be set to YES or the plan is not considered by the optimizer. When a plan status changes to ACCEPTED. plan_name. sql_text.-----------. attribute_value => 'NO').--. ORIGIN allows you to determine whether the plan was automatically captured (AUTOCAPTURE). COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Important Baseline SQL Plan Attributes . either automatically by the system or manually when the user changes it to ACCEPTED. The ENABLED attribute means that the plan is enabled for use by the optimizer.062E+18 SYS_SQL_e23f select.-------.--.attribute_name => 'ENABLED'. SQL_HANDLE.alter_sql_plan_baseline(sql_handle => 'SYS_SQL_6fe28d438dfc352f'. if you have 10 baseline plans and three of them are marked FIXED. and PLAN_NAME are important identifiers for search operations. fixed.. An ACCEPTED plan can be temporarily disabled by removing the ENABLED setting. SQL Plan Management Chapter 15 .. For example.-----------. Important Baseline SQL Plan Attributes When a plan enters the plan history.062E+18 SYS_SQL_6fe2 select. origin. SQL_PLAN_f4gy… AUTO-CAPTURE YES YES NO YES … exec :cnt := dbms_spm. accepted. plan_name => 'SQL_PLAN_6zsnd8f6zsd9g54bc8843'. Oracle and/or its affiliates.Page 12 Oracle University and InfoTech (Pvt. manually evolved (MANUAL-LOAD). autopurge from dba_sql_plan_baselines. it is associated with a number of important attributes: • • • • SIGNATURE. it will continue to be ACCEPTED until DBMS_SPM. sql_handle.--8. The ACCEPTED attribute means that the plan was validated as a good plan.--.---------------. SQL_TEXT. All rights reserved. SIGNATURE SQL_HANDLE SQL_TEXT PLAN_NAME ORIGIN ENA ACC FIX AUT --------. automatically evolved by SQL Tuning Advisor (MANUAL-SQLTUNE). FIXED means that the optimizer considers only those plans and not other plans. the Copyright © 2010.Important Baseline SQL Plan Attributes Plan history Enabled but not accepted GB GB HJ HJ HJ HJ Enabled and accepted select signature. as shown in the slide. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED optimizer uses only the best plan from these three. SQL Plan Management Chapter 15 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.DROP_SQL_PLAN_BASELINE function. You can look at each plan’s attributes by using the DBA_SQL_PLAN_BASELINES view.Page 13 . If new plans are added to a fixed SQL plan baseline. ignoring all the others. Oracle University and InfoTech (Pvt. All rights reserved.ALTER_SQL_PLAN_BASELINE function to change some of them. A SQL plan baseline is said to be FIXED if it contains at least one enabled fixed plan.Note: The DBA_SQL_PLAN_BASELINES view contains additional attributes that enable you to determine when each plan was last used and whether a plan should be automatically purged. The example shown in the slide changes the ENABLED attribute of SQL_PLAN_6zsnd8f6zsd9g54bc8843 to NO. You can also remove plans or a complete plan history by using the DBMS_SPM. these new plans cannot be used until they are manually declared as FIXED. You can then use the DBMS_SPM. Oracle and/or its affiliates. Copyright © 2010. SQL Plan Management Chapter 15 . it first adds the new plan to the plan history. All rights reserved. the first time that a SQL statement is recognized as repeatable.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. it proceeds as usual. the optimizer first uses the traditional cost-based search method to build a best-cost plan. the optimizer can produce a plan that could be either a best-cost plan or a baseline plan. That plan is then used to execute the statement. So the effect of having a SQL plan baseline for a SQL statement is that the optimizer always selects one of the accepted plans in that SQL plan baseline. This information is dumped in the other_xml column of the plan_table upon explain plan. Each time a SQL statement is compiled. then calculates the cost of each accepted plan in the SQL plan baseline. With SQL Plan Management. If a match is found. and picks the one with the lowest cost. If no match is found. the optimizer can only use an accepted and enabled baseline plan. The accepted plans are reproduced using the outline that is stored with each of them. its best-cost plan is added to the corresponding SQL plan baseline. Copyright © 2010.Page 14 Oracle University and InfoTech (Pvt.SQL Plan Selection GB HJ optimizer_use_sql_ plan_baselines=true? HJ Yes Plan part of history? No Plan history Yes No GB GB HJ HJ HJ Plan part of baseline? Yes HJ Plan baseline GB HJ … HJ GB HJ HJ No Select baseline plan with lowest best cost. Then it tries to find a matching plan in the SQL plan baseline. However. GB HJ Yes HJ GB HJ GB < HJ HJ HJ No GB HJ HJ SQL Plan Selection If you are using automatic plan capture. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Plan Selection . Oracle and/or its affiliates. The optimizer uses a comparative plan selection policy when a plan baseline exists for a SQL statement and the OPTIMIZER_USE_SQL_PLAN_BASELINES initialization parameter is set to TRUE (default value). a plan generated by the optimizer using a stored outline is not stored in the SMB even if automatic plan capture has been enabled for the session. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED In addition.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the statement is compiled using the stored outline. you can use the new dbms_xplain.Page 15 .display_sql_plan_baseline function to display one or more execution plans for the specified sql_handle of a plan baseline. If plan_name is also specified. Stored outlines can be migrated to SQL plan baselines by using the MIGRATE_STORED_OUTLINE procedure from the DBMS_SPM package. To preserve backward compatibility. When the migration is complete. Oracle University and InfoTech (Pvt. In addition. the corresponding execution plan is displayed. Copyright © 2010. SQL Plan Management Chapter 15 . if a stored outline for a SQL statement is active for the user session.Note: The Stored Outline feature is deprecated. Oracle and/or its affiliates. you should disable or drop the original stored outline using the DROP_MIGRATED_STORED_OUTLINE procedure of the DBMS_SPM package. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Possible SQL Plan Manageability Scenarios . • New application deployment: The deployment of a new application module means the introduction of new SQL statements into the system. Because of the plan baselines. you can use the automatic SQL plan capture after manual loading to make sure that only better plans will be used for your applications in the future. For this. However. In both scenarios.Page 16 Oracle University and InfoTech (Pvt. The software vendor can ship the application software along with the appropriate SQL plan baselines for the new SQL being introduced.Possible SQL Plan Manageability Scenarios Database Upgrade New Application Deployment Oracle Database 11g Production database Plan history Plan history GB GB GB GB HJ HJ HJ HJ HJ HJ HJ HJ No plan regressions No plan regressions DBA DBA Plan history Well. the plan baselines can be evolved over time to produce better performance. SQL Plan Management Chapter 15 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and then load these plans from the STS into the SQL plan baseline immediately after the upgrade. the new SQL statements will initially run with the plans that are known to give good performance under a standard test configuration. Oracle and/or its affiliates. All rights reserved. you can capture plans for a SQL workload into a SQL tuning set (STS) before the upgrade. if the customer system configuration is very different from the test configuration. Note: In all scenarios in this lesson.GB tuned HJ plan HJ GB GB HJ HJ HJ HJ Well-tuned plan Oracle Database 10g Baseline plans staging table Development database Possible SQL Plan Manageability Scenarios • Database upgrade: Bulk SQL plan loading is especially useful when the system is being upgraded from an earlier version to Oracle Database 11g. assume that OPTIMIZER_USE_SQL_PLAN_BASELINES is set to TRUE. This strategy can minimize plan regressions resulting from the use of the new optimizer version. You can capture pre–Oracle Database 11g plans in an STS and import them into Oracle Database 11g. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Performance Analyzer and SQL Plan Baseline Scenario . This method represents the best form of the plan-seeding process because it helps prevent performance regressions while preserving performance improvements upon database upgrade.2. SQL Plan Management Chapter 15 .Page 17 Oracle University and InfoTech (Pvt.SQL Performance Analyzer and SQL Plan Baseline Scenario Before change O_F_E=10 Oracle Database 11g Plan history GB GB HJ HJ HJ Regressing statements HJ No plan regressions After O_F_E=11 change optimizer_features_enable GB GB GB HJ HJ HJ HJ HJ Oracle Database 10g HJ Well-tuned plans SQL Performance Analyzer and SQL Plan Baseline Scenario A variation of the first method described in the previous slide is through the use of SQL Performance Analyzer.1. Then set the optimizer_features_enable (O_F_E) initialization parameter to 10.0 to make the optimizer behave as if this were a 10g Oracle Database. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.0 and rerun SQL Performance Analyzer for the STS. SQL Performance Analyzer produces a report that lists a SQL statement whose plan has regressed from 10g to 11g. Oracle and/or its affiliates. Copyright © 2010. you can capture their plans using an STS and then load them into the SMB. For those SQL statements that are shown by SQL Performance Analyzer to incur performance regression due to the new optimizer version. set the optimizer_features_enable initialization parameter back to 11. Next run SQL Performance Analyzer for the STS. When that is complete. During this initial time period.0. SQL Plan Management Chapter 15 . Because automatic SQL plan capture is also enabled during this period. set the initialization parameter optimizer_features_enable (O_F_E) to the pre– Oracle Database 11g version value for an initial period of time such as a quarter. All rights reserved. you can remove the setting of O_F_E to take advantage of the new optimizer version while incurring minimal or no plan regressions due to the plan baselines.1 optimizer_capture_sql_plan_baselines=true Loading a SQL Plan Baseline Automatically: Scenario Another upgrade scenario involves using the automatic SQL plan capture mechanism. Regressed plans will use the previous optimizer version. In this case. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Loading a SQL Plan Baseline Automatically .1. the pre–Oracle Database 11g plans produced by the optimizer are captured as SQL plan baselines. Copyright © 2010.Loading a SQL Plan Baseline Automatically Oracle Database 11g No plan regressions Plan history Plan baseline GB Oracle Database 11g New plan waiting verification GB HJ HJ HJ HJ Plan history Plan baseline GB GB HJ HJ GB HJ HJ HJ HJ optimizer_features_enable=10. Oracle and/or its affiliates.2.0. nonregressed statements will benefit from the new optimizer version.0. and execute your workload after upgrade by using the automatic SQL plan capture. because of the O_F_E parameter setting. When the initial time period ends.Page 18 Oracle University and InfoTech (Pvt.2 optimizer_capture_sql_plan_baselines=true No plan regressions optimizer_features_enable=11.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the optimizer is able to reproduce pre–Oracle Database 11g plans for a majority of the SQL statements.1.1 optimizer_capture_sql_plan_baselines=true Oracle Database 11g Better plans Plan history Plan baseline GB HJ HJ GB HJ HJ Welltuned plans Oracle Database 10g GB GB GB HJ HJ HJ HJ HJ HJ optimizer_features_enable=11. DBA_SQL_MANAGEMENT_CONFIG time 105 Alert log SQL Management Base SYSAUX 53 1% 10% 20% 50% space SQL> exec :cnt := dbms_spm.Purging SQL Management Base Policy SQL> exec dbms_spm.20).configure('SPACE_BUDGET_PERCENT'. A limit based on the percentage size of the SYSAUX tablespace is defined. the size of SYSAUX is increased. Copyright © 2010. You can look at the current configuration settings for the SMB by examining the DBA_SQL_MANAGEMENT_CONFIG view. warnings are written to the alert log. By default. or the size of SMB is decreased by purging some of the SQL management objects (such as SQL plan baselines or SQL profiles). In addition.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If SMB space exceeds the defined percentage limit. All rights reserved. To do so. you can configure SMB and change the space budget to a value between 1 percent and 50 percent by using the DBMS_SPM. you can configure SMB and change the unused plan retention period to a value between 5 weeks and 523 weeks (a little more than 10 years).drop_sql_plan_baseline('SYS_SQL_37e0168b04e73efe'). The task runs as an automated task in the maintenance window. Purging SQL Management Base Policy The space occupied by the SQL management base (SMB) is checked weekly against a defined limit. SQL Plan Management Chapter 15 . SQL> exec dbms_spm. Any plan that has not been used for more than 53 weeks is purged. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Purging SQL Management Base Policy . However.DROP_SQL_PLAN_BASELINE function (as shown in the example in the slide).CONFIGURE procedure. you can manually purge the SMB by using the DBMS_SPM. use the DBMS_SPM.CONFIGURE procedure. Warnings are generated weekly until the SMB space limit is increased. the space budget limit for the SMB is set to 10 percent of SYSAUX size. Oracle and/or its affiliates.Page 19 Oracle University and InfoTech (Pvt.105). The space management of SQL plan baselines is done proactively using a weekly purging task. However.configure('PLAN_RETENTION_WEEKS'. pack. unpack.Enterprise Manager and SQL Plan Baselines Enterprise Manager and SQL Plan Baselines Use the SQL Plan Management page to manage SQL profiles. click the Server tab. and SQL plan baselines from one location rather than from separate locations in Enterprise Manager. and evolve selected baselines. drop.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SQL Plan Management Chapter 15 . From this page.Page 20 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Enterprise Manager and SQL Plan Baselines . load. Oracle and/or its affiliates. To navigate to this page. disable. All rights reserved. You can also enable. and then click the SQL Plan Control entry in the Query Optimizer section. Copyright © 2010. SQL patches. you can also configure the various SQL plan baseline settings. Compares the plan it develops with accepted plans in the baselines c. it uses enabled plans in the baselines e. it is stored in the plan history. but it is not part of the baseline until it is verified and marked as accepted. the baseline is used. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Quiz . the optimizer: a. SQL Plan Management Chapter 15 .Page 21 Oracle University and InfoTech (Pvt. Copyright © 2010. Develops plans and adds them to the baselines as verified Answer: b The optimizer always develops an execution plan. Oracle and/or its affiliates. Then it compares the plan with accepted plans in the SQL baseline. Does not develop an execution plan. Does not develop an execution plan. If the plan developed by the optimizer is different.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Compares the plan it develops with enabled plans in the baselines d. it uses an accepted plan in the baselines b.Quiz When the OPTIMIZER_USE_SQL_PLAN_BASELINES parameter is set to TRUE. All rights reserved. If an accepted baseline exists. SQL Plan Management Chapter 15 .Summary In this lesson. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 22 Oracle University and InfoTech (Pvt. you should have learned how to: • Manage SQL performance through changes • Set up SQL Plan Management • Set up various SQL Plan Management scenarios Copyright © 2010. Oracle and/or its affiliates. SQL Plan Management Chapter 15 . All rights reserved. Oracle and/or its affiliates. Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice 15: Overview Using SQL Plan Management Copyright © 2010.Page 23 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Practice 15: Overview Using SQL Plan Management This practice covers the use of SQL Plan Management. SQL Plan Management Chapter 15 .Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates.Page 24 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved.) Ltd use only Using Optimizer Hints . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 16 Copyright © 2010.Page 1 Oracle University and InfoTech (Pvt. Using Optimizer Hints Chapter 16 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ) Ltd use only Using Optimizer Hints . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Optimizer Hints Copyright © 2010.THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 2 Oracle University and InfoTech (Pvt. All rights reserved. Using Optimizer Hints Chapter 16 . Using Optimizer Hints Chapter 16 .Page 3 Oracle University and InfoTech (Pvt. you should be able to : • Use hints when appropriate • Specify hints for: – – – – – – Optimizer mode Query transformation Access path Join orders Join methods Views Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives .Objectives After completing this lesson.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. All rights reserved. No space is permitted. In addition. Changing database conditions as well as query performance enhancements in subsequent releases can have a significant impact on how hints in your code affect performance. The plus sign must follow immediately after the comment delimiter. and only after you have collected statistics on the relevant tables and evaluated the optimizer plan without hints using the EXPLAIN PLAN statement. Oracle and/or its affiliates. checked. This is illustrated in the slide example where you force the optimizer to use the EMPFIRSTNAME_IDX index to retrieve the data. you can use comments in a SQL statement to pass instructions to the optimizer. use hints to force the optimizer to use the optimal execution plan. you might be able to select a more efficient execution plan than the plan that the optimizer recommends. When you use a hint. the use of hints involves extra code that must be managed. you might know that a certain index is more selective for certain queries. Based on this information. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Hints: Overview . For example. In such a case. All rights reserved. and controlled. Hints provide a mechanism to direct the optimizer to select a certain query execution plan based on the specific criteria.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. Optimizer Hints: Overview Hints enable you to influence decisions made by the optimizer. Using Optimizer Hints Chapter 16 . As you can see. it is good practice to also add a comment about that hint.Optimizer Hints: Overview Optimizer hints: • Influence optimizer decisions • Example: SELECT /*+ INDEX(e empfirstname_idx) skewed col */ * FROM employees e WHERE first_name='David' • • HINTS SHOULD ONLY BE USED AS A LAST RESORT. The plus sign (+) causes the system to interpret the comment as a list of hints.Page 4 Oracle University and InfoTech (Pvt. Hints should be used sparingly. ALL_ROWS is an example of a statement hint. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Types of Hints . Using Optimizer Hints Chapter 16 . LEADING is an example of a multitable hint. INDEX and USE_NL are examples of single-table hints. Statement: Statement hints apply to the entire SQL statement. Note: USE_NL(table1 table2) is not considered a multitable hint because it is actually a shortcut for USE_NL(table1) and USE_NL(table2). Oracle and/or its affiliates. except that the hint can specify one or more tables or views. Copyright © 2010. Multitable: Multitable hints are like single-table hints.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Types of Hints Single-table hints Specified on one table or view Multitable hints Specify more than one table or view Query block hints Operate on a single query block Statement hints Apply to the entire SQL statement Types of Hints Single-table: Single-table hints are specified on one table or view.Page 5 Oracle University and InfoTech (Pvt. Query block: Query block hints operate on single query blocks. STAR_TRANSFORMATION and UNNEST are examples of query block hints. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Optimizer Hint Syntax Enclose hints within the comments of a SQL statement. SELECT. INTERSECT) For example.Page 6 Oracle University and InfoTech (Pvt. a compound query consisting of two component queries combined by the UNION operator has two blocks. not to the optimization of the second component query. one for each component query.Specifying Hints Hints apply to the optimization of only one statement block: • A self-contained DML statement against a table • A top-level DML or a subquery MERGE SELECT INSERT /*+ hint */ comment text UPDATE DELETE MERGE SELECT INSERT UPDATE --+ hint comment text DELETE Specifying Hints Hints apply to the optimization of only the block of the statement in which they appear. All rights reserved. MINUS. or DELETE statement • A parent statement or a subquery of a complex statement • A part of a compound query using set operators (UNION. The hint delimiter (+) must come immediately after the comment delimiter. Oracle and/or its affiliates. For this reason. Using Optimizer Hints Chapter 16 . the optimizer does not recognize that the comment contains hints. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Specifying Hints . INSERT. Copyright © 2010. UPDATE. hints in the first component query apply only to its optimization. You can use either style of comment. If you separate them by a space. A statement block is: • A simple MERGE. Hints apply to only the statement block in which they appear. • Hints apply to only the statement block in which they appear and override instance.Page 7 Oracle University and InfoTech (Pvt. The optimizer ignores hints specified incorrectly without raising errors. hints must reference the aliases rather than the table names. Copyright © 2010. If a statement uses aliases. but it can contain multiple hints. but it can contain many hints inside that comment separated by spaces. or UPDATE) of a SQL statement block. Rules for Hints • You must place the hint comment immediately after the first keyword (MERGE. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Rules for Hints .Rules for Hints • • • • • Place hints immediately after the first SQL keyword of a statement block. • A statement block can have only one comment containing hints. DELETE. Oracle and/or its affiliates. However. The Oracle optimizer ignores incorrectly specified hints.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Other (correctly) specified hints in the same comment are considered. INSERT. • If a SQL statement uses aliases. • The Oracle optimizer also ignores combinations of conflicting hints. Each statement block can have only one hint comment.or session-level parameters. be aware of the following situations: • You never get an error message. SELECT. hints must reference the aliases rather than the table names. Using Optimizer Hints Chapter 16 . • Hints may prevent the optimizer from using better execution plans. Be aware of the performance impact of hard-coded hints when they become less valid. Oracle and/or its affiliates. • Hints may become less valid (or even invalid) when the database structure or contents change. Hint Recommendations • Use hints as a last remedy when tuning SQL statements. All rights reserved. Copyright © 2010.Page 8 Oracle University and InfoTech (Pvt.Hint Recommendations • • Use hints carefully because they imply a high-maintenance load. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hint Recommendations . Using Optimizer Hints Chapter 16 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. prod_min_price = (SELECT (pr. The execution plan is as follows: Execution Plan ---------------------------------------------------------0 UPDATE STATEMENT Optimizer=ALL_ROWS (Cost=3 …) 1 0 2 1 3 2 UPDATE OF 'PRODUCTS' TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' (TABLE) (Cost…) INDEX (RANGE SCAN) OF 'PRODUCTS_PROD_CAT_IX' (INDEX) (cost…) 4 5 1 4 TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' (TABLE) (Cost…) INDEX (UNIQUE SCAN) OF 'PRODUCTS_PK' (INDEX (UNIQUE)) (Cost=0 …) The hint shown in the example works only if an index called PRODUCTS_PROD_CAT_IX exists on the PRODUCTS table in the PROD_CATEGORY column. Copyright © 2010. Using Optimizer Hints Chapter 16 . on stock' / Optimizer Hint Syntax: Example The slide shows an example with a hint that advises the cost-based optimizer (CBO) to use the index.Page 9 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.95) FROM products pr WHERE p.prod_list_price*.prod_id = pr.prod_category = 'Men' AND p. All rights reserved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimizer Hint Syntax: Example .Optimizer Hint Syntax: Example UPDATE /*+ INDEX(p PRODUCTS_PROD_CAT_IX)*/ products p SET p.prod_id) WHERE p.prod_status = 'available. Hint Categories There are hints for: • Optimization approaches and goals • Access paths • Query transformations • Join orders • Join operation • Parallel execution • Additional hints Hint Categories Most of these hints are discussed in the following slides. Copyright © 2010. Note: Hints for parallel execution is not covered in this course.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hint Categories . Using Optimizer Hints Chapter 16 . Many of these hints accept the table and index names as arguments.Page 10 Oracle University and InfoTech (Pvt. All rights reserved. Oracle and/or its affiliates. FIRST_ROWS(n): The FIRST_ROWS(n) hint (where n is any positive integer) instructs the Oracle server to optimize an individual SQL statement for fast response. SET OPTIMIZER_MODE statement does not affect SQL that is run from within PL/SQL. The optimizer ignores this hint SELECT statement blocks that include any blocking operations. If you specify hints for access paths or join operations along with either the ALL_ROWS or FIRST_ROWS(n) hint. If you specify either the ALL_ROWS or the FIRST_ROWS(n) hint in a SQL statement.Page 11 Oracle University and InfoTech (Pvt. That is. and if the data dictionary does not have statistics about tables accessed by the statement. The FIRST_ROWS hint. If you specify this hint in any such statement. which optimizes for the best plan to return the first single row. Optimization Goals and Approaches ALL_ROWS: The ALL_ROWS hint explicitly selects the cost-based approach to optimize a statement block with a goal of best throughput. is retained for backward compatibility and plan stability. minimum total resource consumption. Oracle and/or its affiliates. the optimizer gives precedence to the access paths and join operations specified by the hints. All rights reserved. Using Optimizer Hints Chapter 16 .. Such statements cannot be optimized for best response time because Oracle Database must retrieve all rows accessed by the statement before returning the first row. then the optimizer uses default statistical values to estimate the missing statistics and to subsequently select an execution plan. such as sorts or groupings.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Optimization Goals and Approaches . Copyright © 2010.Optimization Goals and Approaches ALL_ROWS Selects a cost-based approach with a goal of best throughput FIRST_ROWS(n) Instructs the Oracle server to optimize an individual SQL statement for fast response Note: The ALTER SESSION. the database optimizes for best throughput. It instructs the server to select the plan that returns the first n rows most efficiently.. Using Optimizer Hints Chapter 16 .Page 12 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Note: The FIRST_ROWS hints are probably the most useful hints. All rights reserved.Oracle University and InfoTech (Pvt. Copyright © 2010. If a hint specifies an unavailable access path.Page 13 Oracle University and InfoTech (Pvt.employees e WHERE last_name LIKE 'K%'. Oracle and/or its affiliates.Hints for Access Paths FULL Performs a full table scan CLUSTER Accesses table using a cluster scan HASH Accesses table using a hash scan ROWID Accesses a table by ROWID INDEX Selects an index scan for the specified table INDEX_ASC Scans an index in ascending order INDEX_COMBINE Explicitly chooses a bitmap access path Hints for Access Paths Specifying one of these hints causes the optimizer to choose the specified access path only if the access path is available based on the existence of an index and on the syntactic constructs of the SQL statement. CLUSTER: The CLUSTER hint instructs the optimizer to use a cluster scan to access the specified table.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. This hint applies only to clustered tables. the optimizer ignores it. HASH: The HASH hint instructs the optimizer to use a hash scan to access the specified table. For example: SELECT /*+ FULL(e) */ employee_id. If the statement uses an alias for the table. You must specify the table to be accessed exactly as it appears in the statement. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Access Paths . last_name FROM hr. All rights reserved. The Oracle server performs a full table scan on the employees table to execute this statement. even if there is an index on the last_name column that is made available by the condition in the WHERE clause. use the alias rather than the table name in the hint. This hint applies only to tables stored in a table cluster. Using Optimizer Hints Chapter 16 . FULL: The FULL hint explicitly selects a full table scan for the specified table. The table name in the hint should not include the schema name if the schema name is present in the statement. bitmap. The optimizer does not consider a full table scan.Page 14 Oracle University and InfoTech (Pvt. and INDEX_SS. Copyright © 2010. the optimizer tries to use some Boolean combination of those particular bitmap indexes. If the statement uses an index range scan. and NO_INDEX_SS. If this hint specifies a list of available indexes. If no indexes are given as arguments for the INDEX_COMBINE hint. INDEX_FFS. there are counter hints. This hint can optionally specify one or more indexes. . For example: SELECT /*+INDEX_COMBINE(customers cust_gender_bix cust_yob_bix)*/ * FROM customers WHERE cust_year_of_birth < 70 AND cust_gender = 'M'. the Oracle server scans the index entries in ascending order of their indexed values. NO_INDEX_FFS. Oracle and/or its affiliates. However. the optimizer considers the cost of a scan on each available index on the table and then performs the index scan with the lowest cost. NO_INDEX. should the default behavior change. However. Note: For INDEX. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED ROWID: The ROWID hint explicitly chooses a table scan by ROWID for the specified table. If this hint specifies a single available index. the optimizer performs a scan on this index. All rights reserved. INDEX_ASC: The INDEX_ASC hint explicitly chooses an index scan for the specified table. The optimizer can also choose to scan multiple indexes and merge the results.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using Optimizer Hints Chapter 16 . it is better if you use INDEX_COMBINE rather than INDEX for bitmap indexes because it is a more versatile hint. if such an access path has the lowest cost. If this hint specifies no indexes. If certain indexes are given as arguments. Because the server’s default behavior for a range scan is to scan index entries in the ascending order of their indexed values. if such an access path has the lowest cost. you might want to use the INDEX_ASC hint to specify ascending range scans explicitly. the optimizer uses a Boolean combination of bitmap indexes that has the best cost estimate for the table. this hint does not specify anything more than the INDEX hint. The optimizer does not consider a full table scan or a scan on another index on the table. The optimizer does not consider a full table scan or a scan on an index not listed in the hint. You can use the INDEX hint for domain. the optimizer considers the cost of a scan on each index in the list and then performs the index scan with the lowest cost. respectively to avoid using those paths.INDEX: The INDEX hint explicitly chooses an index scan for the specified table. and bitmap join indexes. B*-tree. The optimizer can also choose to scan multiple indexes from this list and merge the results. INDEX_COMBINE: The INDEX_COMBINE hint explicitly chooses a bitmap access path for the table. resulting in a scan of the index entries in the ascending order. the system scans the index entries in the descending order of their indexed values. The INDEX_DESC hint explicitly chooses an index scan for the specified table. Using Optimizer Hints Chapter 16 . In a partitioned index.Page 15 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Access Paths . the results are in the descending order within each partition.Hints for Access Paths INDEX_JOIN Instructs the optimizer to use an index join as an access path INDEX_DESC Scans an index in descending order INDEX_FFS Performs a fast-full index scan INDEX_SS Performs an index skip scan NO_INDEX Does not allow using a set of indexes Hints for Access Paths (continued) INDEX_JOIN: The INDEX_JOIN hint explicitly instructs the optimizer to use an index join as an access path. If the statement uses an index range scan and the index is ascending. this hint effectively cancels out the descending order. the following query uses an index join to access the employee_id and department_id columns. For example. department_id FROM hr. both of which are indexed in the employees table: SELECT /*+index_join(employees emp_emp_id_pk emp_department_ix)*/ employee_id. All rights reserved. Oracle and/or its affiliates.employees WHERE department_id > 50.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. a sufficiently small number of indexes must exist that contain all the columns required to resolve the query. For example: Copyright © 2010. For a descending index. For the hint to have a positive effect. INDEX_DESC: The INDEX_DESC hint instructs the optimizer to use a descending index scan for the specified table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SELECT /*+ INDEX_DESC(a ord_order_date_ix) */ a. The NO_INDEX hint applies to function-based. orders o WHERE l.Page 16 Oracle University and InfoTech (Pvt. There are also INDEX_SS_ASC and INDEX_SS_DESC hints. Using Optimizer Hints Chapter 16 . • If this hint specifies a single available index. This behavior is the same as a NO_INDEX hint that specifies a list of all available indexes for the table. If a NO_INDEX hint and an index hint (INDEX. INDEX_DESC. Oracle and/or its affiliates.INDEX_FFS: The INDEX_FFS hint causes a fast-full index scan to be performed rather than a full table scan. NO_INDEX: The NO_INDEX hint explicitly disallows a set of indexes for the specified table.order_id = o.order_id > 50 AND l. For example: SELECT /*+ INDEX_FFS ( o order_pk ) */ COUNT(*) FROM order_items l. Copyright © 2010. the optimizer does not consider a scan on any of the specified indexes. a.promotion_id.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.order_id.order_date.order_id FROM oe. B*-tree. In a partitioned index. or domain indexes.orders a WHERE a. All rights reserved. . bitmap. then both the NO_INDEX hint and the index hint are ignored for the specified indexes and the optimizer considers the specified indexes. • If this hint specifies no indexes. the optimizer does not consider a scan on any index on the table. or INDEX_FFS) both specify the same indexes. Other indexes that are not specified in the list are still considered. If the statement uses an index range scan. INDEX_SS: The INDEX_SS hint instructs the optimizer to perform an index skip scan for the specified indexes of the specified table. INDEX_COMBINE. For example: SELECT /*+NO_INDEX(employees emp_empid)*/ employee_id FROM employees WHERE employee_id > 200. INDEX_ASC. the system scans the index entries in the ascending order of their indexed values. the optimizer does not consider a scan on this index. the results are in the ascending order within each partition. a. Other indexes that are not specified are still considered. • If this hint specifies a list of available indexes.order_date < '01-jan-1985'. CUSTOMERS WHERE ( CUST_GENDER= 'F' AND CUST_MARITAL_STATUS = 'single') OR CUST_YEAR_OF_BIRTH BETWEEN '1917' AND '1920'. and CUST_YEAR_OF_BIRTH) have a bitmap index. CUST_GENDER. The INDEX_COMBINE Hint: Example The INDEX_COMBINE hint is designed for bitmap index operations. Using Optimizer Hints Chapter 16 . Remember the following: • If certain indexes are given as arguments for the hint. Copyright © 2010. All rights reserved. Oracle and/or its affiliates. suppose that all the three columns that are referenced in the WHERE predicate of the statement in the slide (CUST_MARITAL_STATUS.Page 17 Oracle University and InfoTech (Pvt. • If no indexes are named in the hint. all indexes are considered to be hinted. the optimizer tries to use some combination of those particular bitmap indexes. whether or not it considers them to be cost effective. the execution plan of the statement might appear as shown in the next slide.The INDEX_COMBINE Hint: Example SELECT /*+INDEX_COMBINE(CUSTOMERS)*/ cust_last_name FROM SH. When you enable AUTOTRACE. In the example in the slide. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The INDEX_COMBINE Hint: Example . • The optimizer always tries to use hinted indexes.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the following bitmap row sources are used: • BITMAP CONVERSION TO ROWIDS: Converts bitmaps into ROWIDs to access a table • COUNT: Returns the number of entries if the actual values are not needed • BITMAP OR: Computes the bitwise OR of two bitmaps • BITMAP AND: Computes the bitwise AND of two bitmaps • BITMAP INDEX SINGLE VALUE: Looks up the bitmap for a single key • BITMAP INDEX RANGE SCAN: Retrieves bitmaps for a value range • BITMAP MERGE: Merges several bitmaps resulting from a range scan into one (using a bitwise AND operator) Copyright © 2010.Page 18 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using Optimizer Hints Chapter 16 .The INDEX_COMBINE Hint: Example Execution Plan --------------------------------------------------| 0 | SELECT STATEMENT | | 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS | 2 | BITMAP CONVERSION TO ROWIDS | | 3 | BITMAP OR | | 4 | BITMAP MERGE | | 5 | BITMAP INDEX RANGE SCAN | CUST_YOB_BIX | 6 | BITMAP AND | | 7 | BITMAP INDEX SINGLE VALUE| CUST_MARITAL_BIX | 8 | BITMAP INDEX SINGLE VALUE| CUST_GENDER_BIX The INDEX_COMBINE Hint: Example (continued) In the example in the slide. All rights reserved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED The INDEX_COMBINE Hint: Example . without cost consideration. This course does not deal with Materialized Views.Hints for Query Transformation USE_CONCAT Skips all query transformation Rewrites OR into UNION ALL and disables INLIST processing NO_EXPAND Prevents OR expansions REWRITE Rewrites query in terms of materialized views NO_REWRITE Turns off query rewrite UNNEST Merges subquery bodies into surrounding query block NO_UNNEST Turns off unnesting NO_QUERY_TRANSFORMATION Hints for Query Transformation NO_QUERY_TRANSFORMATION: The NO_QUERY_TRANSFORMATION hint instructs the optimizer to skip all query transformations. The USE_CONCAT hint disables IN-list processing.Page 19 Oracle University and InfoTech (Pvt. star transformation. NO_EXPAND: The NO_EXPAND hint prevents the cost-based optimizer from considering ORexpansion for queries having OR conditions or IN-lists in the WHERE clause. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Query Transformation . Using Optimizer Hints Chapter 16 . All rights reserved. view merging. Use the REWRITE hint with or without a view list. UNNEST: The UNNEST hint instructs the optimizer to unnest and merge the body of the subquery into the body of the query block that contains it. USE_CONCAT: The USE_CONCAT hint forces combined OR conditions in the WHERE clause of a query to be transformed into a compound query using the UNION ALL set operator. subquery unnesting. this transformation occurs only if the cost of the query using the concatenations is cheaper than the cost without them. Usually. Generally. when possible. allowing the optimizer to consider them together when evaluating access paths and joins. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. REWRITE: The REWRITE hint instructs the optimizer to rewrite a query in terms of materialized views. and materialized view rewrite. including but not limited to OR-expansion. the optimizer considers using OR expansion and uses this method if it decides that the cost is lower than not using it. it should be placed in the surrounding query. but you can disable this mechanism using the NO_MERGE hint. then the optimizer can merge the view’s query into the accessing statement only if complex view merging is enabled.Page 20 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Query Transformation .Hints for Query Transformation MERGE Merges complex views or subqueries with the surrounding query NO_MERGE Prevents merging of mergeable views STAR_TRANSFORMATION Makes the optimizer use the best plan in which the transformation can be used FACT Indicates that the hinted table should be considered as a fact table NO_FACT Indicates that the hinted table should not be considered as a fact table Hints for Query Transformation (continued) MERGE: The MERGE hint lets you merge a view for each query. If a view’s query contains a GROUP BY clause or a DISTINCT operator in the SELECT list. All rights reserved. it should be placed in the view query block. the optimizer could make a cost-based decision to use the best plan that is generated without the transformation. Complex merging can also be used to merge an IN subquery into the accessing statement if the subquery is not correlated.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. it should be placed in the surrounding query. When the NO_MERGE hint is used without an argument. it should be placed in the view query block. This is the case by default. NO_MERGE: The NO_MERGE hint causes the Oracle server not to merge views that can be merged. Copyright © 2010. When MERGE is used with the view name as an argument. When NO_MERGE is used with the view name as an argument. When the MERGE hint is used without an argument. Without the hint. This hint gives the user more influence over the way in which the view is accessed. Oracle and/or its affiliates. instead of the best plan for the transformed query. STAR_TRANSFORMATION: The STAR_TRANSFORMATION hint causes the optimizer to use the best plan in which the transformation has been used. Using Optimizer Hints Chapter 16 . Oracle University and InfoTech (Pvt. All rights reserved.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. If no subqueries are generated.Page 21 .FACT: The FACT hint is used in the context of the star transformation to indicate to the transformation that the hinted table should be considered as a fact table. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Even if the hint is given. NO_FACT: The NO_FACT hint is used in the context of the star transformation to indicate to the transformation that the hinted table should not be considered as a fact table. Using Optimizer Hints Chapter 16 . there is no guarantee that the transformation will take place. Oracle and/or its affiliates. Copyright © 2010. there is no transformed query. The optimizer generates the subqueries only if it seems reasonable to do so. and the best plan for the untransformed query is used regardless of the hint. the most precise method is to order the tables in the FROM clause in the order of the keys in the index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. You might want to use the ORDERED hint to specify a join order if you know something that the optimizer does not know about the number of rows that are selected from each table. Then use the following hints: /*+ ORDERED USE_NL(FACTS) INDEX(facts fact_concat) */ Here. LEADING: The LEADING hint instructs the optimizer to use the specified set of tables as the prefix in the execution plan. If you specify two or more LEADING hints on different tables. With a nested loops example. with the large table at the end. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Join Orders . If you specify the ORDERED hint. If you omit the ORDERED hint from a SQL statement performing a join. the optimizer selects the order in which to join the tables. A more general method is to use the STAR hint. The LEADING hint is ignored if the tables specified cannot be joined first in the order specified because of dependencies in the join graph. Oracle and/or its affiliates. Using Optimizer Hints Chapter 16 . Copyright © 2010. it overrides all LEADING hints.Page 22 Oracle University and InfoTech (Pvt.Hints for Join Orders ORDERED Causes the Oracle server to join tables in the order in which they appear in the FROM clause LEADING Uses the specified tables as the first table in the join order Hints for Join Orders The following hints are used to suggest join orders: ORDERED: The ORDERED hint causes the Oracle server to join tables in the order in which they appear in the FROM clause. facts is the table and fact_concat is the index. all the hints are ignored. If an index is specified. rather than for best throughput. All rights reserved. then you can force the optimizer to select a nested loop join by using the USE_NL hint. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints for Join Operations . if no index is specified. Oracle and/or its affiliates. However. Use of the USE_NL and USE_MERGE hints is recommended with the ORDERED hint. The Oracle server uses these hints when the referenced table is forced to be the inner table of a join. If you want to optimize the statement for best response time or for the minimal elapsed time that is necessary to return the first row selected by the query. the optimizer must be able to use that index with at least one join predicate as the index key. However. NO_USE_NL: The NO_USE_NL hint causes the optimizer to exclude the nested loops join.Page 23 Oracle University and InfoTech (Pvt. the table name in the hint should not include the schema name if the schema name is present in the statement.Hints for Join Operations USE_NL Joins the specified table using a nested loop join NO_USE_NL Does not use nested loops to perform the join USE_NL_WITH_INDEX Similar to USE_NL. you must specify a table exactly the same way as it appears in the statement. you must use the alias rather than the table name in the hint. If the statement uses an alias for the table. In the hint. USE_NL_WITH_INDEX: The USE_NL_WITH_INDEX hint is similar to the USE_NL hint. the optimizer must be able to use some index with at least one join predicate as the index key.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. USE_NL: The USE_NL hint causes the Oracle server to join each specified table to another row source with a nested loops join. Using Optimizer Hints Chapter 16 . but must be able to use an index for the join USE_MERGE Joins the specified table using a sort-merge join NO_USE_MERGE Does not perform sort-merge operations for the join USE_HASH Joins the specified table using a hash join NO_USE_HASH Does not use hash join DRIVING_SITE Instructs the optimizer to execute the query at a different site than that selected by the database Hints for Join Operations Each hint described here suggests a join operation for a table. using the specified table as the inner table. the hints are ignored if the referenced table is the outer table. unit_price*quantity) FROM oe.Page 24 Oracle University and InfoTech (Pvt.order_id = h.In many cases. order_items is accessed through the index order_id. resulting in an execution plan similar to one that is used on larger systems. Copyright © 2010.department_id.order_date.order_items l WHERE l.oe.product_id. USE_HASH: The USE_HASH hint causes the Oracle server to join each specified table with another row source using a hash join.unit_price * l. l. l2.order_id.order_id. .department_id = departments.order_id = h. departments WHERE employees. l. l.order_date.order_items l2 WHERE l. Using Optimizer Hints Chapter 16 . oe.order_id GROUP BY l2.orders l. In the following statement in which a nested loop is forced through a hint. For every row that meets the filter condition. NO_USE_MERGE: The NO_USE_MERGE hint causes the optimizer to exclude the sort-merge join to join each specified table to another row source using the specified table as the inner table. even though it might not be particularly efficient here.order_id. SUM(l2. DRIVING_SITE: This hint instructs the optimizer to execute the query at a different site than that selected by the database. USE_MERGE: The USE_MERGE hint causes the Oracle server to join each specified table with another row source by using a sort-merge join. as in the following example: SELECT /*+USE_MERGE(employees departments)*/ * FROM employees. NO_USE_HASH: The NO_USE_HASH hint causes the optimizer to exclude the hash join to join each specified table to another row source using the specified table as the inner table.department_id = departments. a nested loop join returns the first row faster than a sort-merge join does. Here is another example: SELECT /*+use_hash(employees departments)*/ * FROM hr.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.orders h . But a sort-merge join cannot return the first row until after reading and sorting all selected rows of both tables and then combining the first rows of each sorted row source.order_id filter condition is applied to every row. orders is accessed through a full table scan and the l.quantity FROM oe. SELECT /*+ USE_NL(l h) */ h. Oracle and/or its affiliates. Adding an INDEX hint to the query could avoid the full table scan on orders. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED However. hr.employees. A nested loop join can return the first row after reading the first selected row from one table and the first matching row from the other and combining them. In such cases.departments WHERE employees. All rights reserved. l. as in the following example: SELECT /*+USE_HASH(l l2) */ l. in some cases tables can only be joined using nested loops.order_id = l2. This hint is useful if you are using distributed query optimization to decide on which site a join should be executed.product_id.customer_id. the optimizer ignores the hint for those tables.department_id. and direct-path INSERT is the default in parallel mode. the Oracle server executes the SQL statement without any attempt to replace literals with bind variables. In direct-path INSERT. (Conventional INSERT is the default in serial mode. In other words. This is controlled with the CURSOR_SHARING startup parameter. Note: In Enterprise Edition. As a result. and direct-path INSERT is the default in parallel mode. NOAPPEND: The NOAPPEND hint disables direct-path INSERT by disabling parallel mode for the duration of the INSERT statement. Using Optimizer Hints Chapter 16 . Conventional INSERT is the default in serial mode. data is appended to the end of the table rather than using existing space currently allocated to the table. Your database is in serial mode if you are not using Enterprise Edition. All rights reserved. a session must be placed in parallel mode for direct-path insert to be the default. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Additional Hints .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Additional Hints APPEND Enables direct-path INSERT NOAPPEND Enables regular INSERT CURSOR_SHARING_EXACT Prevents replacing literals with bind variables CACHE Overrides the default caching specification of the table PUSH_PRED Pushes join predicate into view PUSH_SUBQ Evaluates nonmerged subqueries first DYNAMIC_SAMPLING Controls dynamic sampling to improve server performance Additional Hints APPEND: The APPEND hint lets you enable direct-path INSERT if your database runs in serial mode. direct-path INSERT can be considerably faster than the conventional INSERT.Page 25 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates.) CURSOR_SHARING_EXACT: The Oracle server can replace literals in SQL statements with bind variables if it is safe to do so. The CURSOR_SHARING_EXACT hint causes this behavior to be disabled. Copyright © 2010. This hint is useful for small lookup tables. Copyright © 2010.Page 26 Oracle University and InfoTech (Pvt. Sampling defaults to the cursor level unless you specify a table. DYNAMIC_SAMPLING: The DYNAMIC_SAMPLING hint lets you control dynamic sampling to improve server performance by determining more accurate selectivity and cardinality estimates.The CACHE and NOCACHE hints affect system statistics table scans (long tables) and table scans (short tables). If the subquery is relatively inexpensive and reduces the number of rows significantly.. . evaluating the subquery earlier can improve performance. Generally. Consider the following example: SELECT /*+ dynamic_sampling(1) */ * FROM . The higher the level. Using Optimizer Hints Chapter 16 . All rights reserved. as shown in the V$SYSSTAT data dictionary view. PUSH_SUBQ: The PUSH_SUBQ hint instructs the optimizer to evaluate nonmerged subqueries at the earliest possible step in the execution plan. This example enables dynamic sampling if all the following conditions are true: • There is more than one table in the query.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED CACHE: The CACHE hint instructs the optimizer to place the blocks retrieved for the table in the corresponding hot part of the buffer cache when a full table scan is performed. • The optimizer determines that a relatively expensive table scan is required for the table that has not been analyzed. • At least one table has not been analyzed and has no indexes. the more effort the compiler puts into dynamic sampling and the more broadly it is applied. This hint has no effect if the subquery is applied to a remote table or one that is joined using a merge join. PUSH_PRED: The PUSH_PRED hint instructs the optimizer to push a join predicate into the view. subqueries that are not merged are executed as the last step in the execution plan. You can set the value of DYNAMIC_SAMPLING to a value from 0 to 10.. OPT_PARAM: The OPT_PARAM hint lets you set an initialization parameter for the duration of the current query only. Oracle and/or its affiliates. This hint is valid only when the CONTROL_MANAGEMENT_PACK_ACCESS parameter is set to DIAGNOSTIC+TUNING. RESULT_CACHE: The RESULT_CACHE hint instructs the database to cache the results of the current query or query fragment in memory and then to use the cached results in future executions of the query or query fragment. NO_MONITOR: The NO_MONITOR hint disables real-time SQL monitoring for the query. Using Optimizer Hints Chapter 16 . In this case. and STAR_TRANSFORMATION_ENABLED Copyright © 2010. This hint is valid only for the following parameters: OPTIMIZER_DYNAMIC_SAMPLING. OPTIMIZER_INDEX_COST_ADJ. All rights reserved. the NO_RESULT_CACHE hint disables such caching for the current query.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. NO_RESULT_CACHE: The optimizer caches query results in the result cache if the RESULT_CACHE_MODE initialization parameter is set to FORCE.Page 27 Oracle University and InfoTech (Pvt. OPTIMIZER_INDEX_CACHING.Additional Hints MONITOR Forces real-time query monitoring NO_MONITOR Disables real-time query monitoring RESULT_CACHE Caches the result of the query or query fragment NO_RESULT_CACHE Disables result caching for the query or query fragment OPT_PARAM Sets initialization parameter for query duration Additional Hints (continued) MONITOR: The MONITOR hint forces real-time SQL monitoring for the query. OPTIMIZER_SECURE_VIEW_MERGING. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Additional Hints . even if the statement is not long running. Mergeable Views The optimizer can merge a view into a referencing query block if the view definition does not contain the following: • Set operators (UNION. This appears as a VIEW step in execution plans. Using Optimizer Hints Chapter 16 . UNION ALL. When these transformations are impossible. In particular. INTERSECT. Use view-optimization techniques: – Statement transformation – Results accessed like a table • Hints can be used on mergeable views and nonmergeable views.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. hints in views are handled differently from hints on views depending on whether or not the view is mergeable into the top-level query. View Optimization The statement is normally transformed into an equivalent statement that accesses the view’s base tables. MINUS) Copyright © 2010. the view’s query is executed and the result is accessed as if it were a table. such hints can result in unexpected plans. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Hints and Views .Hints and Views • • Do not use hints in views. All rights reserved. Oracle and/or its affiliates.Page 28 Oracle University and InfoTech (Pvt. • Push the predicate of the referencing query block inside the view. Hints and Views You should not use hints in or on views because views can be defined in one context and used in another. The optimizer can use one of the following techniques to transform the statement: • Merge the view’s query into the referencing query block in the accessing statement. accessmethod and join hints in the view are always preserved. Oracle and/or its affiliates. Because nonmergeable views are optimized separately from the top-level query. MAX. For the same reason. Access-method and join hints on referenced views are ignored unless the view contains a single table (or references another view with a single table). optimizer-mode hints in the view are ignored. For such single-table views. access-method and join hints in the view are preserved only if the top-level query references no other tables or views (that is. However. if the FROM clause of the SELECT statement contains only the view). Using Optimizer Hints Chapter 16 .Page 29 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. MIN. all access-method and join hints in the view are preserved when the view is merged with the top-level query. Access-method and join hints can also appear in a view definition: • If the view is a subquery (that is. SUM) in the select list Hints and Mergeable Views Optimization-approach and goal hints can occur in a top-level query or in views: • If there is such a hint in the top-level query. Hints and Nonmergeable Views With nonmergeable views. access-method hints on the view in the top-level query are ignored. mode hints in referenced views are used as long as all mode hints in the views are consistent. The top-level query decides the optimization mode. • For views that are not subqueries. whether default or user specified. join hints on the view in the top-level query are preserved because (in this case) a nonmergeable view is similar to a table. • If two or more mode hints in the referenced views conflict. All rights reserved. COUNT. all mode hints in the views are discarded and the session mode is used. that hint is used regardless of any such hints in the views. an access-method hint or a join hint on the view applies to the table in the view. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED • . if it appears in the FROM clause of a SELECT statement). Copyright © 2010. • If there is no top-level optimizer-mode hint.The CONNECT BY clause • The ROWNUM pseudocolumn • Group functions (AVG. it is recommended that you use global hints instead of embedding the hint in the view. in a UNION statement). rather than to tables inside any views that are referenced by the statement. All rights reserved. Global Table Hints Hints that specify a table generally refer to tables in the DELETE.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. SELECT /*+ index(v. For example.c cust_credit_limit_idx) */ v. When you want to specify hints for tables that appear inside views. an optional query block name can precede the table specification. SELECT. v. In addition. by using the global hint structure. The table hints can be transformed into global hints by using an extended table specification syntax that includes view names with the table name as shown in the slide. you can avoid the modification of a view with the specification of an index hint in the body of view.cust_credit_limit FROM city_view v WHERE cust_credit_limit > 5000. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Global Table Hints . Using Optimizer Hints Chapter 16 .Page 30 Oracle University and InfoTech (Pvt. the hint applies to only the first instance of the table (or alias).cust_last_name. Oracle and/or its affiliates. Note: If a global hint references a table name or alias that is used twice in the same query (for example. or UPDATE query block in which the hint occurs.Global Table Hints • • Extended hint syntax enables specifying for tables that appear in views References a table name in the hint with a recursive dot notation CREATE SELECT FROM WHERE view city_view AS * customers c cust_city like 'S%'. SEL$DB579D14 / E@SEL$1 Specifying a Query Block in a Hint You can specify an optional query block name in many hints to specify the query block to which the hint applies. because you specify the FULL hint to apply to the strange query block in the main query block. Plan hash value: 615168685 --------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost(%CPU)| --------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 41 | 7 (15)| |* 1 | HASH JOIN | | 1 | 41 | 7 (15)| |* 2 | TABLE ACCESS FULL| DEPT | 1 | 21 | 3 (0)| |* 3 | TABLE ACCESS FULL| EMP | 3 | 60 | 3 (0)| --------------------------------------------------------------Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------1 . the optimizer does not use the index in question. Using Optimizer Hints Chapter 16 .Page 31 Oracle University and InfoTech (Pvt. You can see that the execution plan exhibits a full table scan on the DEPT table.SEL$DB579D14 / DEPT@STRANGE 3 . In addition. you do not have to specify the @queryblock syntax. When you specify a hint in the query block itself to which the hint applies.Specifying a Query Block in a Hint explain plan for select /*+ FULL(@strange dept) */ ename from emp e.deptno and d. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Specifying a Query Block in a Hint .SEL$DB579D14 2 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL.deptno = d. The syntax of the query block argument is of the @queryblock form. Oracle and/or its affiliates. the output of the plan clearly shows the systemgenerated names for each query block in the original query. You can see that the SELECT statement uses an inline view. The queryblock identifier can either be system-generated or user-specified.loc = 'C'. The slide gives you an example. All rights reserved. where queryblock is an identifier that specifies a query block in the query. Copyright © 2010. The example assumes that there is an index on the DEPTNO column of the DEPT table so that the optimizer would normally choose that index to access the DEPT table. The corresponding query block is given the name strange through the use of the QB_NAME hint. This syntax lets you specify in the outer query a hint that applies to an inline view. NULL. (select /*+ QB_NAME(strange) */ * from dept where deptno=10) d where e. However. 'ALL')). salary) total_sal FROM hr. In the example.last_name. Oracle and/or its affiliates.employee_id AND e1. and if you specify only the INDEX hint for a given table.Page 32 Oracle University and InfoTech (Pvt.start_date GROUP BY e1. if you have a very complex query consisting of many table joins. Copyright © 2010. For example. Specifying a Full Set of Hints When using hints. Therefore. the optimizer might not necessarily use that hint because the optimizer might have determined that the requested index cannot be used due to the join methods and access paths that were selected by the optimizer. then the optimizer needs to determine the remaining access paths to be used as well as the corresponding join methods. the LEADING hint specifies the exact join order to be used.Specifying a Full Set of Hints SELECT /*+ LEADING(e2 e1) USE_NL(e1) INDEX(e1 emp_emp_id_pk) USE_MERGE(j) FULL(j) */ e1. hr.job_history j WHERE e1.first_name. The join methods to be used on the different tables are also specified. you might sometimes need to specify a full set of hints to ensure the optimal execution plan. even though you gave the INDEX hint.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.employee_id = e2. hr.manager_id AND e1.job_id ORDER BY total_sal.employees e2.employees e1.first_name. sum(e2.last_name.employee_id = j. e1.hire_date = j. All rights reserved.job_id. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Specifying a Full Set of Hints . j. j. e1. Using Optimizer Hints Chapter 16 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Summary In this lesson. Using Optimizer Hints Chapter 16 . All rights reserved. you should have learned how to: • Use hints when appropriate • Specify hints for: – – – – – – Optimizer mode Query transformation Access path Join orders Join methods Views Summary In this lesson. To specify a hint.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Use hints as a last remedy when tuning SQL statements. By using hints. you can influence the optimizer at the statement level. one of which includes hints for access-path methods. There are several hint categories. Copyright © 2010. you should have learned about additional optimizer settings and hints.Page 33 Oracle University and InfoTech (Pvt. use the hint syntax in the SQL statement. Using Optimizer Hints Chapter 16 . Oracle and/or its affiliates. All rights reserved.Page 34 .Practice Appendix B: Overview This practice covers using various hints to influence execution plans. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Practice Appendix B: Overview Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Chapter 17 Copyright © 2010. Oracle and/or its affiliates. Using SQL Developer Chapter 17 .THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.Page 1 Oracle University and InfoTech (Pvt.) Ltd use only Using SQL Developer . All rights reserved. THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using SQL Developer Copyright © 2010.) Ltd use only Using SQL Developer .Page 2 Oracle University and InfoTech (Pvt. Using SQL Developer Chapter 17 . Oracle and/or its affiliates. You learn how to use SQL Developer for your database development tasks. You learn how to use SQL Worksheet to execute SQL statements and SQL scripts. Using SQL Developer Chapter 17 . Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Objectives . Copyright © 2010. you are introduced to the graphical tool called SQL Developer.Page 3 Oracle University and InfoTech (Pvt. you should be able to do the following: • List the key features of Oracle SQL Developer • Identify menu items of Oracle SQL Developer • Create a database connection • Manage database objects • Use SQL Worksheet • Save and run SQL scripts • Create and save reports Objectives In this appendix. All rights reserved.Objectives After completing this appendix.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. You can connect to any target Oracle database schema by using standard Oracle database authentication. and to migrate from these databases to an Oracle database. Copyright © 2010. the visual tool for database development. you can perform operations on objects in the database. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED What Is Oracle SQL Developer? . SQL Developer includes support for Oracle Application Express 3. test SQL statements.0. Oracle and/or its affiliates.Page 4 Oracle University and InfoTech (Pvt. and Microsoft Access. All rights reserved. Microsoft SQL Server. and view optimizer plans. and you can view metadata and data in these databases. simplifies the following tasks: • Browsing and managing database objects • Executing SQL statements and scripts • Editing and debugging PL/SQL statements • Creating reports You can connect to any target Oracle database schema by using standard Oracle database authentication. You can also connect to schemas for selected third-party (non-Oracle) databases such as MySQL. Using SQL Developer Chapter 17 .What Is Oracle SQL Developer? • • Oracle SQL Developer is a graphical tool that enhances productivity and simplifies database development tasks. With just a few clicks. When connected. you can easily create and debug stored procedures. The SQL Developer tightly integrates with Developer Migration Workbench that provides users with a single point to browse database objects and data in third-party databases.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.1 (Oracle APEX). SQL Developer. SQL Developer What Is Oracle SQL Developer? Oracle SQL Developer is a free graphical tool designed to improve your productivity and simplify the development of everyday database tasks. Additionally. com/technology/products/database/sql_de veloper/index. SQL Developer 2. With SQL Developer. it is a cross-platform tool. The tool runs on Windows.Page 5 Oracle University and InfoTech (Pvt. you will have to download and install SQL Developer. and Mac OS X platforms Default connectivity by using the Java Database Connectivity (JDBC) thin driver Connects to Oracle Database version 9.2.html. users can connect to Oracle Databases 9. and Mac operating system (OS) X platforms. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Specifications of SQL Developer . Linux. Note For Oracle Database versions earlier than Oracle Database 11g Release 2.com/docs/cd/E15846_01/index. and all Oracle database editions including Express Edition.1 and later Freely downloadable from the following link: – http://www.oracle.0. For instructions on how to install SQL Developer 2. Linux.1 and later.Specifications of SQL Developer • • • • • • Shipped along with Oracle Database 11g Release 2 Developed in Java Supports Windows. SQL Developer does not require an installer and you need to simply unzip the downloaded file.1.5 is shipped along with Oracle Database 11g Release 2. and therefore. Default connectivity to the database is through the JDBC thin driver. All rights reserved.2.html Specifications of SQL Developer Oracle SQL Developer 1. Using SQL Developer Chapter 17 .htm Copyright © 2010. you can visit the following link: http://download.oracle.com/technology/products/database/sql_developer /index.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. no Oracle Home is required.1 is the current version and is freely downloadable from the following link: http://www. SQL Developer is developed in Java leveraging the Oracle JDeveloper integrated development environment (IDE). Oracle and/or its affiliates. Therefore.0.oracle. Files SQL Developer 2. This tab does not appear by default.Page 6 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Note: You need to define at least one connection to be able to connect to a database schema and issue SQL queries or run procedures/functions. from left to right: • Connections tab: By using this tab. You can customize many aspects of the appearance and behavior of SQL Developer by setting preferences. Use the View > Files menu to activate it.1 Interface Reports You must define a connection to start using SQL Developer for running SQL queries on a database schema.1 Interface . this tab enables you to access files from your local machine without having to use the File > Open menu. Copyright © 2010. and the right side to display information about selected objects. this tab enables you to run predefined reports or create and add your own reports. All rights reserved. General Navigation and Use SQL Developer uses the left side for navigation to find and select objects.1 Interface The SQL Developer 2.SQL Developer 2.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Files tab: Identified by the Files folder icon.1 interface contains three main navigation tabs. • Reports tab: Identified by the Reports icon. Using SQL Developer Chapter 17 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED SQL Developer 2. you can browse database objects and users to which you have access. The following menus contain standard entries. Preferences. and SQL Worksheet Note: The Run menu also contains options that are relevant when a function or procedure is selected for debugging. All rights reserved. plus entries for features specific to SQL Developer: • View: Contains options that affect what is displayed in the SQL Developer interface • Navigate: Contains options for navigating to various panes and for executing subprograms • Run: Contains the Run File and Execution Profile options that are relevant when a function or procedure is selected. Oracle and/or its affiliates. and also debugging options • Edit: Contains options for use when you edit functions and procedures • Versioning: Provides integrated support for the following versioning and source control systems: Concurrent Versions System (CVS) and Subversion • Migration: Contains options related to migrating third-party databases to an Oracle database • Tools: Invokes SQL Developer tools such as SQL*Plus. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Menus . Copyright © 2010.Page 7 Oracle University and InfoTech (Pvt.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using SQL Developer Chapter 17 . define TNS_ADMIN as a system environment variable. • You can export connections to an Extensible Markup Language (XML) file. You can export connections to an XML file that you can reuse later.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Using SQL Developer Chapter 17 . Note: On Windows. You can create and test connections for multiple databases and for multiple schemas. By default. You can use network service names defined in the tnsnames. or imported. Using the Connection dialog menu you can select Create Local connections which will create a connection for every open account on the local database. but it can also be in the directory specified by the TNS_ADMIN environment variable or registry value.Creating a Database Connection • • You must have at least one database connection to use SQL Developer. Each additional database connection created is listed in the Connections Navigator hierarchy.ora file exists but its connections are not being used by SQL Developer. To use SQL Developer. you must have at least one database connection. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating a Database Connection . Oracle and/or its affiliates. You can create additional connections as different users to the same database or to connect to the different databases.ora file to specify service names for you connections.Page 8 Oracle University and InfoTech (Pvt. • Creating a Database Connection A connection is a SQL Developer object that specifies the necessary information for connecting to a specific database as a specific user of that database. which may be existing. created. the tnsnames.ora file on your system. You can create and test connections for multiple: – Databases – Schemas • SQL Developer automatically imports any connections defined in the tnsnames.ora file is located in the $ORACLE_HOME/network/admin directory. if the tnsnames. Copyright © 2010. Port is already set to 1521. right-click Connections and select New Connection. a) From the Role drop-down list. enter the connection name. c) Click Test to ensure that the connection has been set correctly. - TNS: You can select any one of the database aliases imported from the tnsnames.Creating a Database Connection 2 1 3 Creating a Database Connection (continued) To create a database connection. perform the following steps: 1. In the New/Select Database Connection window. Enter the username and password of the schema that you want to connect to. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating a Database Connection . Or you can also choose to enter the Service name directly if you use a remote database connection. All rights reserved.Page 9 Oracle University and InfoTech (Pvt. 2. (You choose SYSDBA for the sys user or any user with database administrator privileges. enter the host name and SID for the database you want to connect to.) b) You can select the connection type as: - Basic: In this type. - LDAP: You can look up database services in Oracle Internet Directory which is a component of Oracle Identity Management. - Advanced: You can define a custom JDBC URL to connect to the database.ora file.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. Oracle and/or its affiliates. Using SQL Developer Chapter 17 . On the Connections tabbed page. you can select either default or SYSDBA. and view metadata and data. So. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED d) Click Connect . the password is saved to an XML file. these connections are read-only connections that enable you to browse objects and data in that data source. dependencies. Sybase Adaptive Server. Microsoft SQL Server.Page 10 Oracle University and InfoTech (Pvt. and so on.If you select the Save Password check box. All rights reserved. Using SQL Developer Chapter 17 . Oracle and/or its affiliates. You can expand the connection to view the database objects and view object definitions—for example. However. Microsoft Access. and IBM DB2. details. Note: From the same New/Select Database Connection window.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Copyright © 2010. such as MySQL. The connection gets added in the Connections Navigator. statistics. 3. You can also connect to schemas for selected third-party (non-Oracle) databases. you are not prompted for the password. after you close the SQL Developer connection and open it again. Using the Data tab.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Indexes. If you want to see the definition of the EMPLOYEES table as shown in the slide. triggers. Using SQL Developer Chapter 17 . grants. For example. Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Browsing Database Objects . Expand the Connections node in the Connections Navigator. if you select a table in the Navigator. Packages. update data. you can use the Connections Navigator to browse through many objects in a database schema including Tables. By default. Expand Tables. All rights reserved. statistics. and Types. and commit these changes to the database.Page 11 Oracle University and InfoTech (Pvt. Triggers. perform the following steps: 1. Click EMPLOYEES. 3. you can view the table data and also enter new rows. the details about columns. You can see the definition of the objects broken into tabs of information that is pulled out of the data dictionary. the Columns tab is selected. Procedures. and so on are displayed on an easy-to-read tabbed page. Views.Browsing Database Objects Use the Connections Navigator to: • Browse through many objects in a database schema • Review the definitions of objects at a glance Browsing Database Objects After you create a database connection. It shows the column description of the table. constraints. 2. Oracle and/or its affiliates. Displaying the Table Structure Use the DESCRIBE command to display the structure of a table: Displaying the Table Structure In SQL Developer. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Displaying the Table Structure .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 12 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Using SQL Developer Chapter 17 . The result of the command is a display of column names and data types as well as an indication if a column must contain data. you can also display the structure of a table using the DESCRIBE command. Copyright © 2010. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. double-click a file name to display its contents in the SQL worksheet area. • To view the files navigator. All rights reserved. click the Files tab. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Browsing Files . or select View > Files.Page 13 Oracle University and InfoTech (Pvt. • To view the contents of a file.Browsing Files Use the File Navigator to explore the file system and open system files. Oracle and/or its affiliates. Using SQL Developer Chapter 17 . Copyright © 2010. Browsing Database Objects You can use the File Navigator to browse and open system files. View the data definition language (DDL) for adjustments such as creating a new object or editing an existing schema object. An Export DDL option is available if you want to create the full DDL for one or more objects in the schema. The slide shows how to create a table using the context menu. Alternatively. Oracle and/or its affiliates. As new objects are created or existing objects are edited.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. you can create objects using the context menus. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating a Schema Object . Using SQL Developer Chapter 17 . Copyright © 2010. right-click Tables and select New Table. All rights reserved. the DDL for those adjustments is available for review. each reflecting a logical grouping of properties for that type of object.Page 14 Oracle University and InfoTech (Pvt. When created. To open a dialog box for creating a new table. The dialog boxes to create and edit database objects have multiple tabs.Creating a Schema Object • SQL Developer supports the creation of any schema object by: – Executing a SQL statement in SQL Worksheet – Using the context menu • • Edit the objects by using an edit dialog box or one of the many context-sensitive menus. Creating a Schema Object SQL Developer supports the creation of any schema object by executing a SQL statement in SQL Worksheet. you can edit the objects using an edit dialog box or one of the many context-sensitive menus. perform the following steps: 1. Copyright © 2010. Oracle and/or its affiliates. All rights reserved.Page 15 Oracle University and InfoTech (Pvt. if you do not select the Advanced check box. 3. in which you can specify an extended set of features while you create the table. Select New TABLE. the Create Table dialog box changes to one with multiple options. Sometimes. right-click the table in the Connections Navigator and select Edit.Creating a New Table: Example Creating a New Table: Example In the Create Table dialog box. right-click Tables. If you select the Advanced check box. 5. Although it is not required. In the Connections Navigator. The example in the slide shows how to create the DEPENDENTS table by selecting the Advanced check box. 2. 4. Specify column information. In the Create Table dialog box. you can create a table quickly by specifying columns and some frequently used features. you may want to edit the table that you have created. Using SQL Developer Chapter 17 . to do so. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating a New Table: Example . you should also specify a primary key by using the Primary Key tab in the dialog box.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. To create a new table. select Advanced. Click OK. • Click the Open SQL Worksheet icon. a SQL Worksheet window for that connection automatically opens. SQL*Plus statements that are not supported by the SQL Worksheet are ignored and not passed to the database. Using SQL Developer Chapter 17 . PL/SQL. PL/SQL. or Using the SQL Worksheet When you connect to a database. The SQL Worksheet supports SQL*Plus statements to a certain extent. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using the SQL Worksheet . You can use the SQL Worksheet to enter and execute SQL.Using the SQL Worksheet • • Use the SQL Worksheet to enter and execute SQL. such as: • Creating a table • Inserting data • Creating and editing a trigger • Selecting data from a table • Saving the selected data to a file You can display a SQL Worksheet by using one of the following: • Select Tools > SQL Worksheet. and SQL *Plus statements.Page 16 Oracle University and InfoTech (Pvt. Oracle and/or its affiliates. Copyright © 2010. You can specify actions that can be processed by the database connection associated with the worksheet.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. Specify any actions that can be processed by the database connection associated with the worksheet. Select SQL Worksheet from the Tools menu. Click the Open SQL Worksheet icon. and SQL*Plus statements. and viewing the history of SQL statements that you have executed.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. running a script. 3. You can use substitution variables in the SQL statements. Rollback: Discards any changes to the database. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using the SQL Worksheet . Execute Statement: Executes the statement where the cursor is located in the Enter SQL Statement box. Run Script: Executes all statements in the Enter SQL Statement box by using the Script Runner. Unshared SQL Worksheet: Create a unshared SQL Worksheet Copyright © 2010. You can use the SQL Worksheet toolbar that contains icons to perform the following tasks: 1. but not bind variables. 2. Oracle and/or its affiliates. without writing them to the database. Using SQL Developer Chapter 17 . All rights reserved. but not substitution variables. You can use bind variables in the SQL statements. and ends the transaction 7. Autotrace: Generates trace information for the statement 4.Page 17 Oracle University and InfoTech (Pvt.Using the SQL Worksheet 2 1 4 3 6 5 10 8 7 9 Using the SQL Worksheet (continued) You may want to use the shortcut keys or icons to perform certain tasks such as executing a SQL statement. Execute Explain Plan: Generates the execution plan. which you can see by clicking the Explain tab 5. Commit: Writes any changes to the database and ends the transaction 6. Change Case: Step through: To Uppercase. Oracle and/or its affiliates. and Lower Case. Clear: Erases the statement or statements in the Enter SQL Statement box 10. SQL History: Displays a dialog box with information about SQL statements that you have executed Oracle University and InfoTech (Pvt. Using SQL Developer Chapter 17 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 8. and Initial Capitalization Copyright © 2010.Page 18 . All rights reserved.9.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. The SQL Worksheet currently supports a number of SQL*Plus commands. Using the SQL Worksheet (continued) When you connect to a database. SQL*Plus commands used in the SQL Developer have to be interpreted by the SQL Worksheet before being passed to the database. All SQL and PL/SQL commands are supported as they are passed directly from the SQL Worksheet to the Oracle database. and SQL*Plus statements.Page 19 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using the SQL Worksheet . You can use the SQL Worksheet to enter and execute SQL.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. • Click the Open SQL Worksheet icon. Through the SQL Worksheet. All rights reserved. PL/SQL. PL/SQL.Using the SQL Worksheet • • Use the SQL Worksheet to enter and execute SQL. and SQL*Plus statements. a SQL Worksheet window for that connection automatically opens. Specify any actions that can be processed by the database connection associated with the worksheet. you can execute SQL statements and some of the SQL*Plus commands. You can display a SQL Worksheet by using any of the following two options: • Select Tools > SQL Worksheet. Copyright © 2010. Commands not supported by the SQL Worksheet are ignored and are not sent to the Oracle database. Using SQL Developer Chapter 17 . Oracle and/or its affiliates. Results are shown here. Enter SQL statements. Ctrl+Enter F5 Executing SQL Statements The example in the slide shows the difference in output for the same query when the Run Statement command (Ctrl+Enter key) is used versus the output when Run Script (F5 key) is used.Executing SQL Statements Use the Enter SQL Statement box to enter single or multiple SQL statements.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved.Page 20 Oracle University and InfoTech (Pvt. Using SQL Developer Chapter 17 . COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Executing SQL Statements . Copyright © 2010. Oracle and/or its affiliates. Saving SQL Scripts You can save your SQL statements from the SQL Worksheet into a text file. All rights reserved.Saving SQL Scripts 1 3 Click the Save icon to save your SQL statement to a file. 2 Identify a location. Script Pathing You can select a default path to look for scripts and to save scripts. The contents of the saved file are visible and editable in your SQL Worksheet window. 3. Copyright © 2010. Using SQL Developer Chapter 17 . and click Save. You can have multiple files open at the same time. Each file displays as a tabbed page. Click Save. After you save the contents to a file. 2. Click the Save icon or use the File > Save menu item. enter a file name. follow these steps: 1. enter a file name and the location where you want the file saved. Oracle and/or its affiliates. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Saving SQL Scripts .Page 21 Oracle University and InfoTech (Pvt. the Enter SQL Statement window displays a tabbed page of your file contents. To save the contents of the Enter SQL Statement box. Under Tools > Preferences > Database > Worksheet Parameters. enter a value in the “Select default path to look for scripts” field. In the Windows Save dialog box.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. ) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. All rights reserved. 3. 3 To run the code. Select a connection from the connection drop-down list. Select File > Open. a connection dialog box will appear. 4. In the files navigator. 2. click the Run Script (F5) icon on the SQL Worksheet toolbar. In the Open dialog box. select (or navigate to) the script file that you want to open. select (or navigate to) the script file that you want to open. 2.Page 22 Oracle University and InfoTech (Pvt. perform the following: 1. 4. click the Run Script (F5) icon on the SQL Worksheet toolbar. Oracle and/or its affiliates. The Open dialog box is displayed. Alternatively. Copyright © 2010. 2.Executing Saved Script Files: Method 1 1. Select the connection you want to use for the script execution. Double-click to open. 5. To run the code. The code of the script file is displayed in the SQL Worksheet area. 1 Use the Files tab to locate the script file that you want to open. To run the code. The code of the script file is displayed in the SQL Worksheet area. 3. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Executing Saved Script Files: Method 1 . you can also: 1. Select the connection you want to use for the script execution. Click Open. Double-click the script to display the code in the SQL Worksheet. Using SQL Developer Chapter 17 . If you have not selected a connection from the connection drop-down list. click either: • Run Script (F5). or • Run Statement (Ctrl +Enter) 2 Select a connection from the drop-down list. Select a connection from the connection drop-down list. Executing Saved Script Files: Method 1 To open a script file and display the code in the SQL Worksheet area. If you have not selected a connection from the connection drop-down list. a connection dialog box will appear. in the Enter SQL Statement window.Executing Saved Script Files: Method 2 Use the @ command followed by the location and name of the file you want to execute. Copyright © 2010. followed by the location. Click the Run Script icon. and click the Run Script icon.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. 2. The output from the script is displayed on the Script Output tabbed page. Use the @ command. and name of the file you want to run. Oracle and/or its affiliates. perform the following: 1. Executing Saved Script Files: Method 2 To run a saved SQL script.Page 23 Oracle University and InfoTech (Pvt. All rights reserved. Using SQL Developer Chapter 17 . You can also save the script output by clicking the Save icon on the Script Output tabbed page. The Windows Save dialog box appears and you can identify a name and location for your file. The results from running the file are displayed on the Script Output tabbed page. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Executing Saved Script Files: Method 2 . In the example in the slide. and line separation of the SQL code. Using SQL Developer Chapter 17 . After formatting. right-click in the statement area and select Format SQL.Page 24 Oracle University and InfoTech (Pvt. capitalization.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the SQL code is beautified with the keywords capitalized and the statement properly indented. Copyright © 2010.Formatting the SQL Code Before formatting After formatting Formatting the SQL Code You may want to beautify the indentation. To format the SQL code. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Formatting the SQL Code . spacing. All rights reserved. Oracle and/or its affiliates. before formatting. the SQL code has the keywords not capitalized and the statement not properly indented. SQL Developer has a feature for formatting SQL code. Copyright © 2010. To display Snippets. From the drop-down list. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Snippets . You can use the drop-down list to select a group. Snippets are code fragments such as SQL functions. All rights reserved. and miscellaneous PL/SQL programming techniques. Using Snippets You may want to use certain code fragments when you use the SQL Worksheet or create or edit a PL/SQL function or procedure. SQL Developer has the feature called Snippets. select View > Snippets. Using SQL Developer Chapter 17 . Optimizer hints.Page 25 Oracle University and InfoTech (Pvt. The Snippets window is displayed at the right side. you can select the functions category that you want.Using Snippets Snippets are code fragments that may be just syntax or examples. You can drag snippets into the Editor window.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Oracle and/or its affiliates. Then the CONCAT function syntax is edited and the rest of the statement is added as in the following: SELECT CONCAT(first_name.Page 26 Oracle University and InfoTech (Pvt. place the cursor over the function name. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using Snippets: Example .Using Snippets: Example Inserting a snippet Editing the snippet Using Snippets: Example To insert a Snippet into your code in a SQL Worksheet or in a PL/SQL function or procedure. Copyright © 2010. All rights reserved. The example in the slide shows that CONCAT(char1.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Then you can edit the syntax so that the SQL function is valid in the current context. drag the snippet from the Snippets window into the desired place in your code. Using SQL Developer Chapter 17 . char2)is dragged from the Character Functions group in the Snippets window. Oracle and/or its affiliates. last_name) FROM employees. To see a brief description of a SQL function in a tool tip. to start running or debugging from the beginning of the function or procedure. • Garbage Collection removes invalid objects from the cache in favor of more frequently accessed and more valid objects. • Step to End of Method goes to the last statement of the current method. Copyright © 2010. Oracle and/or its affiliates. Use the Debug menu options to set breakpoints.Page 27 Oracle University and InfoTech (Pvt. • Step Over bypasses the next method and goes to the next statement after the method. you can perform the following debugging tasks: • Find Execution Point goes to the next execution point. Using the Debug menu options. thus allowing you to resume execution. • Step Into goes to the first statement in the next method. You cannot resume execution from this point. These options are also available as icons on the debugging toolbar. instead. • Terminate halts and exits the execution. Using SQL Developer Chapter 17 . click the Run or Debug icon on the Source tab toolbar.Debugging Procedures and Functions • • • Use SQL Developer to debug PL/SQL functions and procedures. All rights reserved. and to perform the step into and step over tasks. • Pause halts execution but does not exit. Debugging Procedures and Functions In SQL Developer. you can debug PL/SQL procedures and functions.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Use the “Compile for Debug” option to perform a PL/SQL compilation so that the procedure can be debugged. • Resume continues execution. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Debugging Procedures and Functions . • Step Out leaves the current method and goes to the next statement. Oracle and/or its affiliates. click the Reports tab at the left side of the window. These reports can be grouped into the following categories: • About Your Database reports • Database Administration reports • Table reports • PL/SQL reports • Security reports • XML reports • Jobs reports • Streams reports • All Objects reports • Data Dictionary reports • User-Defined reports To display reports.Page 28 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Database Reporting .Database Reporting SQL Developer provides a number of predefined reports about the database and its objects. Database Reporting SQL Developer provides many reports about the database and its objects. Individual reports are displayed in tabbed panes at the right side of the window. and for each report.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. Using SQL Developer Chapter 17 . All rights reserved. For reports Copyright © 2010. you can select (using a drop-down list) the database connection for which to display the report. Oracle and/or its affiliates. You can also create your own user-defined reports. and the rows are usually ordered by Owner. Copyright © 2010. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED about objects.Page 29 . Using SQL Developer Chapter 17 .) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. the objects shown are only those visible to the database user associated with the selected database connection.Oracle University and InfoTech (Pvt. Then click Apply. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating a User-Defined Report .Page 30 Oracle University and InfoTech (Pvt. right-click the User Defined Reports node or any folder name under that node and select Add Folder. Oracle and/or its affiliates. To create a user-defined report. is stored in a file named UserReports. Organize reports in folders. Creating a User-Defined Report User-defined reports are reports created by SQL Developer users.xml under the directory for user-specific information. Copyright © 2010. To create a folder for user-defined reports. and select Add Report. An optional description is provided indicating that the report contains details of employees with salary >= 10000.Creating a User-Defined Report Create and save user-defined reports for repeated use. the report name is specified as emp_sal. You can organize user-defined reports in folders. Using SQL Developer Chapter 17 . Right-click the User Defined Reports node under Reports. specify the report name and the SQL query to retrieve information for the report. 2. and you can create a hierarchy of folders and subfolders. You can also include an optional tool tip to be displayed when the cursor stays briefly over the report name in the Reports navigator display. Information about user-defined reports.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In the example in the slide. including any folders for these reports. All rights reserved. The complete SQL statement for retrieving the information to be displayed in the user-defined report is specified in the SQL box. In the Create Report dialog box. perform the following steps: 1. available to you. select External Tools. C. 2. SQL Developer allows you to add shortcut icons to some of the frequently used tools such as Notepad.External Tools Shortcuts to frequently used tools 1 2 Search Engines and External Tools To enhance productivity of the SQL developers. perform the following: A.Page 31 Oracle University and InfoTech (Pvt. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED External Tools . Using SQL Developer Chapter 17 . From the Tools menu. B. Oracle and/or its affiliates. Click Delete to remove any tool from the list. In the External Tools dialog box. perform the following: 1. You can add external tools to the existing list or even delete shortcuts to tools that you do not use frequently. To do so. Copyright © 2010. All rights reserved. Click New to invoke the wizard to add new tools.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. and Dreamweaver. Microsoft Word. Click Edit to invoke the wizard to modify the availability and parameters of the selected tool. select Preferences.Page 32 Oracle University and InfoTech (Pvt. Setting Preferences You can customize many aspects of the SQL Developer interface and environment by modifying SQL Developer preferences according to your preferences and needs. and so on Copyright © 2010. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Setting Preferences . Oracle and/or its affiliates.Setting Preferences • • Customize the SQL Developer interface and environment. then Preferences.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. In the Tools menu. select Tools. To modify SQL Developer preferences. Using SQL Developer Chapter 17 . The preferences are grouped into the following categories: • Environment • Accelerators (keyboard shortcuts) • Code Editors • Database • Debugger • Documentation • Extensions • File Types • Migration • PL/SQL Compilers • PL/SQL Debugger. All rights reserved. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Resetting the SQL Developer Layout . All rights reserved. if the Connections Navigator disappears or if you cannot dock the Log window in its original place. Copyright © 2010. Restart SQL Developer.xml and delete it. 3. Using SQL Developer Chapter 17 .Page 33 Oracle University and InfoTech (Pvt. Exit from SQL Developer.Resetting the SQL Developer Layout Resetting the SQL Developer Layout While working with SQL Developer.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.xml. 2. 4. Oracle and/or its affiliates. Open a terminal window and use the locate command to find the location of windowinglayout. Go to the directory which has windowinglayout. perform the following steps to fix the problem: 1. you should have learned how to use SQL Developer to do the following: • Browse. You can use SQL Worksheet to run SQL statements and scripts. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Summary .Page 34 Oracle University and InfoTech (Pvt. Using SQL Developer Chapter 17 . All rights reserved. create. create. Oracle and/or its affiliates. Using SQL Developer. and edit database objects • Execute SQL statements and scripts in SQL Worksheet • Create and save custom reports Summary SQL Developer is a free graphical tool to simplify database development tasks.Summary In this appendix. and edit database objects. you can browse. SQL Developer enables you to create and save your own special set of reports for repeated use. Copyright © 2010.) Ltd use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY.
Copyright © 2024 DOKUMEN.SITE Inc.