D52163GC10 sg1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 410

Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
o n -tr
Oracle Database n 11g: SQL Tuning
a
s eฺ
Workshop h a
e s ) u id

ir ent G
ฺ m
d gp SVolume
tud
I • Student Guide

b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

D52163GC10
Edition 1.0
June 2008
D54947
Author Copyright © 2008, Oracle. All rights reserved.

Jean-François Verrier Disclaimer

This document contains proprietary information and is protected by copyright and


Technical Contributors 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
and Reviewers any way. Except where your use constitutes "fair use" under copyright law, you may
Muriel Fry (Special thanks) not use, share, download, upload, copy, print, display, perform, reproduce, publish,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

license, post, transmit, or distribute this document in whole or in part without the
Joel Goodman express authorization of Oracle.
Harald van Breederode
The information contained in this document is subject to change without notice. If you
Jörn Bartels 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
Pekka Siltala warranted to be error-free.
Bernard Soleillant
Restricted Rights Notice

James Spiller 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
n s e
Clay Fuller applicable:
ice
Ira Singer
b l el
Christopher Andrews
U.S. GOVERNMENT RIGHTS

fe r a
The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or
Magnus Isaksson n s
disclose these training materials are restricted by the terms of the applicable Oracle
a
Sean Kim n - tr
license agreement and/or the applicable U.S. Government contract.

o
an
Trademark Notice
Trevor Bauwen
Yash Jain
s
a deฺ
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other
h
)
s Gui
names may be trademarks of their respective owners.
e
r ฺ
Editors
p ฺ mi dent
Amitha Narayan d g Stu
Raj Kumar
b i a@ this
i a (ja use
b
Graphic Designer
A to
ni o
Priya Saxena
t o
e An
Jos
Publishers
Jothi Lakshmi
Veena Narasimhan
Contents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1 Exploring the Oracle Database Architecture


Objectives 1-2
Oracle Database Server Architecture: Overview 1-3
Connecting to the Database Instance 1-4
Oracle Database Memory Structures: Overview 1-6
Database Buffer Cache 1-7 n s e
i ce
Redo Log Buffer 1-8
b l el
Shared Pool 1-9
fe r a
Processing a DML Statement: Example 1-10
a n s
COMMIT Processing: Example 1-11
n - tr
o
Large Pool 1-12
s an
Java Pool and Streams Pool 1-13
) h a deฺ
Program Global Area (PGA) 1-14
r ฺ e s Gui
p ฺ mi dent
Background Process Roles 1-15
d g Stu
Automatic Shared Memory Management 1-16
i a@ this
Automated SQL Execution Memory Management 1-17
b
i a (ja use
Automatic Memory Management 1-18
b to
Database Storage Architecture 1-19
A
o n io
Logical and Physical Database Structures 1-21
nt
e A Segments, Extents, and Blocks 1-23

J o s SYSTEM and SYSAUX Tablespaces 1-24


Summary 1-25
Practice 1: Overview 1-26

2 Introduction to SQL Tuning


Objectives 2-2
Reasons for Inefficient SQL Performance 2-3
Inefficient SQL: Examples 2-4
Performance Monitoring Solutions 2-6
Monitoring and Tuning Tools: Overview 2-8
EM Performance Pages for Reactive Tuning 2-9
Tuning Tools: Overview 2-10
SQL Tuning Tasks: Overview 2-11
CPU and Wait Time Tuning Dimensions 2-12
Scalability with Application Design, Implementation, and Configuration 2-13

iii
Common Mistakes on Customer Systems 2-14
Proactive Tuning Methodology 2-16
Simplicity in Application Design 2-17
Data Modeling 2-18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Table Design 2-19


Index Design 2-20
Using Views 2-21
SQL Execution Efficiency 2-22
Writing SQL to Share Cursors 2-24
Performance Checklist 2-25
Summary 2-26
n s e
Practice 2: Overview 2-27
i ce
b l el
3 Introduction to the Optimizer
fer a
Objectives 3-2
a n s
Structured Query Language 3-3 n -tr
o
SQL Statement Representation 3-4
s an
SQL Statement Implementation 3-5
) h a deฺ
SQL Statement Processing: Overview 3-6
r ฺ e s Gui

SQL Statement Processing: Steps 3-7
p mi dent
Step 1: Create a Cursor 3-8 d g Stu
i a@ this
Step 2: Parse the Statement 3-9
b
i a (ja use
Steps 3 and 4: Describe and Define 3-10
Ab to
Steps 5 and 6: Bind and Parallelize 3-11
n io
Steps 7 Through 9 3-12
o
nt
e A SQL Statement Processing PL/SQL: Example 3-13

J o s SQL Statement Parsing: Overview 3-14


Why Do You Need an Optimizer? 3-16
Optimization During Hard Parse Operation 3-18
Transformer: OR Expansion Example 3-19
Transformer: Subquery Unnesting Example 3-20
Transformer: View Merging Example 3-21
Transformer: Predicate Pushing Example 3-22
Transformer: Transitivity Example 3-23
Cost-Based Optimizer 3-24
Estimator: Selectivity 3-25
Estimator: Cardinality 3-26
Estimator: Cost 3-27
Plan Generator 3-28
Controlling the Behavior of the Optimizer 3-29
Optimizer Features and Oracle Database Releases 3-34

iv
Summary 3-35
Practice 3: Overview 3-36

4 Optimizer Operators
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Objectives 4-2
Row Source Operations 4-3
Main Structures and Access Paths 4-4
Full Table Scan 4-5
Full Table Scans: Use Cases 4-6
ROWID Scan 4-7
Sample Table Scans 4-8
n s e
Indexes: Overview 4-10
i ce
Normal B*-tree Indexes 4-12
b l el
Index Scans 4-13
fer a
Index Unique Scan 4-14
a n s
Index Range Scan 4-15 n -tr
o
Index Range Scan: Descending 4-16
s an
Descending Index Range Scan 4-17
) h a deฺ
ฺ e s Gui
Index Range Scan: Function-Based 4-18
r
Index Full Scan 4-19
p ฺ mi dent
d g Stu
Index Fast Full Scan 4-20
i a@ this
Index Skip Scan 4-21
b
i a (ja use
Index Skip Scan: Example 4-23
Ab to
Index Join Scan 4-24
o n io
The AND-EQUAL Operation 4-25
nt
e A B*-tree Indexes and Nulls 4-26
J o s Using Indexes: Considering Nullable Columns 4-27
Index-Organized Tables 4-28
Index-Organized Table Scans 4-29
Bitmap Indexes 4-30
Bitmap Index Access: Examples 4-31
Combining Bitmap Indexes: Examples 4-32
Combining Bitmap Index Access Paths 4-33
Bitmap Operations 4-34
Bitmap Join Index 4-35
Composite Indexes 4-36
Invisible Index: Overview 4-37
Invisible Indexes: Examples 4-38
Guidelines for Managing Indexes 4-39
Investigating Index Usage 4-41
Practice 4: Overview 4-43

v
Clusters 4-44
When Are Clusters Useful? 4-45
Cluster Access Path: Examples 4-47
Sorting Operators 4-48
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Buffer Sort Operator 4-49


Inlist Iterator 4-50
View Operator 4-51
Count Stop Key Operator 4-52
Min/Max and First Row Operators 4-53
Join Methods 4-54
Nested Loops Join 4-55
n s e
Nested Loops Join: Prefetching 4-56
i ce
Nested Loops Join: 11g Implementation 4-57
b l el
Sort Merge Join 4-58
fer a
Hash Join 4-59
a n s
Cartesian Join 4-60 n -tr
o
Join Types 4-61
s an
Equijoins and Nonequijoins 4-62
) h a deฺ
Outer Joins 4-63
r ฺ e s Gui
Semijoins 4-64
p ฺ mi dent
Antijoins 4-65 d g Stu
i a@ this
Other N-Array Operations 4-66
b
i a (ja use
Filter Operations 4-67
Ab to
Concatenation Operation 4-68
n io
UNION [ALL], INTERSECT, MINUS 4-69
o
nt
e A Result Cache Operator 4-70
J o s Summary 4-71
Practice 4: Overview 4-72

5 Interpreting Execution Plans


Objectives 5-2
What Is an Execution Plan? 5-3
Where to Find Execution Plans? 5-4
Viewing Execution Plans 5-6
The EXPLAIN PLAN Command 5-7
The EXPLAIN PLAN Command: Example 5-9
PLAN_TABLE 5-10
Displaying from PLAN_TABLE: Typical 5-11
Displaying from PLAN_TABLE: ALL 5-12
Displaying from PLAN_TABLE: ADVANCED 5-14

vi
AUTOTRACE 5-15
The AUTOTRACE Syntax 5-16
AUTOTRACE: Examples 5-17
AUTOTRACE: Statistics 5-18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Using the V$SQL_PLAN View 5-20


The V$SQL_PLAN Columns 5-21
The V$SQL_PLAN_STATISTICS View 5-22
Links Between Important Dynamic Performance Views 5-23
Querying V$SQL_PLAN 5-25
Automatic Workload Repository (AWR) 5-27
Managing AWR with PL/SQL 5-29
n s e
Important AWR Views 5-31 i ce
Querying the AWR 5-32 b l el
Generating SQL Reports from AWR Data 5-34 fer a
a n s
SQL Monitoring: Overview 5-35
n - tr
SQL Monitoring Report: Example 5-37 o
Interpreting an Execution Plan 5-40 s an
) h a deฺ
r ฺ e s Gui
Execution Plan Interpretation: Example 1 5-42

p ฺ mi dent
Execution Plan Interpretation: Example 2 5-45
Execution Plan Interpretation: Example 3 5-47
d g Stu
b i a@ this
Reading More Complex Execution Plans 5-48

i a (ja use
Reviewing the Execution Plan 5-49

Ab to
Looking Beyond Execution Plans 5-50

o n io
Summary 5-51
nt Practice 5: Overview 5-52
e A
J o s
6 Case Study: Star Transformation
Objectives 6-2
The Star Schema Model 6-3
The Snowflake Schema Model 6-4
Star Query: Example 6-5
Execution Plan Without Star Transformation 6-6
Star Transformation 6-7
Star Transformation: Considerations 6-8
Star Transformation: Rewrite Example 6-9
Retrieving Fact Rows from One Dimension 6-10
Retrieving Fact Rows from All Dimensions 6-11
Joining the Intermediate Result Set with Dimensions 6-12
Star Transformation Plan: Example 1 6-13
Star Transformation: Further Optimization 6-14

vii
Using Bitmap Join Indexes 6-15
Star Transformation Plan: Example 2 6-16
Star Transformation Hints 6-17
Bitmap Join Indexes: Join Model 1 6-18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Bitmap Join Indexes: Join Model 2 6-19


Bitmap Join Indexes: Join Model 3 6-20
Bitmap Join Indexes: Join Model 4 6-21
Summary 6-22
Practice 6: Overview 6-23

7 Optimizer Statistics
n s e
Objectives 7-2
i ce
Optimizer Statistics 7-3
b l el
Types of Optimizer Statistics 7-4
fer a
Table Statistics (DBA_TAB_STATISTICS) 7-5
a n s
Index Statistics (DBA_IND_STATISTICS) 7-6 n -tr
o
Index Clustering Factor 7-8
s an
h
Column Statistics (DBA_TAB_COL_STATISTICS) 7-10
) a deฺ
Histograms 7-11 r ฺ e s Gui
Frequency Histograms 7-12 p ฺ mi dent
d g Stu
Viewing Frequency Histograms 7-13
b i a@ this
Height-Balanced Histograms 7-14
a (ja use
Viewing Height-Balanced Histograms 7-15
i
Ab to
Histogram Considerations 7-16
o n io
t
Multicolumn Statistics: Overview 7-17
n
e A Expression Statistics: Overview 7-19
J o s Gathering System Statistics 7-20
Gathering System Statistics: Example 7-22
Mechanisms for Gathering Statistics 7-24
Statistic Preferences: Overview 7-25
When to Gather Statistics Manually 7-27
Manual Statistics Gathering 7-28
Manual Statistics Collection: Factors 7-29
Managing Statistics Collection: Example 7-30
Optimizer Dynamic Sampling: Overview 7-31
Optimizer Dynamic Sampling at Work 7-32
OPTIMIZER_DYNAMIC_SAMPLING 7-33
Locking Statistics 7-34
Summary 7-35
Practice 7: Overview 7-36

viii
8 Using Bind Variables
Objectives 8-2
Cursor Sharing and Different Literal Values 8-3
Cursor Sharing and Bind Variables 8-4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Bind Variables in SQL*Plus 8-5


Bind Variables in Enterprise Manager 8-6
Bind Variable Peeking 8-7
Cursor Sharing Enhancements 8-9
The CURSOR_SHARING Parameter 8-11
Forcing Cursor Sharing: Example 8-12
Adaptive Cursor Sharing: Overview 8-13
n s e
Adaptive Cursor Sharing: Architecture 8-14
i ce
Adaptive Cursor Sharing: Views 8-16
b l el
Adaptive Cursor Sharing: Example 8-18
fer a
Interacting with Adaptive Cursor Sharing 8-19
a n s
Summary 8-20 n -tr
o
Practice 8: Overview 8-21
s an
) h a deฺ
9 Using Optimizer Hints
r ฺ e s Gui
Objectives 9-2
p ฺ mi dent
d
Optimizer Hints: Overview g9-3 Stu
i
Types of Hints b9-4 a@ this
Specifying
a ja 9-5use
(Hints
i
bfor Hints to9-6
o A
Rules

t o ni Hint Recommendations 9-7


An Optimizer Hint Syntax: Example 9-8
e
Jos Hint Categories 9-9
Optimization Goals and Approaches 9-10
Hints for Access Paths 9-11
The INDEX_COMBINE Hint: Example 9-15
Hints for Query Transformation 9-17
Hints for Join Orders 9-20
Hints for Join Operations 9-21
Additional Hints 9-23
Hints and Views 9-26
Global Table Hints 9-28
Specifying a Query Block in a Hint 9-29
Specifying a Full Set of Hints 9-30
Summary 9-31
Practice 9: Overview 9-32

ix
10 Application Tracing
Objectives 10-2
End-to-End Application Tracing Challenge 10-3
End-to-End Application Tracing 10-4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Location for Diagnostic Traces 10-5


What Is a Service? 10-6
Use Services with Client Applications 10-7
Tracing Services 10-8
Use Enterprise Manager to Trace Services 10-10
Service Tracing: Example 10-11
Session Level Tracing: Example 10-12
n s e
Trace Your Own Session 10-13
i ce
The trcsess Utility 10-14
b l el
Invoking the trcsess Utility 10-15
fer a
The trcsess Utility: Example 10-16 a n s
SQL Trace File Contents 10-17 n -tr
o
SQL Trace File Contents: Example 10-18
s an
Formatting SQL Trace Files: Overview 10-19
) h a deฺ
Invoking the tkprof Utility 10-20 r ฺ e s Gui
tkprof Sorting Options 10-22 p ฺ mi dent
Output of the tkprof Command d g10-24Stu
tkprof Output with a Nob ia@Example
Index: t h is 10-29
tkprof Outputia
(j
with Index:u se 10-30
Example
A b to
Summaryo
ni 10: Overview 10-32
10-31
t o
An
Practice
e
Jos 11 Automating SQL Tuning
Objectives 11-2
Tuning SQL Statements Automatically 11-3
Application Tuning Challenges 11-4
SQL Tuning Advisor: Overview 11-5
Stale or Missing Object Statistics 11-6
SQL Statement Profiling 11-7
Plan Tuning Flow and SQL Profile Creation 11-8
SQL Tuning Loop 11-9
Access Path Analysis 11-10
SQL Structure Analysis 11-11
SQL Tuning Advisor: Usage Model 11-12
Database Control and SQL Tuning Advisor 11-13
Running SQL Tuning Advisor: Example 11-14

x
Implementing Recommendations 11-15
SQL Access Advisor: Overview 11-16
SQL Access Advisor: Usage Model 11-17
Possible Recommendations 11-18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL Access Advisor Session: Initial Options 11-19


SQL Access Advisor: Workload Source 11-21
SQL Access Advisor: Recommendation Options 11-22
SQL Access Advisor: Schedule and Review 11-23
SQL Access Advisor: Results 11-24
SQL Access Advisor: Results and Implementation 11-25
SQL Tuning Loop 11-26
n s e
Automatic SQL Tuning 11-27
i ce
Automatic Tuning Process 11-28
b l el
Automatic SQL Tuning Controls 11-30
fer a
Automatic SQL Tuning Task 11-31
a n s
Configuring Automatic SQL Tuning 11-32 n - tr
o
Automatic SQL Tuning: Result Summary 11-33
s an
h
Automatic SQL Tuning: Result Details 11-34
) a deฺ
e s Gui
Automatic SQL Tuning Result Details: Drilldown 11-35
r ฺ
ฺ mi dent
Automatic SQL Tuning Considerations 11-36
p
Summary 11-37 d g Stu
i a@ this
Practice 11: Overview 11-38
b
i a (ja use
A
Appendix to and Solutions
b A: Practices
ni o
n t o
e A Index

Jos

xi
J
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

o s e
n A
t o n
Aio
b i a
b i
to
(ja use
d p ฺ
a@ this

xii
r ฺ
g Stu
e ) h
mi dent
s
s Gui
an
o
a deฺ
tr n -
a n s fe
r
a b l el
i
ce n s e
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Exploring the Oracle Database Architecture

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• List the major architectural components of the Oracle


Database server
• Explain memory structures
• Describe background processes
• Correlate logical and physical storage structures s e
ce n
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
A
Objectives nio
nto provides an overview of the Oracle Database server architecture. You learn about
ThisAlesson
e
osphysical and logical structures and about the various components.
J

Oracle Database 11g: SQL Tuning Workshop 1 - 2


Oracle Database Server Architecture: Overview

Instance
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SGA

BGP1 BGP2 BGP3 BGPn


n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
Database
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io Server Architecture: Overview A
n
Oracle Database
o
An A nt Database server consists of an Oracle Database and one or more Oracle Database
Oracle
e
Josinstances. An instance consists of memory structures and background processes. Every time an
instance is started, a shared memory area called the System Global Area (SGA) is allocated and the
background processes are started.
The SGA contains data and control information for one Oracle Database instance.
The background processes consolidate functions that would otherwise be handled by multiple Oracle
Database server programs running for each user process. They asynchronously perform input/output
(I/O) and monitor other Oracle Database processes to provide increased parallelism for better
performance and reliability.
The database consists of physical files and logical structures discussed later in this lesson. Because
the physical and logical structures are separate, the physical storage of data can be managed without
affecting access to the logical storage structures.
Note: Oracle Real Application Clusters (Oracle RAC) comprises two or more Oracle Database
instances running on multiple clustered computers that communicate with each other by means of an
interconnect and access the same Oracle Database.

Oracle Database 11g: SQL Tuning Workshop 1 - 3


Connecting to the Database Instance

User
• Connection: Bidirectional network process
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

pathway between a user process


on a client or middle tier and an User
process
Oracle process on the server
Shared
• Session: Representation of Dispatcher Server
Listener
a specific login by a user D000 S000
process
n s e
i ce
b l el
Connection
fe r a
n
aSGAs
SQL> Select …
User
process
Server
process n -tr
Dedicated n
o
s a
User
) h a
Server
i d eฺ Server host

ฺes nt database
Session (Specificirconnected
Gu user)
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Connectingnto
o iothe Database Instance
t connect to an Oracle Database server, they are connected to an Oracle Database instance.
nusers
A
When
e database instance services those users by allocating other memory areas in addition to the SGA,
JosThe
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. Most environments have separate machines for the client
processes. A user process also manages communication with a corresponding server process
through a program interface.
• Oracle Database server creates server processes to handle requests from connected user
processes. 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.
An Oracle Database instance can be configured to vary the number of user processes for each server
process. In a dedicated server configuration, a server process handles requests for a single user
process.
A shared server configuration enables many user processes to share a small number of shared server
processes, minimizing the number of server processes and maximizing the use of available system
resources. One or more dispatcher processes are then used to queue user process requests in the SGA
and dequeue shared server responses.

Oracle Database 11g: SQL Tuning Workshop 1 - 4


Connecting to the Database Instance (continued)
The Oracle Database server runs a listener that is responsible for handling network connections. The
application connects to the listener that creates a dedicated server process or handles the connection
to a dispatcher.
Connections and sessions are closely related to user processes, but are very different in meaning:
• A connection is a communication pathway between a user process and an Oracle Database
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

instance. 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, and communicate through a network).
• A session represents the state of a current database user login to the database instance. For
example, when a user starts SQL*Plus, the user must provide a valid database username and
password, and then a session is established for that user. A session lasts from the time a user
connects until the user disconnects or exits the database application. n s e
e
licusing
Note: Multiple sessions can be created and exist concurrently for a single Oracle databasele user
the same username. For example, a user with the username/password of HR/HR can connect r a b to the
e
same Oracle Database instance several times.
a nsf
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 1 - 5


Oracle Database Memory Structures: Overview

SGA
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Database buffer Redo log Shared pool Large pool


cache buffer

Java Streams
pool pool

n s e
i ce
b l el
Server Server … Background
fer a
process process process
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s … PGA G ui …
Aggregated

p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io Memory Structures: Overview A
t o n
Oracle Database
A
Oracle nDatabase creates and uses memory structures for various purposes. For example, memory
s e
Jo stores the program code that is run, data that is shared among users, and private data areas for each
connected user. Two basic memory structures are associated with an instance:
• System Global Area (SGA): The SGA is shared by all server and background processes. 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, such as Oracle backup and recovery
operations, 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. A PGA is suballocated from the aggregated PGA area.
Note: The Oracle database uses initialization parameters to create and manage memory structures.

Oracle Database 11g: SQL Tuning Workshop 1 - 6


Database Buffer Cache

• Is a part of the SGA


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Holds copies of data blocks that are read from data files
• Is shared by all concurrent processes
SGA

Server
Database
process
buffer
n s e
cache
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ DBWn

r ฺ e s Database G ui writer
Data files
p ฺ mi dent process
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io Cache A
n
Database Buffer
o
TheA nt buffer cache is the portion of the SGA that holds copies of data blocks that are read
database
e data files. All users concurrently connected to the instance share access to the database buffer
Josfrom
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.

Oracle Database 11g: SQL Tuning Workshop 1 - 7


Redo Log Buffer
• Is a circular buffer in the SGA (based on the number of
CPUs)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Contains redo entries that have the information to redo


changes made by operations, such as DML and DDL
SGA

Server
process Redo log
buffer n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ LGWR

e s G i
Loguwriter
r ฺ
Redo log
files p ฺ mi dent process
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io A
n
Redo Log Buffer
o
TheA nt log buffer is a circular buffer in the SGA that holds information about changes made to the
redo
e
Josdatabase. 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.

Oracle Database 11g: SQL Tuning Workshop 1 - 8


Shared Pool

• Is part of the SGA


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Contains: Server
process
– Library cache
— Shared parts of SQL and
PL/SQL statements
SGA
– Data dictionary cache
– Result cache: n s e
i ce
— SQL queries Data
Resultb l el
PL/SQL functions Library
dictionary
cache fe r a
cache

cache a n s
(row cache)
– Control structures
o n -tr
— Locks
s an Control
h a de ฺ structures
s ) u i
m i rฺe nt G Shared pool
d gpฺ Stude
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io A
Shared Pool
o n
TheA nt pool portion of the SGA contains the following main parts:
shared
e
Jos • 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.

Oracle Database 11g: SQL Tuning Workshop 1 - 9


Processing a DML Statement: Example

Database
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

DBWn SGA

Database
2 4
2 buffer cache
Data Server Redo log
files process 3 buffer

Shared pool n s e
i ce
Control
5
1 Library cache b l el
files fer a
User a n s
process n -tr
o
s an
Redo ) h a deฺ
log files r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Processingna DML Statement: Example io A
TheA ntoinvolved in executing a data manipulation language (DML) statement are as follows:
steps
e
Jos 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 rollback 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
rollback buffers are modified. This is called write-ahead logging.
4. The rollback buffers contain values of the data before it is modified. The rollback 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).
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.
Oracle Database 11g: SQL Tuning Workshop 1 - 10
COMMIT Processing: Example

Database
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

DBWn SGA

Database
buffer cache
Data Server Redo log
files process 1 buffer
SGA

Shared pool n s e
i ce
Control
3
Library cache b l el
files fe r a
User a n s
o n -tr process
s an
Redo ) h a deฺ
log files r ฺ e s Gui 2
ฺ m i ent
LGWR

d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
i o A Example
COMMIT Processing:
n
to is issued, the following steps are performed:
nCOMMIT
A
When
e
Jos 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 the 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 the
Oracle Database to perform consistency checks without depending on the date and time of the
operating system.
2. The background LGWR 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, the Oracle Database
can guarantee that the changes are not lost even if there is an instance failure.
3. 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.

Oracle Database 11g: SQL Tuning Workshop 1 - 11


Large Pool

• Provides large memory allocations for:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Session memory for the shared server and Oracle XA


interface
– Parallel execution buffers
– I/O server processes Server
– Oracle Database backup process
and restore operations n s e
i ce
• Optional pool better suited b l el
when using the following: SGA fe ra
a n s
– Parallel execution I/O buffer
o n -tr memory
Free

– Recovery Manager
s an
Response Request
– Shared server h a d e ฺ
s ) u i
queue queue

m i rฺe nt G Large pool



gp Stud e
d
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
A
Large Poolnio
ntoconfigure an optional memory area called the large pool to provide large memory
YouAcan
e
Josallocations 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. This was the
shared pool original design 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.
• Shared server: In a shared server architecture, the session memory for each client process is
included in the shared pool.

Oracle Database 11g: SQL Tuning Workshop 1 - 12


Java Pool and Streams Pool

• Java pool memory is used in server memory for all session-


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
Java pool
r ฺ e s Streams G ui pool
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
ioStreams Pool A
Java Pool and
o n
nt memory is used for all session-specific Java code and data in the JVM. Java pool memory
JavaApool
e
Josis used in different ways, depending on the mode in which Oracle Database runs.
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.

Oracle Database 11g: SQL Tuning Workshop 1 - 13


Program Global Area (PGA)

• PGA is a memory area that contains:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Session information Server


process
– Cursor information
– SQL execution work areas:
— Sort area
— Hash join area
Bitmap merge area
Stack
User Global Area (UGA)
User
n s e
— Space Session Cursor SQL
ice
el
Status Area
Data
Bitmap create area l

a b
• Work area size influences SQL performance. s f er
• Work areas can be automatically or manually - t r an
managed.
n no
s eฺa
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Program Global
o io Area (PGA)
TheA nt of the PGA memory varies, depending on whether or not the instance runs the shared
content
e
Josserver option.
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.

Oracle Database 11g: SQL Tuning Workshop 1 - 14


Background Process Roles
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

RCBG MMON CJQ0 QMNn MMAN

Database
SGA buffer
Redo log
Shared pool
n s e
cache
buffer
i ce
b l el
fer a
a n s
o n -tr
s a n ARCn
CKPT PMON SMON DBWn
) h a deฺLGWR

r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
o
iProcess A
Background
t o n Roles
TheA n
background processes commonly seen in non-RAC, non-ASM environments can include the
s e
Jo 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 recoverer 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
• 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 Database 11g: SQL Tuning Workshop 1 - 15
Automatic Shared Memory Management

SGA_TARGET + STATISTICS_LEVEL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SGA

Shared pool Fixed SGA Large pool

Java Streams Database Redo log


pool pool buffer cache buffer n s e
i ce
b l el
fe r a
a n s
o n -tr
Which size to choose?
s an
) h a deฺ
r ฺ e s Gui
Automatically tuned SGA componentsฺm
i ent
d p
g Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Automatic n i o A Memory Management
Shared
A ntoreleases of the Oracle Database, you did not have exact control over the total size of the
In earlier
e The reason was that memory was allocated by Oracle for the fixed SGA and for other internal
osSGA.
J metadata allocations over and above the total size of the user-specified SGA parameters. In earlier
releases, you had to manually specify the amount of memory to be allocated for the database buffer
cache, shared pool, Java pool, Streams pool, and large pool. It was often a challenge to optimally size
these components. Undersizing often resulted in poor performance and out-of-memory errors,
whereas oversizing wasted memory.
Since Oracle Database 10g, 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.
The system uses an SGA size parameter (SGA_TARGET) that includes all the memory in the SGA,
including all the automatically sized components, manually sized components, and any internal
allocations during startup. ASMM simplifies the configuration of the SGA by enabling you to specify
a total memory amount to be used for all SGA components. The Oracle Database then periodically
redistributes memory between the automatically tuned components, according to workload
requirements.
Note: You must set STATISTICS_LEVEL to TYPICAL or ALL to use ASMM.

Oracle Database 11g: SQL Tuning Workshop 1 - 16


Automated SQL Execution Memory Management
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PGA_AGGREGATE_TARGET

Server
process
Server
process
… Background
process

n s e
i ce
Aggregated
… PGA

b l el
fer a
a n s
n -tr
o
Which size to choose? s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
AutomatednSQL
o io Execution Memory Management
nt provides an automatic mode for allocating memory to working areas in the PGA. You
ThisAfeature
e use the PGA_AGGREGATE_TARGET parameter to specify the total amount of memory that
Joscan
should be allocated to the PGA areas of the instance’s sessions. In automatic mode, working areas
that are used by memory-intensive operators (sorts and hash joins) can be automatically and
dynamically adjusted.
This feature offers several performance and scalability benefits for decision support system (DSS)
workloads and mixed workloads with complex queries. The overall system performance is
maximized, and the available memory is allocated more efficiently among queries to optimize both
throughput and response time. In particular, the savings that are gained from improved use of
memory translate to better throughput at high loads.
Note: In earlier releases of Oracle Database server, you had to manually specify the maximum work
area size for each type of SQL operator, such as sort or hash join. 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, it is
recommended that you leave automatic PGA memory management enabled.

Oracle Database 11g: SQL Tuning Workshop 1 - 17


Automatic Memory Management

• Sizing of each memory component is vital for SQL execution


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

performance.
• It is difficult to manually size each component.
• Automatic memory management automates memory
allocation of each SGA component and aggregated PGA.

n s e
ce
MMAN
SGA memory Aggregated PGA memory
eli
a b l
fer
Streams pool

Buffer cache
Shared pool

s
Large pool
Other SGA

Untunable
n
Java pool

SQL areas
-tr a

Private

Free

PGA
o n
s an
) h a deฺ
r ฺ e s Gui
MEMORY_TARGET
p ฺ mi + STATISTICS_LEVEL
d e nt
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Automatic n
o io Management
Memory
A
As seenntalready, the size of the various memory areas of the instance directly impacts the speed of
e processing. Depending on the database workload, it is difficult to size those components
Josmanually.
SQL

With Automatic Memory Management, the system automatically adapts the size of each memory’s
components to your workload memory needs.
Set your MEMORY_TARGET initialization parameter for the database instance and the MMAN
background process automatically tunes to the target memory size, redistributing memory as needed
between the internal components of the SGA and between the SGA and the aggregated PGAs.
In more details, 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). Statistics and memory advisory data are periodically captured in memory by
MMON. MMAN coordinates the sizing of the memory components according to MMON decisions.
Note: Currently, this mechanism is only implemented on Linux, Solaris, HP-UX, AIX, and
Windows.

Oracle Database 11g: SQL Tuning Workshop 1 - 18


Database Storage Architecture
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Control files Data files Online redo log files

n s e
i ce
b l el
f er a
Parameter file Backup files Archived s
an redo log
- t r
n on files
s a
) h a
i d eฺ
i r ฺes nt Gu
Password file g p ฺm Alert u delog and trace files
d t
i a @ his S
j e t© 2008, Oracle. All rights reserved.
ab Copyright
(
ia to u s
b
n i
Database Storageo A Architecture
TheA ntothat constitute an Oracle database are organized into the following:
files
e
Jos • Control files: Contain data about the database itself (that is, physical database structure information). These files are critical to the database. Without them, you cannot open data files to
access the data in the database.
• Data files: Contain the user or application data of the database, as well as metadata and the data
dictionary
• Online redo log files: Allow for instance recovery of the database. If the database server crashes
and does not lose any data files, the instance can recover the database with the information in
these files.
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, sysoper, and sysasm to connect remotely to the database
and perform administrative tasks
• Backup files: Are used for database recovery. You typically restore a backup file when a media
failure or user error has damaged or deleted the original file.
• Archived redo log files: Contain an ongoing history of the data changes (redo) that are
generated by the instance. Using these files and a backup of the database, you can recover a lost
data file. That is, archive logs enable the recovery of restored data files.

Oracle Database 11g: SQL Tuning Workshop 1 - 19


Database Storage Architecture (continued)
• Trace files: Each server and background process can write to an associated trace file. When an
internal error is detected by a process, the process dumps information about the error to its trace
file. Some of the information written to a trace file is intended for the developer, whereas other
information is for Oracle Support Services.
• Alert log file: These are special trace entries. The alert log of a database is a chronological log
of messages and errors. Each instance has one alert log file. It is recommended that you review
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

this periodically.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 1 - 20


Logical and Physical Database Structures

Logical Physical
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Database

Only 1 with
bigfile
tablespaces
Schema Tablespace Data file
0, 1, or many

n s e
ce
Undo tablespaces
Segment
never have 0

eli
a b l
fe r
a n s
Extent
o n -tr
s an
Oracle data s) h
a deฺ
block irฺe G ui OS block
p ฺ m dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o io
Logical andnPhysical Database Structures
TheA
t
n has logical structures and physical structures.
database
e
JosTablespaces
A database is divided into logical storage units called tablespaces, which group related logical
structures together. For example, tablespaces commonly group all of an application’s objects to
simplify some administrative operations. You may have a tablespace for different applications.
Databases, Tablespaces, and Data Files
The relationship among databases, tablespaces, and data files is illustrated in the slide. Each database
is logically divided into one or more tablespaces. One or more data files are explicitly created for
each tablespace to physically store the data of all logical structures in a tablespace. If it is a
TEMPORARY tablespace instead of a data file, the tablespace has a temporary file.

Oracle Database 11g: SQL Tuning Workshop 1 - 21


Logical and Physical Database Structures (continued)
Schemas
A schema is a collection of database objects that are owned by a database user. Schema objects are
the logical structures that directly refer to the database’s data. Schema objects include structures,
such as tables, views, sequences, stored procedures, synonyms, indexes, clusters, and database links.
In general, schema objects include everything that your application creates in the database.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Data Blocks
At the finest level of granularity, an Oracle database’s data is stored in data blocks. One data block
corresponds to a specific number of bytes of physical database space on the disk. A data block size is
specified for each tablespace when it is created. A database uses and allocates free database space in
Oracle data blocks.
Extents
The next level of logical database space is an extent. An extent is a specific number of contiguous ns
e
data blocks (obtained in a single allocation) that are used to store a specific type of information. e l ice
Segments r a bl
n s fe
The level of logical database storage above an extent is called a segment. A
- t r a
segment is a set of
extents that are allocated for a certain logical structure. Different types
n o nof segments include:
• Data segments: Each nonclustered, non-index-organized table
s a has a data segment, with the
exception of external tables and global temporary tables
) h a eฺ no segments, and partitioned
that have
i d
i r ฺes ntAllGofu the table’s data is stored in the
tables in which each table has one or more segments.

g p ฺm table,
extents of its data segment. For a partitioned
u d e each partition has a data segment. Each
cluster has a data segment. The data d of everyS t table in the cluster is stored in the cluster’s data
segment. ia @ i s
j a b e thindex
• Index segments: Each
i a ( index
u s
has an segment that stores all of its data. For a partitioned
index, each A b hastan
partition o index segment.
• Undo n i o
segments: One UNDO tablespace is created for each database instance. This tablespace
t o
n numerous undo segments to temporarily store undo information. The information in an
e Acontains
Jos recovery, to roll back uncommitted transactions for users.
undo segment is used to generate read-consistent database information and, during database

• Temporary segments: Temporary segments are created by the Oracle Database when a SQL
statement needs a temporary work area to complete execution. When the statement finishes
execution, the temporary segment’s extents are returned to the instance for future use. Specify
either a default temporary tablespace for every user, or a default temporary tablespace that is
used across the database.
The Oracle Database dynamically allocates space. When the existing extents of a segment are full,
additional extents are added. Because extents are allocated as needed, the extents of a segment may
or may not be contiguous on the disk.

Oracle Database 11g: SQL Tuning Workshop 1 - 22


Segments, Extents, and Blocks

• Segments exist in a tablespace.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Segments are collections of extents.


• Extents are collections of data blocks.
• Data blocks are mapped to disk blocks.

n s e
i ce
b l el
fer a
a n s
o n -tr
s an
Segment Extents
) h aData deฺ Disk
s blocks u i blocks
m i rฺe nt G
d gpฺ Stude
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io A
Segments, n Extents, and Blocks
t o
An objects, such as tables and indexes are stored as segments in tablespaces. Each segment
Database
e
Joscontains one or more extents. An extent consists of contiguous data blocks, which means that each
extent can exist only in one data file. Data blocks are the smallest unit of I/O in the database.
When the database requests a set of data blocks from the operating system (OS), the OS maps this to
an actual file system or disk block on the storage device. Because of this, you do not need to know
the physical address of any of the data in your database. This also means that a data file can be
striped or mirrored on several disks.
The size of the data block can be set at the time of database creation. The default size of 8 KB is
adequate for most databases. If your database supports a data warehouse application that has large
tables and indexes, a larger block size may be beneficial.
If your database supports a transactional application in which reads and writes are random,
specifying a smaller block size may be beneficial. The maximum block size depends on your OS.
The minimum Oracle block size is 2 KB; it should rarely (if ever) be used.
You can have tablespaces with a nonstandard block size. For details, see the Oracle Database
Administrator’s Guide.

Oracle Database 11g: SQL Tuning Workshop 1 - 23


SYSTEM and SYSAUX Tablespaces

• The SYSTEM and SYSAUX tablespaces are mandatory


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

tablespaces that are created at the time of database


creation. They must be online.
• The SYSTEM tablespace is used for core functionality (for
example, data dictionary tables).
• The auxiliary SYSAUX tablespace is used for additional
n s e
database components (such as the Enterprise Manager ce
li
Repository). ble ra
f e
a ns
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
ia to u
SYSTEM and o
iSYSAUX Tablespaces
Ab
t o n
Each n Database must contain a SYSTEM tablespace and a SYSAUX tablespace, which are
AOracle
e
Josautomatically created when the database is created. The system default is to create a smallfile
tablespace. You can also create bigfile tablespaces, 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). The SYSTEM tablespace is always
online when the database is open. It stores tables that support the core functionality of the database,
such as the data dictionary tables.
The SYSAUX tablespace is an auxiliary tablespace to the SYSTEM tablespace. The SYSAUX
tablespace stores many database components, 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, whereas
this is not possible in the case of the SYSTEM tablespace. Neither of them may be made read-only.

Oracle Database 11g: SQL Tuning Workshop 1 - 24


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• List the major architectural components of the Oracle


Database server
• Explain memory structures
• Describe background processes
• Correlate logical and physical storage structures s e
ce n
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
( j a Copyright
s e © 2008, Oracle. All rights reserved.
bia to u
io A
t o n
A n
o s e
J

Oracle Database 11g: SQL Tuning Workshop 1 - 25


Practice 1: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Listing the different components of an Oracle Database


server
• Looking at some instance and database components directly
on your machine

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 1 - 26


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Introduction to SQL Tuning

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Objectives nio
nto gives you an understanding of the tuning process and the different components of an
ThisAlesson
e Database that may require tuning.
osOracle
J

Oracle Database 11g: SQL Tuning Workshop 2 - 2


Reasons for Inefficient SQL Performance

• Stale or missing optimizer statistics


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Missing access structures


• Suboptimal execution plan selection
• Poorly constructed SQL

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Reasons for n o
iInefficient SQL Performance
n t o
SQLAstatements can perform poorly for a variety of reasons:
s e
Jo • Stale optimizer statistics: SQL execution plans are generated by the cost-based optimizer
(CBO). For CBO to effectively choose the most efficient plan, it needs accurate information on
the data volume and distribution of tables and indexes referenced in the queries. Without
accurate optimizer statistics, the CBO can easily be mislead and generate suboptimal execution
plans.
• Missing access structures: Absence of access structures, such as indexes, materialized views,
and partitions is a common reason for poor SQL performance. The right set of access structures
can improve SQL performance by several orders of magnitude.
• Suboptimal execution plan selection: The CBO can sometimes select a suboptimal execution
plan for a SQL statement. This happens for most part because of incorrect estimates of some
attributes of that SQL statement, such as its cost, cardinality, or predicate selectivity.
• Poorly constructed SQL: If the SQL statement is designed poorly, there is not much that the
optimizer can do to improve its performance. A missing join condition leading to a Cartesian
product, or the use of more expensive SQL constructs like UNION in place of UNION ALL, are
just a couple of examples of inefficient SQL design.
These four main causes of poor SQL optimization can have a drastic impact on performance.
Note: Additional reasons for poor performance might be connected with hardware-related issues,
such as memory, I/Os, CPUs, and so on.
Oracle Database 11g: SQL Tuning Workshop 2 - 3
Inefficient SQL: Examples

SELECT COUNT(*) FROM products p


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHERE prod_list_price <


1
1.15 * (SELECT avg(unit_cost) FROM costs c
WHERE c.prod_id = p.prod_id)

SELECT * FROM job_history jh, employees e


2 WHERE substr(to_char(e.employee_id),2) =
substr(to_char(jh.employee_id),2)
n s e
i ce
3 SELECT * FROM orders WHERE order_id_char = 1205
b l el
fer a
4
SELECT * FROM employees
a n s
o n -tr WHERE to_char(salary) = :sal

SELECT * FROM parts_old s an


5 UNION ) h a deฺ
r ฺ e s Gui
mi dent
SELECT * FROM parts_new
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
n i
Inefficient SQL: o AExamples
TheA ntoshows five examples of possibly poorly constructed SQL that could easily result in
slide
e
Josinefficient execution. 1. This is a common business question type. The query determines how many products have list
prices less than 15% above the average cost of the product. This statement has a correlated
subquery, which means that the subquery is run for every row found in the outer query. The
query is better written as:
SELECT COUNT(*) FROM products p,
(SELECT prod_id, AVG(unit_cost) ac FROM costs
GROUP BY prod_id) c
WHERE p.prod_id = c.prod_id AND
p.prod_list_price < 1.15 * c.ac
2. This query applies functions to the join columns, restricting the conditions where indexes can be
used. Use a simple equality, if you can. Otherwise, a function-based index may be necessary.
3. This query has a condition that forces implicit data type conversion; the ORDER_ID_CHAR
column is a character type, and the constant is a numeric type. You should make the literal
match the column type.

Oracle Database 11g: SQL Tuning Workshop 2 - 4


Inefficient SQL: Examples (continued)
4. The fourth query uses a data type conversion function in it to make the data types match in the
comparison. The problem here is that the TO_CHAR function is applied to the column value,
rather than to the constant. This means that the function is called for every row in the table. It
would be better to convert the literal once, and not convert the column. This is better queried
as:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM employees


WHERE salary = TO_NUMBER(:sal)
5. The UNION operator, as opposed to the UNION ALL operator, ensures that there are no
duplicate rows in the result set. However, this requires an extra step, a unique sort, to eliminate
any duplicates. If you know that there are no rows in common between the two UNIONed
queries, use UNION ALL instead of UNION. This eliminates the unnecessary sort.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 2 - 5


Performance Monitoring Solutions

Automatic
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Fore- 60 mn
In-memory
-ground MMON
statistics
SGA AWR
ASH

ADDM
Snapshots
n s e
Snapshots
i ce
b l el
Alerts
fe a
r AST
a n s
o n -tr
Statspack
s an
) h a deฺ
ADDM
r ฺ e s results G ui AWR report
i
ฺm uden t
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.

A bia to
Performance
o n ioMonitoring Solutions
A nt to the classical reactive tuning capabilities of previous releases, such as Statspack, SQL
In addition
e
Jostrace files, and performance views, Oracle Database 10g introduced new methodologies to monitor
your database in two categories:
• Proactive monitoring:
- With Automatic Database Diagnostic Monitor (ADDM): This component is the ultimate
solution for Oracle database tuning. ADDM automatically identifies bottlenecks within the
Oracle Database. Additionally, working with other manageability components, ADDM
makes recommendations on the options available for fixing these bottlenecks.
- Oracle Database 11g further automates the SQL tuning process by identifying problematic
SQL statements, running SQL Tuning Advisor on them, and implementing the resulting
SQL profile recommendations to tune the statement without requiring user intervention.
Automatic SQL Tuning uses the AUTOTASK framework through a new task called
Automatic SQL Tuning that runs every night by default.

Oracle Database 11g: SQL Tuning Workshop 2 - 6


Performance Monitoring Solutions (continued)
• Reactive monitoring:
- Server-generated alerts: The Oracle Database can automatically detect problematic
situations. In reaction to a detected problem, the Oracle Database sends you an alert
message with possible remedial action.
- The Oracle Database has powerful new data sources and performance-reporting
capabilities. Enterprise Manager provides an integrated performance management console
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

that uses all relevant data sources. Using a drill-down method, you can manually identify
bottlenecks with just a few mouse clicks.
New data sources are introduced to capture important information about your database’s health—for
example, new memory statistics (for current diagnostics) as well as statistics history stored in
Automatic Workload Repository (AWR).
Note: Accessing Enterprise Manager or tools discussed here may require additional licenses and
certain privileges generally reserved for database administrators.
ce nse
l e li
ra b
f e
a ns
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 2 - 7


Monitoring and Tuning Tools: Overview

Services
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EM Metric ADDM Direct


Alert SQL Perf AWR Hang
Statspack perf base SPA and SGA analyzer
log traces views reports pages line advisors monitor

tkprof trcsess n s e
AWR SQL
i ce
baseline
b l el
report

fe r a
Optimizer SQL
Base/
Histograms Metrics ASH a n
Services
o n -tr Compared
statistics
periods
statistics
Segment
statistics
statistics

a n
h a s eฺ
System
Wait Time
e s
OS) u id ASH
Session
model model ฺ
irstatistics n tG Alerts
report
statistics ฺ m e
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Monitoringnandi oA Tuning Tools: Overview
t o
AnOracle Database 10g, Release 2, you can generate SQL reports from AWR data
Since
e
Jos($ORACLE_HOME/rdbms/admin/awrsqrpt.sql), basically, the equivalent to sqrepsql.sql in Statspack.
Note
• SPA stands for SQL Performance Analyzer.
• ASH stands for Active Session History.
• AWR stands for Automatic Workload Repository.
• ADDM stands for Automatic Database Diagnostic Monitor.

Oracle Database 11g: SQL Tuning Workshop 2 - 8


EM Performance Pages for Reactive Tuning

Home
ASH Run
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Report ADDM
Performance

Nonidle Top Top Duplicate Blocking Instance Instance Baseline


Hang
wait Activity Consu- SQL Sessions Analysis Locks Activity Normalized
classes -mers Metrics

n s e
i ce
b l el
Wait
Top Top Top Top Top Top f r
eTop a
class
SQL Sessions Services Modules Actions s
an Objects
Files

o n -tr details

SPA
s an
Wait SQL SQL
) h a deฺ
event Tuning Enable/Disable
e s u i
Enable/Disable View System
histograms Advisor
Tuning ฺ
ir ent G
aggregation SQL trace SQL trace file statistics
Sets
p ฺ m d
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
EM Performancen i o APages for Reactive Tuning
A
There naretocases where real-time problem diagnosis must be performed. An irate user calls you, or
e
Josyou see a sudden spike in the activity of the system on the monitor. 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.

Oracle Database 11g: SQL Tuning Workshop 2 - 9


Tuning Tools: Overview

• Automatic Database Diagnostic Monitor (ADDM)


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• SQL Tuning Advisor


• SQL Tuning Sets
• SQL Access Advisor
• SQL Performance Analyzer
• SQL Monitoring n s e
i ce
• SQL Plan Management
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Tuning Tools:
o n ioOverview
A nt Database Diagnostic Monitor: Continually analyzes the performance data that is collected
Automatic
e
Josfrom the database instance
SQL Tuning Advisor: Analyzes SQL statements that have been identified as problematic, in an
effort to retune them. By default, this is an automated task. You can also, at any time, run the SQL
Tuning Advisor on a specific SQL workload, to look for ways to improve performance.
SQL Tuning Sets: Serve as a repository for sets of SQL statements. For example, the SQL Tuning
Advisor can run against a workload that is represented by a SQL Tuning Set. They can even be
transported from database to database, to perform analysis on different machines.
SQL Access Advisor: Analyzes a SQL statement, and provides advice on materialized views,
indexes, materialized view logs, and partitions
SQL Performance Analyzer: Automates the process of assessing the overall effect of a change,
such as upgrading a database or adding new indexes, on the full SQL workload by identifying
performance divergence for each statement
SQL Monitoring: The real-time SQL monitoring feature of Oracle Database enables you to monitor
the performance of SQL statements while they execute.
Note: SQL Plan Management can be used to control execution plan evolution. This topic is not
covered in this course. Refer to Oracle Database 11g: Performance Tuning for more information.

Oracle Database 11g: SQL Tuning Workshop 2 - 10


SQL Tuning Tasks: Overview

• Identifying high-load SQL


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Gathering statistics
• Generating system statistics
• Rebuilding existing indexes
• Maintaining execution plans
• Creating new index strategies n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Tuning n o Overview
iTasks:
o
t tuning tasks should be performed on a regular basis. You may see a way to rewrite a
nSQL
A
Many
e clause, but it may depend on a new index being built. This list of tasks gives you a
JosWHERE
background of some important tasks that must be performed, 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.
The ADDM is the ideal tool for this particular task.
• By default, the Oracle Database gathers optimizer statistics automatically. For this, a job is
scheduled to run in the maintenance windows.
• 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.
• Often, there is a beneficial impact on performance by rebuilding indexes. For example,
removing nonselective indexes to speed the data manipulation language (DML), or adding
columns to the index to improve selectivity.
• You can maintain the existing execution plan of SQL statements over time by using stored
statistics, outlines, or SQL plan baselines.

Oracle Database 11g: SQL Tuning Workshop 2 - 11


CPU and Wait Time Tuning Dimensions

Scalability is a system’s ability to process more workload with a


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

proportional increase in system resource use.

CPU
time
Possibly Scalable
needs SQL application
tuning
n s e
i ce
b l el
f er a
Scalable Needs s
anNo gain achieved
instance/RAC n-t
r by adding
application o
tuning a n CPUs/nodes
h a s eฺ
e s ) u id

ir ent G Wait
ฺ m
d gp Stud time
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
CPU and Wait
o n ioTime Tuning Dimensions
t tune your system, it is important that you compare the CPU time with the wait time of
nyou
WhenA
e system. By comparing CPU time with wait time, you can determine how much of the response
Jostime
your
is spent on useful work and how much on waiting for resources potentially held by other
processes.
As a general rule, the systems where CPU time is dominant usually need less tuning than the ones
where wait time is dominant. On the other hand, high CPU usage can be caused by badly-written
SQL statements.
Although the proportion of CPU time to wait time always tends to decrease as load on the system
increases, steep increases in wait time are a sign of contention and must be addressed for good
scalability.
Adding more CPUs to a node, or nodes to a cluster, would provide very limited benefit under
contention. Conversely, a system where the proportion of CPU time does not decrease significantly
as load increases can scale better, and would most likely benefit from adding CPUs or Real
Application Clusters (RAC) instances if needed.
Note: AWR reports display CPU time together with wait time in the Top 5 Timed Events section, if
the CPU time portion is among the top five events.

Oracle Database 11g: SQL Tuning Workshop 2 - 12


Scalability with Application Design,
Implementation, and Configuration
Applications have a significant impact on scalability.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Poor schema design can cause expensive SQL that does


not scale.
• Poor transaction design can cause locking and serialization
problems.
• Poor connection management can cause unsatisfactory e
n s
response times. i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Scalability n
o io Application Design, Implementation, and Configuration
with
nt
PoorAapplication design, implementation, and configuration have a significant impact on scalability.
s e
Jo This results in:
• Poor SQL and index design, 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, design is not the only problem. The physical implementation of the application can be the
weak link, as in the following examples:
• Systems can move to production environments with poorly written SQL that cause high I/O.
• Infrequent transaction COMMITs or ROLLBACKs can cause long locks on resources.
• The production environment can use different execution plans than those generated in testing.
• Memory-intensive applications that allocate a large amount of memory without much thought
for freeing the memory can cause excessive memory fragmentation.
• Inefficient memory usage places high stress on the operating virtual memory subsystem. This
affects performance and availability.

Oracle Database 11g: SQL Tuning Workshop 2 - 13


Common Mistakes on Customer Systems

1. Bad connection management


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2. Bad use of cursors and the shared pool


3. Excess of resources consuming SQL statements
4. Use of nonstandard initialization parameters
5. Poor database disk configuration
6. Redo log setup problems n s e
7. Excessive serialization lice
a b le
8. Inappropriate full table scans s f er
9. Large number of space-management or parse-related - t r an
n o n
generated SQL statements a
h a s eฺ
10. Deployment and migration errorss ) u i d
m i rฺe nt G
gpฺ StudUSERS
EDUCATE
d
e
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io on Customer Systems
Common Mistakes
o
nt connection management: The application connects and disconnects for each database
1.ABad
e
Jos interaction. This problem is common with stateless middleware in application servers. It has
over two orders of magnitude impact on performance, and is totally unscalable.
2. Bad use of cursors and the shared pool: Not using cursors results in repeated parses. If bind
variables are not used, there may be hard parsing of all similar SQL statements. This has an
order of magnitude impact on performance, and it is totally unscalable. Use cursors with bind
variables that open the cursor and execute it many times. Be suspicious of applications
generating dynamic SQL.
3. Bad SQL: Bad SQL is SQL that uses more resources than appropriate for the application. 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. SQL that consumes significant system
resources should be investigated for potential improvement. ADDM identifies high-load SQL
and the SQL Tuning Advisor can be used to provide recommendations for improvement.

Oracle Database 11g: SQL Tuning Workshop 2 - 14


Common Mistakes on Customer Systems (continued)
4. Use of nonstandard initialization parameters: These might have been implemented based on
poor advice or incorrect assumptions. Most systems give acceptable performance using only the
set of basic parameters. In particular, undocumented optimizer features can cause a great deal of
problems that may require considerable investigation. Likewise, optimizer parameters set in the
initialization parameter file can override proven optimal execution plans. For these reasons,
schemas, schema statistics, and optimizer settings should be managed together as a group to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ensure consistency of performance.


5. Getting database I/O wrong: Many sites lay out their databases poorly over the available disks.
Other sites specify the number of disks incorrectly because they configure disks by disk space
and not by I/O bandwidth.
6. Redo log setup problems: Many sites run with too small redo log files. Small redo logs cause
system checkpoints to continuously put a high load on the buffer cache and the I/O system. If
there are very few redo logs, the archive cannot keep up, and the database waits for the archivense
process to catch up.
e l ice
7. Excessive serialization: Serialization of data blocks in the buffer cache due to shortage
r a bl of undo
segments is particularly common in applications with large numbers of active
n s feusers and a few
undo segments. Use Automatic Segment Space Management (ASSM)
- a automatic undo
trand
management to solve this problem.
n o n
8. Long full table scans: Long full table scans for high-volume a or interactive online operations
sor poor
could indicate poor transaction design, missing indexes,h a d ฺ optimization. Long table
eSQL
) i
i r ฺes nt Gu
scans, by nature, are I/O-intensive and unscalable.
9. High amounts of recursive (SYS) SQL:
g p ฺmLarge u d e
amounts of recursive SQL executed by SYS
d t
S such as extent allocations, take place. This is
could indicate that space management activities,
i a @ i s
e th SQL executed under another user ID is probably SQL
unscalable and impacts user response time. Use locally managed tablespaces to reduce recursive
( jab uRecursive
SQL due to extentaallocation. s
and PL/SQL,Aso bithis is not
toa problem.
o
ni and migration errors: In many cases, an application uses too many resources
10. Deployment
t o
n the schema owning the tables has not been successfully migrated from the development
e Abecause
Jos environment or from an older implementation. Examples of this are missing indexes or incorrect
statistics. These errors can lead to suboptimal execution plans and poor interactive user
performance. When migrating applications of known performance, export the schema statistics
to maintain plan stability using the DBMS_STATS package.
Although these errors are not directly detected by ADDM, ADDM highlights the resulting high-load
SQL.

Oracle Database 11g: SQL Tuning Workshop 2 - 15


Proactive Tuning Methodology

• Simple design
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Data modeling
• Tables and indexes
• Using views
• Writing efficient SQL
• Cursor sharing n s e
i ce
• Using bind variables
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n io Methodology
Proactive Tuning
o
t
nusually
e A
Tuning implies fixing a performance problem. However, tuning should be part of the life
s
Jo The
cycle of an application, through the analysis, design, coding, production, and maintenance stages.
tuning phase is often left until the system is in production. At that time, tuning becomes a
reactive exercise, where the most important bottleneck is identified and fixed.
The slide lists some of the issues that affect performance and that should be tuned proactively instead
of reactively. These are discussed in more detail in the following slides.

Oracle Database 11g: SQL Tuning Workshop 2 - 16


Simplicity in Application Design

• Simple tables
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Well-written SQL
• Indexing only as required
• Retrieving only required information

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Simplicity in n o
iApplication Design
t o
An are no different from any other designed and engineered product. If the design looks
Applications
e
Josright, it probably is right. This principle should always be kept in mind when building applications.
Consider some of the following design issues:
• If the table design is so complicated that nobody can fully understand it, the table is probably
designed badly.
• 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 statement, underlying transaction, or
table design.
• If there are many indexes on a table and the same columns are repeatedly indexed, there is
probably a bad index design.
• If queries are submitted without suitable qualification (the WHERE clause) for rapid response for
online users, there is probably a bad user interface or transaction design.

Oracle Database 11g: SQL Tuning Workshop 2 - 17


Data Modeling

• Accurately represent business practices


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Focus on the most frequent and important business


transactions
• Use modeling tools
• Appropriately normalize data (OLTP versus DW)

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Data Modeling
o n io
nt
DataAmodeling is important in successful relational application design. This should be done in a way
s e
Jo that quickly and accurately represents the business practices. Apply your greatest modeling efforts to
those entities affected by the most frequent business transactions. Use of modeling tools can then
rapidly generate schema definitions and can be useful when a fast prototype is required.
Normalizing data prevents duplication. When data is normalized, you have a clear picture of the keys
and relationships. It is then easier to perform the next step of creating tables, constraints, and indexes.
A good data model ultimately means that your queries are written more efficiently.

Oracle Database 11g: SQL Tuning Workshop 2 - 18


Table Design

• Compromise between flexibility and performance:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Principally normalize
– Selectively denormalize
• Use Oracle performance and management features:
– Default values
– Constraints e
n s
– Materialized views i ce
– Clusters b l el
fer a
– Partitioning a n s
n -tr
• Focus on business-critical tables o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Table Design
o n io
t is largely a compromise between flexibility and performance of core transactions. To
ndesign
A
Table
e the database flexible and able to accommodate unforeseen workloads, the table design should
Joskeep
be very similar to the data model, and it should be normalized to at least third normal form.
However, certain core transactions can require selective denormalization for performance purposes.
Use the features supplied with Oracle Database to simplify table design for performance, such as
storing tables prejoined in clusters, adding derived columns and aggregate values, and using
materialized views or partitioned tables. Additionally, create check constraints and columns with
default values to prevent bad data from getting into the tables.
Design should be focused on business-critical tables so that good performance can be achieved in
areas that are the most used. For noncritical tables, shortcuts in design can be adopted to enable a
more rapid application development. If, however, a noncore table becomes a performance problem
during prototyping and testing, remedial design efforts should be applied immediately.

Oracle Database 11g: SQL Tuning Workshop 2 - 19


Index Design

• Create indexes on the following:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Primary key (can be automatically created)


– Unique key (can be automatically created)
– Foreign keys (good candidates)
• Index data that is frequently queried (select list).
• Use SQL as a guide to index design. s e
ce n
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Index Design
o n io
t is also a largely iterative process based on the SQL that is generated by application
ndesign
A
Index
e
Joskey
designers. However, it is possible to make a sensible start by building indexes that enforce foreign
constraints (to reduce response time on joins between primary key tables and foreign key tables)
and creating indexes on frequently accessed data, such as a person’s name. Primary keys and unique
keys are automatically indexed except for the DISABLE VALIDATE and DISABLE
NOVALIDATE RELY constraints. As the application evolves and testing is performed on realistic
sizes of data, certain queries need performance improvements, for which building a better index is a
good solution.
The following indexing design ideas should be considered when building a new index.
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. This can be done by appending to index all the columns of the
table referenced by the query. These columns are the select list columns and any required join or sort
columns. This technique is particularly useful in speeding up an online application’s response times
when time-consuming I/Os are reduced. This is best applied when testing the application with
properly-sized data for the first time. The most aggressive form of this technique is to build an index-
organized table (IOT).

Oracle Database 11g: SQL Tuning Workshop 2 - 20


Using Views

• Simplifies application design


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Is transparent to the developer


• Can cause suboptimal execution plans

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Using Views
o n io
t speed up and simplify application design. A simple view definition can mask data model
ncan
A
Views
e
Joscomplexity from the programmers whose priorities are to retrieve, display, collect, and store data.
However, though views provide clean programming interfaces, they can cause suboptimal, resource-
intensive queries when nested too deeply. The worst type of view use is creating joins on views that
reference other views, which in turn reference other views. In many cases, developers can satisfy the
query directly from the table without using a view. Because of their inherent properties, views
usually make it difficult for the optimizer to generate the optimal execution plan.

Oracle Database 11g: SQL Tuning Workshop 2 - 21


SQL Execution Efficiency

• Good database connectivity


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Minimizing parsing
• Share cursors
• Using bind variables

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Execution
o n io Efficiency
An A nt
application that is designed for SQL execution efficiency must support the following
s e
Jo characteristics:
Good database connection management: Connecting to the database is an expensive operation that
is highly unscalable. Therefore, the number of concurrent connections to the database should be
minimized as much as possible. A simple system, where a user connects at application initialization,
is ideal. However, in a Web-based or multitiered application, where application servers are used to
multiplex database connections to users, this can be difficult. With these types of applications, design
efforts should ensure that database connections are pooled and not reestablished for each user
request.
Good cursor usage and management: Maintaining user connections is equally important for
minimizing the parsing activity on the system. Parsing is the process of interpreting a SQL statement
and creating an execution plan for it. This process has many phases, including syntax checking,
security checking, execution plan generation, and loading shared structures into the shared pool.

Oracle Database 11g: SQL Tuning Workshop 2 - 22


SQL Execution Efficiency (continued)
There are two types of parse operations:
• Hard parsing: A SQL statement is submitted for the first time, and no match is found in the
shared pool. Hard parses are the most resource-intensive and unscalable because they perform
all the operations involved in a parse.
• Soft parsing: A SQL statement is submitted for the first time, and a match is found in the shared
pool. The match can be the result of previous execution by another user. The SQL statement is
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

shared, which is good for performance. However, soft parses are not ideal because they still
require syntax and security checking, which consume system resources.
Because parsing should be minimized as much as possible, application developers should design
their applications to parse SQL statements once and execute them many times. This is done through
cursors. Experienced SQL programmers should be familiar with the concept of opening and
reexecuting cursors.
n s e
Application developers must also ensure that SQL statements are shared within the shared pool. To
e l iceIf
do this, bind variables to represent the parts of the query that change from execution to execution.
a l
busers.
f r
this is not done, the SQL statement is likely to be parsed once and never reused by other
e statements.To
n
ensure that SQL is shared, use bind variables and do not use string literals with
a s
SQL
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 2 - 23


Writing SQL to Share Cursors

• Create generic code using the following:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Stored procedures and packages


– Database triggers
– Any other library routines and procedures
• Write to format standards (improves readability):
– Case e
n s
– White space i ce
– Comments b l el
fer a
– Object references a n s
– Bind variables n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Writing SQL
o n itoo Share Cursors
ForA nt to share cursors, the code must be written in the same way character wise (This allows the
Oracle
e to recognize that two statements are the same and thus can be shared.), unless you use some
Jossystem
special initialization parameters, such as CURSOR_SHARING discussed later in the lesson titled
“Using Bind Variables.” You should, therefore, develop coding conventions for SQL statements in
ad hoc queries, SQL scripts, and Oracle Call Interface (OCI) calls.
Use generic shared code:
• Write and store procedures that can be shared across applications.
• Use database triggers.
• Write referenced triggers and procedures when using application development tools.
• Write library routines and procedures in other environments.
Write to format standards:
• Develop format standards for all statements, including those in PL/SQL code.
• Develop rules for the use of uppercase and lowercase characters.
• Develop rules for the use of white space (spaces, tabs, returns).
• Develop rules for the use of comments (preferably keeping them out of the SQL statements
themselves).
• Use the same names to refer to identical database objects. If possible, prefix each object with a
schema name.

Oracle Database 11g: SQL Tuning Workshop 2 - 24


Performance Checklist

• Set initialization parameters and storage options.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Verify resource usage of SQL statements.


• Validate connections by middleware.
• Verify cursor sharing.
• Validate migration of all required objects.
• Verify validity and availability of optimizer statistics. n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Performance
o n ioChecklist
ntthe minimal number of initialization parameters. Ideally, most initialization parameters
• ASet
e should be left at default. If there is more tuning to perform, this shows up when the system is
Jos under load. Set storage options for tables and indexes in appropriate tablespaces.
• Verify that all SQL statements are optimal and understand their resource usage.
• 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.
• Validate that the SQL statements use cursors efficiently. Each SQL statement should be parsed
once and then executed multiple times. This does not happen mostly because bind variables are
not used properly and the WHERE clause predicates are sent as string literals.
• Validate that all schema objects are correctly migrated from the development environment to the
production database. This includes tables, indexes, sequences, triggers, packages, procedures,
functions, Java objects, synonyms, grants, and views. Ensure that any modifications made in
testing are made to the production system.
• As soon as the system is rolled out, establish a baseline set of statistics from the database and
operating system. This first set of statistics validates or corrects any assumptions made in the
design and rollout process.

Oracle Database 11g: SQL Tuning Workshop 2 - 25


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 2 - 26


Practice 2: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Rewriting queries for better performance


• Rewriting applications for better performance

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 2 - 27


J
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

o s e
n A
t o n
Aio
b i a
b i
to
(ja use
d p ฺ
a@ this
r ฺ
g Stu
e ) h
mi dent
s
s Gui
an
o
a deฺ
tr n -
a n s fe
r
a b l el
i
ce n s e
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Introduction to the Optimizer

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 3 - 2


Structured Query Language

DML ESS
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

INSERT DDL
UPDATE DECLARE
DELETE CREATE CONNECT
MERGE DROP OPEN
ALTER CLOSE
SELECT RENAME DESCRIBE
TRUNCATE WHENEVER
GRANT PREPARE
n s e
REVOKE EXECUTE
i ce
TCS AUDIT FETCH
b l el
NOAUDIT
fe r a
COMMIT COMMENT
a n s
ROLLBACK
n r CS
-Stession
o
a n ALTER SESSION
SAVEPOINT
SET TRANSACTION s eฺ SET ROLE
SystemCS) ha i d
i r es t Gu
ฺSYSTEM
g p ฺm uden
ALTER
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Structured n
o io Language
Query
t
SQLAisnthe language with which all programs and users access data in an Oracle Database.
e
Josdirectly,
Application programs and Oracle tools often allow users to access the database without using SQL
but these applications in turn must use SQL when executing user requests. The system
strives to comply with industry-accepted standards and participates actively in SQL standards
committees (ANSI and ISO). The latest SQL standard was adopted in July 2003 and is often called
SQL:2003. You can categorize SQL statements into six main sets:
• Data manipulation language (DML) statements manipulate or query data in existing schema
objects.
• Data definition language (DDL) statements define, alter the structure of, and drop schema
objects.
• Transaction control statements (TCS) manage the changes made by DML statements and group
DML statements into transactions.
• System Control Statements change the properties of the Oracle Database instance.
• Session Control Statements manage the properties of a particular user’s session.
• Embedded SQL statements incorporate DDL, DML, and TCS within a procedural language
program, such as PL/SQL and Oracle precompilers. This incorporation is done using the
statements listed in the slide under the ESS category.
Note: SELECT statements are the most used statements. Although the rest of this course focuses
mainly on queries, it is important to note that any type of SQL statement is subject to optimization.

Oracle Database 11g: SQL Tuning Workshop 3 - 3


SQL Statement Representation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Private Private Private Private Private


SQL area SQL area SQL area SQL area SQL area
n s e
i ce
b l el
fer a
a n s
o n -tr
a n
Shared
h s
a SQL eฺarea
Shared
) i d
ฺes nt Gu
SQL area
i r
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
SQL Statement
o n io Representation
t
nDatabase
e A
Oracle represents each SQL statement it runs with a shared SQL area and a private SQL
s
Jo area. Oracle Database recognizes when two users execute the same SQL statement and reuses the
shared SQL area for those users. However, each user must have a separate copy of the statement’s
private SQL area.
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, which often happens when many users run the same application.
Note: In evaluating whether statements are similar or identical, Oracle Database considers SQL
statements issued directly by users and applications, as well as recursive SQL statements issued
internally by a DDL statement.

Oracle Database 11g: SQL Tuning Workshop 3 - 4


SQL Statement Implementation

User User User User User


process process process process process
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Client
Server
Server Server Server Server Server
process process process process process

Private Private Private Aggregated Private Private


SQL area SQL area SQL area PGA SQL area SQL area
n s e
i ce
SGA
b l el
fer a
Shared Shared
a n s
o n -tr Java pool SQL area
Library cache
SQL area

Buffer cache
s an
Data dictionary
cache
Result cache
) h a dOther
i eฺ
Redo log i r ฺes nt Gu
buffer
g p ฺm ude
SHARED_POOL
Streams pool
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
SQL Statement
o n io Implementation
t
nDatabase
e A
Oracle creates and uses memory structures for various purposes. For example, memory
s
Jo stores program codes that are run, data that is shared among users, and private data areas for each
connected user.
Oracle Database allocates memory from the shared pool when a new SQL statement is parsed, to
store in the shared SQL area. The size of this memory depends on the complexity of the statement. If
the entire shared pool has already been allocated, 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. If Oracle Database deallocates a shared SQL area, the associated SQL
statement must be reparsed and reassigned to another shared SQL area at its next execution.

Oracle Database 11g: SQL Tuning Workshop 3 - 5


SQL Statement Processing: Overview

OPEN
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PARSE

yes yes
query? describe? DESCRIBE

no no
no yes
more?

DEFINE

no yes
more?
yes

reparse?
no
bind?
yes
BIND
n s e
i ce
el
no
no yes
more?

a b l
PARALLELIZE

fer
EXECUTE
a n s
n - tr
yes
o
an
query? FETCH

no

h s
a deฺ
yes execute
others?
e )
s Gui
no
more?
yes

r ฺ
mi dent
no

p ฺ
g Stu
CLOSE

d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
Abia to u
n
SQL Statement
o io Processing: Overview
TheA nt in the slide shows all the steps involved in query execution and these steps can be found
graphic
J osineOracle® Database Concepts 11g Release 1 (11.1).

Oracle Database 11g: SQL Tuning Workshop 3 - 6


SQL Statement Processing: Steps

1. Create a cursor.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2. Parse the statement.


3. Describe query results.
4. Define query output.
5. Bind variables.
6. Parallelize the statement. n s e
i ce
7. Execute the statement.
b l el
8. Fetch rows of a query. fer a
a n s
9. Close the cursor. n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Statement
o n io Processing: Steps
A
Note nt not all statements require all these steps. For example, nonparallel DDL statements are
that
e
Josrequired in only two steps: Create and Parse.
Parallelizing the statement involves deciding that it can be parallelized as opposed to actually
building parallel execution structures.

Oracle Database 11g: SQL Tuning Workshop 3 - 7


Step 1: Create a Cursor

• A cursor is a handle or name for a private SQL area.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• It contains information for statement processing.


• It is created by a program interface call in expectation of a
SQL statement.
• The cursor structure is independent of the SQL statement
that it contains. e
n s
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Step 1: Create
o ioa Cursor
A nt can be thought of as an association between a cursor data area in a client program and
A cursor
e server’s data structures. Most Oracle tools hide much of cursor handling from the user, but
JosOracle
Oracle Call Interface (OCI) programs need the flexibility to be able to process each part of query
execution separately. Therefore, precompilers allow explicit cursor declaration. Most of this can also
be done using the DBMS_SQL package as well.
A handle is similar to the handle on a mug. When you have a hold of the handle, you have a hold of
the cursor. It is a unique identifier for a particular cursor that can only be obtained by one process at
a time.
Programs must have an open cursor to process a SQL statement. The cursor contains a pointer to the
current row. The pointer moves as rows are fetched until there are no more rows left to process.
The following slides use the DBMS_SQL package to illustrate cursor management. This may be
confusing to people unfamiliar with it; however, it is more friendly than PRO*C or OCI. It is slightly
problematic in that it performs FETCH and EXECUTE together, so the execute phase cannot be
separately identified in the trace.

Oracle Database 11g: SQL Tuning Workshop 3 - 8


Step 2: Parse the Statement

• Statement passed from the user process to the Oracle


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Step 2: Parse
o n iothe Statement
t
nparsing,
e A
During the SQL statement is passed from the user process to the Oracle instance, and a
s
Jo parsed representation of the SQL statement is loaded into a shared SQL area.
Translation and verification involve checking if the statement already exists in the library cache.
For distributed statements, check for the existence of database links.
Typically, the parse phase is represented as the stage where the query plan is generated.
The parse step can be deferred by the client software to reduce network traffic. What this means is
that the PARSE is bundled with the EXECUTE, so there are fewer round-trips to the server.
Note: When checking if statements are identical, they must be identical in every way including case
and spacing.

Oracle Database 11g: SQL Tuning Workshop 3 - 9


Steps 3 and 4: Describe and Define

• The describe step provides information about the select list


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

items; it is relevant when entering dynamic queries through


an OCI application.
• The define step defines location, size, and data type
information required to store fetched values in variables.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
ioDescribe and Define
n4:
Steps 3 and
n to
Step A
e 3: Describe
o s
J The describe stage is necessary only if the characteristics of a query’s result are not known, for
example, when a query is entered interactively by a user. In this case, the describe stage determines
the characteristics (data types, lengths, and names) of a query’s result. Describe tells the application
what select list items are required. If, for example, you enter a query such as:
SQL> select * from employees;,
information about the columns in the employees table is required.
Step 4: Define
In the define stage, you specify the location, size, and data type of variables defined to receive each
fetched value. These variables are called define variables. Oracle Database performs data type
conversion, if necessary.
These two steps are generally hidden from users in tools such as SQL*Plus. However, with
DBMS_SQL or OCI, it is necessary to tell the client what the output data is and which the setup areas
are.

Oracle Database 11g: SQL Tuning Workshop 3 - 10


Steps 5 and 6: Bind and Parallelize

• Bind any bind values:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Enables memory address to store data values


– Allows shared SQL even though bind values may change
• Parallelize the statement:
– SELECT
– INSERT e
n s
– UPDATE i ce
– b l el
MERGE
fer a
– DELETE
a n s
– n -tr
CREATE o
– ALTER s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
ioBind and Parallelize
n6:
Steps 5 and
n to
Step A
e 5: Bind
o s
J At this point, Oracle Database knows the meaning of the SQL statement, but still does not have
enough information to run the statement. Oracle Database needs values for any variables listed in the
statement. The process of obtaining these values is called binding variables.
Step 6: Parallelize
Oracle Database can parallelize the execution of SQL statements (such as SELECT, INSERT,
UPDATE, MERGE, DELETE), and some DDL operations, such as index creation, creating a table with
a subquery, and operations on partitions. Parallelization causes multiple server processes to perform
the work of the SQL statement, so it can complete faster.
Parallelization involves dividing the work of a statement among a number of slave processes.
Parsing has already identified if a statement can be parallelized or not and has built the appropriate
parallel plan. At execution time, this plan is then implemented if sufficient resource is available.

Oracle Database 11g: SQL Tuning Workshop 3 - 11


Steps 7 Through 9

• Execute:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Drives the SQL statement to produce the desired results


• Fetch rows:
– Into defined output variables
– Query results returned in table format
– Array fetch mechanism e
n s
• Close the cursor. i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Steps 7 Through
o io 9
t Oracle Database has all the necessary information and resources, so the statement is
npoint,
A
At this
e If the statement is a query (without the FOR UPDATE clause) statement, no rows need to be
Joslocked
run.
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.

Oracle Database 11g: SQL Tuning Workshop 3 - 12


SQL Statement Processing PL/SQL: Example

SQL> variable c1 number


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> execute :c1 := dbms_sql.open_cursor;

SQL> variable b1 varchar2


SQL> execute dbms_sql.parse
2 (:c1
3 ,'select null from dual where dummy = :b1'
4 ,dbms_sql.native);
n s e
lice
SQL> execute :b1:='Y';
a b le
fer
SQL> exec dbms_sql.bind_variable(:c1,':b1',:b1);
s
- t r an
SQL> variable r number
o n
s eฺ an
SQL> execute :r := dbms_sql.execute(:c1);
h a
SQL> variable r number ฺes) u id
ฺ m ir ent G
SQL> execute :r := dbms_sql.close_cursor(:c1);
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
SQL Statementn i oAProcessing PL/SQL: Example
n t o
ThisAexample summarizes the various steps discussed previously.
s e
Jo 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 13


SQL Statement Parsing: Overview
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Syntactic and semantic check

Privileges check

Private Parse
SQL area Allocate private SQL Area call
n s e
ice
l el
Parsed representation
b
fe r a
Existing shared No
a n s
Parse operation

SQL area? (Hard parse)


o n -tr
(Optimization)

s a n shared SQL area


Allocate
Yes (Soft parse)
) h a deฺ
r ฺ e s Gui Shared
Execute statement
m i
ฺ ude n t SQL area
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.

A bia to
SQL Statement
o n io Parsing: Overview
A ntis one stage in the processing of a SQL statement. When an application issues a SQL
Parsing
e
JosDatabase
statement, the application makes a parse call to Oracle Database. During the parse call, Oracle
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.

Oracle Database 11g: SQL Tuning Workshop 3 - 14


SQL Statement Parsing: Overview (continued)
• 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 3 - 15


Why Do You Need an Optimizer?

Query to optimize
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM emp WHERE job = 'MANAGER';

Schema
How can I retrieve these rows? information
Possible access paths
Read
Use the
1 each row
n s e
index.
and check.
i ce
b l el
fer a
Statistics Which one is faster? 2
a n s
o n -tr
Only 1% of employees are managers
s an
) h a deฺ
r ฺ e s Gui
3 Use the
indexp ฺ mi denIthave a plan!
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Why Do You n o an Optimizer?
iNeed
o
nt should always return the correct result as quickly as possible.
TheA optimizer
e
JosThe 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 16


Why Do You Need an Optimizer?

Query to optimize
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM emp WHERE job = 'MANAGER';

Schema
How can I retrieve these rows? information
Possible access paths
Read
Use the
1 each row
n s e
index.
and check.
i ce
b l el
fer a
Statistics Which one is faster? 2
a n s
o n -tr
80% of employees are managers
s an
) h a deฺ
r ฺ e s Gui
3 Table Scanmi
Use Full
nIthave a plan!
p ฺ
g Stu d e
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Why Do You n o an Optimizer? (continued)
iNeed
o
nt in the slide shows you that if statistics change, the optimizer adapts its execution plan.
TheA example
e
Josfull
In this case, statistics show that 80 percent of the employees are managers. In the hypothetical case, a
table scan is probably a better solution than using the index.

Oracle Database 11g: SQL Tuning Workshop 3 - 17


Optimization During Hard Parse Operation

Parsed representation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

(query blocks)

Optimizer
Transformer

Statistics
n s e
Estimator i ce
CBO
Plan Generator
Dictionary
b l el
fer a
a n s
o n -tr
Execution Plan
s an
) h a deฺ
r ฺ e s Gui Shared
ฺ m i ent SQL area

d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Optimization
o n ioDuring Hard Parse Operation
TheA nt creates the execution plan for a SQL statement.
optimizer
e
JosSQL 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 18


Transformer: OR Expansion Example

• Original query: B*-tree Index


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT *
FROM emp
WHERE job = 'CLERK' OR deptno = 10;

• Equivalent transformed query:


n s e
SELECT *
lice
FROM emp
a b le
WHERE job = 'CLERK' s f er
- t r an
UNION ALL
n o n
SELECT *
s a
FROM emp ) h a
i d eฺ
s Gu
irฺe <> 'CLERK';
WHERE deptno = 10 AND job tฺm uden
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
Transformer: ioOR Expansion Example Ab
o n
If a A nt contains a WHERE clause with multiple conditions combined with OR operators, the
query
e
Josoptimizer 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 19


Transformer: Subquery Unnesting Example

• Original query:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT *
FROM accounts
WHERE custno IN
(SELECT custno FROM customers);

n s e
• Equivalent transformed query: ice
b l el
SELECT accounts.* f er a
an s
FROM accounts, customers
- t r
non
WHERE accounts.custno = customers.custno;
a
h a s eฺ
) i d
i r ฺes nt Gu Primary or unique key

g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
ioSubquery Unnesting Example Ab
Transformer:
o n
A nt a query, the optimizer may choose to transform the original query into an equivalent JOIN
To unnest
e
Josstatement, 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 20


Transformer: View Merging Example

• Original query: Index


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CREATE VIEW emp_10 AS


SELECT empno, ename, job, sal, comm, deptno
FROM emp
WHERE deptno = 10;

n s e
i ce
el
SELECT empno FROM emp_10 WHERE empno > 7800;
a b l
fer
• Equivalent transformed query: a n s
o n -tr
SELECT empno
a n
FROM emp h s
a deฺ
e )
s > 7800; ui
WHERE deptno = 10 AND empno r ฺ G
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Transformer: n i oView Merging Example
t o
An the view’s query into a referencing query block in the accessing statement, the optimizer
To merge
e
J osreplaces 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 21


Transformer: Predicate Pushing Example
• Original query: Index

CREATE VIEW two_emp_tables AS


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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;


n s e
• Equivalent transformed query: i ce
b l el
SELECT ename
f a
er
n s
adeptno
FROM ( SELECT empno, ename, job,sal, comm,
- t r
FROM emp1 WHERE deptno = 20non
s a
UNION
) h a
i d eฺ
SELECT empno, ename,
i r ฺesjob,sal, t G u comm, deptno
FROM emp2 WHERE
g p ฺm deptnou d en = 20 );
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Transformer:
o n ioPredicate Pushing Example
TheA nt can transform a query block that accesses a nonmergeable view by pushing the query
optimizer
e
Josblock’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.

Oracle Database 11g: SQL Tuning Workshop 3 - 22


Transformer: Transitivity Example

• Original query: Index


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT *
FROM emp, dept
WHERE emp.deptno = 20 AND emp.deptno = dept.deptno;

• Equivalent transformed query:


n s e
SELECT *
lice
FROM emp, dept
a b le
s
WHERE emp.deptno = 20 AND emp.deptno = dept.deptno f er
- t r an
AND dept.deptno = 20; n no
a
s eฺ
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Transformer:
o n ioTransitivity Example
t
nconditions
e A
If two in the WHERE clause involve a common column, the optimizer sometimes can infer
s
Jo aoptimize
third condition, using the transitivity principle. The optimizer can then use the inferred condition to
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.

Oracle Database 11g: SQL Tuning Workshop 3 - 23


Cost-Based Optimizer

• Piece of code:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Estimator
– Plan generator
• Estimator determines cost of optimization suggestions made
by the plan generator:
– Cost: Optimizer’s best estimate of the number of standardized e
I/Os made to execute a particular statement optimization n s
i ce
• Plan generator: b l el
fe ra
– Tries out different statement optimization techniquesn s
– Uses the estimator to cost each optimization n - tra
suggestion
n o
– a
Chooses the best optimization suggestion
s ebased on cost
h a ฺ
– Generates an execution plan esfor id
) bestuoptimization
m i rฺ nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
io
Cost-BasednOptimizer
o
TheA nt
combination of the estimator and plan generator code is commonly called the cost-based
s e
Jo optimizer (CBO).
The estimator generates three types of measures: selectivity, cardinality, and cost. These measures
are related to each other. Cardinality is derived from selectivity and often the cost depends on
cardinality. The end goal of the estimator is to estimate the overall cost of a given plan. If statistics
are available, the estimator uses these to improve the degree of accuracy when computing the
measures.
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. Many different plans are possible because of the various
combinations of different access paths, join methods, and join orders that can be used to access and
process data in different ways and produce the same result. The number of possible plans for a query
block is proportional to the number of join items in the FROM clause. This number rises
exponentially with the number of join items.
The optimizer uses various pieces of information to determine the best path: WHERE clause,
statistics, initialization parameters, supplied hints, and schema information.

Oracle Database 11g: SQL Tuning Workshop 3 - 24


Estimator: Selectivity

Number of rows satisfying a condition


Selectivity =
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Total number of rows

• Selectivity is the estimated proportion of a row set retrieved


by a particular predicate or combination of predicates.
• It is expressed as a value between 0.0 and 1.0:
– High selectivity: Small proportion of rows
– Low selectivity: Big proportion of rows n s e
i ce
• Selectivity computation: b l el
fe r a
– If no statistics: Use dynamic sampling s
ra n
of-trows
– If no histograms: Assume even distribution n
n o
• Statistic information: a
s eฺ
h a
– DBA_TABLES and DBA_TAB_STATISTICS
e s ) u i(dNUM_ROWS)
– DBA_TAB_COL_STATISTICS ฺ
ir (NUM_DISTINCT
n tG
HIGH/LOW_VALUE,…)g p ฺ m d e , DENSITY,

d S tu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Estimator: n i oA
Selectivity
A nto represents a fraction of rows from a row set. The row set can be a base table, a view, or
Selectivity
e
Josthe result of a join or a GROUP BY operator. The selectivity is tied to a query predicate, such as
last_name = 'Smith', or a combination of predicates, such as last_name = 'Smith'
AND job_type = 'Clerk'. A predicate acts as a filter that filters a certain number of rows
from a row set. Therefore, the selectivity of a predicate indicates the percentage of rows from a row
set that passes the predicate test. Selectivity lies in a value range from 0.0 to 1.0. A selectivity of 0.0
means that no rows are selected from a row set, and a selectivity of 1.0 means that all rows are
selected.
If no statistics are available, the optimizer either uses dynamic sampling or an internal default value,
depending on the value of the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter. When
statistics are available, the estimator uses them to estimate selectivity. For example, for an equality
predicate (last_name = 'Smith'), 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. Thus,
even distribution is assumed. If a histogram is available in the LAST_NAME column, the estimator
uses it instead of the number of distinct values. The histogram captures the distribution of different
values in a column, so it yields better selectivity estimates.
Note: It is important to have histograms in columns that contain values with large variations in the
number of duplicates (data skew).

Oracle Database 11g: SQL Tuning Workshop 3 - 25


Estimator: Cardinality

Cardinality = Selectivity * Total number of rows


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Expected number of rows retrieved by a particular operation


in the execution plan
• Vital figure to determine join, filters, and sort costs
• Simple example:
n s e
ce
SELECT days FROM courses WHERE dev_name = 'ANGEL';
el i
– The number of distinct values in DEV_NAME is 203. a b l
s f er
n -t ran is 1018.
– The number of rows in COURSES (original cardinality)
– Selectivity = 1/203 = 4.926*e-03 o
s an
– Cardinality = (1/203)*1018 = 5.01
) h a(rounded
i d eฺ off to 6)
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Estimator: n
o io
Cardinality
TheA nt
cardinality of a particular operation in the execution plan of a query represents the estimated
s e
Jo number of rows retrieved by that particular operation. Most of the time, the row source can be a base
table, a view, or the result of a join or GROUP BY operator.
When costing a join operation, it is important to know the cardinality of the driving row source. With
nested loops join, for example, the driving row source defines how often the system probes the inner
row source.
Because sort costs are dependent on the size and number of rows to be sorted, cardinality figures are
also vital for sort costing.
In the example in the slide, based on assumed statistics, the optimizer knows that there are 203
different values in the DEV_NAME column, and that the total number of rows in the COURSES table
is 1018. Based on this assumption, the optimizer deduces that the selectivity of the
DEV_NAME='ANGEL' predicate is 1/203 (assuming there are no histograms), and also deduces the
cardinality of the query to be (1/203)*1018. This number is then rounded off to the nearest integer, 6.

Oracle Database 11g: SQL Tuning Workshop 3 - 26


Estimator: Cost

• Cost is the optimizer’s best estimate of the number of


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

standardized I/Os it takes to execute a particular statement.


• 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. e
n s
i ce
b l el
Single block I/O cost Multiblock I/O cost CPU cost fe r a
a n s
o n -tr Cost=
#SRds*sreadtim + #MRds*mreadtim + #CPUCycles/cpuspeed
sreadtim
s an
#SRds: Number of single block reads
) h
Sreadtim: aSingle block
i d eฺread time
#MRds: Number of multiblock reads
r s G
ฺeMreadtim: u read time
Multiblock
#CPUCycles: Number of CPU Cycles i
ฺm uden Cpuspeed: t Millions instructions per second

d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.

A bia to
Estimator: n
o io
Cost
TheA nt of a statement represents the optimizer’s best estimate of the number of standardized
cost
e
Josinputs/outputs (I/Os) it takes to execute that statement. Basically, 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, so one cost unit corresponds to one single block random read. 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; often
it is the only contribution to the cost (in cases of in-memory sort, hash, predicate evaluation, and
cached I/O).
This model is straightforward for serial execution. For parallel execution, necessary adjustments are
made while computing estimates for #SRds, #MRds, and #CPUCycles.
Note: #CPUCycles includes CPU cost of query processing (pure CPU cost) and CPU cost of data
retrieval (CPU cost of the buffer cache get).

Oracle Database 11g: SQL Tuning Workshop 3 - 27


Plan Generator

select e.last_name, c.loc_id


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

from employees e, classes c where e.emp_id = c.instr_id;

Join order[1]: DEPARTMENTS[D]#0 EMPLOYEES[E]#1


NL Join: Cost: 41.13 Resp: 41.13 Degree: 1
SM cost: 8.01
HA cost: 6.51
Best:: JoinMethod: Hash
Cost: 6.51 Degree: 1 Resp: 6.51 Card: 106.00
Join order[2]: EMPLOYEES[E]#1 DEPARTMENTS[D]#0
NL Join: Cost: 121.24 Resp: 121.24 Degree: 1
n s e
ce
SM cost: 8.01
HA cost: 6.51
eli
Join order aborted
a b l
Final cost for query block SEL$1 (#0)
fer
All Rows Plan:
a n s
tr
Best join order: 1
+----------------------------------------------------------------+
o n -
an
| Id | Operation | Name | Rows | Bytes | Cost |
+----------------------------------------------------------------+
| 0 | SELECT STATEMENT | | |
h
| s
a deฺ 7 |
| 1 | HASH JOIN | |
e )
s Gui
106 | 6042 | 7 |
| 2 |
r
TABLE ACCESS FULL | DEPARTMENTS|
ฺ 27 | 810 | 3 |
| 3 |
p ฺ mi dent
TABLE ACCESS FULL | EMPLOYEES | 107 | 2889 | 3 |

g Stu
+----------------------------------------------------------------+
d
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Plan Generator
o io
TheA nt generator explores various plans for a query block by trying out different access paths, join
plan
e
Josmethods, and join orders. Ultimately, the plan generator delivers the best execution plan for your
statement. The slide shows you an extract of an optimizer trace file generated for the select
statement. As you can see from the trace, the plan generator has six possibilities, or six different
plans to test: Two join orders, and for each, three different join methods. It is assumed that there are
no indexes in this example.
To retrieve the rows, you can start to join the DEPARTMENTS table to the EMPLOYEES table. For
that particular join order, you can use three possible join mechanisms that the optimizer knows:
Nested Loop, Sort Merge, or Hash Join. For each possibility, you have the cost of the corresponding
plan. The best plan is the one shown at the end of the trace.
The plan generator uses an internal cutoff to reduce the number of plans it tries when finding the one
with the lowest cost. The cutoff is based on the cost of the current best plan. If the current best cost is
large, the plan generator tries harder (in other words, explores more alternate plans) to find a better
plan with lower cost. If the current best cost is small, the plan generator ends the search swiftly
because further cost improvement is not significant. The cutoff works well if the plan generator starts
with an initial join order that produces a plan with a cost close to optimal. Finding a good initial join
order is a difficult problem.
Note: Access path, join methods, and plan are discussed in more detail in the lessons titled
“Optimizer Operators” and “Interpreting Execution Plans.”
Oracle Database 11g: SQL Tuning Workshop 3 - 28
Controlling the Behavior of the Optimizer

• CURSOR_SHARING: SIMILAR, EXACT, FORCE


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• DB_FILE_MULTIBLOCK_READ_COUNT
• PGA_AGGREGATE_TARGET
• STAR_TRANSFORMATION_ENABLED
• RESULT_CACHE_MODE: MANUAL, FORCE
• RESULT_CACHE_MAX_SIZE n s e
i ce
• RESULT_CACHE_MAX_RESULT
b l el
• RESULT_CACHE_REMOTE_EXPIRATION fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Controllingnthe
o io Behavior of the Optimizer
t
nparameters
e A
These control the optimizer behavior:
s
Jo • CURSOR_SHARING determines what kind of SQL statements can share the same cursors:
- FORCE: Forces statements that may differ in some literals, but are otherwise identical, to
share a cursor, unless the literals affect the meaning of the statement
- SIMILAR: Causes statements that may differ in some literals, but are otherwise identical,
to share a cursor, unless the literals affect either the meaning of the statement or the degree
to which the plan is optimized. Forcing cursor sharing among similar (but not identical)
statements can have unexpected results in some decision support system (DSS)
applications, or applications that use stored outlines.
- EXACT: Only allows statements with identical text to share the same cursor. This is the
default.
• 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. It specifies the maximum number of blocks read in
one I/O operation during a sequential scan. The total number of I/Os needed to perform a full
table scan or an index fast full scan depends on factors, such as the size of the segment, the
multiblock read count, and whether parallel execution is being utilized for the operation. As of
Oracle Database 10g, Release 2, the default value of this parameter is a value that corresponds to
the maximum I/O size that can be performed efficiently. This value is platform-dependent and is
1 MB for most platforms.

Oracle Database 11g: SQL Tuning Workshop 3 - 29


Controlling the Behavior of the Optimizer (continued)
Because the parameter is expressed in blocks, 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. Note
that if the number of sessions is extremely large, the multiblock read count value is decreased to
avoid the buffer cache getting flooded with too many table scan buffers. Even though the default
value may be a large value, the optimizer does not favor large plans if you do not set this
parameter. It would do so only if you explicitly set this parameter to a large value. Basically, if
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

this parameter is not set explicitly (or is set is 0), the optimizer uses a default value of 8 when
costing full table scans and index fast full scans. Online transaction processing (OLTP) and
batch environments typically have values in the range of 4 to 16 for this parameter. DSS and
data warehouse environments tend to benefit most from maximizing the value of this parameter.
The optimizer is more likely to select a full table scan over an index, if the value of this
parameter is high.
• PGA_AGGREGATE_TARGET specifies the target aggregate PGA memory available to all serverse
c e n
processes attached to the instance. Setting PGA_AGGREGATE_TARGET to a nonzero value i
e lThis
has
the effect of automatically setting the WORKAREA_SIZE_POLICY parameter to AUTO. b l
means that SQL working areas used by memory-intensive SQL operators (such f e rasasort, group-
by, hash-join, bitmap merge, and bitmap create) are automatically sized. t r a nAsnonzero value for
this parameter is the default since, unless you specify otherwise,othe n -system sets it to 20% of the
SGA or 10 MB, whichever is greater. Setting PGA_AGGREGATE_TARGETan
sets the WORKAREA_SIZE_POLICY parameter )to h
s
a dThis e ฺ means thatto SQL
0 automatically
work areas
s MANUAL.
u i
are sized using the *_AREA_SIZE parameters.
m i rฺe Thentsystem
G attempts to keep the amount of
private memory below the target specified
d g pฺ bytuthisdeparameter by adapting the size of the work
areas to private memory. When increasingSthe value of this parameter, you indirectly increase
the memory allotted to workb i a@ h is
areas. tConsequently, more memory-intensive operations are able to
run fully in memory ( j
and
a a lesssnumber
e of them work their way over to disk. When setting this
b i a to u
parameter, you
i o A should examine the total memory on your system that is available to the Oracle
ton and subtract the SGA. You can assign the remaining memory to
instance
n
e APGA_AGGREGATE_TARGET.
Jo s • STAR_TRANSFORMATION_ENABLED determines whether a cost-based query transformation
is applied to star queries. This optimization is explained in the lesson titled “Case Study: Star
Transformation.”
• The query optimizer manages the result cache mechanism depending on the settings of the
RESULT_CACHE_MODE parameter in the initialization parameter file. You can use this
parameter to determine whether or not the optimizer automatically sends the results of queries to
the result cache. The possible parameter values are MANUAL, and FORCE:
- When set to MANUAL (the default), you must specify, by using the RESULT_CACHE hint,
that a particular result is to be stored in the cache.
- When set to FORCE, all results are stored in the cache. For the FORCE setting, if the
statement contains a [NO_]RESULT_CACHE hint, the hint takes precedence over the
parameter setting.

Oracle Database 11g: SQL Tuning Workshop 3 - 30


Controlling the Behavior of the Optimizer (continued)
• The memory size allocated to the result cache depends on the memory size of the SGA as well
as the memory management system. You can change the memory allocated to the result cache
by setting the RESULT_CACHE_MAX_SIZE parameter. The result cache is disabled if you set
its value to 0. The value of this parameter is rounded to the largest multiple of 32 KB that is not
greater than the specified value. If the rounded value is 0, the feature is disabled.
• Use the RESULT_CACHE_MAX_RESULT parameter to specify the maximum amount of cache
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

memory that can be used by any single result. The default value is 5%, but you can specify any
percentage value between 1 and 100.
• 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. The
default value is 0, which implies that results using remote objects should not be cached. Setting
this parameter to a nonzero value can produce stale answers, for example, if the remote table
used by a result is modified at the remote database.
ce nse
l e li
ra b
f e
a ns
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 3 - 31


Controlling the Behavior of the Optimizer

• OPTIMIZER_INDEX_CACHING
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• OPTIMIZER_INDEX_COST_ADJ
• OPTIMIZER_FEATURES_ENABLED
• OPTIMIZER_MODE: ALL_ROWS, FIRST_ROWS, FIRST_ROWS_n
• OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES
• OPTIMIZER_USE_SQL_PLAN_BASELINES n s e
• lice
OPTIMIZER_DYNAMIC_SAMPLING
a b le
• OPTIMIZER_USE_INVISIBLE_INDEXES s f er
n
• OPTIMIZER_USE_PENDING_STATISTICS n-tra
a no
h a s eฺ
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Controllingnthe
o io Behavior of the Optimizer (continued)
•A nt
OPTIMIZER_INDEX_CACHING: This parameter controls the costing of an index probe in
s e
Jo conjunction with a nested loop or an inlist iterator. The range of values 0 to 100 for
OPTIMIZER_INDEX_CACHING indicates percentage of index blocks in the buffer cache,
which modifies the optimizer’s assumptions about index caching for nested loops and inlist
iterators. 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. The
default for this parameter is 0, which results in default optimizer behavior. Use caution when
using this parameter because execution plans can change in favor of index caching.
• OPTIMIZER_INDEX_COST_ADJ lets you tune optimizer behavior for access path selection to
be more or less index friendly. That is, to make the optimizer more or less prone to selecting an
index access path over a full table scan. The range of values is 1 to 10000. The default for this
parameter is 100 percent, at which the optimizer evaluates index access paths at the regular cost.
Any other value makes the optimizer evaluate the access path at that percentage of the regular
cost. For example, a setting of 50 makes the index access path look half as expensive as normal.
• OPTIMIZER_FEATURES_ENABLED acts as an umbrella parameter for enabling a series of
optimizer features based on an Oracle release number.

Oracle Database 11g: SQL Tuning Workshop 3 - 32


Controlling the Behavior of the Optimizer (continued)
For example, if you upgrade your database from release 10.1 to release 11.1, but you want
to keep the release 10.1 optimizer behavior, you can do so by setting this parameter to
10.1.0. At a later time, you can try the enhancements introduced in releases up to and
including release 11.1 by setting the parameter to 11.1.0.6. However, 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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

execution plan changes, consider using SQL plan management instead.


• OPTIMIZER_MODE establishes the default behavior for selecting an optimization approach for
either the instance or your session. 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). This is the default value.
- FIRST_ROWS_n: The optimizer uses a cost-based approach, regardless of the presence of n s e
statistics, and optimizes with a goal of best response time to return the first n numberlic ofe
rows; n can equal 1, 10, 100, or 1000. bl e
- FIRST_ROWS: The optimizer uses a mix of cost and heuristics to findsafe r a
best plan for fast
delivery of the first few rows. Using heuristics sometimes leads tthe n
raquery optimizer to
generate a plan with a cost that is significantly larger than the n -
o cost of a plan without
applying the heuristic. FIRST_ROWS is available for a n
stability; use FIRST_ROWS_n instead. h a s backward
e ฺ compatibility and plan

e s ) enablesu id or disables the automatic


• OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES ฺ
iasr welleasnttheGgeneration of SQL plan baselines for
recognition of repeatable SQL statements, ฺ m
such statements. d gp Stud
b i
• OPTIMIZER_USE_SQL_PLAN_BASELINES a@ this enables or disables the use of SQL plan
baselines stored in j a
( Managements e
b i aSQL t o u Base. When enabled, the optimizer looks for a SQL plan
baseline for A the SQL statement being compiled. If one is found in SQL Management Base, the
i o
n costs each of the baseline plans and picks one with the lowest cost.
optimizer
•A nto
OPTIMIZER_DYNAMIC_SAMPLING controls the level of dynamic sampling performed by the
e
Jos optimizer. If OPTIMIZER_FEATURES_ENABLE is set to:
- 10.0.0 or later, the default value is 2
- 9.2.0, the default value is 1
- 9.0.1 or earlier, the default value is 0
• OPTIMIZER_USE_INVISIBLE_INDEXES enables or disables the use of invisible indexes.
• OPTIMIZER_USE_PENDING_STATISTICS specifies whether or not the optimizer uses
pending statistics when compiling SQL statements.
Note: Invisible indexes, pending statistics, and dynamic sampling are discussed later in this course.

Oracle Database 11g: SQL Tuning Workshop 3 - 33


Optimizer Features and Oracle Database Releases

OPTIMIZER_FEATURES_ENABLED
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Features 9.0.0 to 9.2.0 10.1.0 to 10.1.0.5 10.2.0 to 10.2.0.2 11.1.0.6


Index fast full scan
Consideration of bitmap access to paths for tables with
only B-tree indexes
Complex view merging
Peeking into user-defined bind variables
Index joins
Dynamic sampling
Query rewrite enables
n s e
ice
el
Skip unusable indexes
Automatically compute index statistics as part of creation
a b l
Cost-based query transformations
fe r
Allow rewrites with multiple MVs and/or base tables
a n s
Adaptive cursor sharing
n - tr
o
Use extended statistics to estimate selectivity

s an
Use native implementation for full outer joins

) h a deฺ
Partition pruning using join filtering

r ฺ e s Gui
mi dent
Group by placement optimization
Null aware antijoins
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
n io and Oracle Database Releases
Optimizer Features
o
A nt
OPTIMIZER_FEATURES_ENABLED acts as an umbrella parameter for enabling a series of
s e
Jo optimizer features based on an Oracle release number. 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.

Oracle Database 11g: SQL Tuning Workshop 3 - 34


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 3 - 35


Practice 3: Overview

This practice covers exploring a trace file to understand the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

optimizer’s decisions.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 3 - 36


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Optimizer Operators

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Describe most of the SQL operators


• List the possible access paths
• Explain how join operations are performed

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Objectives nio
nto helps you understand the execution plans.
ThisAlesson
e
Jos

Oracle Database 11g: SQL Tuning Workshop 4 - 2


Row Source Operations

• Unary operations
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Access Path
• Binary operations
– Joins
• N-ary operations

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Row Source n o
iOperations
o
t is an iterative control structure that processes a set of rows in an iterated manner and
nsource
A
A row
e
Josproduces a row set.
You can classify row sources as follows:
• Unary operations: Operations that act on only one input, such as an access path
• Binary operations: Operations that act on two inputs, such as joins
• N-ary operations: Operations that act on several inputs, such as a relational operator
Access paths are ways in which data is retrieved from the database. In general, index access paths
should be used for statements that retrieve a small subset of table rows, while full scans are more
efficient when accessing a large portion of the table. Online transaction processing (OLTP)
applications, which consist of short-running SQL statements with high selectivity, are often
characterized by the use of index access paths. Decision support systems (DSS), on the other hand,
tend to use partitioned tables and perform full scans of the relevant partitions.

Oracle Database 11g: SQL Tuning Workshop 4 - 3


Main Structures and Access Paths

Structures
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Access Paths
1. Full Table Scan
Tables
2. Rowid Scan
3. Sample Table Scan
4. Index Scan (Unique)
5. Index Scan (Range)
n s e
i ce
6. Index Scan (Full)
b l el
er
7. Index Scan (Fast Full) a
Indexes sf
ran
8. Index Scan (Skip)
n - t
9. Index Scan o
n (Index Join)
a
sBitmap
10. Using
) h a
i d eฺ Indexes
i r
11. ฺesCombiningt G u Bitmap Indexes
g p ฺm uden
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
Abia to
n
Main Structures
o io and Access Paths
AnyArownt can be located and retrieved with one of the methods mentioned in the slide.
e
JosIn general, index access paths should be used for statements that retrieve a small subset of table rows,
while full scans are more efficient when accessing a large portion of the table. To decide on the
alternative, the optimizer gives each alternative (execution plan) a cost. The one with the lower cost
is elected.
There are special types of table access paths including clusters, index-organized tables, and
partitions, which have not been mentioned in the slide.
Clusters are an optional method of storing table data. A cluster is a group of tables that share the
same data blocks because they share common columns and are often used together. For example, the
EMP and DEPT table share the DEPTNO column. When you cluster the EMP and DEPT tables, Oracle
physically stores all rows for each department from both the EMP and DEPT tables in the same data
blocks.
Hash clusters are single-table clusters in which rows with the same hash-key values are stored
together. Oracle uses a mathematical hash function to select the location of a row within the cluster.
All rows with the same key value are stored together on disk.
The special types of access paths are discussed later in this course.

Oracle Database 11g: SQL Tuning Workshop 4 - 4


Full Table Scan

• Performs multiblock reads


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

(here DB_FILE_MULTIBLOCK_READ_COUNT = 4)
• Reads all formatted blocks below the high-water mark HWM
• May filter rows
B B B B ... B B B B B
• Faster than index
range scans for large amount of data e
n s
i ce
el
select * from emp where ename='King';

a b l
---------------------------------------------------------------
fer
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
a n s
---------------------------------------------------------------
n -tr
o
an
| 0 | SELECT STATEMENT | | 1 | 37 | 3 (0)|
|* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 3 (0)|
h s
a deฺ
---------------------------------------------------------------

e )
s Gui
Predicate Information (identified by operation id):
r ฺ
mi dent
---------------------------------------------------
1 - filter("ENAME"='King')
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
n
Full Table Scan
o io
t scan sequentially reads all rows from a table and filters out those that do not meet the
ntable
A
A full
e
Josselection criteria. During a full table scan, 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. Each block is read only
once. The high-water mark indicates the amount of used space, or space that was formatted to receive
data. Each row is examined to determine whether it satisfies the statement’s WHERE clause using the
applicable filter conditions specified in the query.
You can see the filter conditions in the “Predicate Information” section of the explain plan. The filter
to be applied returns only rows where EMP.ENAME='King'.
Because a full table scan reads all the formatted blocks in a table, it reads blocks that are physically
adjacent to each other. This means that performance benefits can be reaped by utilizing input/output
(I/O) calls that read multiple blocks at the same time. 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.
Note: In Oracle 6, full table scans flooded the buffer cache because there was no difference in the
way blocks were handled between full table scans and reads by the index. Since Oracle V7, blocks
read by full table scans do not flood the buffer cache because the space they are allowed to occupy is
restricted to a small amount. Currently, hash joins and parallel query rely heavily on full table scans
for efficiency.

Oracle Database 11g: SQL Tuning Workshop 4 - 5


Full Table Scans: Use Cases

• No suitable index
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Low selectivity filters (or no filters)


• Small table
• High degree of parallelism
• Full table scan hint: FULL (<table name>)
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io Use Cases
Full Table Scans:
o
TheA nt uses a full table scan in any of the following cases:
optimizer
e
Jos • aLack of index: If the query is unable to use any existing indexes, it uses a full table scan (unless
ROWID filter or a cluster access path is available). For example, if there is a function used on
the indexed column in the query, the optimizer cannot use the index and instead uses a full table
scan. If you need to use the index for case-independent searches, either do not permit mixed-
case data in the search columns or create a function-based index, such as
UPPER(last_name) on the search column.
• Large amount of data (low selectivity): If the optimizer thinks that the query accesses enough
blocks in the table, it may use a full table scan even though indexes might be available.
• Small table: If a table contains less than DB_FILE_MULTIBLOCK_READ_COUNT blocks
under the high-water mark, a full table scan might be cheaper than an index range scan,
regardless of the fraction of tables being accessed or indexes present.
• High degree of parallelism: A high degree of parallelism for a table skews the optimizer
towards full table scans over range scans. Examine the DEGREE column in ALL_TABLES for
the table to determine the degree of parallelism.
• Full table scan hints: Use the FULL(table alias) hint to instruct the optimizer to use a
full table scan.

Oracle Database 11g: SQL Tuning Workshop 4 - 6


ROWID Scan

select * from scott.emp where rowid='AAAQ+LAAEAAAAAfAAJ';


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 1|
| 1 | TABLE ACCESS BY USER ROWID| EMP | 1 | 37 | 1|
------------------------------------------------------------------

n s e
i ce
el
1034,JF,V,10,…

2145,MH,V,20,…
B B . B B
s
B
fe r a b l
tra n
-
Block 6959–Row 2

o n
Row migration
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio
ROWID Scan
to
TheA n of a row specifies the data file and data block containing the row and the location of the
rowid
e
Josrow in that block. 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.
To access a table by rowid, the system first obtains the rowids of the selected rows, either from the
statement’s WHERE clause or through an index scan of one or more of the table’s indexes. The
system then locates each selected row in the table based on its rowid.
Mostly, the optimizer uses rowids after retrieving them from an index (See the “Index Scans”
slides.). The table access might be required for columns in the statement that are not present in the
index. A table access by rowid does not need to follow every index scan. If the index contains all the
columns needed for the statement, table access by rowid might not occur.
Rowids are the system’s internal representation of where data is stored. Accessing data based on
position is not recommended because rows can move around due to row migration and chaining, and
also after export and import.
Note: Due to row migration, a rowid can sometimes point to an address different from the actual row
location, resulting in more than one block being accessed to locate a row. For example, an update to
a row may cause the row to be placed in another block with a pointer in the original block. The
rowid, however, still has only the address of the original block.

Oracle Database 11g: SQL Tuning Workshop 4 - 7


Sample Table Scans
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM emp SAMPLE BLOCK (10) [SEED (1)];

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 99 | 2 (0)|
| 1 | TABLE ACCESS SAMPLE| EMP | 4 | 99 | 2 (0)|
---------------------------------------------------------------------

n s e
i ce
b l el
fer a
B B B B
a n sB
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Sample Table
o n ioScans
A nt table scan retrieves a random sample of data from a simple table or a complex SELECT
A sample
e
Josstatement, such as a statement involving joins and views. This access path is used when a statement’s
FROM clause includes the SAMPLE clause or the SAMPLE BLOCK clause. To perform a sample
table scan when sampling by rows with the SAMPLE clause, the system reads a specified percentage
of rows in the table. To perform a sample table scan when sampling by blocks with the SAMPLE
BLOCK clause, the system reads a specified percentage of table blocks.
• SAMPLE option: To perform a sample table scan when sampling by rows, 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.
• SAMPLE BLOCK option: To perform a sample table scan when sampling by blocks, 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.
The sample percent is a number specifying the percentage of the total row or block count to be
included in the sample. The sample value must be in the [0.000001 , 99.999999] range.
This percentage indicates the probability of each row, or each cluster of rows in the case of block
sampling, being selected as part of the sample. It does not mean that the database retrieves exactly
sample_percent of the rows of table.

Oracle Database 11g: SQL Tuning Workshop 4 - 8


Sample Table Scans (continued)
• SEED seed_value: Specify this clause to instruct the database to attempt to return the same
sample from one execution to the next. seed_value must be an integer between 0 and
4294967295. If you omit this clause, the resulting sample changes from one execution to the
next.
In row sampling, more blocks need to be accessed given a particular sample size, but the results are
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

usually more accurate. Block samples are less costly, but may be inaccurate; more so with smaller
samples.
Note: Block sampling is possible only during full table scans or index fast full scans. If a more
efficient execution path exists, Oracle Database does not perform block sampling. If you want to
guarantee block sampling for a particular table or index, use the FULL or INDEX_FFS hint.

n s e
i ce
b l el
fer a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 9


Indexes: Overview

Index storage techniques:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• B*-tree indexes: The default and the most common


– Normal
– Function based: Precomputed value of a function or
expression
– Index-organized table (IOT)
n s e
– Bitmap indexes
i ce
– Cluster indexes: Defined specifically for cluster b l el
fe r a
• Index attributes:
a n s
o n -tr – Key compression
s an – Reverse key
) h a deฺ
r ฺ e s Gui – Ascending, descending
• Domain indexes: Specific p ฺ mtoi andapplication
e nt or cartridge
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i oA
Indexes: Overview
An A ntois an optional database object that is logically and physically independent of the table data.
index
e
JosBeing independent structures, they require storage space. Just as the index of a book helps you locate
information fast, an Oracle Database index provides a faster access path to table data. The Oracle
Database may use an index to access data that is required by a SQL statement, or it may use indexes
to enforce integrity constraints. The system automatically maintains indexes when the related data
changes. You can create and drop indexes at any time. If you drop an index, all applications continue
to work. However, access to previously indexed data might be slower. Indexes can be unique or
nonunique.
A composite index, also called a concatenated index, is an index that you create on multiple columns
(up to 32) in a table. Columns in a composite index can appear in any order and need not be adjacent
in the table.
For standard indexes, the database uses B*-tree indexes that are balanced to equalize access times.
B*-tree indexes can be normal, reverse key, descending, or function based.
• B*-tree indexes: They are by far the most common indexes. Similar in construct to a binary
tree, B*-tree indexes provide fast access, by key, to an individual row or range of rows, normally
requiring few reads to find the correct row. However, the “B” in “B*-tree” does not stand for
“binary,” but rather for “balanced.”

Oracle Database 11g: SQL Tuning Workshop 4 - 10


Indexes: Overview (continued)
• 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.
• Reverse key indexes: These are B*-tree indexes whereby the bytes in the key are reversed.
Reverse key indexes can be used to obtain a more even distribution of index entries throughout
an index that is populated with increasing values. For example, if you use a sequence to generate
a primary key, the sequence generate values, such as 987500, 987501, 987502, and so on. With a
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

reverse key index, the database logically indexes 005789, 105789, 205789, and so on, instead of
987500, 987501, and 987502. Because these reverse keys are now likely to be placed in different
locations this can reduce contention for particular blocks that may otherwise be targets for
contention. However, only equality predicates can benefit from these indexes.
• 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. The prefix is built on the leading columns of
the concatenated index and has many repeating values. The suffix is built on the trailing n s e
columns in the index key and is the unique component of the index entry within the prefix. l e
icThis
is not compression in the same manner that ZIP files are compressed; rather, thisa isbane
l optional
compression that removes redundancies from concatenated (multicolumn) s fe r
indexes.
• Function-based indexes: These are B*-tree or bitmap indexes that store n
tra the computed result of
a function on a row’s column or columns, and not the column data n -
o itself. You can consider them
as indexes on a virtual (derived or hidden) column. In other a n
h a s thiswords,
e ฺ it is a column that is not
physically stored in the table. You can gather statistics
s ) on
u i d virtual column.
• Index-organized tables: These are tables stored
m i rฺe innatB*-tree
G structure. While rows of data in a
heap organized table are stored in anp
g ฺ udefashion (data goes wherever there is available
unorganized
d
space), data in an IOT is stored and sortedS byt a primary key. IOTs behave like regular tables as
@ s
j a ia
far as your application isbconcerned.
e thi
• Bitmap indexes:a In (a normaluB*-tree,
s there is a one-to-one relationship between an index entry
b i o
t entry points to a row. With bitmap indexes, a single index entry uses
i o A is, an index
and a row, that
a bitmap
t o n to point to many rows simultaneously. They are appropriate for repetitive data (data
n
Awith few distinct values relative to the total number of rows in the table) that is mostly read-
s e only. Bitmap indexes should never be considered in an OLTP database for concurrency-related
Jo issues.
• Bitmap join indexes: A bitmap join index is a bitmap index for the join of two or more tables.
A bitmap join index can be used to avoid actual joins of tables, or to greatly reduce the volume
of data that must be joined, by performing restrictions in advance. Queries using bitmap join
indexes can be sped up using bit-wise operations.
• Application domain indexes: These are indexes you build with packages and store yourself,
either in the database or even outside the database. You tell the optimizer how selective your
index is and how costly it is to execute, and the optimizer decides whether or not to use your
index based on that information.

Oracle Database 11g: SQL Tuning Workshop 4 - 11


Normal B*-tree Indexes

Index entry
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Root

Branch n s e
li ce
b l e
Index entry header
e r a
a
Key column n sflength
Leaf o n -tr
a nKey column value
h a s rowid eฺ
)
s Gu i d
Tablei r ฺedata n t
retrieved by using rowid
ฺ m e
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Normal B*-treei o Indexes
A
Each nto index has a root block as a starting point. Depending on the number of entries, there are
B*-tree
e
Josmultiple branch blocks that can have multiple leaf blocks. 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).
Indexes are always balanced, and they grow from the top down. In certain situations, the balancing
algorithm can cause the B*-tree height to increase unnecessarily. It is possible to reorganize indexes.
This is done by the ALTER INDEX … REBUILD | COALESCE command.
The internal structure of a B*-tree index allows rapid access to the indexed values. The system can
directly access rows after it has retrieved the address (the ROWID) from the index leaf blocks.
Note: The maximum size of a single index entry is approximately one-half of the data block size.

Oracle Database 11g: SQL Tuning Workshop 4 - 12


Index Scans

Types of index scans:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Unique
• Min/Max B-Tree index IX_EMP

• Range (Descending)
• Skip
• Full and fast full n s e
i ce
• Index join
b l el
fe r a
a n s
B : block n -tr
o
s an
Table EMP
) h a deฺ
B irฺe
s B Gui B
B B
p ฺ m dent B
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Index Scans
on io A
An A n t
index scan can be one of the following types:
s e
Jo A row is retrieved by traversing the index, using the indexed column values specified by the
statement’s WHERE clause. An index scan retrieves data from an index based on the value of one or
more columns in the index. To perform an index scan, the system searches the index for the indexed
column values accessed by the statement. If the statement accesses only columns of the index, the
system reads the indexed column values directly from the index, rather than from the table.
The index contains not only the indexed value, but also the rowids of rows in the table that have the
value. Therefore, if the statement accesses other columns in addition to the indexed columns, the
system can find the rows in the table by using either a table access by rowid or a cluster scan.
Note: The graphic shows a case where four rows are retrieved from the table using their rowids
obtained by the index scan.

Oracle Database 11g: SQL Tuning Workshop 4 - 13


Index Unique Scan
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

index UNIQUE Scan PK_EMP

create unique index PK_EMP on EMP(empno)

select * from emp where empno = 9999;


n s e
i ce
el
--------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost|
a b l
--------------------------------------------------------------------
fer
| 0 | SELECT STATEMENT | | 1 | 37 | 1|
a n s
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 1|
n -tr
o
| 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | |
s an0|

) h a deฺ
--------------------------------------------------------------------

r ฺ e s Gui
Predicate Information (identified by operation id):

mi dent
---------------------------------------------------
2 - access("EMPNO"=9999)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Unique
o n ioScan
An A nt unique scan returns, at most, a single ROWID. The system performs a unique scan if a
index
e
Josstatement contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row
is accessed. This access path is used when all the columns of a unique (B*-tree) index are specified
with equality conditions.
Key values and ROWIDs are obtained from the index, and table rows are obtained using ROWIDs.
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.”). Here the
system accesses only matching rows for which EMPNO=9999.
Note: Filter conditions filter rows after the fetch operation and output the filtered rows.

Oracle Database 11g: SQL Tuning Workshop 4 - 14


Index Range Scan

Index Range SCAN I_DEPTNO


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index I_DEPTNO on EMP(deptno);

select /*+ INDEX(EMP I_DEPTNO) */ *


from emp where deptno = 10 and sal > 1000;

n s e
ce
---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost
eli
---------------------------------------------------------------------
a b l
| 0 | SELECT STATEMENT | | 3 | 261 | 2 fer
a n s
tr
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 3 | 261 | 2
| 2 | INDEX RANGE SCAN | I_DEPTNO | 3 | | 1
o n -
an
---------------------------------------------------------------------
s
Predicate Information (identified by operation id):
) h a deฺ
r ฺ e s Gui
---------------------------------------------------

mi dent
1 - filter("SAL">1000)
2 - access("DEPTNO"=10)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Range n o
iScan
o
nt range scan is a common operation for accessing selective data. It can be bounded (on both
An A
index
e or unbounded (on one or both sides). Data is returned in the ascending order of index columns.
Jossides)
Multiple rows with identical values are sorted in the ascending order by ROWID.
The optimizer uses a range scan when it finds one or more leading columns of an index specified in
conditions (the WHERE clause), such as col1 = :b1, col1 < :b1, col1 > :b1, and any
combination of the preceding conditions.
Wildcard searches (col1 like '%ASD') should not be in a leading position, as this does not
result in a range scan.
Range scans can use unique or nonunique indexes. 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.
An index range scan descending is identical to an index range scan, except that the data is returned in
the descending order. The optimizer uses index range scan descending when an order by descending
clause can be satisfied by an index.
In the example in the slide, using index I_DEPTNO, the system accesses rows for which
EMP.DEPTNO=10. It gets their ROWIDs, fetches other columns from the EMP table, and finally,
applies the EMP.SAL >1000 filter from these fetched rows to output the final result.

Oracle Database 11g: SQL Tuning Workshop 4 - 15


Index Range Scan: Descending

Index Range SCAN IDX


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index IDX on EMP(deptno);

select * from emp where deptno>20 order by deptno desc;

n s e
ce
---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
eli
---------------------------------------------------------------------
a b l
| 0 | SELECT STATEMENT | | 6 | 522 | 2| fer
a n s
tr
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 6 | 522 | 2|
| 2 | INDEX RANGE SCAN DESCENDING| IDX | 6 | | 1|
o n -
an
---------------------------------------------------------------------
s
Predicate Information (identified by operation id):
) h a deฺ
r ฺ e s Gui
---------------------------------------------------

mi dent
2 - access("DEPTNO">20)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Range n o Descending
iScan:
o
nt to index range scans in ascending order, which are described in the previous slide, the
A
In addition
e
Jossystem is also able to scan indexes in the reverse order as illustrated by the graphic in the slide.
The example retrieves rows from the EMP table by descending order on the DEPTNO column. You
can see the DESCENDING operation row source for ID 2 in the execution plan that materialized this
type of index scans.
Note: By default an index range scan is done in the ascending order.

Oracle Database 11g: SQL Tuning Workshop 4 - 16


Descending Index Range Scan

Index Range SCAN IX_D


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index IX_D on EMP(deptno desc);

select * from emp where deptno <30;

---------------------------------------------------------------------
n s e
ce
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
eli
| 0 | SELECT STATEMENT | | 9 | 333 | 2|
a b l
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 9 | 333 | 2| fer
a n s
tr
| 2 | INDEX RANGE SCAN | IX_D | 1 | | 1|
---------------------------------------------------------------------
o n -
Predicate Information (identified by operation id):
s an
---------------------------------------------------
) h a deฺ
r ฺ e s Gui
2 - access(SYS_OP_DESCEND("DEPTNO")>HEXTORAW('3EE0FF') )

mi dent
filter(SYS_OP_UNDESCEND(SYS_OP_DESCEND("DEPTNO"))<30)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Descending n o Range Scan
iIndex
o
nt index range scan is identical to an index range scan, except that the data is returned in
A
A descending
e
Josdescending order. Descending indexes allow for data to be sorted from “big to small” (descending)
instead of “small to big” (ascending) in the index structure. Usually, this scan is used when ordering
data in a descending order to return the most recent data first, or when seeking a value less than a
specified value as in the example in the slide.
The optimizer uses descending index range scan when an order by descending clause can be satisfied
by a descending index.
The INDEX_DESC(table_alias index_name) hint can be used to force this access path if
possible.
Note: The system treats descending indexes as function-based indexes. 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.

Oracle Database 11g: SQL Tuning Workshop 4 - 17


Index Range Scan: Function-Based

Index Range SCAN IX_FBI


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index IX_FBI on EMP(UPPER(ename));

select * from emp where upper(ENAME) like 'A%';

---------------------------------------------------------------------
n s e
ce
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
eli
| 0 | SELECT STATEMENT | | 1 | 37 | 2|
a b l
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 2| fer
a n s
tr
| 2 | INDEX RANGE SCAN | IX_FBI | 1 | | 1|
---------------------------------------------------------------------
o n -
Predicate Information (identified by operation id):
s an
---------------------------------------------------
) h a deฺ
2 - access(UPPER("ENAME") LIKE 'A%')
r ฺ e s Gui
mi dent
filter(UPPER("ENAME") LIKE 'A%')
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Range n o Function-Based
iScan:
nt o
e A
A function-based index can be stored as B*-tree or bitmap structures. These indexes include columns
s
Jo that are either transformed by a function, such as the UPPER function, or included in an expression,
such as col1 + col2. With a function-based index, you can store computation-intensive
expressions in the index.
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. This allows the system to bypass computing the value of the expression when processing
SELECT and DELETE statements. Therefore, a function-based index can be beneficial when
frequently-executed SQL statements include transformed columns, or columns in expressions, in a
WHERE or ORDER BY clause.
For example, function-based indexes defined with the UPPER(column_name) or
LOWER(column_name) keywords allow non-case-sensitive searches, such as shown in the slide.

Oracle Database 11g: SQL Tuning Workshop 4 - 18


Index Full Scan
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index I_DEPTNO on EMP(deptno);

select *
from emp
where sal > 1000 and deptno is not null
index Full Scan I_DEPTNO
order by deptno;

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |Cost|
n s e
i ce
el
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 444 | 2|
a b l
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 12 | 444 | 2|
fer
| 2 | INDEX FULL SCAN | I_DEPTNO | 14 | | 1|
a n s
---------------------------------------------------------------------
n -tr
o
an
Predicate Information (identified by operation id):
---------------------------------------------------
h s
a deฺ
1 - filter("SAL">1000)
e )
s Gui
2 - filter("DEPTNO" IS NOT NULL)
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Index Full Scan
o io
t is available if a predicate references one of the columns in the index. The predicate does
nscan
A
A full
e need to be an index driver (leading column). A full scan is also available when there is no
Josnot
predicate, if both the conditions are met:
• All the columns in the table referenced in the query are included in the index.
• At least one of the index columns is not null.
A full scan can be used to eliminate a sort operation because the data is ordered by the index key.
Note: An index full scan reads index using single-block input/output (I/O) (unlike a fast full index
scan).

Oracle Database 11g: SQL Tuning Workshop 4 - 19


Index Fast Full Scan

LEGEND:
db_file_multiblock_read_count = 4 SH=segment header
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

R=root block
multiblock read multiblock read B=branch block
L=leaf block

SH R L L L B L L B ... L
discard discard discard

create index I_DEPTNO on EMP(deptno);


select /*+ INDEX_FFS(EMP I_DEPTNO) */ deptno from emp n s e
i ce
el
where deptno is not null;
----------------------------------------------------------------
a b l
| Id | Operation | Name | Rows | Bytes | Cost |
fe r
----------------------------------------------------------------
a n s
| 0 | SELECT STATEMENT | | 14 | 42 | 2|
n -tr
o
| 1 | INDEX FAST FULL SCAN| I_DEPTNO | 14 | 42 | 2|
s an
) h a deฺ
----------------------------------------------------------------

r ฺ e s Gui
Predicate Information (identified by operation id):

mi dent
---------------------------------------------------
1 - filter("DEPTNO" IS NOT NULL)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Fast n
o io Scan
Full
t full scans are an alternative to full table scans when the index contains all the columns that
nfast
A
Index
e needed for the query and at least one column in the index key has a NOT NULL constraint. A fast
Josare
full scan accesses the data in the index itself without accessing the table.
It cannot be used to eliminate a sort operation because the data is not ordered by the index key. It can
be used for the min/avg/sum aggregate functions. In this case, the optimizer must know that all
table rows are represented in the index; at least one NOT NULL column.
This operation reads the entire index using multiblock reads (unlike a full index scan). Fast full index
scans cannot be performed against bitmap indexes. A fast full scan is faster than a normal full index
scan because it can use multiblock I/O just as a table scan.
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.
Note: Index fast full scans are used against an index when it is rebuilt offline.

Oracle Database 11g: SQL Tuning Workshop 4 - 20


Index Skip Scan

SELECT * FROM employees WHERE age BETWEEN 20 AND 29


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Index on (GENDER, AGE)


R
B1 Min M10 B2

Min F16 F20 F26 F30 M10 M16 M20 M26 M30

n s e
F10 F16 F20 F26 F30 M10 M16 M20 M26 M30
lice
F11 F17 F21 F27 F31 M11 M17 M21 M27 M31
a b le
F12 F18 F22 F28 F32 M12 M18 r
M22 M28 feM32
F13 F19 F23 F29 F33 M13 M19 M23 ra
s
n M33
M29
F14 F24 F34 M14 n - t M34
F15 F25 F35 M15 noM24
a M25 M35
h a s eฺ
) i d
i r ฺ es t Gu
L1 L2 L3 L4
g pL5ฺm udeL6n L7 L8 L9 L10
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
io Ab
Index Skip n Scan
o
t scans improve index scans by skipping blocks that could never contain keys matching the
nskip
Index
e A
Josscanning
filter column values. Scanning index blocks is often faster than scanning table data blocks. Skip
can happen when the initial (leading) column of the composite index is not specified in a
query. Suppose that there is a concatenated index on the GENDER and AGE columns in the
EMPLOYEES table. This example illustrates how skip scanning is processed to answer the query in
the slide.
The system starts from the root of the index [R] and proceeds to the left branch block [B1]. From
there, the system identifies a first entry to be F16, goes to the left leaf [L1], and starts to scan it
because it could contain A25 (that is, where the “gender” is before “F” in the alphabet). The server
identifies that this is not possible because the first entry is F10. It is thus not possible to find an entry
such as A25 in this leaf, so it can be skipped.
Backtracking to the first branch block [B1], the server identifies that the next subtree (F16) does not
need to be scanned because the next entry in [B1] is F20. 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.
Returning to [B1], the server finds that the next two entries have a common prefix of F2. This
identifies possible subtrees to scan. The system knows that these subtrees are ordered by age.

Oracle Database 11g: SQL Tuning Workshop 4 - 21


Index Skip Scan (continued)
So the third and fourth leaf blocks [L3–L4] are scanned and some values are retrieved. By looking at
the fourth entry in the first branch block [B1], the system determines that it is no longer possible to
find an F2x entry. Thus, it is not necessary to scan that subtree [L5].
The same process continues with the right part of this index. Note that out of a total of 10 leaf blocks,
only five are scanned.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fe ra
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 22


Index Skip Scan: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Index on (DEPTNO, SAL)

create index IX_SS on EMP(DEPTNO,SAL);

select /*+ index_ss(EMP IX_SS) */ * from emp where SAL < 1500;

--------------------------------------------------------------------- n s e
i ce
el
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
a b l
| 0 | SELECT STATEMENT | | 6 | 222 | 6 |
fer
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 6 | 222 | 6 |
a n s
| 2 | INDEX SKIP SCAN | IX_SS | 6 | | 5 |
n -tr
o
an
---------------------------------------------------------------------
s
Predicate Information (identified by operation id):
) h a deฺ
r ฺ e s Gui
---------------------------------------------------

mi dent
2 - access("SAL"<1500)
filter("SAL"<1500)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index Skip n
o io Example
Scan:
TheA nt in the slide finds employees who have salary less than 1500 using an index skip scan.
example
e
JosIt is assumed that there is a concatenated index on the DEPTNO and SAL columns.
As you can see, the query does not have a predicate on the DEPTNO leading column. This leading
column only has some discrete values, that is, 10, 20 and 30.
Skip scanning lets a composite index be split logically into smaller subindexes. The number of
logical subindexes is determined by the number of distinct values in the initial column.
The system pretends that the index is really three little index structures hidden inside one big one. In
the example, it is three index structures:
• where deptno = 10
• where deptno = 20
• where deptno = 30
The output is ordered by 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.

Oracle Database 11g: SQL Tuning Workshop 4 - 23


Index Join Scan

alter table emp modify (SAL not null, ENAME not null);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create index I_ENAME on EMP(ename);


create index I_SAL on EMP(sal);

select /*+ INDEX_JOIN(e) */ ename, sal from emp e;

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
---------------------------------------------------------------------
n s e
ce
| 0 | SELECT STATEMENT | | 14 | 140 |
| 1 | VIEW | index$_join$_001 | 14 | 140 |
el i
| 2 | HASH JOIN | | | |
a b l
fe r
| 3 | INDEX FAST FULL SCAN| IX_SS | 14 | 140 |
a n s
-tr
| 4 | INDEX FAST FULL SCAN| I_ENAME | 14 | 140 |

n o n --------------------------------------------------------------------

s aid):
Predicate Information (identified by operation
) h a deฺ
---------------------------------------------------
r ฺ e s Gui
mi dent
2 - access(ROWID=ROWID)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
Index Join n i
ScanoA
An A ntojoin is a hash join of several indexes that together contain all the table columns that are
index
e
Josreferenced in the query. If an index join is used, no table access is needed because all the relevant
column values can be retrieved from the indexes. An index join cannot be used to eliminate a sort
operation.
The index join is not a real join operation (note that the example is a single table query), but is built
using index accesses followed by a join operation on rowid. The example in the slide assumes that
you have two separate indexes on the ENAME and SAL columns of the EMP table.
Note: You can specify an index join with the INDEX_JOIN hint as shown in the example.

Oracle Database 11g: SQL Tuning Workshop 4 - 24


The AND-EQUAL Operation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT /*+ AND_EQUAL(emp isal ijob) */ *


FROM emp
WHERE sal=1000 and job='CLERK';

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 87 | 2 |
n s e
i ce
el
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 87 | 2 |
| 2 | AND-EQUAL | | | | |
a b l
| 3 | INDEX RANGE SCAN | ISAL | 1 | | 1 |
fer
| 4 | INDEX RANGE SCAN | IJOB | 4 | | 1 |
a n s
---------------------------------------------------------------------
n -tr
o
an
Predicate Information (identified by operation id):
---------------------------------------------------
h s
a deฺ
1 - filter("SAL"=1000 AND "JOB"='CLERK')
e )
s Gui
3 - access("SAL"=1000)
r ฺ
4 - access("JOB"='CLERK')
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
The AND-EQUAL
o n io Operation
TheA nt
AND-EQUAL operation allows the system to merge the scans on several single-column indexes
s e
Jo before accessing the table.
This is illustrated using the example in the slide.
Basically, this operation works when there are two (or more, up to five in theory) equality conditions
in the WHERE clause, and each column is indexed with a single column index. The idea is then to
search indexes for matching ROWIDs. If a ROWID occurs in all relevant indexes, it satisfies the
conditions, and the matching row from the table can be retrieved.
Note: To force the AND-EQUAL operation, you used the AND_EQUAL hint.

Oracle Database 11g: SQL Tuning Workshop 4 - 25


B*-tree Indexes and Nulls
create table nulltest ( col1 number, col2 number not null);
create index nullind1 on nulltest (col1);
create index notnullind2 on nulltest (col2);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select /*+ index(t nullind1) */ col1 from nulltest t;


--------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
--------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 11 (0)|
| 1 | TABLE ACCESS FULL| NULLTEST | 10000 | 126K| 11 (0)|

select col1 from nulltest t where col1=10;


-------------------------------------------------------------------
n s e
i ce
el
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
-------------------------------------------------------------------
a b l
| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)|
fer
| 1 | INDEX RANGE SCAN| NULLIND1 | 1 | 13 | 1 (0)|
a n s
n -tr
select /*+ index(t notnullind2) */ col2 from nulltest t;
o
an
---------------------------------------------------------------------
s
| Id | Operation | Name
a deฺ
| Rows | Bytes | Cost (%CPU)|
) h
r ฺ e s Gui
---------------------------------------------------------------------

mi dent
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)|
|
p ฺ
1 | INDEX FULL SCAN | NOTNULLIND2 |
g Stu
1 | 13 | 2 (0)|
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
n
B*-tree Indexes
o io and Nulls
an
It isA
t
common mistake to forget about nulls when dealing with B*-tree indexes. Single-column
e
JosB*-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.
In the slide example, you create a table containing a nullable column called COL1, and COL2, which
cannot have null values. One index is built on top of each column.
The first query, retrieves all COL1 values. Because COL1 is nullable, the index cannot be used
without a predicate. Hinting the index on COL1 (nullind1) to force index utilization makes no
difference because COL1 is nullable. Because you only search for COL1 values, there is no need to
read the table itself.
However, with the second query, the effect of the predicate against COL1 is to eliminate nulls from
the data returned from the column. This allows the index to be used.
The third query can directly use the index because the corresponding column is declared NOT NULL
at table-creation time.
Note: The index could also be used by forcing the column to return only NOT NULL values using
the COL1 IS NOT NULL predicate.

Oracle Database 11g: SQL Tuning Workshop 4 - 26


Using Indexes: Considering Nullable Columns

Column Null? CREATE UNIQUE INDEX person_ssn_ix


ON person(ssn);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SSN Y
FNAME Y SELECT COUNT(*) FROM person;
LNAME N
SELECT STATEMENT |
SORT AGGREGATE |
PERSON TABLE ACCESS FULL| PERSON
DROP INDEX person_ssn_ix;
n s e
i ce
ALTER TABLE person ADD CONSTRAINT pk_ssn
b l el
f e rColumn Null? PRIMARY KEY (ssn); a
SSN N s
an FROM
SELECT /*+ INDEX(person) */ COUNT(*)
- t r
on
FNAME Y person;
LNAME N
a n
SELECT STATEMENT h a s |eฺ
) i d
SORT AGGREGATE
i r ฺes FULL t G u |
PERSON INDEX
p ฺ m den
FAST SCAN| PK_SSN
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Using Indexes:n i o Considering Nullable Columns
t o
Some
e Anqueries look as if they should use an index to compute a simple count of rows in the table. This
Josis typically more efficient that scanning the table. But the index to be used must not be built on a
column that can contain null values. Single-column B*-tree indexes never store null values, so the
rows are not represented in the index, and thus, do not contribute to the COUNT being computed, for
example.
In the example in the slide, there is a unique index on the SSN column of the PERSON table. The
SSN column is defined as allowing null values, and creating a unique index on it does nothing to
change that. This index is not used when executing the count query in the slide. Any rows with null
for SSN are not represented in the index, so the count across the index is not necessarily accurate.
This is one reason why it is better to create a primary key rather than a unique index. A primary key
column cannot contain null values. In the slide, after the unique index is dropped in the place of
designating a primary key, the index is used to compute the row count.
Note: The PRIMARY KEY constraints combine a NOT NULL constraint and a unique constraint in a
single declaration.

Oracle Database 11g: SQL Tuning Workshop 4 - 27


Index-Organized Tables

Indexed Accessing
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

access on table index-organized table

n s e
i ce
el
ROWID
a b l
fe r
a n s
o n -tr
a n Nonkey columns
h a s eฺ
e s ) u id Key column

ir ent G
p ฺ m d
Row header
d g S t u
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n
Index-Organized
o io Tables
An A nt
index-organized table (IOT) is a table physically stored in a concatenated index structure. The
s e
Jo key• values (for the table and the B*-tree index) are stored in the same segment. An IOT contains:
Primary key values
• Other (nonkey) column values for the row
The B*-tree structure, which is based on the primary key of the table, is organized in the same way
as an index. The leaf blocks in this structure contain the rows instead of the ROWIDs. This means that
the rows in the IOT are always maintained in the order of the primary key.
You can create additional indexes on IOTs. The primary key can be a composite key. Because large
rows of an IOT can destroy the dense and efficient storage of the B*-tree structure, you can store part
of the row in another segment, which is called an overflow area.
Index-organized tables provide fast key-based access to table data for queries involving exact match
and range searches. Changes to the table data result only in updating the index structure. Also,
storage requirements are reduced because key columns are not duplicated in the table and index. The
remaining non-key columns are stored in the index structure. IOTs are particularly useful when you
use applications that must retrieve data based on a primary key and have only a few, relatively short
nonkey columns.
Note: The descriptions mentioned here are correct if no overflow segments exist. Overflow segments
should be used with long rows.

Oracle Database 11g: SQL Tuning Workshop 4 - 28


Index-Organized Table Scans

select * from iotemp where empno=9999;


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost|
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 87 | 1|
| 1 | INDEX UNIQUE SCAN| SYS_IOT_TOP_75664 | 1 | 87 | 1|
---------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("EMPNO"=9999)
n s e
i ce
select * from iotemp where sal>1000;
b l el
---------------------------------------------------------------------
fer a
| Id | Operation | Name | Rows | Bytes |
a n s
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 1044 | n -tr
o
| 1 | INDEX FAST FULL SCAN| SYS_IOT_TOP_75664 | 12 | 1044 |
s an
) h a deฺ
---------------------------------------------------------------------

r ฺ e s Gui
Predicate Information (identified by operation id):

mi dent
---------------------------------------------------
1 - filter("SAL">1000)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Index-Organized
o n io Table Scans
t
nexample
e A
In the in the slide, it is assumed that IOEMP is an index-organized table defined with the
J s
o following statement:
create table iotemp
( empno number(4) primary key, ename varchar2(10) not null,
job varchar2(9), mgr number(4), hiredate date,
sal number(7,2) not null, comm number(7,2), deptno number(2))
organization index;
Index-organized tables are just like indexes. They use the same access paths that you saw for normal
indexes.
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.
Note: SYS_IOT_TOP_75664 is the system-generated name of the segment used to store the IOT
structure. You can retrieve the link between the table name and the segment from USER_INDEXES
with these columns: INDEX_NAME, INDEX_TYPE, TABLE_NAME.

Oracle Database 11g: SQL Tuning Workshop 4 - 29


Bitmap Indexes

Table
File 3
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Block 10
Index

Block 11

Block 12
n s e
ice
b l el
fer a
a n s
<Blue, 10.0.3, 12.8.3, 100010000 -tr
010000000
o n 010010100>

an
<Green, 10.0.3, 12.8.3, 000101000 000000000 100100000>
<Red, 10.0.3, 12.8.3, 010000000
h s
a deฺ 001100000 000001001>
<Yellow, 10.0.3, 12.8.3,
e )
s Gui
001000000 100000000 001000010>
Endi r ฺ
Key Start
p ฺ m dent Bitmap
ROWID
d g ROWID
S t u
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
Abia to
n
Bitmap Indexes
o io
In aA nt there is a one-to-one relationship between an index entry and a row; an index entry
B*-tree,
e
Josindex
points to a row. A bitmap index is organized as a B*-tree index but, with bitmap indexes, a single
entry uses a bitmap to point to many rows simultaneously. If a bitmap index involves more
than one column, there is a bitmap for every possible combination. Each bitmap header stores start
and end ROWIDs. Based on these values, the system uses an internal algorithm to map bitmaps onto
ROWIDs. This is possible because the system knows the maximum possible number of rows that can
be stored in a system block. Each position in a bitmap maps to a potential row in the table even if
that row does not exist. The contents of that position in the bitmap for a particular value indicate
whether that row has that value in the bitmap columns. The value stored is 1 if the row values match
the bitmap condition; otherwise it is 0. Bitmap indexes are widely used in data warehousing
environments. These environments typically have large amounts of data and ad hoc queries, but no
concurrent data manipulation language (DML) transactions because when locking a bitmap, you lock
many rows in the table at the same time. For such applications, bitmap indexing provides reduced
response time for large classes of ad hoc queries, reduced storage requirements compared to other
indexing techniques, dramatic performance gains even on hardware with a relatively small number of
CPUs or a small amount of memory, and efficient maintenance during parallel DML and loads.
Note: Unlike most other types of indexes, bitmap indexes include rows that have NULL values.
Indexing of nulls can be useful for some types of SQL statements, such as queries with the aggregate
function COUNT. The IS NOT NULL predicate can also benefit from bitmap indexes. Although
bitmaps are compressed internally, they are split in multiple leaves if the number of rows increases.
Oracle Database 11g: SQL Tuning Workshop 4 - 30
Bitmap Index Access: Examples

SELECT * FROM PERF_TEAM WHERE country='FR';


---------------------------------------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 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 - access("COUNTRY"='FR')
n s e
i ce
SELECT * FROM PERF_TEAM WHERE country>'FR';
b l el
---------------------------------------------------------------------
fer a
| Id | Operation | Name | Rows | Bytes |
a n s
---------------------------------------------------------------------
n -tr
| 0 | SELECT STATEMENT | | 1 | 45 |
o
| 1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 |
s an
45 |
| 2 | BITMAP CONVERSION TO ROWIDS|
)
|
h a deฺ
| |
| 3 | BITMAP INDEX RANGE SCAN | IX_B2
r ฺ e s Gui
| | |

mi dent
---------------------------------------------------------------------
p ฺ
Predicate: 3 - access("COUNTRY">'FR') filter("COUNTRY">'FR')
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Bitmap Index
o n ioAccess: Examples
TheA nt in the slide illustrate two possible access paths for bitmap indexes—BITMAP INDEX
examples
e
JosSINGLE VALUE and BITMAP INDEX RANGE SCAN—depending on the type of predicate you
use in the queries.
The first query scans the bitmap for country “FR” for positions containing a “1.” Positions with a “1”
are converted into ROWIDs and have their corresponding rows returned for the query.
In some cases (such as a query counting the number of rows with COUNTRY FR), the query might
simply use the bitmap itself and count the number of 1s (not needing the actual rows). This is
illustrated in the following example:
SELECT count(*) FROM PERF_TEAM WHERE country>'FR';
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 2 (0)|
| 1 | SORT AGGREGATE | | 1 | 3 | |
| 2 | BITMAP CONVERSION COUNT | | 1 | 3 | 2 (0)|
| 3 | BITMAP INDEX RANGE SCAN| IX_B2 | | | |
-----------------------------------------------------------------------------
Predicate: 3 - access("COUNTRY">'FR') filter("COUNTRY">'FR')

Oracle Database 11g: SQL Tuning Workshop 4 - 31


Combining Bitmap Indexes: Examples

SELECT * FROM PERF_TEAM WHERE country in('FR','DE');


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

FR 0 0 1 1 1 1 0 0 0 0 0 0
OR 0 1 1 1 1 1 0 0 0 0 0
DE 0 1 0 0 0 0 0 0 0 0 0 0

n s e
i ce
b l el
fer a
F 0 0 1 1 1 1 0 0 0 0 0 0 s
r a n
AND 1 n1-t0 0 0 0 0
0 0 1 0 o
s an M 1 1 1 0 1 1 0 1 0 1 1 1
) h a deฺ
r ฺ e s Gui
SELECT * FROM EMEA_PERF_TEAM m
ฺ i ecountry='FR'
T WHERE nt and gender='M';
d g p t u d
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.

A bia to
io Indexes: Examples
CombiningnBitmap
o
A
Bitmapntindexes are the most effective for queries that contain multiple conditions in the WHERE
e
Josclause. Rows that satisfy some, but not all, conditions are filtered out before the table itself is
accessed. This improves response time, often dramatically. As the bitmaps from bitmap indexes can
be combined quickly, it is usually best to use single-column bitmap indexes.
Due to fast bit-and, bit-minus, and bit-or operations, bitmap indexes are efficient when:
• Using IN (value_list)
• Predicates are combined with AND or OR

Oracle Database 11g: SQL Tuning Workshop 4 - 32


Combining Bitmap Index Access Paths

SELECT * FROM PERF_TEAM WHERE country in ('FR','DE');


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

---------------------------------------------------------------------
| 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 - access("COUNTRY"='DE' OR "COUNTRY"='FR')

n s e
SELECT * FROM PERF_TEAM WHERE country='FR' and gender='M';
i ce
---------------------------------------------------------------------
b l el
| Id | Operation | Name | Rows | Bytes |
fer a
| 0 | SELECT STATEMENT | | 1 | 45 |
a n s
| 1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM | 1 | 45 |
n -tr
| 2 | BITMAP CONVERSION TO ROWIDS| | | | o
| 3 | BITMAP AND | | |
s an |
| 4 | BITMAP INDEX SINGLE VALUE| IX_B1
)
|
h a deฺ
| |
| 5 | BITMAP INDEX SINGLE VALUE| IX_B2
r ฺ e s Gui
| | |

p ฺ mi dent
Predicate: 4 - access("GENDER"='M') 5 - access("COUNTRY"='FR')

d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
io Index Access Paths
CombiningnBitmap
o
A
Bitmapntindexes can be used efficiently when a query combines several possible values for a column
e
Josor when two separately-indexed columns are used.
In some cases, a WHERE clause might reference several separately indexed columns as in the
examples shown in the slide.
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. The more complex the compound WHERE clauses
become, the more benefit you get from bitmap indexing.

Oracle Database 11g: SQL Tuning Workshop 4 - 33


Bitmap Operations

• BITMAP CONVERSION:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– TO ROWIDS
– FROM ROWIDS
– COUNT
• BITMAP INDEX:
– SINGLE VALUE e
n s
– RANGE SCAN i ce
– FULL SCAN b l el
fer a
• BITMAP MERGE a n s
n - tr
• BITMAP AND/OR o
s an
• BITMAP MINUS
) ha ideฺ
• BITMAP KEY ITERATIONirฺes t G u
d g p ฺm u
t den
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
io Ab
n
Bitmap Operations
o
TheA nt summarizes all the possible bitmap operations. The following operations have not been
slide
e
Josexplained 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. After the bitmap comparison has been done, the resultant bitmap is
converted back into ROWIDs (BITMAP CONVERSION TO ROWIDS) to perform the data
lookup.
• 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, and zeros to ones. The bitmap minus operation can then be performed as
a BITMAP AND operation using this negated bitmap. This would typically be the case with the
following combination of predicates: C1=2 and C2<>6.
• BITMAP KEY ITERATION takes each row from a table row source and finds the
corresponding bitmap from a bitmap index. This set of bitmaps is then merged into one bitmap
in a BITMAP MERGE operation.

Oracle Database 11g: SQL Tuning Workshop 4 - 34


Bitmap Join Index

CREATE BITMAP INDEX cust_sales_bji


ON sales(c.cust_city)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

FROM sales s, customers c


1.2.3
WHERE c.cust_id = s.cust_id;
Sales
Customers

n s e
i ce
b l el
fe r a
a n s
o n -tr 10.8000.3
a n
<Rognes, 1.2.3, 10.8000.3, h s eฺ
a 100010010010100…>
) i d
<Aix-en-Provence, 1.2.3, 10.8000.3,
i r ฺ es t Gu 000101000100000…>
<Marseille, 1.2.3,
g p ฺm 10.8000.3,
u d en 010000001000001…>
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Bitmap Join n o
iIndex
o
nt to a bitmap index on a single table, you can create a bitmap join index. A bitmap join
A
In addition
e
Josindex is a bitmap index for the join of two or more tables. A bitmap join index is a space-efficient
way of reducing the volume of data that must be joined by performing restrictions in advance.
Here, you create a new bitmap join index named cust_sales_bji on the SALES table. The key
of this index is the CUST_CITY column of the CUSTOMERS table. 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. The CUST_ID column is the primary key of
CUSTOMERS, but is also a foreign key inside SALES, although not required.
The FROM and WHERE clause in the CREATE statement allow the system to make the link between
the two tables. They represent the natural join condition between the two tables. The middle part of
the graphic shows you a theoretical implementation of this bitmap join index. Each entry or key in
the index represents a possible city found in the CUSTOMERS table. A bitmap is then associated to
one particular key. Each bit in a bitmap corresponds to one row in the SALES table. In the first key
in the slide (Rognes), you see that the first row in the SALES table corresponds to a product sold to a
Rognes customer, while the second bit is not a product sold to a Rognes customer. By storing the
result of a join, the join can be avoided completely for SQL statements using a bitmap join index.
Note: Bitmap join indexes are much more efficient in storage than materialized join views. For a row
source example, see the lesson titled “Case Study: Star Transformation.”

Oracle Database 11g: SQL Tuning Workshop 4 - 35


Composite Indexes
MAKE MODEL

CARS
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Index columns
create index cars_make_model_idx on cars(make, model);

select *
from cars
where make = 'CITROËN' and model = '2CV'; n s e
i ce
b l el
-----------------------------------------------------------------
| Id | Operation | Name |
fer a
-----------------------------------------------------------------
a n s
| 0 | SELECT STATEMENT | |
n -tr
o
an
| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
|* 2 | INDEX RANGE SCAN | CARS_MAKE_MODEL_IDX
h s
a deฺ
|
-----------------------------------------------------------------
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
io
CompositenIndexes
o
A nt index is also referred to as a concatenated index because it concatenates column values
A composite
e
Josare
together to form the index key value. In the illustration in the slide, the MAKE and MODEL columns
concatenated together to form the index. It is not required that the columns in the index are
adjacent. And, you can include up to 32 columns in the index, unless it is a bitmap composite index,
in which case the limit is 30.
Composite indexes can provide additional advantages over single-column indexes:
• Improved selectivity: Sometimes two or more columns or expressions, each with poor
selectivity, can be combined to form a composite index with higher selectivity.
• 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.
A composite index is mainly useful when you often have a WHERE clause that references all, or the
leading portion of the columns in the index. If some keys are used in WHERE clauses more
frequently, and you decided to create a composite index, 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.
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. This is possible since index skip scans and fast full scans
were implemented.

Oracle Database 11g: SQL Tuning Workshop 4 - 36


Invisible Index: Overview

Use index. Do not use index.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Optimizer view point

VISIBLE INVISIBLE
Index Index
OPTIMIZER_USE_INVISIBLE_INDEXES=FALSE

n s e
i ce
b l el
fer a
Data view point
a n s
o n -tr
s an
) h aindex.deฺ
Update index.
r ฺ e s Gui Update table.
Update
Update table.
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i
Invisible Index: oA
Overview
t o
n index is an index that is ignored by the optimizer unless you explicitly set the
An A
invisible
e
JosOPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at the session or system level. The default value for this parameter is FALSE.
Making an index invisible is an alternative to making it unusable or dropping it. Using invisible
indexes, you can perform the following actions:
• Test the removal of an index before dropping it.
• Use temporary index structures for certain operations or modules of an application without
affecting the overall application.
Unlike unusable indexes, an invisible index is maintained during DML statements.

Oracle Database 11g: SQL Tuning Workshop 4 - 37


Invisible Indexes: Examples

• Index is altered as not visible to the optimizer:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ALTER INDEX ind1 INVISIBLE;

• Optimizer does not consider this index:

SELECT /*+ index(TAB1 IND1) */ COL1 FROM TAB1 WHERE …;


n s e
i ce
• Optimizer considers this index:
b l el
fer a
ALTER INDEX ind1 VISIBLE; a n s
o n -tr
• Create an index as invisible initially: a n
h a s eฺ
e s ) u id
CREATE INDEX IND1 ON TAB1(COL1) ฺ
ir eINVISIBLE;
n tG
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i o A Examples
Invisible Indexes:
A
When nantoindex is invisible, the optimizer selects plans that do not use the index. If there is no
e
Josdiscernible drop in performance, you can drop the index. You can also create an index initially as
invisible, perform testing, and then determine whether to make the index visible.
You can query the VISIBILITY column of the *_INDEXES data dictionary views to determine
whether the index is VISIBLE or INVISIBLE.
Note: For all the statements given in the slide, it is assumed that
OPTIMIZER_USE_INVISIBLE_INDEXES is set to FALSE.

Oracle Database 11g: SQL Tuning Workshop 4 - 38


Guidelines for Managing Indexes

• Create indexes after inserting table data.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Index the correct tables and columns.


• Order index columns for performance.
• Limit the number of indexes for each table.
• Drop indexes that are no longer required.
• Specify the tablespace for each index. n s e
i ce
• Consider parallelizing index creation.
b l el
• Consider creating indexes with NOLOGGING. fer a
a n s
-tr
• Consider costs and benefits of coalescing ornrebuilding
o
indexes.
s an

ha ideconstraints.
• Consider cost before disabling sor) dropping
u
rฺe nt G
m i
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Guidelinesn
o ioManaging Indexes
for
nt indexes after inserting table data: Data is often inserted or loaded into a table using
• ACreate
e
Jos either the SQL*Loader or an import utility. It is more efficient to create an index for a table after
inserting or loading the data.
• 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.
- To improve performance on joins of multiple tables, index the columns used for joins.
- Small tables do not require indexes.
• Columns suitable for indexing: Some columns are strong candidates for indexing:
- Values are relatively unique in the column.
- There is a wide range of values (good for regular indexes).
- There is a small range of values (good for bitmap indexes).
- The column contains many nulls, but queries often select all rows having a value.
• Columns not suitable for indexing:
- There are many nulls in the column and you do not search on the not null values.
- The LONG and LONG RAW columns cannot be indexed.
- Virtual columns: You can create unique or nonunique indexes on virtual columns.

Oracle Database 11g: SQL Tuning Workshop 4 - 39


Guidelines for Managing Indexes (continued)
• Order index columns for performance: The order of columns in the CREATE INDEX
statement can affect query performance. In general, specify the most frequently used columns
first.
• Limit the number of indexes for each table: A table can have any number of indexes.
However, the more indexes there are, the more overhead is incurred as the table is modified.
Thus, there is a trade-off between the speed of retrieving data from a table and the speed of
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

updating the table.


• 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,
it can be more convenient to perform database maintenance, such as tablespace backup.
• Consider parallelizing index creation: You can parallelize index creation, just as you can
parallelize table creation. This speeds up index creation. However, an index created with an
INITIAL value of 5M and a parallel degree of 12 consumes at least 60 MB of storage duringnse
index creation.
e l ice
• Consider creating indexes with NOLOGGING: You can create an index and generate
r a bl minimal
redo log records by specifying NOLOGGING in the CREATE INDEX statement.
n s fe Because
indexes created using NOLOGGING are not archived, perform a backup
n - t raafter you create the
index. Note that NOLOGGING is the default in a NOARCHIVELOG
n o database.
• Consider costs and benefits of coalescing or rebuilding a
indexes: Improper sizing or increased
s reduce
growth can produce index fragmentation. To eliminate ) h a or i d eฺfragmentation, you can rebuild
or coalesce the index. But before you perform r s task,
ฺeeither G uweigh the costs and benefits of each
option, and select the one that worksp
i
ฺmfor uyour
best n t
esituation.
g d
t constraints: Because unique and primary keys
• Consider cost before disabling d S
or dropping
have associated indexes,byou
@ s
i in the cost of dropping and creating indexes when
ia shouldthfactor
considering whether j a
(to disable e
sor drop a UNIQUE or PRIMARY KEY constraint. If the
b i a t o u
i o A for a UNIQUE
associated index key or PRIMARY KEY constraint is extremely large, you can
n by leaving the constraint enabled rather than dropping and re-creating the large index.
saveotime
t
n
AYou also have the option of explicitly specifying that you want to keep or drop the index when
o s e dropping or disabling a UNIQUE or PRIMARY KEY constraint.
J

Oracle Database 11g: SQL Tuning Workshop 4 - 40


Investigating Index Usage

An index may not be used for one of many reasons:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• There are functions being applied to the predicate.


• There is a data type mismatch.
• Statistics are old.
• The column can contain null.
• Using the index would actually be slower than not using it. n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Investigating
o n ioIndex Usage
nt often run a SQL statement expecting a particular index to be used, and it is not. This can be
YouAmay
e
Josbecause the optimizer is unaware of some information, or because it should not use the index.
Functions
If you apply a function to the indexed column in the WHERE clause, the index cannot be used; the
index is based on column values without the effect of the function. For example, the following
statement does not use an index on the salary column:
SELECT * FROM employees WHERE 1.10*salary > 10000
If you want an index to be used in this case, you can create a function-based index. Function-based
indexes were covered under “Index Range Scan: Function-Based” earlier in this lesson.
Data Type Mismatch
If there is a data type mismatch between the indexed column and the compared value, the index is
not used. This is due to the implicit data type conversion. For example, if the SSN column is of the
VARCHAR2 type, the following does not use the index on SSN:
SELECT * FROM person WHERE SSN = 123456789

Oracle Database 11g: SQL Tuning Workshop 4 - 41


Investigating Index Usage (continued)
Old Statistics
The statistics are considered by the optimizer when deciding whether to use an index. If they are
outdated, they may influence the optimizer to make poor decisions about indexes.
Null Columns
If a column can contain nulls, it may prevent the use of an index on that column. This is covered later
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

in this lesson.
Slower Index
Sometimes the use of an index is not efficient. This is covered later in this lesson.

n s e
i ce
b l el
fe ra
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 42


Practice 4: Overview

This practice covers using different access paths for better


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

optimization.
• Case 1 through case 13

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 43


Clusters

ORD_NO PROD QTY ... Cluster Key


------ ------ ------ (ORD_NO)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

101 A4102 20 101 ORD_DT CUST_CD


102 A2091 11 05-JAN-97 R01
102 G7830 20 PROD QTY
102 N9587 26 A4102 20
101 A5675 19 A5675 19
101 W0824 10 W0824 10
102 ORD_DT CUST_CD
07-JAN-97 N45
PROD QTY
ORD_NO ORD_DT CUST_CD A2091 11
n s e
ce
------ ------ ------ G7830 20
101 05-JAN-97 R01 N9587 26
eli
102 07-JAN-97 N45
a b l
fe r
a n s
o n -tr
s an
Unclustered ORDERS and
) h a Clustered
i d eฺ ORDERS and
ORDER_ITEMS tables
i r ฺes nt GORDER_ITEMS
u tables
m
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Clusters nio
o an optional method for storing table data. A cluster is a group of tables that share the
A ntare
Clusters
e data blocks because they share common columns and are often used together. For example, the
Jossame
ORDERS and ORDER_ITEMS table share the ORDER_ID column. When you cluster the ORDERS
and ORDER_ITEMS tables, 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. Such an index contains
an entry for each cluster key value. To locate a row in a cluster, the cluster index is used to find the
cluster key value, which points to the data block associated with that cluster key value. Therefore, the
system accesses a given row with a minimum of two I/Os.
Hash clusters: Hashing is an optional way of storing table data to improve the performance of data
retrieval. To use hashing, you create a hash cluster and load tables into the cluster. 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. To find or store a row in a hash cluster, the system applies the hash function to the
row’s cluster key value; the resulting hash value corresponds to a data block in the cluster, which the
system then reads or writes on behalf of the issued statement.
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 Database 11g: SQL Tuning Workshop 4 - 44


When Are Clusters Useful?

• Index cluster:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– 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 e
n s
– Storage for all cluster keys allocated initially i ce
– In either equality (=) or nonequality (<>) searches b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io Useful?
When Are Clusters
o
nt clusters allow row data from one or more tables that share a cluster key value to be stored
• AIndex
e
Jos inkeysame block. You can locate these rows using a cluster index, which has one entry per cluster
value and not for each row. Therefore, the index is smaller and less costly to access for
finding multiple rows. 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. The smaller index and smaller group of blocks reduce
the cost of access by reducing block visits to the buffer cache. 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. They are also useful for all filter operations or searches. 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.
• Hash clusters allow row data from one or more tables that share a cluster key value to be stored
in same block. You can locate these rows using a system-provided or user-provided 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. 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.

Oracle Database 11g: SQL Tuning Workshop 4 - 45


When Are Clusters Useful?

• Single-table hash cluster:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– 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 e
n s
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n io Useful? (continued)
When Are Clusters
o
nt means that in a hash cluster the clustering factor is also very good and a row may be
• AThis
e
Jos accessed by its key with one block visit only and without needing an index. Hash clusters
allocate all the storage for all the hash buckets when the cluster is created, so they may waste
space. They also do not perform well other than on equality searches or nonequality searches.
Like index clusters if they contain multiple tables, full scans are more expensive for the same
reason.
• Single-table hash clusters are similar to a hash cluster, but are optimized in the block structures
for access to a single table, thereby providing the fastest possible access to a row other than by
using a rowid filter. As they only have one table, full scans, if they happen, cost as much as they
would in a heap table.
• Sorted hash clusters are designed to reduce costs of accessing ordered data by using a hashing
algorithm on the hash key. 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. 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), thereby, eliminating the need for a sort
to process the order by clause. These clusters are very good for batch reporting, billing, and so
on.

Oracle Database 11g: SQL Tuning Workshop 4 - 46


Cluster Access Path: Examples

SELECT * FROM calls WHERE origin_number=33442395322;


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

----------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 56 | 0 (0)|
| 1 | TABLE ACCESS HASH| CALLS | 1 | 56 | |
----------------------------------------------------------------
1 - access("ORIGIN_NUMBER"=33442395322)

n s e
li
SELECT * FROM emp,dept WHER emp.deptno=dept.deptno;
ce
l e
-----------------------------------------------------------------
| Name | Rows | Bytes | Costfe |
rab
ns
| Id | Operation
-----------------------------------------------------------------
t r a
| 0 | SELECT STATEMENT | | 1 | n- | 33 ||
117
1 | no117 |
1a|
| 1 | NESTED LOOPS | |
| 2 | TABLE ACCESS FULL
TABLE ACCESS CLUSTER| DEPT |h
| EMP |
a s ฺ 87 | 21 ||
1 |de 30 |
| 3 |
r ฺ e s) Gui
-----------------------------------------------------------------
ฺ mi dent
3 - filter("EMP"."DEPTNO"="DEPT"."DEPTNO")
p
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Cluster Accessn i oAPath: Examples
t o
n in the slide shows you two different cluster access paths.
TheA example
e
JosIn the top one, a hash scan is used to locate rows in a hash cluster, based on a hash value. In a hash
cluster, all rows with the same hash value are stored in the same data block. To perform a hash scan,
the system first obtains the hash value by applying a hash function to a cluster key value specified by
the statement. The system then scans the data blocks containing rows with that hash value.
The second one assumes that a cluster index was used to cluster both the EMP and DEPT tables. In
this case, a cluster scan is used to retrieve, from a table stored in an indexed cluster, all rows that
have the same cluster key value. In an indexed cluster, all rows with the same cluster key value are
stored in the same data block. To perform a cluster scan, the system first obtains the ROWID of one
of the selected rows by scanning the cluster index. The system then locates the rows based on this
ROWID.
Note: You see examples of how to create clusters in the labs for this lesson.

Oracle Database 11g: SQL Tuning Workshop 4 - 47


Sorting Operators

• SORT operator:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– AGGREGATE: Single row from group function


– UNIQUE: To eliminate duplicates
– JOIN: Precedes a merge join
– GROUP BY, ORDER BY: For these operators
• HASH operator: e
n s
– GROUP BY: For this operator i ce
– UNIQUE: Equivalent to SORT UNIQUE b l el
fer a
• If you want ordered results, always use ORDER n
BY.
a s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Sorting Operators
o
nt
SortAoperations result when users specify an operation that requires a sort. Commonly encountered
s e
Jo operations include the following:
• SORT AGGREGATE does not involve a sort. It retrieves a single row that is the result of
applying a group function to a group of selected rows. Operations such as COUNT and MIN are
shown as SORT AGGREGATE.
• SORT UNIQUE sorts output rows to remove duplicates. It occurs if a user specifies a
DISTINCT clause or if an operation requires unique values for the next step.
• SORT JOIN happens during a sort-merge join, if the rows need to be sorted by the join key.
• SORT GROUP BY is used when aggregates are computed for different groups in the data. The
sort is required to separate the rows into different groups.
• SORT ORDER BY is required when the statement specifies an ORDER BY that cannot be
satisfied by one of the indexes.
• HASH GROUP BY hashes a set of rows into groups for a query with a GROUP BY clause.
• HASH UNIQUE hashes a set of rows to eliminate duplicates. It occurs if a user specifies a
DISTINCT clause or if an operation requires unique values for the next step. This is similar to
SORT UNIQUE.
Note: Several SQL operators cause implicit sorts (or hashes since Oracle Database 10g, Release 2),
such as DISTINCT, GROUP BY, UNION, MINUS, and INTERSECT. However, do not rely on these
SQL operators to return ordered rows. If you want to have rows ordered, use the ORDER BY clause.

Oracle Database 11g: SQL Tuning Workshop 4 - 48


Buffer Sort Operator
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select ename, emp.deptno, dept.deptno, dname


from emp, dept
where ename like 'A%';

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 124 | 5
| 1 | MERGE JOIN CARTESIAN | | 4 | 124 | 5
n s e
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 9 | 2
i ce
| 3 | INDEX RANGE SCAN | I_ENAME | 1 | | 1
b l el
| 4 | BUFFER SORT | | 4 | 88 | 3
fer a
| 5 | TABLE ACCESS FULL | DEPT | 4 | 88 | 3
a n s
---------------------------------------------------------------------
n -tr
o
an
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("ENAME" LIKE 'A%') h s
a deฺ
e )
s Gui
filter("ENAME" LIKE 'A%')
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
io
Buffer SortnOperator
o
TheA nt
BUFFER SORT operator uses a temporary table or a sort area in memory to store intermediate
s e
Jo data. However, the data is not necessarily sorted.
This is needed if there is an operation that needs all the input data before it can start (See “Cartesian
Join.”).
So BUFFER SORT uses the buffering mechanism of a traditional sort, but it does not do the sort
itself. The system simply buffers the data, in the User Global Area (UGA) or Program Global Area
(PGA), to avoid multiple table scans against real data blocks.
The whole sort mechanism is reused, including the swap to disk when not enough sort area memory
is available, but without sorting the data.
The difference between a temporary table and a buffer sort is as follows:
• A temporary table uses System Global Area (SGA).
• A buffer sort uses UGA.
Both of these can be swapped in the temporary files. The biggest difference is that the temporary
table has to go through the buffer cache, which introduces latching effects that do not appear for
buffer sorts.

Oracle Database 11g: SQL Tuning Workshop 4 - 49


Inlist Iterator
Every value executed separately
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

deptno=1 deptno=2

select * from emp where deptno in (1,2);


select * from emp where deptno = 1 or deptno =2 ;
---------------------------------------------------------------------
n s e
i ce
el
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
a b l
| 0 | SELECT STATEMENT | | 2 | 78 | 2|
fer
| 1 | INLIST ITERATOR | | | | |
a n s
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 2 | 78 | 2|
n -tr
o
an
| 3 | INDEX RANGE SCAN | IX_SS | 2 | | 1|
s
a deฺ
---------------------------------------------------------------------
h
)
s Gui
Predicate Information (identified by operation id):
e
r ฺ
---------------------------------------------------
ฺ mi dent
3 - access("DEPTNO"=1 OR "DEPTNO"=2)
p
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Inlist Iterator
o n io
It isA nt when a query contains an IN clause with values or multiple equality predicates on the same
used
e
Joscolumn linked with ORs.
The INLIST ITERATOR operator iterates over the enumerated value list, and every value is
executed separately.
The execution plan is identical to the result of a statement with an equality clause instead of IN,
except for one additional step. The extra step occurs when INLIST ITERATOR feeds the equality
clause with unique values from the list.
You can view this operator as a FOR LOOP statement in PL/SQL. In the example in the slide, you
iterate the index probe over two values: 1 and 2.
Also, it is a function that uses an index, which is scanned for each value in the list. An alternative
handling is UNION ALL of each value or a FILTER of the values against all the rows, this is
significantly more efficient.
The optimizer uses an inlist iterator when an IN clause is specified with values, and the optimizer
finds a selective index for that column. If there are multiple OR clauses using the same index, the
optimizer selects this operation rather than CONCATENATION or UNION ALL, because it is more
efficient.

Oracle Database 11g: SQL Tuning Workshop 4 - 50


View Operator

create view V as select /*+ NO_MERGE */ DEPTNO, sal from emp ;


select * from V;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
| 0 | SELECT STATEMENT | | 14 | 364 | 1 (0)| 0:01
| 1 | VIEW | V | 14 | 364 | 1 (0)| 0:01
| 2 | INDEX FULL SCAN| IX_SS | 14 | 98 | 1 (0)| 0:01
---------------------------------------------------------------------

select v.*,d.dname from (select DEPTNO, sum(sal) SUM_SAL


n s e
ce
from emp group by deptno) v, dept d where v.deptno=d.deptno;
---------------------------------------------------------------------
eli
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
a b l
fer
| 0 | SELECT STATEMENT | | 3 | 144 | 5 (20)|
a n s
tr
| 1 | HASH JOIN | | 3 | 144 | 5 (20)|
| 2 | VIEW | | 3 | 78 | 1 (0)|
o n -
an
| 3 | HASH GROUP BY | | 3 | 21 | 1 (0)|
| 4 | INDEX FULL SCAN| IX_SS | 14 | 98 |
h
1
s
(0)|
a deฺ
| 5 | TABLE ACCESS FULL| DEPT | 4 |
e )
s Gui
88 | 3 (0)|

---------------------------------------------------------------------
r
ฺ mi dent
Predicate: 1 - access("V"."DEPTNO"="D"."DEPTNO")
p
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
View Operator
o n io
EachA nt produces a variable set of data in the form of a table. A view simply gives a name to this
query
e
Josset of data.
When views are referenced in a query, the system can handle them in two ways. If a number of
conditions are met, they can be merged into the main query. This means that the view text is
rewritten as a join with the other tables in the query. Views can also be left as standalone views and
selected from directly as in the case of a table. Predicates can also be pushed into or pulled out of the
views as long as certain conditions are met.
When a view is not merged, you can see the VIEW operator. The view operation is executed
separately. All rows from the view are returned, and the next operation can be done.
Sometimes a view cannot be merged and must be executed independently in a separate query block.
In this case, you can also see the VIEW operator in the explain plan. The VIEW keyword indicates
that the view is executed as a separate query block. For example, views containing GROUP BY
functions cannot be merged.
The second example in the slide shows a nonmergeable inline view. An inline view is basically a
query within the FROM clause of your statement.
Basically, this operator collects all rows from a query block before they can be processed by higher
operations in the plan.

Oracle Database 11g: SQL Tuning Workshop 4 - 51


Count Stop Key Operator

select count(*)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

from (select /*+ NO_MERGE */ *


from TC where C1 ='1' and rownum < 10);

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 4 (0)|
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | VIEW | | 9 | | 4 (0)|
n s e
| 3 | COUNT STOPKEY | | | | |
i ce
| 4 | TABLE ACCESS FULL| TC | 4282 | 4190K| 4 (0)|
b l el
---------------------------------------------------------------------
fer a
Predicate Information (identified by operation id):
a n s
---------------------------------------------------
n -tr
3 - filter(ROWNUM<10)
o
4 - filter("C1"='1')
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Count StopnKey
o io Operator
A
COUNTnt STOPKEY limits the number of rows returned. The limitation is expressed by the ROWNUM
e
osexpression in the WHERE clause. It terminates the current operation when the count is reached.
J
Note: The cost of this operator depends on the number of occurrences of the values you try to
retrieve. If the value appears very frequently in the table, the count is reached quickly. If the value is
very infrequent, and there are no indexes, the system has to read most of the table’s blocks before
reaching the count.

Oracle Database 11g: SQL Tuning Workshop 4 - 52


Min/Max and First Row Operators
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select min(id) FROM t WHERE id > 500000;

---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 3|
| 1 | SORT AGGREGATE | | 1 | 13 | |
| 2 | FIRST ROW | | 717K| 9113K| 3|
| 3 | INDEX RANGE SCAN (MIN/MAX)| IXT | 717K| 9113K| 3|
n s e
i ce
el
---------------------------------------------------------------------

a b l
Predicate Information (identified by operation id):
fer
---------------------------------------------------
a n s
n -tr
o
an
3 - access("ID">500000)

h s
a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Min/Max and
o n ioFirst Row Operators
A
FIRST nt ROW retrieves only the first row selected by a query. It stops accessing the data after the first
e is returned. This is an optimization introduced in Oracle 8i and it works with the index range
Josvalue
scan and the index full scan.
In the example in the slide, it is assumed that there is an index on the ID column.

Oracle Database 11g: SQL Tuning Workshop 4 - 53


Join Methods

• A join defines the relationship between two row sources.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• A join is a method of combining data from two data sources.


• It is controlled by join predicates, which define how the
objects are related.
• Join methods:
– Nested loops
– Sort-merge join n s e
i ce
– Hash join
b l el
SELECT e.ename, d.dname f a
er
FROM dept d JOIN emp e USING (deptno) Join s
anpredicate
WHERE e.job = 'ANALYST' OR e.empno = 9999; - t r
n Nonjoin predicate
a no
SELECT e.ename,d.dname
h a s eฺ
FROM emp e, dept d
e s ) u id
WHERE e.deptno = d.deptno AND i r ฺ n t G Join predicate
m
(e.job = 'ANALYST'
d gpฺ ORSe.empno
tud
e = 9999); Nonjoin predicate
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
Join Methods i oA
to is a set of data that can be accessed in a query. It can be a table, an index, a
nsource
A
A row
e
Josnonmergeable view, or even the result set of a join tree consisting of many different objects.
A join predicate is a predicate in the WHERE clause that combines the columns of two of the tables in
the join.
A nonjoin predicate is a predicate in the WHERE clause that references only one table.
A join operation combines the output from two row sources (such as tables or views) and returns one
resulting row source (data set). 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. Hash joins generally
perform better than sort-merge joins. On the other hand, sort-merge joins can perform better
than hash joins if one or two row sources are already sorted.
• 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 then scans the larger table, probing
the hash table to find the joined rows. This method is best used when the smaller table fits in the
available memory. The cost is then limited to a single read pass over the data for the two tables.
Note: The slide shows you the same query using both the American National Standards Institute
(ANSI) and non-ANSI join syntax.

Oracle Database 11g: SQL Tuning Workshop 4 - 54


Nested Loops Join
NL
• Driving row source is scanned TAF TAR
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Each row returned drives a lookup in Driving

inner row source For


IS
each Inner
• Joining rows are then returned
select ename, e.deptno, d.deptno, d.dname
from emp e, dept d
where e.deptno = d.deptno and ename like 'A%';
n s e
ice
el
---------------------------------------------------------------------
| Id | Operation | Name | Rows |Cost |
a b l
---------------------------------------------------------------------
fe r
| 0 | SELECT STATEMENT | | 2 | 4 |
a n s
| 1 | NESTED LOOPS | | 2 | 4 |
n -tr
| 2 | TABLE ACCESS FULL | EMP | 2 | 2 |
o
| 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 |
s an
1 |
| 4 | INDEX UNIQUE SCAN | PK_DEPT |
) h a deฺ
1 | |

r ฺ e s Gui
---------------------------------------------------------------------

mi dent
2 - filter("E"."ENAME" LIKE 'A%')

p ฺ
4 - access("E"."DEPTNO"="D"."DEPTNO")
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Nested Loops
o n ioJoin
t
OneAofnthe two tables is defined as the outer table, or the driving table, or the left-hand side. The
e
Josother table is called the inner table, or the right-hand side.
For each row in the outer (driving) table that matches the single table predicates, all rows in the inner
table that satisfy the join predicate (matching rows) are retrieved.
Any nonjoin predicates on the inner table are considered after this initial retrieval, unless a composite
index combining both the join and the nonjoin predicate is used.
Based on the example in the slide, 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, with a good driving
condition between the two tables. You drive from the outer loop to the inner loop, so the order of
tables in the execution plan is important. Therefore, you should use other join methods when two
independent row sources are joined.

Oracle Database 11g: SQL Tuning Workshop 4 - 55


Nested Loops Join: Prefetching

NL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

TAF TAR
Driving
IRS
Inner

select ename, e.deptno, d.deptno, d.dname


from emp e, dept d
where e.deptno = d.deptno and ename like 'A%';
n s e
i ce
el
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 84 | 5
a b l
| 1 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 22 | 1
fer
| 2 | NESTED LOOPS | | 2 | 84 | 5
a n s
|* 3 | TABLE ACCESS FULL | EMP | 2 | 40 | 3
n -tr
|* 4 | INDEX RANGE SCAN | IDEPT | 1 | | 0
o
an
---------------------------------------------------------------------
s
3 - filter("E"."ENAME" LIKE 'A%')
) h a deฺ
4 - access("E"."DEPTNO"="D"."DEPTNO")
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Nested Loops
o n ioJoin: Prefetching
t introduced a mechanism called nested loops prefetching. The idea is to improve I/O
n9iR2
A
Oracle
e
Josutilization, therefore response time, of index access with table lookup by batching rowid lookups into
parallel block reads.
This change to the plan output is not considered a different execution plan. It does not affect the join
order, join method, access method, or parallelization scheme.
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.
The prefetching mechanism is used by table lookup. When an index access path is chosen and the
query cannot be satisfied by the index alone, the data rows indicated by the ROWID also must be
fetched. This ROWID to data row access (table lookup) is improved using data block prefetching,
which involves reading an array of blocks which are pointed at by an array of qualifying ROWIDs.
Without data block prefetching, accessing a large number of rows using a poorly clustered B*-tree
index could be expensive. Each row accessed by the index would likely be in a separate data block
and thus would require a separate I/O operation.
With data block prefetching, 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,
rather than reading a single data block at a time.

Oracle Database 11g: SQL Tuning Workshop 4 - 56


Nested Loops Join: 11g Implementation

NL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

NL TAR

TAF IRS
Driving Inner

select ename, e.deptno, d.deptno, d.dname


from emp e, dept d
where e.deptno = d.deptno and ename like 'A%';
n s e
i ce
el
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 84 | 5
a b l
| 1 | NESTED LOOPS | | | |
fer
| 2 | NESTED LOOPS | | 2 | 84 | 5
a n s
|* 3 | TABLE ACCESS FULL | EMP | 2 | 40 | 3
n -tr
|* 4 | INDEX RANGE SCAN | DDEPT | 1 | | 0
o
| 5 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 22 |
s an 1

) h a deฺ
---------------------------------------------------------------------
3 - filter("E"."ENAME" LIKE 'A%')
r ฺ e s Gui
mi dent
4 - access("E"."DEPTNO"="D"."DEPTNO")

p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Nested Loops
o n ioJoin: 11g Implementation
t
nDatabase
e A
Oracle 11g introduces a new way of performing joins with NESTED LOOPS operators.
s
Jo With this NESTED LOOPS implementation, the system first performs a NESTED LOOPS join
between the other table and the index. This produces a set of ROWIDs that you can use to look up the
corresponding rows from the table with the index. Instead of going to the table for each ROWID
produced by the first NESTED LOOPS join, the system batches up the ROWIDs and performs a
second NESTED LOOPS join between the ROWIDs and the table. This ROWID batching technique
improves performance as the system only reads each block in the table once.

Oracle Database 11g: SQL Tuning Workshop 4 - 57


Sort Merge Join Merged
MJ

• First and second row sources are sorted SJ SJ

Sorted
Sorted
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

by same sort key. TAF TAF


• Sorted rows from both side are merged. Independent
select ename, e.deptno, d.deptno, dname
from emp e, dept d
where e.deptno = d.deptno and ename > 'A';
---------------------------------------------------------------------
n s e
ce
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
| 0 | SELECT STATEMENT | | 2 | 84 | 8 (25)|
el i
| 1 | MERGE JOIN | | 2 | 84 | 8 (25)|
a b l
| 2 | SORT JOIN | | 2 | 40 | 4 (25)|
fe r
| 3 | TABLE ACCESS FULL| EMP | 2 | 40 | 3 (0)|
a n s
| 4 | SORT JOIN | | 4 | 88 | 4 (25)|
n -tr
o
| 5 | TABLE ACCESS FULL| DEPT | 4 | 88 | 3
s an
(0)|

) h a deฺ
---------------------------------------------------------------------
Predicate: 3 - filter("ENAME">'A')
r ฺ e s Gui
mi dent
4 - access("E"."DEPTNO"="D"."DEPTNO")

p ฺ
filter("E"."DEPTNO"="D"."DEPTNO")
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Sort MergenJoin
o io
In aA ntmerge join, there is no concept of a driving table. A sort merge join is executed as follows:
sort
e
Jos 1.2. Get the first data set, using any access and filter predicates, and sort it on the join columns.
Get the second data set, using any access and filter predicates, and sort it on the join columns.
3. For each row in the first data set, find the start point in the second data set and scan until you
find a row that does not join.
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.
If one row source has already been sorted in a previous operation (there is an index on the join
column, for example), the sort merge operation skips the sort on that row source. When you perform
a merge join, you must fetch all rows from the two row sources before to return the first row to the
next operation. Sorting could make this join technique expensive, especially if sorting cannot be
performed in memory. 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:
• The join condition between two tables is not an equijoin.
• Sorts already required by previous operations.
Note: Sort merge joins are useful when the join condition between two tables is an inequality
condition (but not a nonequality), such as <, <=, >, or >=.

Oracle Database 11g: SQL Tuning Workshop 4 - 58


Hash Join
HJ
• The smallest row source is used
Driving TAF TAR
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

to build a hash table.


Build hash
• The second row source is hashed table in IS
memory
and checked against the hash table. Probe

select ename, e.deptno, d.deptno, dname from emp e, dept d


where e.deptno = d.deptno and ename like 'A%';

n s e
ce
---------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost
eli
---------------------------------------------------------------------
a b l
fer
| 0 | SELECT STATEMENT | | 3 | 66 | 6
a n s
|
|
1 | HASH JOIN
2 |
|
TABLE ACCESS BY INDEX ROWID| EMP
|
|
3 |
3 |
66 |
27 |
6
2 n -tr
o
| 3 | INDEX FULL SCAN | EDEPT | 14 | |
s an1
| 4 | TABLE ACCESS FULL | DEPT | 4 |
) h a deฺ
52 | 3

r ฺ e s Gui
---------------------------------------------------------------------

mi dent
Predicate: 1 - access("E"."DEPTNO"="D"."DEPTNO")
p ฺ
2 - filter("ENAME" LIKE 'A%')
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Hash Join nio
A nto a hash join between two row sources, the system reads the first data set and builds an
To perform
e of hash buckets in memory. A hash bucket is little more than a location that acts as the starting
Josarray
point for a linked list of rows from the build table. 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.
The system starts to read the second set of rows, using whatever access mechanism is most
appropriate for acquiring the rows, and uses the same hash function on the join column or columns to
calculate the number of the relevant hash bucket. The system then checks to see if there are any rows
in that bucket. This is known as probing the hash table.
If there are no rows in the relevant bucket, the system can immediately discard the row from the
probe table.
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. Any rows that survive the exact check can immediately be
reported (or passed on to the next step in the execution plan). So, when you perform a hash join, 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, and are most useful when joining large amount of
data.

Oracle Database 11g: SQL Tuning Workshop 4 - 59


Cartesian Join
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select ename, e.deptno, d.deptno, dname


from emp e, dept d where ename like 'A%';

--------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
n s e
--------------------------------------------------------------------
i ce
| 0 | SELECT STATEMENT | | 11 | 242 | 8 (0)|
b l el
| 1 | MERGE JOIN CARTESIAN| | 11 | 242 | 8 (0)|
fer a
| 2 | TABLE ACCESS FULL | EMP | 3 | 27 | 3 (0)|
a n s
| 3 | BUFFER SORT | | 4 | 52 | 5 (0)|
n -tr
| 4 | TABLE ACCESS FULL | DEPT | 4 | 52 | 2 (0)|
o
an
--------------------------------------------------------------------
s
Predicate Information (identified by operation id):
) h a deฺ
e s Gui
---------------------------------------------------
r ฺ
2 - filter("ENAME" LIKE 'A%')
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Cartesian Join
o io
A nt join is used when one or more of the tables does not have any join conditions to any
A Cartesian
e tables in the statement. The optimizer joins every row from one data source with every row
Josother
from the other data source, creating the Cartesian product of the two sets.
A Cartesian join can be seen as a nested loop with no elimination; the first row source is read and
then for every row, all the rows are returned from the other row source.
Note: Cartesian join is generally not desirable. However, it is perfectly acceptable to have one with
single-row row source (guaranteed by an unique index for example) joined to some other table.

Oracle Database 11g: SQL Tuning Workshop 4 - 60


Join Types

• A join operation combines the output from two row sources


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

and returns one resulting row source.


• Join operation types include the following :
– Join (Equijoin/Natural – Nonequijoin)
– Outer join (Full, Left, and Right)
– Semi join: EXISTS subquery
n s e
– Anti join: NOT IN subquery i ce
– Star join (Optimization) b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Join Typesnio
nto types include the following:
JoinAoperation
e
Jos •• 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. Find one match in the inner table,
then stop search.
• Anti join: Returns rows with no match in the NOT IN subquery. Stop as soon as one match is
found.
• Star join: This is not a join type, but just a name for an implementation of a performance
optimization to better handle the fact and dimension model.
Antijoin and semijoin are considered to be join types, even though the SQL constructs that cause
them are subqueries. 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.

Oracle Database 11g: SQL Tuning Workshop 4 - 61


Equijoins and Nonequijoins

SELECT e.ename, e.sal, s.grade


FROM emp e ,salgrade s
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHERE e.sal = s.hisal;

--------------------------------------- Equijoin
| Id | Operation | Name |
---------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH JOIN | |
| 2 | TABLE ACCESS FULL| EMP |
| 3 | TABLE ACCESS FULL| SALGRADE |
--------------------------------------- SELECT e.ename, e.sal, s.grade
n s e
FROM emp e ,salgrade s
i ce
el
1 - access("E"."SAL"="S"."HISAL")
WHERE e.sal between s.hisal and s.hisal;
a b l
fer
a n s
---------------------------------------

tr
| Id | Operation | Name |
|
n
0 | SELECT STATEMENT
o - | |

an
| 1 | NESTED LOOPS | |

Nonequijoin
|
|
2 |
h
3 | s
TABLE ACCESS FULL| EMP
a deฺ
TABLE ACCESS FULL| SALGRADE |
|

e )
s Gui
---------------------------------------
r ฺ
mi dent
3 - filter("E"."SAL">="S"."HISAL" AND

p ฺ
g Stu
"E"."SAL"<="S"."HISAL")

d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
Equijoins and
o n Nonequijoins io A
TheA join
t
n condition determines whether a join is an equijoin or a nonequijoin. An equijoin is a join
e
Joswith a join condition containing an equality operator. When a join condition relates two tables by an
operator other than equality, it is a nonequijoin.
Equijoins are the most commonly used. An example each of an equijoin and a nonequijoin are shown
in the slide. Nonequijoins are less frequently used.
To improve SQL efficiency, use equijoins whenever possible. Statements that perform equijoins on
untransformed column values are the easiest to tune.
Note: If you have a nonequijoin, a hash join is not possible.

Oracle Database 11g: SQL Tuning Workshop 4 - 62


Outer Joins

An outer join also returns a row SELECT d.deptno,d.dname,e.empno,e.ename


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

if no match is found.
FROM emp e, dept d
WHERE e.deptno(+)=d.deptno;
EMP
DEPT ---------------------------------------
20
10 | Id | Operation | Name |
10 ---------------------------------------
20 | 0 | SELECT STATEMENT | |
20
30 | 1 | HASH JOIN RIGHT OUTER | |
30 | 2 | TABLE ACCESS FULL | EMP |
40 | 3 | TABLE ACCESS FULL | DEPT |
10
-----------------------------------
1 - access("E"."DEPTNO"(+)="D"."DEPTNO")
n s e
i ce
el
SELECT d.deptno,d.dname,e.empno,e.ename
FROM emp e, dept d SELECT d.deptno,d.dname,e.empno,e.ename
FROM emp e, dept d
a b l
r
WHERE e.deptno(+)=d.deptno;

----------------------------------------------
WHERE e.deptno(+)=d.deptno;

n s fe
| Id | Operation | Name |
-tr a
-----------------------------------
---------------------------------------------- | Id | Operation
o n | Name |

an
| 0 | SELECT STATEMENT | | -----------------------------------
| 0 | SELECT STATEMENT | |
|
|
1 | NESTED LOOPS OUTER
2 | TABLE ACCESS FULL
|
| DEPT |
|
|
h s
a deฺ
1 | HASH JOIN OUTER | |
| 3 | TABLE ACCESS BY INDEX ROWID| EMP |

e )
s Gui
| 2 | TABLE ACCESS FULL| DEPT |
| 4 | INDEX RANGE SCAN

| EDEPT |
r
| 3 | TABLE ACCESS FULL| EMP |

mi dent
---------------------------------------------- -----------------------------------

p ฺ
4 - access("E"."DEPTNO"(+)="D"."DEPTNO")
g Stu
1 - access("E"."DEPTNO"(+)="D"."DEPTNO")

d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Outer Joinsnio
TheA nto join is the most commonly used within the system. Other joins open up extra
simple
e
Josfunctionality, but have much more specialized uses. The outer join operator is placed on the deficient
side of the query. In other words, it is placed against the table that has the missing join information.
Consider EMP and DEPT. There may be a department that has no employees. If EMP and DEPT are
joined together, this particular department would not appear in the output because there is no row
that matches the join condition for that department. By using the outer join, the missing department
can be displayed.
• Outer join with nested loops: The left/driving table is always the table whose rows are being
preserved (DEPT in the example). For each row from DEPT, look for a matching rows in EMP. If
none is found, output DEPT values with null values for the EMP columns. If rows are found,
output DEPT values with these EMP values.
• Hash Outer joins: The left/outer table whose rows are being preserved is used to build the hash
table, and the right/inner table is used to probe the hash table. When a match is found, the row is
output and the entry in the hash table is marked as matched to a row. After the inner table is
exhausted, the hash table is read over once again, and any rows that are not marked as matched
are output with null values for the EMP columns. With Oracle Database 10g, HASH JOIN
RIGHT OUTER is available. The system hashes the table whose rows are not being preserved,
and then reads the table whose rows are being preserved, probing the hash table to see whether
there was a row to join to.
Note: You can also use the ANSI syntax for full, left, and right outer joins (not shown in the slide).
Oracle Database 11g: SQL Tuning Workshop 4 - 63
Semijoins

EMP
Semijoins only look for the first match. DEPT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

20
10
10
20
10
30
30
40
10

SELECT deptno, dname


FROM dept
n s e
ce
WHERE EXISTS (SELECT 1 FROM emp WHERE emp.deptno=dept.deptno);

eli
--------------------------------------------------------------------
a b l
fe r
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
a n s
--------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 105 | 7 (15)| n -tr
o
| 1 | HASH JOIN SEMI | | 3 | 105 |
an
7 (15)|
s
| 2 | TABLE ACCESS FULL| DEPT | 4 | 88 |
) h a deฺ
3 (0)|
| 3 | TABLE ACCESS FULL| EMP | 14 |
r ฺ e s Gui
182 | 3 (0)|

mi dent
--------------------------------------------------------------------
p ฺ
1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Semijoins nio
A ntoreturn a result when you hit the first joining record. A semijoin is an internal way of
Semijoins
e
Jostransforming an EXISTS subquery into a join. However, you cannot see this occur anywhere.
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.
In the above diagram, for each DEPT record, only the first matching EMP record is returned as a join
result. This prevents scanning huge numbers of duplicate rows in a table when all you are interested
in is if there are any matches.
When the subquery is not unnested, a similar result could be achieved by using a FILTER operation
and scanning a row source until a match is found, then returning it.
Note: The optimizer can use nested loops, merge-join or hash-join algorithms to perform semijoins.
In addition, the new HASH JOIN RIGHT SEMI optimization is also possible.

Oracle Database 11g: SQL Tuning Workshop 4 - 64


Antijoins

Reverse of what would have been returned by a join


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EMP
DEPT
SELECT deptno, dname 20
10
FROM dept 10
20
WHERE deptno not in 20
30
(SELECT deptno FROM emp); 30
40
--------------------------------------- 10
| Id | Operation | Name |
---------------------------------------
| 0 | SELECT STATEMENT | |
SELECT deptno, dname
n s e
ce
FROM dept
| 1 | NESTED LOOPS ANTI | | WHERE deptno not in
eli
| 2 | TABLE ACCESS FULL| DEPT | (SELECT deptno FROM emp);
a b l
| 3 | INDEX RANGE SCAN | I_DEPTNO |
fer
-----------------------------------
---------------------------------------
| Id | Operation
a n s
| Name |
3 - access("DEPTNO"="DEPTNO")
n -tr
-----------------------------------
o
|
s an
0 | SELECT STATEMENT | |

)
|
h a deฺ
1 | HASH JOIN ANTI | |
EMP DEPT

r ฺ e s Gui
| 2 | TABLE ACCESS FULL| DEPT |

mi dent
| 3 | TABLE ACCESS FULL| EMP |

p ฺ
g Stu
-----------------------------------
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Antijoins nio
A ntoreturn rows that fail to match (NOT IN) the subquery at the right side. For example, an
Antijoins
e
Josantijoin can select a list of departments which do not have any employee.
The optimizer uses a nested loops algorithm for NOT IN subqueries by default. However, if the
MERGE_AJ, HASH_AJ, or NL_AJ hints are used and various required conditions are met, the NOT
IN uncorrelated subquery can be changed into a sort merge or hash antijoin. Another possible
optimization that was introduced in Oracle Database 11g is to perform a HASH [RIGTH] ANTI NA
(NA for Null Aware) operation.
Although antijoins are mostly transparent to the user, it is useful to know that these join types exist
and could help explain unexpected performance changes between releases.

Oracle Database 11g: SQL Tuning Workshop 4 - 65


Other N-Array Operations

• FILTER
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• CONCATENATION
• UNION ALL/UNION
• INTERSECT
• MINUS
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 66


Filter Operations

• Accepts a set of rows


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Eliminates some of them


• Returns the rest
SELECT deptno, dname
SELECT deptno, sum(sal) SUM_SAL
FROM dept d WHERE NOT EXISTS
FROM emp
(select 1 from emp e
GROUP BY deptno
HAVING sum(sal) > 9000;
where e.deptno=d.deptno);

n s e
i ce
el
------------------------------------
------------------------------------
| Id | Operation | Name |
| Id | Operation | Name |
a b l
------------------------------------
fer
------------------------------------

| 0 | SELECT STATEMENT | |
| 0 | SELECT STATEMENT
a
|
n s
| 1 | FILTER | |
|
|
1 | FILTER
2 |
n -tr |
TABLE ACCESS FULL| DEPT
o
an
| 2 | HASH GROUP BY | |
| 3 | INDEX RANGE SCAN |I_DEPTNO
| 3 | TABLE ACCESS FULL| EMP |
------------------------------------
h s
------------------------------------
a deฺ
1 - filter(SUM("SAL")>9000)
e )
s Gui
1 - filter( NOT EXISTS

r ฺ (SELECT 0 FROM "EMP" "E" WHERE

p ฺ mi dent "E"."DEPTNO"=:B1))
3 - access("E"."DEPTNO"=:B1)
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Filter Operations
o
A
A FILTERnt operation is any operation that discards rows returned by another step, but is not involved
eretrieving the rows itself. All sorts of operations can be filters, including subqueries and single
Josintable predicates.
In the example at the left, FILTER applies to the groups that are created by the GROUP BY
operation.
In the example at the right, FILTER is almost used in the same way as NESTED LOOPS. DEPT is
accessed once, and for each row from DEPT, EMP is accessed by its index on DEPTNO (See
operation 3.)
This operation is done as many times as the number of rows in DEPT.
The FILTER operation is applied, for each row, after DEPT rows are fetched. The FILTER discards
rows for which operation 3 returned at least one row (NOT EXIST (select 0 from emp
where emp.deptno=:B1) is TRUE.

Oracle Database 11g: SQL Tuning Workshop 4 - 67


Concatenation Operation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM emp WHERE deptno=1 or sal=2;

--------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
--------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8 | 696 |
| 1 | CONCATENATION | | | |
n s e
ce
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 4 | 348 |
| 3 | INDEX RANGE SCAN | I_SAL | 2 | |
eli
| 4 | TABLE ACCESS BY INDEX ROWID| EMP | 4 | 348 |
a b l
| 5 | INDEX RANGE SCAN | I_DEPTNO | 2 | | fer
a n s
tr
--------------------------------------------------------------------
Predicate Information (identified by operation id):
o n -
---------------------------------------------------
s an
3 - access("SAL"=2)
) h a deฺ
4 - filter(LNNVL("SAL"=2))
r ฺ e s Gui
mi dent
5 - access("DEPTNO"=1)
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Concatenation
o n io Operation
A nt
CONCATENATION concatenates the rows returned by two or more row sets. This works like UNION
s e
Jo ALL and does not remove duplicate rows.
It is used with OR expansions. However, OR does not return duplicate rows, so for each component
after the first, it appends a negation of the previous components (LNNVL):
CONCATENATION
- BRANCH 1 - SAL=2
- BRANCH 2 - DEPTNO = 1 AND NOT row in Branch 1
The LNNVL function is generated by the OR clause to process this negation.
The LNNVL() function returns TRUE, if the predicate is NULL or FALSE.
So filter (LNNVL(SAL=2)) returns all rows for which SAL != 2 or SAL is NULL.

Oracle Database 11g: SQL Tuning Workshop 4 - 68


UNION [ALL], INTERSECT, MINUS
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SORT UNIQUE
UNION UNION-ALL 1 3 4 1 3 4
UNION ALL INDEX FULL SCAN 2 3 2 3
INDEX FAST FULL SCAN 5 5

INTERSECTION
SORT UNIQUE NOSORT
3
INTERSECT INDEX FULL SCAN
2
1
3
4
n s e
SORT UNIQUE 5
i ce
INDEX FAST FULL SCAN
b l el
fe r a
MINUS
a n s
SORT UNIQUE NOSORT
o -tr
1n 3
a n2 3
MINUS INDEX FULL SCAN 4
SORT UNIQUE
h s
a deฺ 5
INDEX FAST FULL SCAN
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
UNION [ALL],
o n io INTERSECT, MINUS
nt duplicate rows with an ALL or DISTINCT modifier in different places in the language.
SQLAhandles
e preserves duplicates and DISTINCT removes them. Here is a quick description of the possible
JosSQL
ALL
set operations:
• INTERSECTION: Operation accepting two sets of rows and returning the intersection of the
sets, eliminating duplicates. Subrow sources are executed or optimized individually. This is very
similar to sort-merge-join processing: Full rows are sorted and matched.
• MINUS: Operation accepting two sets of rows and returning rows appearing in the first set, but
not in the second, eliminating duplicates. Subrow sources are executed or optimized
individually. Similar to INTERSECT processing. However, instead of match-and-return, it is
match-and-exclude.
• UNION: Operation accepting two sets of rows and returning the union of the sets, eliminating
duplicates. Subrow sources are executed or optimized individually. Rows retrieved are
concatenated and sorted to eliminate duplicate rows.
• UNION ALL: Operation accepting two sets of rows and returning the union of the sets, and not
eliminating duplicates. The expensive sort operation is not necessary. Use UNION ALL if you
know you do not have to deal with duplicates.

Oracle Database 11g: SQL Tuning Workshop 4 - 69


Result Cache Operator
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EXPLAIN PLAN FOR


SELECT /*+ RESULT_CACHE */ department_id, AVG(salary)
FROM employees
GROUP BY department_id;

--------------------------------------------------------------
n s e
ce
| Id | Operation | Name |Rows
el i
--------------------------------------------------------------
a b l
s f er| 11
| 0 | SELECT STATEMENT |
| 1 | RESULT CACHE | 8fpza04gtwsfr6n595au15yj4y
- t r an |
| 2 | HASH GROUP BY |
n on | 11
TABLE ACCESS FULL| EMPLOYEES a
| 3 |
a s ฺ | 107
) h
--------------------------------------------------------------
s u i de
m i rฺe nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Result Cache i oOperator
TheA ntoquery result cache enables explicit caching of query result sets and query fragments in
SQL
e
Josdatabase memory. A dedicated memory buffer stored in the shared pool can be used for storing and
retrieving the cached results. The query results stored in this cache become invalid when data in the
database objects that are accessed by the query is modified. Although the SQL query cache can be
used for any query, good candidate statements are the ones that need to access a very high number of
rows to return only a fraction of them. This is mostly the case for data warehousing applications.
If you want to use the query result cache and the RESULT_CACHE_MODE initialization parameter is
set to MANUAL, you must explicitly specify the RESULT_CACHE hint in your query. This introduces
the ResultCache operator into the execution plan for the query. When you execute the query, the
ResultCache operator looks up the result cache memory to check whether the result for the query
already exists in the cache. If it exists, the result is retrieved directly out of the cache. If it does not
yet exist in the cache, the query is executed, the result is returned as output, and is also stored in the
result cache memory.
If the RESULT_CACHE_MODE initialization parameter is set to FORCE, and you do not want to store
the result of a query in the result cache, you must then use the NO_RESULT_CACHE hint in your
query.

Oracle Database 11g: SQL Tuning Workshop 4 - 70


Summary

In this lesson, you should have learned to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Describe most of the SQL operators


• List the possible access paths
• Explain how join operations are performed

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 71


Practice 4: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Using different access paths for better optimization


– Case 14 to case 16
• Using the result cache

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 4 - 72


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Interpreting Execution Plans

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Gather execution plans


• Display execution plans
• Interpret execution plans

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 2


What Is an Execution Plan?

• The execution plan of a SQL statement is composed of small


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

building blocks called row sources for serial execution plans.


• The combination of row sources for a statement is called the
execution plan.
• By using parent-child relationships, the execution plan can
be displayed in a tree-like structure (text or graphical).
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
What Is an n
o io
Execution Plan?
An A n t
execution plan is the output of the optimizer and is presented to the execution engine for
s e
Jo implementation. It instructs the execution engine about the operations it must perform for retrieving
the data required by a query most efficiently.
The EXPLAIN PLAN statement gathers execution plans chosen by the Oracle optimizer for the
SELECT, UPDATE, INSERT, and DELETE statements. The steps of the execution plan are not
performed in the order in which they are numbered. There is a parent-child relationship between
steps. The row source tree is the core of the execution plan. 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, such as filter, sort, or aggregation
In addition to the row source tree (or data flow tree for parallel operations), the plan table contains
information about the following:
• Optimization, such as the cost and cardinality of each operation
• Partitioning, such as the set of accessed partitions
• Parallel execution, such as the distribution method of join inputs
The EXPLAIN PLAN results help you determine whether the optimizer selects a particular
execution plan, such as nested loops join.

Oracle Database 11g: SQL Tuning Workshop 5 - 3


Where to Find Execution Plans?

• PLAN_TABLE (EXPLAIN PLAN or SQL*Plus autotrace)


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• V$SQL_PLAN (Library Cache)


• V$SQL_PLAN_MONITOR (11g)
• DBA_HIST_SQL_PLAN (AWR)
• STATS$SQL_PLAN (Statspack)
• SQL Management Base (SQL Plan Management n s e
Baselines) lice
a b le
• SQL tuning set
s f er
• Trace files generated by DBMS_MONITOR -tran
n o n
• Event 10053 trace file a
a s
h 10gR2 eฺ
• Process state dump trace filessince ) i d
i r ฺe nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Where to Find
o n ioExecution Plans?
A
There naret many ways to retrieve execution plans inside the database. The most well-known ones are
e
Joslisted in the slide: • The EXPLAIN PLAN command enables you to view the execution plan that the optimizer
might use to execute a SQL statement. 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.
• V$SQL_PLAN provides a way to examine the execution plan for cursors that were recently
executed. Information in V$SQL_PLAN is very similar to the output of an EXPLAIN PLAN
statement. However, while EXPLAIN PLAN shows a theoretical plan that can be used if this
statement was executed, V$SQL_PLAN contains the actual plan used.
• V$SQL_PLAN_MONITOR displays plan-level monitoring statistics for each SQL statement
found in V$SQL_MONITOR. Each row in V$SQL_PLAN_MONITOR corresponds to an
operation of the execution plan that is monitored.
• The Automatic Workload Repository (AWR) infrastructure and statspack reports store plans
from top SQLs. Plans are recorded into DBA_HIST_SQL_PLAN and STATS$SQL_PLAN.
• Plan and row source operations are dumped in trace files generated by DBMS_MONITOR.

Oracle Database 11g: SQL Tuning Workshop 5 - 4


Where to Find Execution Plan? (continued)
• The SQL management base (SMB) is a part of the data dictionary that resides in the SYSAUX
tablespace. It stores statement log, plan histories, and SQL plan baselines, as well as SQL
profiles.
• The event 10053, which is used to dump cost-based optimizer (CBO) computations may include
a plan.
• Starting with Oracle Database 10g, Release 2, when you dump process state (or errorstack from
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

a process), execution plans are included in the trace file that is generated.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 5


Viewing Execution Plans

• The EXPLAIN PLAN command followed by:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– SELECT from PLAN_TABLE


– DBMS_XPLAN.DISPLAY()
• SQL*Plus Autotrace: SET AUTOTRACE ON
• DBMS_XPLAN.DISPLAY_CURSOR()
• DBMS_XPLAN.DISPLAY_AWR() s e
ce n
• DBMS_XPLAN.DISPLAY_SQLSET()
le li
• DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE()fer a b
ans
n - t r
a no
h a s eฺ
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io Plans
Viewing Execution
o
TheA nt
DBMS_XPLAN package supplies five table functions:
s e To format and display the contents of a plan table
Jo •• DISPLAY:
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

Oracle Database 11g: SQL Tuning Workshop 5 - 6


The EXPLAIN PLAN Command

• Generates an optimizer execution plan


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Stores the plan in PLAN_TABLE


• Does not execute the statement itself

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
The EXPLAIN
o n ioPLAN Command
TheA nt
EXPLAIN PLAN command is used to generate the execution plan that the optimizer uses to
s e
Jo used,
execute a SQL statement. It does not execute the statement, but simply produces the plan that may be
and inserts this plan into a table. If you examine the plan, you can see how the Oracle Server
executes the statement.
Using EXPLAIN PLAN
• First use the EXPLAIN PLAN command to explain a SQL statement.
• Then retrieve the plan steps by querying PLAN_TABLE.
PLAN_TABLE is automatically created as a global temporary table to hold the output of an
EXPLAIN PLAN statement for all users. PLAN_TABLE is the default sample output table into
which the EXPLAIN PLAN statement inserts rows describing execution plans.
Note: You can create your own PLAN_TABLE using the
$ORACLE_HOME/rdbms/admin/utlxplan.sql script if you want to keep the execution plan
information for a long term.

Oracle Database 11g: SQL Tuning Workshop 5 - 7


The EXPLAIN PLAN Command
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EXPLAIN PLAN
SET STATEMENT_ID
= 'text'

n s e
i ce
INTO your plan table
b l el
fer a
a n s
n -tr
FOR statement o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nioPLAN Command (continued)
The EXPLAIN
to
n
ThisAcommand inserts a row in the plan table for each step of the execution plan.
s e
o In the syntax diagram in the slide, the fields in italics have the following meanings:
J
Field Meaning
text This is an optional identifier for the statement. You should
enter a value to identify each statement so that you can later
specify the statement that you want explained. This is
especially important when you share the plan table with
others, or when you keep multiple execution plans in the
same plan table.

schema.table This is the optional name for the output table. The default
is PLAN_TABLE.

statement This is the text of the SQL statement.

Oracle Database 11g: SQL Tuning Workshop 5 - 8


The EXPLAIN PLAN Command: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> EXPLAIN PLAN


2 SET STATEMENT_ID = 'demo01' FOR
3 SELECT e.last_name, d.department_name
4 FROM hr.employees e, hr.departments d
5 WHERE e.department_id = d.department_id;

Explained. n s e
i ce
b l el
SQL>
fer a
a n s
Note: The EXPLAIN PLAN command does n - actually
not
tr
o
execute the statement. s an
a ฺ h de
s ) u i
m i rฺe nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
nioPLAN Command: Example
The EXPLAIN
to
n
ThisAcommand inserts the execution plan of the SQL statement in the plan table and adds the optional
s e
o demo01 name tag for future reference. You can also use the following syntax:
J EXPLAIN PLAN
FOR
SELECT e.last_name, d.department_name
FROM hr.employees e, hr.departments d
WHERE e.department_id =d.department_id;

Oracle Database 11g: SQL Tuning Workshop 5 - 9


PLAN_TABLE

• PLAN_TABLE:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Is automatically created to hold the EXPLAIN PLAN output.


– You can create your own using utlxplan.sql.
– Advantage: SQL is not executed
– Disadvantage: May not be the actual execution plan
• PLAN_TABLE is hierarchical. e
n s
• Hierarchy is established with the ID and PARENT_ID i ce
columns. b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio
PLAN_TABLE
to
Anare various available methods to gather execution plans. Now, you are introduced only to the
There
e
JosEXPLAIN PLAN statement. This SQL statement gathers the execution plan of a SQL statement
without executing it, and outputs its result in the PLAN_TABLE table. Whatever the method to
gather and display the explain plan, the basic format and goal are the same. However, PLAN_TABLE
just shows you a plan that might not be the one chosen by the optimizer. PLAN_TABLE is
automatically created as a global temporary table and is visible to all users. PLAN_TABLE is the
default sample output table into which the EXPLAIN PLAN statement inserts rows describing
execution plans. 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. While a PLAN_TABLE table is automatically set up for each user, you can use the
utlxplan.sql SQL script to manually create a local PLAN_TABLE in your schema and use it to
store the results of EXPLAIN PLAN. The exact name and location of this script depends on your
operating system. On UNIX, it is located in the $ORACLE_HOME/rdbms/admin directory. 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. This can cause scripts to fail or cause TKPROF to
fail, if you are specifying the table.
Note: If you want an output table with a different name, first create PLAN_TABLE manually with
the utlxplan.sql script, and then rename the table with the RENAME SQL statement.

Oracle Database 11g: SQL Tuning Workshop 5 - 10


Displaying from PLAN_TABLE: Typical

SQL> EXPLAIN PLAN SET STATEMENT_ID = 'demo01' FOR SELECT * FROM emp
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2 WHERE ename = 'KING';

Explained.

SQL> SET LINESIZE 130


SQL> SET PAGESIZE 0
SQL> select * from table(DBMS_XPLAN.DISPLAY());

Plan hash value: 3956160932


n s e
i ce
el
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
a b l
--------------------------------------------------------------------------
fe r
| 0 | SELECT STATEMENT | | 1 | 37 | 3
n s
(0)| 00:00:01 |
a
o n -r
(0)| t00:00:01 ||* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 3

an
--------------------------------------------------------------------------

h s
a deฺ
Predicate Information (identified by operation id):

e )
s Gui

---------------------------------------------------
r
1 - filter("ENAME"='KING')
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Displaying n i
fromo APLAN_TABLE: Typical
to in the slide, the EXPLAIN PLAN command inserts the execution plan of the SQL
nexample
A
In the
e
Josstatement in PLAN_TABLE and adds the optional demo01 name tag for future reference. It is important to note that the SELECT statement itself is not executed, but is simply optimized to
determine the best execution plan in that context. The DISPLAY function of the DBMS_XPLAN
package can be used to format and display the last statement stored in PLAN_TABLE. You can also
use the following syntax to retrieve the same result: SELECT * FROM
TABLE(dbms_xplan.display('plan_table','demo01','typical',null));
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. TYPICAL displays
the most relevant information in the plan: operation ID, name and option, number of rows, bytes, and
optimizer cost. The last parameter for the DISPLAY function is the one corresponding to
filter_preds. This parameter represents a filter predicate or predicates to restrict the set of rows
selected from the table where the plan is stored. When value is null (the default), the plan displayed
corresponds to the last executed explain plan. This parameter can reference any column of the table
where the plan is stored and can contain any SQL construct, for example, subquery or function calls.
Note: Alternatively, you can run the utlxpls.sql (or utlxplp.sql for parallel queries) script
(located in the ORACLE_HOME/rdbms/admin/ directory) to display the execution plan stored in
PLAN_TABLE for the last statement explained. This script uses the DISPLAY table function from the
DBMS_XPLAN package.

Oracle Database 11g: SQL Tuning Workshop 5 - 11


Displaying from PLAN_TABLE: ALL

SQL> select * from table(DBMS_XPLAN.DISPLAY(null,null,'ALL'));


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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):
n s e
i ce
el
-------------------------------------------------------------
1 - SEL$1 / EMP@SEL$1
a b l
Predicate Information (identified by operation id):
fe r
---------------------------------------------------
a n s
o n -tr 1 - filter("ENAME"='KING')

an
Column Projection Information (identified by operation id):
-----------------------------------------------------------
h s eฺ
a "EMP"."JOB"[VARCHAR2,9],
1 - "EMP"."EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10],
) i d
i r ฺes nt Gu"EMP"."SAL"[NUMBER,22],
"EMP"."MGR"[NUMBER,22], "EMP"."HIREDATE"[DATE,7],

g p ฺm ude
"EMP"."COMM"[NUMBER,22], "EMP"."DEPTNO"[NUMBER,22]

d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Displaying n
o io PLAN_TABLE: ALL
from
A
Here nt use the same EXPLAIN PLAN command example as in the previous slide. The ALL
you
e
Josoption used with the DISPLAY function allows you to output the maximum user level information. It
includes information displayed with the TYPICAL level, with additional information such as
PROJECTION, ALIAS, and information about REMOTE SQL, if the operation is distributed.
For finer control on the display output, the following keywords can be added to the format parameter
to customize its default behavior. 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). Format keywords must be separated by either a comma or a space:
• ROWS: If relevant, shows the number of rows estimated by the optimizer
• BYTES: If relevant, shows the number of bytes estimated by the optimizer
• COST: If relevant, shows optimizer cost information
• PARTITION: If relevant, shows partition pruning information
• PARALLEL: If relevant, shows PX information (distribution method and table queue
information)
• PREDICATE: If relevant, shows the predicate section
• PROJECTION: If relevant, shows the projection section
• ALIAS: If relevant, shows the “Query Block Name/Object Alias” section

Oracle Database 11g: SQL Tuning Workshop 5 - 12


Displaying from PLAN_TABLE: ALL (continued)
• REMOTE: If relevant, shows the information for the distributed query (for example, remote from
serial distribution and remote SQL)
• NOTE: If relevant, shows the note section of the explain plan

If the target plan table also stores plan statistics columns (for example, it is a table used to capture the
content of the fixed view V$SQL_PLAN_STATISTICS_ALL), additional format keywords can be
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

used to specify which class of statistics to display when using the DISPLAY function. These
additional format keywords are IOSTATS, MEMSTATS, ALLSTATS and LAST.
Note: Format keywords can be prefixed with the “-” sign to exclude the specified information. For
example, “-PROJECTION” excludes projection information.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 13


Displaying from PLAN_TABLE: ADVANCED

select plan_table_output from table(DBMS_XPLAN.DISPLAY(null,null,'ADVANCED


-PROJECTION -PREDICATE -ALIAS'));
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
-------------
i ce
/*+
b l el
BEGIN_OUTLINE_DATA
fe ra
FULL(@"SEL$1" "EMP"@"SEL$1")
a n s
-tr
OUTLINE_LEAF(@"SEL$1")

o n ALL_ROWS

s an DB_VERSION('11.1.0.6')
OPTIMIZER_FEATURES_ENABLE('11.1.0.6')
) h a deฺ
IGNORE_OPTIM_EMBEDDED_HINTS
r ฺ e s Gui
mi dent
END_OUTLINE_DATA
*/
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
Displaying n i
fromo APLAN_TABLE: ADVANCED
TheA nto
ADVANCED format is available only from Oracle Database 10g, Release 2 and later versions.
s e
Jo 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.
This section may be useful if you want to reproduce a particular execution plan in a different
environment.
This is the same section, which is displayed in the trace file for event 10053.
Note: When the ADVANCED format is used with V$SQL_PLAN, there is one more section called
Peeked Binds (identified by position).

Oracle Database 11g: SQL Tuning Workshop 5 - 14


AUTOTRACE

• AUTOTRACE is a SQL*Plus facility.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Introduced with Oracle7.3


• Needs a PLAN_TABLE
• Needs the PLUSTRACE role to retrieve statistics from some
V$ views
• By default, it produces the execution plan and statistics after s e
n
running the query.
e l ice
l
• May not be the actual plan when using bind peeking erab
(recursive EXPLAIN PLAN) a n sf
r n -t
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
AUTOTRACEnio
to
When
e Anrunning SQL statements under SQL*Plus, you can automatically get a report on the execution
Josplan and the statement execution statistics. The report is generated after successful SQL DML (that
is, SELECT, DELETE, UPDATE and INSERT) statements. It is useful for monitoring and tuning the
performance of these statements.
To use this feature, you must have a PLAN_TABLE available in your schema and then have the
PLUSTRACE role granted to you. Database administrator (DBA) privileges are required to grant the
PLUSTRACE role. The PLUSTRACE role is created and granted to the DBA role by running the
supplied $ORACLE_HOME/sqlplus/admin/plustrce.sql script.
On some versions and platforms, this is run by the database creation scripts. If this is not the case on
your platform, connect as SYSDBA and run the plustrce.sql script.
The PLUSTRACE role contains the select privilege on three V$ views. These privileges are necessary
to generate AUTOTRACE statistics.
AUTOTRACE is an excellent diagnostic tool for SQL statement tuning. Because it is purely
declarative, it is easier to use than EXPLAIN PLAN.
Note: The system does not support EXPLAIN PLAN for statements performing implicit type
conversion of date bind variables. With bind variables in general, the EXPLAIN PLAN output might
not represent the real execution plan.

Oracle Database 11g: SQL Tuning Workshop 5 - 15


The AUTOTRACE Syntax
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

OFF
SET AUTOTRACE ON
TRACE[ONLY]

n s e
i ce
EXPLAIN
b l el
STATISTICS
fer a
a n s
n -tr
o
SHOW AUTOTRACE s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
The AUTOTRACE
o n io Syntax
YouAcannt enable AUTOTRACE in various ways using the syntax shown in the slide. The command
e are as follows:
Josoptions
• 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, but does not display statistics
• STATISTICS: Displays statistics, but does not display execution plans

Note: If both the EXPLAIN and STATISTICS command options are omitted, execution plans and
statistics are displayed by default.

Oracle Database 11g: SQL Tuning Workshop 5 - 16


AUTOTRACE: Examples

• To start tracing statements using AUTOTRACE:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set autotrace on

• To display the execution plan only without execution:


SQL> set autotrace traceonly explain
n s e
i ce
• To display rows and statistics:
b l el
fe r a
SQL> set autotrace on statistics
a n s
o n -tr
• To get the plan and the statistics onlys(suppress an rows):
h a e ฺ
SQL> set autotrace traceonly e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
AUTOTRACE: n i oA
Examples
ntocontrol the report by setting the AUTOTRACE system variable. The following are some
YouAcan
e
Josexamples: • SET AUTOTRACE ON: The AUTOTRACE report includes both the optimizer execution plan
and the SQL statement execution statistics.
• SET AUTOTRACE TRACEONLY EXPLAIN: The AUTOTRACE report shows only the
optimizer execution path without executing the statement.
• SET AUTOTRACE ON STATISTICS: The AUTOTRACE report shows the SQL statement
execution statistics and rows.
• SET AUTOTRACE TRACEONLY: This is similar to SET AUTOTRACE ON, but it suppresses
the printing of the user’s query output, if any. If STATISTICS is enabled, the query data is still
fetched, but not printed.
• SET AUTOTRACE OFF: No AUTOTRACE report is generated. This is the default.

Oracle Database 11g: SQL Tuning Workshop 5 - 17


AUTOTRACE: Statistics
SQL> show autotrace
autotrace OFF
SQL> set autotrace traceonly statistics
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SELECT * FROM oe.products;

288 rows selected.

Statistics
--------------------------------------------------------
1334 recursive calls
n s e
ce
0 db block gets
686 consistent gets
eli
a b l
394 physical reads
fer
0 redo size
a n s
103919 bytes sent via SQL*Net to client
n -tr
o
an
629 bytes received via SQL*Net from client
21 SQL*Net roundtrips to/from client
h s
a deฺ
22 sorts (memory)
e )
s Gui
r ฺ
mi dent
0 sorts (disk)
288 rows processed
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
o
niStatistics
AUTOTRACE:
TheA n are recorded by the server when your statement executes and indicate the system
statistics
to
e
Josresources required to execute your statement. The results include the following statistics:
• recursive calls is the number of recursive calls generated at both the user and system
level. Oracle Database maintains tables used for internal processing. When Oracle Database
needs to make a change to these tables, it internally generates an internal SQL statement, which
in turn generates a recursive call.
• db block gets is the number of times a CURRENT block was requested.
• consistent gets is the number of times a consistent read was requested for a block.
• 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.
• redo size is the total amount of redo generated in bytes.
• bytes sent via SQL*Net to client is the total number of bytes sent to the client
from the foreground processes.
• bytes received via SQL*Net from client is the total number of bytes received
from the client over Oracle Net.
• SQL*Net roundtrips to/from client is the total number of Oracle Net messages
sent to and received from the client.
Note: The statistics printed by AUTOTRACE are retrieved from V$SESSTAT.

Oracle Database 11g: SQL Tuning Workshop 5 - 18


AUTOTRACE: Statistics (continued)
• sorts (memory) is the number of sort operations that were performed completely in
memory and did not require any disk writes.
• 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.
The client referred to in the statistics is SQL*Plus. Oracle Net refers to the generic process
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

communication between SQL*Plus and the server, regardless of whether Oracle Net is installed. You
cannot change the default format of the statistics report.
Note: db block gets indicates reads of the current block from the database. consistent
gets are reads of blocks that must satisfy a particular system change number (SCN). physical
reads indicates reads of blocks from disk. db block gets and consistent gets are the
two statistics that are usually monitored. These should be low compared to the number of rows
retrieved. Sorts should be performed in memory rather than on disk. e
n s
i ce
b l el
fer a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 19


Using the V$SQL_PLAN View

• V$SQL_PLAN provides a way of examining the execution


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

plan for cursors that are still in the library cache.


• 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.
– V$SQL_PLAN contains the actual plan used.
n s e
• It contains the execution plan of every cursor in the library ce
cache (including child). le li
a b
• Link to V$SQL: s f er
t n ra
on-
– ADDRESS, HASH_VALUE, and CHILD_NUMBER
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
to nio
Using the V$SQL_PLAN View
n provides a way of examining the execution plan for cursors that are still in the library
ThisAview
e
JosEXPLAIN
cache. The information in this view is very similar to the information in PLAN_TABLE. However,
PLAN shows a theoretical plan that can be used if this statement were to be executed,
whereas V$SQL_PLAN contains the actual plan used. The execution plan obtained by the EXPLAIN
PLAN statement can be different from the execution plan used to execute the cursor. This is because
the cursor might have been compiled with different values of session parameters.
V$SQL_PLAN shows the plan for a cursor rather than for all cursors associated with a SQL
statement. The difference is that a SQL statement can have more than one cursor associated with it,
with each cursor further identified by a CHILD_NUMBER. For example, the same statement executed
by different users has different cursors associated with it if the object that is referenced is in a
different schema. Similarly, different hints can cause different cursors. The V$SQL_PLAN table can
be used to see the different plans for different child cursors of the same statement.
Note: Another useful view is V$SQL_PLAN_STATISTICS, which provides the execution statistics
of each operation in the execution plan for each cached cursor. Also, 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.

Oracle Database 11g: SQL Tuning Workshop 5 - 20


The V$SQL_PLAN Columns

HASH_VALUE Hash value of the parent statement in the


library cache
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
i ce
PARENT_ID ID of the next execution step that operates on
b l el
the output of the current step
f er a
Number assigned to each step in the an s
ID
- t r
execution plan onn
a
asthe SQLฺ plan for the cursor
PLAN_HASH_VALUE Numerical representation of
h e
s )
rฺe nt G uid
m i
Note: This is onlygapฺpartialdlisting e of the columns.
d t u
i a @ his S
j e t© 2008, Oracle. All rights reserved.
ab Copyright
(
ia to u s
A b
The V$SQL_PLAN
o n io Columns
TheA nt contains almost all the PLAN_TABLE columns, in addition to others. The columns that are
view
e present in PLAN_TABLE have the same values:
Josalso
• ADDRESS
• HASH_VALUE

The ADDRESS and HASH_VALUE columns can be used to join with V$SQLAREA to add the cursor-
specific information.
The ADDRESS, HASH_VALUE, and CHILD_NUMBER columns can be used to join with V$SQL to
add the child cursor–specific information.
The PLAN_HASH VALUE column is a numerical representation of the SQL plan for the cursor. By
comparing one PLAN_HASH_VALUE with another, you can easily identify whether the two plans are
the same or not (rather than comparing the two plans line-by-line).
Note: Since Oracle Database 10g, SQL_HASH_VALUE in V$SESSION has been complemented
with SQL_ID, which you retrieve in many other V$ views. SQL_HASH_VALUE is a 32-bit value
and deemed to not be unique enough for large repositories of AWR data.
SQL_ID is a 64-bit hash value, which is more unique, the bottom 32 bits of which are
SQL_HASH_VALUE. It is normally represented as a character string to make it more manageable.

Oracle Database 11g: SQL Tuning Workshop 5 - 21


The V$SQL_PLAN_STATISTICS View

• V$SQL_PLAN_STATISTICS provides actual execution


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
actual execution statistics. ce
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o n io
The V$SQL_PLAN_STATISTICS View
TheA n t
V$SQL_PLAN_STATISTICS view provides the actual execution statistics for every operation
s e
Jo inoutput
the plan, such as the number of output rows, and elapsed time. All statistics, except the number of
rows, are cumulative. For example, the statistics for a join operation also include the statistics
for its two inputs. 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.
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.

Oracle Database 11g: SQL Tuning Workshop 5 - 22


Links Between Important
Dynamic Performance Views
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

V$SQLAREA V$SQL V$SQL_WORKAREA

V$SQLSTATS
Estimated statistics
for each row source

n s e
V$SQL_PLAN
i ce
b l el
Execution statistics
for each row source
fer a
a n s
o n -tr
V$SQL_PLAN_STATISTICS n
s a
) h a
i d eฺ
r s Gu
ฺeV$SQL_PLAN_STATISTICS_ALL
ฺ i
m dent
d p
g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Links Between
o n io Important Dynamic Performance Views
A nt displays statistics on shared SQL areas and contains one row per SQL string. It
V$SQLAREA
e
Josprovides statistics on SQL statements that are in memory, parsed, and ready for execution:
• SQL_ID is the SQL identifier of the parent cursor in the library cache.
• VERSION_COUNT is the number of child cursors that are present in the cache under this parent.

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.
• HASH_VALUE is the value of the parent statement in the library cache.
• SQL_ID is the SQL identifier of the parent cursor in the library cache.
• PLAN_HASH_VALUE is a numeric representation of the SQL plan for this cursor. By comparing
one PLAN_HASH_VALUE with another, you can easily identify if the two plans are the same or
not (rather than comparing the two plans line-by-line).
• CHILD_NUMBER is the number of this child cursor.

Statistics displayed in V$SQL are normally updated at the end of query execution. However, for
long-running queries, they are updated every five seconds. This makes it easy to see the impact of
long-running SQL statements while they are still in progress.

Oracle Database 11g: SQL Tuning Workshop 5 - 23


Links Between Important Dynamic Performance Views (continued)
V$SQL_PLAN contains the execution plan information for each child cursor loaded in the library
cache. The ADDRESS, HASH_VALUE, and CHILD_NUMBER columns can be used to join with
V$SQL to add the child cursor-specific information.
V$SQL_PLAN_STATISTICS provides execution statistics at the row source level for each child
cursor. The ADDRESS and HASH_VALUE columns can be used to join with V$SQLAREA to locate
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

the parent cursor. The ADDRESS, HASH_VALUE, and CHILD_NUMBER columns can be used to join
with V$SQL to locate the child cursor using this area.
V$SQL_PLAN_STATISTICS_ALL 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.
V$SQL_WORKAREA displays information about work areas used by SQL cursors. Each SQL
statement stored in the shared pool has one or more child cursors that are listed in the V$SQL view. se
V$SQL_WORKAREA lists all work areas needed by these child cursors. V$SQL_WORKAREA can c e
ben
el i
joined with V$SQLAREA on (ADDRESS, HASH_VALUE) and with V$SQL on (ADDRESS, b l
HASH_VALUE, CHILD_NUMBER).
s fera
You can use this view to find answers to the following questions:
- t r an
• What are the top 10 work areas that require the most cache area?
n on
• For work areas allocated in the AUTO mode, what percentage s a of work areas run using maximum
memory? ) h a
i d eฺ
V$SQLSTATS displays basic performance statistics i r ฺesfornSQL
t
u with each row representing the
Gcursors,
data for a unique combination of SQL text g p ฺmoptimizer
and u de plan (that is, unique combination of
SQL_ID and PLAN_HASH_VALUE). d t
S definitions for columns in V$SQLSTATS are
i a @ Thehicolumn
s
identical to those in the V$SQL
j a e t
b and V$SQLAREA views. However, the V$SQLSTATS view differs
from V$SQL and V$SQLAREA ( s
ia toinuthat it is faster, more scalable, and has a greater data retention (the
statistics may ostill
b
Aappear in this view, even after the cursor has been aged out of the shared pool).
n i
Note that
n toV$SQLSTATS contains a subset of columns that appear in V$SQL and V$SQLAREA.
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 24


Querying V$SQL_PLAN
SELECT PLAN_TABLE_OUTPUT FROM
TABLE(DBMS_XPLAN.DISPLAY_CURSOR('47ju6102uvq5q'));
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL_ID 47ju6102uvq5q, child number 0


-------------------------------------
SELECT e.last_name, d.department_name
FROM hr.employees e, hr.departments d WHERE
e.department_id =d.department_id

Plan hash value: 2933537672


--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU|
--------------------------------------------------------------------------------
n s e
| 0 | SELECT STATEMENT | | | | 6 (100|
i ce
|
|
1 | MERGE JOIN
2 |
|
TABLE ACCESS BY INDEX ROWID| DEPARTMENTS |
| 106 | 2862 |
27 | 432 |
6 (17|
2
b
(0| l el
| 3 | INDEX FULL SCAN | DEPT_ID_PK | 27 | |
fe
1 r a
(0|
|* 4 | SORT JOIN | | 107 | 1177 |
a n s 4 (25|

-tr
| 5 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0|

o n --------------------------------------------------------------------------------

an
Predicate Information (identified by operation id):

h s
a deฺ
---------------------------------------------------

4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
e )
s Gui
r ฺ
mi dent
filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

24 rows selected.
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
i oA
Querying V$SQL_PLAN
n
ntoquery V$SQL_PLAN using the DBMS_XPLAN.DISPLAY_CURSOR() function to display
YouAcan
e
Josthe current or last executed statement (as shown in the example). You can pass the value of SQL_ID
for the statement as a parameter to obtain the execution plan for a given statement. SQL_ID is the
SQL_ID of the SQL statement in the cursor cache. You can retrieve the appropriate value by
querying the SQL_ID column in V$SQL or V$SQLAREA. Alternatively, you could select the
PREV_SQL_ID column for a specific session out of V$SESSION. This parameter defaults to null
in which case the plan of the last cursor executed by the session is displayed. To obtain SQL_ID,
execute the following query:
SELECT e.last_name, d.department_name
FROM hr.employees e, hr.departments d
WHERE e.department_id =d.department_id;

SELECT SQL_ID, SQL_TEXT FROM V$SQL


WHERE SQL_TEXT LIKE '%SELECT e.last_name,%' ;

13saxr0mmz1s3 select SQL_id, sql_text from v$SQL …


47ju6102uvq5q SELECT e.last_name, d.department_name …
CHILD_NUMBER is the child number of the cursor to display. If not supplied, the execution plan of
all cursors matching the supplied SQL_ID parameter are displayed. CHILD_NUMBER can be
specified only if SQL_ID is specified.

Oracle Database 11g: SQL Tuning Workshop 5 - 25


Querying V$SQL_PLAN (continued)
The FORMAT parameter controls the level of detail for the plan. In addition to the standard values
(BASIC, TYPICAL, SERIAL, ALL, and ANDVANCED), 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), this format shows I/O statistics for ALL (or only for
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

LAST) executions of the cursor.


• MEMSTATS: Assuming that the Program Global Area (PGA) memory management is enabled
(that is, the pga_aggregate_target parameter is set to a nonzero value), this format
allows to display memory management statistics (for example, execution mode of the operator,
how much memory was used, number of bytes spilled to disk, and so on). These statistics only
apply to memory-intensive operations, such as hash joins, sort or some bitmap operators.
• ALLSTATS: A shortcut for 'IOSTATS MEMSTATS'
n s e
e l ice
• LAST: By default, plan statistics are shown for all executions of the cursor. The LAST keyword
can be specified to see only the statistics for the last execution.
ra bl
f e
a ns
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 5 - 26


Automatic Workload Repository (AWR)

• Collects, processes, and maintains performance statistics


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

for problem-detection and self-tuning purposes


• Statistics include:
– Object statistics
– Time-model statistics
– Some system and session statistics e
n s
l– Active Session History (ASH) statistics ice
• Automatically generates snapshots of the performanceradata ble
n s fe
n - tra
a no
h a s eฺ
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Automatic n i o A Repository (AWR)
Workload
TheA ntois part of the intelligent infrastructure introduced with Oracle Database 10g. This
AWR
e
Josinfrastructure is used by many components, such as Automatic Database Diagnostic Monitor (ADDM) for analysis. The AWR automatically collects, processes, and maintains system-
performance statistics for problem-detection and self-tuning purposes and stores the statistics
persistently in the database.
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, 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, based on criteria, such as elapsed
time, CPU time, buffer gets, and so on
• ASH statistics, representing the history of recent sessions

Oracle Database 11g: SQL Tuning Workshop 5 - 27


Automatic Workload Repository (continued)
The database automatically generates snapshots of the performance data once every hour and collects
the statistics in the workload repository. The data in the snapshot interval is then analyzed by
ADDM. The ADDM compares the differences between snapshots to determine 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Note: By using PL/SQL packages, such as DBMS_WORKLOAD_REPOSITORY or Oracle Enterprise


Manager, you can mange the frequency and retention period of SQL that is stored in the AWR.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 28


Managing AWR with PL/SQL

• Creating snapshots:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT ('ALL');

• Dropping snapshots:
SQL> exec DBMS_WORKLOAD_REPOSITORY.DROP_SNAPSHOT_RANGE –
(low_snap_id => 22, high_snap_id => 32, dbid => 3310949047);
n s e
i ce
• Managing snapshot settings:
b l el
fer a
SQL> exec DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS –
a n s
(retention => 43200, interval => 30, dbid => 3310949047);
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Managing AWR
o io with PL/SQL
A nt the primary interface for managing the AWR is Enterprise Manager, monitoring functions
Although
e be managed with procedures in the DBMS_WORKLOAD_REPOSITORY package.
oscan
J
Snapshots are automatically generated for an Oracle Database; however, you can use
DBMS_WORKLOAD_REPOSITORY procedures to manually create, drop, and modify the snapshots
and baselines that are used by the ADDM. Snapshots and baselines are sets of historical data for
specific time periods that are used for performance comparisons. To invoke these procedures, a user
must be granted the DBA role.
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. Here is an example:
Exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT ('ALL');
In this example, a snapshot for the instance is created immediately with the flush level specified to
the default flush level of TYPICAL. You can view this snapshot in the DBA_HIST_SNAPSHOT
view.

Oracle Database 11g: SQL Tuning Workshop 5 - 29


Managing AWR with PL/SQL (continued)
Dropping Snapshots
You can drop a range of snapshots using the DROP_SNAPSHOT_RANGE procedure. To view a list
of the snapshot IDs along with database IDs, check the DBA_HIST_SNAPSHOT view. For example,
you can drop the following range of snapshots:
Exec DBMS_WORKLOAD_REPOSITORY.DROP_SNAPSHOT_RANGE - (low_snap_id =>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

22, high_snap_id => 32, dbid => 3310949047);


In the example, the range of snapshot IDs to drop is specified from 22 to 32. The optional database
identifier is 3310949047. If you do not specify a value for dbid, the local database identifier is
used as the default value.
ASH data that belongs to the time period specified by the snapshot range is also purged when the
DROP_SNAPSHOT_RANGE procedure is called.
Modifying Snapshot Settings n s e
lic e
You can adjust the interval and retention of snapshot generation for a specified database ID.
b l e
However, note that this can affect the precision of the Oracle diagnostic tools. e r a
n
The INTERVAL setting specifies how often (in minutes) snapshots are automatically
a sf generated. The
RETENTION setting specifies how long (in minutes) snapshots are stored
o n -trin the workload repository.
To adjust the settings, use the MODIFY_SNAPSHOT_SETTINGS
s a nprocedure, as in the following
example:
) h a deฺ
ฺ e s Gui
Exec DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
r -retention
=> 43200, interval => 30, ฺdbid i t
m =>en3310949047);
d p d
g Stasu43,200 minutes (30 days), and the interval
In this example, the retention period is specified
i
between each snapshot is specified
b a@as 30thminutes.
is If NULL is specified, the existing value is
preserved. The optional
i a (ja uidentifier
database se is 3310949047. If you do not specify a value for dbid,
b
the local databaseAidentifier isto
used as the default value. You can check the current settings for your
database o i o
n with the DBA_HIST_WR_CONTROL view.
instance
n t
eA
Jos

Oracle Database 11g: SQL Tuning Workshop 5 - 30


Important AWR Views

• V$ACTIVE_SESSION_HISTORY
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• V$ metric views
• DBA_HIST views:
– DBA_HIST_ACTIVE_SESS_HISTORY
– DBA_HIST_BASELINE
DBA_HIST_DATABASE_INSTANCE e
n s
– DBA_HIST_SNAPSHOT i ce
– DBA_HIST_SQL_PLAN b l el
fer a
– DBA_HIST_WR_CONTROL
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Important AWR
o io Views
nt view the AWR data on Oracle Enterprise Manager screens or in AWR reports. However,
YouAcan
e
Josyou can also view the statistics directly from the following views:
V$ACTIVE_SESSION_HISTORY: This view displays active database session activity, sampled
once every second.
V$ metric views provide metric data to track the performance of the system. The metric views are
organized into various groups, such as event, event class, system, session, service, file, and
tablespace metrics. These groups are identified in the V$METRICGROUP view.
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.
• DBA_HIST_BASELINE displays information about the baselines captured in the system.
• DBA_HIST_DATABASE_INSTANCE displays information about the database environment.
• DBA_HIST_SNAPSHOT displays information about snapshots in the system.
• DBA_HIST_SQL_PLAN displays SQL execution plans.
• DBA_HIST_WR_CONTROL displays the settings for controlling AWR.

Oracle Database 11g: SQL Tuning Workshop 5 - 31


Querying the AWR

• Retrieve all execution plans stored for a particular SQL_ID.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE (DBMS_XPLAN.DISPLAY_AWR('454rug2yva18w'));

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------
SQL_ID 454rug2yva18w
--------------------
select /* example */ * from hr.employees natural join hr.departments

Plan hash value: 4179021502

----------------------------------------------------------------------------------
n s e
ce
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
el i
| 0 | SELECT STATEMENT | | | | 6 (100)| |
a b l
|
|
1 | HASH JOIN
2 |
| |
TABLE ACCESS FULL| DEPARTMENTS |
11 |
11 |
968 |
220 | 2
fe r
6 (17)| 00:00:01 |
(0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| DEPARTMENTS | 11 | 220 | 2
n s
(0)| 00:00:01 |
a
o n -tr | 3 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------

• Display all execution plans of all statements s a n containing “JF.”


) h a deฺ
SELECT tf.* FROM DBA_HIST_SQLTEXT ht, table
r ฺ e s Gui)) tf
mi dent
(DBMS_XPLAN.DISPLAY_AWR(ht.sql_id,null, null, 'ALL'
WHERE ht.sql_text like '%JF%';
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
i A
oAWR
Querying the n
ntouse the DBMS_XPLAN.DISPLAY_AWR() function to display all stored plans in the AWR.
YouAcan
e
JosIn the example in the slide, you pass in a SQL_ID as an argument. SQL_ID is the SQL_ID of the
SQL statement in the cursor cache. The DISPLAY_AWR() function also takes the
PLAN_HASH_VALUE, DB_ID, and FORMAT parameters.
The steps to complete this example are as follows:
1. Execute the SQL statement:
SQL> select /* example */ * from hr.employees natural
join hr.departments;
2. Query V$SQL_TEXT to obtain the SQL_ID:
SQL> select sql_id, sql_text from v$SQL
where sql_text
like '%example%';
SQL_ID SQL_TEXT
------------- -------------------------------------------
F8tc4anpz5cdb select sql_id, sql_text from v$SQL …
454rug2yva18w select /* example */ * from …
3. Using the SQL_ID, verify that this statement has been captured in the DBA_HIST_SQLTEXT
dictionary view. If the query does not return rows, it indicates that the statement has not yet been
loaded in the AWR.

Oracle Database 11g: SQL Tuning Workshop 5 - 32


Querying the AWR (continued)
SQL> SELECT SQL_ID, SQL_TEXT FROM dba_hist_sqltext WHERE SQL_ID ='
454rug2yva18w';
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:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> exec dbms_workload_repository.create_snapshot;

PL/SQL procedure successfully completed.

SQL> SELECT SQL_ID, SQL_TEXT FROM dba_hist_sqltext WHERE SQL_ID


=' 454rug2yva18w';
SQL_ID SQL_TEXT
-------------- ------------------------------- n s e
454rug2yva18w select /* example */ * from … lice e
r a bl
4. Use the DBMS_XPLAN.DISPLAY_AWR () function to retrieve the execution
n fe
s plan:
SQL>SELECT PLAN_TABLE_OUTPUT FROM TABLE -tr a
(DBMS_XPLAN.DISPLAY_AWR('454rug2yva18w’)); n o n
s a
) h a
i d eฺ
i r ฺes nt Gu
g p ฺm ude
d S t
@ s
j a b ia
e thi
b ia ( to us
o A
t o ni
A n
e
Jos

Oracle Database 11g: SQL Tuning Workshop 5 - 33


Generating SQL Reports from AWR Data

SQL> @$ORACLE_HOME/rdbms/admin/awrsqrpt
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Specify the Report Type …


Would you like an HTML report, 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: 6g1p4s9ra6ag8
Specify the Report Name …
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
GeneratingnSQL
o io Reports from AWR Data
t Database 10g, Release 2, it is possible to generate SQL reports from AWR data,
nOracle
A
Since
e
Jossprepsql.sql
basically, the equivalent to sqrepsql.sql with Statspack. In 10.1.0.4.0, the equivalent to
is not available in AWR. However in 10gR2, the equivalent of sprepsql.sql
is available. In 10gR2, the AWR SQL report can be generated by calling the
$ORACLE_HOME/rdbms/admin/awrsqrpt.sql file.
You can display the plan information in AWR by using the display_awr table function in the
dbms_xplan PL/SQL package.
For example, this displays the plan information for a SQL_ID in AWR:
select * from table(dbms_xplan.display_awr('6g1p4s9ra6ag8'));
You can retrieve the appropriate value for the SQL statement of interest by querying the SQL_ID in
DBA_HIST_SQLTEXT column.

Oracle Database 11g: SQL Tuning Workshop 5 - 34


SQL Monitoring: Overview
STATISTICS_LEVEL=TYPICAL|ALL

+
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CONTROL_MANAGEMENT_PACK_ACCESS=DIAGNOSTIC+TUNING

SQL monitoring
Every
second V$SQL_MONITOR

V$SQL_PLAN_MONITOR
n s e
i ce
Parallel
b l el
fer a
>5sec MONITOR n s
DBMS_SQLTUNE.REPORT_SQL_MONITOR
a
(CPU|IO) hint
n -tr
o
s an
V$SQL
h
V$SQL_PLAN
) a deฺ
NO_MONITOR
r ฺ s Gui
V$ACTIVE_SESSION_HISTORY
e
mi dent
hint V$SESSION_LONGOPS

p ฺ
g Stu
V$SESSION

d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
Abia to u
n
SQL Monitoring:
o io Overview
TheA nt monitoring feature is enabled by default when the STATISTICS_LEVEL initialization
SQL
e
Josparameter is either set to ALL or TYPICAL (the default value).
Additionally, 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.
By default, SQL monitoring is automatically started when a SQL statement runs parallel, or when it
has consumed at least five seconds of the CPU or I/O time in a single execution.
As mentioned, SQL monitoring is active by default. However, two statement-level hints are available
to force or prevent a SQL statement from being monitored. To force SQL monitoring, use the
MONITOR hint. To prevent the hinted SQL statement from being monitored, 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, an entry is added to the dynamic performance V$SQL_MONITOR view.
This entry tracks key performance metrics collected for the execution, including the elapsed time,
CPU time, number of reads and writes, I/O wait time, and various other wait times. These statistics
are refreshed in near real time as the statement executes, generally once every second.

Oracle Database 11g: SQL Tuning Workshop 5 - 35


SQL Monitoring: Overview (continued)
After the execution ends, monitoring information is not deleted immediately, but is kept in the
V$SQL_MONITOR view for at least one minute. The entry is eventually deleted so its space can be
reclaimed as new statements are monitored.
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:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

V$SQL, V$SQL_PLAN, V$ACTIVE_SESSION_HISTORY, V$SESSION_LONGOPS, and


V$SESSION
Instead, you can use the SQL monitoring report to view SQL monitoring data.
The SQL monitoring report is also available in a GUI version through Enterprise Manager.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 36


SQL Monitoring Report: Example
SQL> set long 10000000
SQL> set longchunksize 10000000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set linesize 200


SQL> select dbms_sqltune.report_sql_monitor from dual;

SQL Monitoring Report


In a different session
SQL Text
--------------------------
select count(*) from sales SQL> select count(*) from sales;

Global Information
Status : EXECUTING
n s e
ce
Instance ID : 1
Session ID : 125
eli
SQL ID : fazrk33ng71km
a b l
SQL Execution ID : 16777216
fer
Plan Hash Value : 1047182207
a n s
tr
Execution Started : 02/19/2008 21:01:18
First Refresh Time : 02/19/2008 21:01:22
o n -
an
Last Refresh Time : 02/19/2008 21:01:42
------------------------------------------------------------
| Elapsed | Cpu | IO | Other | Buffer | Reads |
h s
a deฺ
| Time(s) | Time(s) | Waits(s) | Waits(s) | Gets |
e )
s Gui |

r ฺ
------------------------------------------------------------
| 22 | 3.36 | 0.01 |
p ฺ mi dent
19 | 259K | 199K |

g Stu
------------------------------------------------------------
d
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Monitoring
o n io Report: Example
t
nexample,
e A
In this it is assumed that you SELECT from SALES from a different session than the one
s
Jo used to print the SQL monitoring report.
The DBMS_SQLTUNE.REPORT_SQL_MONITOR function accepts several input parameters to
specify the execution, the level of detail in the report, and the report type (TEXT, HTML, or XML). By
default, 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.
After the SELECT statement is started, and while it executes, you print the SQL monitoring report
from a second session.
From the report, you can see that the SELECT statement executes currently.
The Global Information section gives you some important information:
• To uniquely identify two executions of the same SQL statement, a composite key called an
execution key is generated. This execution key consists of three attributes, 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)
- A start execution time stamp (SQL_EXEC_START)
The report also shows you some important statistics calculated so far.

Oracle Database 11g: SQL Tuning Workshop 5 - 37


SQL Monitoring Report: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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 |
| | | | | | | |
n s e
ce
====================================================================================

l e li
==================================================================================
r a b
Starts | Rows | Activity | Activity Detail
| s
f e
| Progress |
| (Actual) | (percent) | (sample #)
t r n
a| |
1 | | |
o n - |
1 |
1 |
|
42081K |
|
100.00 | Cpu (4)
a n |
| 74%
|
|
s
a deฺ
==================================================================================
h
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
SQL Monitoringi o AReport: Example (continued)
TheA nto then displays the execution path currently used by your statement. SQL monitoring gives
report
e
Josyou the display of the current operation that executes in the plan. This enables you to detect parts of
the plan that are the most time consuming, so that you can focus your analysis on those parts. 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 Start Active column shows, in seconds, when the operation in the execution plan started relative
to the SQL statement execution start time. In this report, 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 Starts column shows the number of times the operation in the execution plan was executed.
The Rows (Actual) column indicates the number of rows produced, and the Rows (Estim) column
shows the estimated cardinality from the optimizer.
The Activity (percent) and Activity Detail (sample #) columns are derived by joining the
V$SQL_PLAN_MONITOR and V$ACTIVE_SESSION_HISTORY views. Activity (percent) shows
the percentage of database time consumed by each operation of the execution plan. Activity Detail
(sample#) shows the nature of that activity (such as CPU or wait event).

Oracle Database 11g: SQL Tuning Workshop 5 - 38


SQL Monitoring Report: Example (continued)
In this report, the Activity Detail (sample #) column shows that most of the database time, 100%, is
consumed by operation Id 2 (TABLE ACCESS FULL of SALES). So far, this activity consists of
4 samples, which are only attributed to CPU.
The last column, Progress, shows progress monitoring information for the operation from the
V$SESSION_LONGOPS view. In this report, it shows that, so far, the TABLE ACCESS FULL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

operation is 74% complete. This column only appears in the report after a certain amount of time,
and only for the instrumented row sources.
Note: Not shown by this particular report, the Memory and Temp columns indicate the amount of
memory and temporary space consumed by corresponding operation of the execution plan.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 39


Interpreting an Execution Plan

Transform it into a tree.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

id= 1 (pid= ) root/parent


id= 2 (pid=1) (pos=1) parent/child
id= 3 (pid=2) (pos=1) child/leaf
id= 4 (pid=2) (pos=2) parent/child
id= 5 (pid=4) (pos=1) child/leaf
id= 6 (pid=4) (pos=2) child/leaf
id= 7 (pid=1) (pos=2) parent/child
id= 8 (pid=7) (pos=1) child/leaf
id= 9 (pid=7) (pos=2) parent/child
id=10 (pid=9) (pos=1) child/leaf
n s e
Root/Parent Level 1lice
Executed first Executed next

a b le
From
top/down
sfer Level 2
an
Parent/Child Parent/Child

n - t r
no
From left/right

a
Parent/Childas Child/Leaf
Child/Leaf
) h i d eฺ Parent/Child Level 3

i r ฺes nt Gu
p m de Child/Leaf
ฺChild/Leaf Child/Leaf Level 4
d g S tu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Interpretingnan
o io Execution Plan
A
Explainntplan output is a representation of a tree of row sources.
e
JosEach step (line in the execution plan or node in the tree) represents a row source.
The explain plan utility indents nodes to indicate that they are the children of the parent above it.
The order of the nodes under the parent indicates the order of execution of the nodes within that
level. If two steps are indented at the same level, the first one is executed first.
In the tree format, the leaf at the left on each level of the tree is where the execution starts.
The steps of the execution plan are not performed in the order in which they are numbered. there is a
parent–child relationship between steps.
In PLAN_TABLE and V$SQL_PLAN, the important elements to retrieve the tree structure are the
ID, PARENT_ID, and POSITION columns. In a trace file, these columns correspond to the id,
pid, and pos fields, respectively.
One way to read an execution plan is by converting it into a graph that has a tree structure. You can
start from the top, with id=1, which is the root node in the tree. Next, you must find the operations
that feed this root node. That is accomplished by operations, which have parent_id or pid with
value 1.
Note: The course focuses on serial plans and does not discusses parallel execution plans.

Oracle Database 11g: SQL Tuning Workshop 5 - 40


Interpreting an Execution Plan (continued)
To draw plan as a tree, do the following:
1. Take the ID with the lowest number and place it at the top.
2. Look for rows which have a PID (parent) equal to this value.
3. Place these in the tree below the Parent according to their POS values from the lowest to the
highest, ordered from left to right.
4. After all the IDs for a parent have been found, move down to the next ID and repeat the process,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

finding new rows with the same PID.


The first thing to determine in an explain plan is which node is executed first. The method in the
slide explains this, but sometimes with complicated plans it is difficult to do this and also difficult to
follow the steps through to the end. Large plans are exactly the same as smaller ones, but with more
entries. The same basic rules apply. You can always collapse the plan to hide a branch of the tree
which does not consume much of the resources.
n s e
Standard explain plan interpretation:
1. Start at the top. e l ice
a l
bconsume
2. Move down the row sources until you get to one which produces data, but does
fe r
not
any. This is the start row source. n s
3. Look at the siblings of this row source. These row sources are executed
n - tranext.
4. After the children are executed, the parent is executed next.a no
5. Now that this parent and its children are completed, work
h a s backeupฺ the tree, and look at the
siblings of the parent row source and its parents. s )
Execute u
as d
ibefore.
ฺ e tG
irare exhausted.
6. Move back up the plan until all row sources
ฺ m e n
Standard tree interpretation: d gp Stud
1. Start at the top.
b i a@ this
2. Move down the tree (to
a jathe left
u e you reach the left node. This is executed first.
suntil
3. Look at the A i
b of this
siblings torow source. These row sources are executed next.
o
ni children are executed, the parent is executed next.
4. After the
t o
n that this parent and its children are completed, work back up the tree, and look at the
5. Now
e Asiblings
Jos 6. Move back up the tree until all row sources are exhausted.
of the parent row source and its parents. Execute as before.

If you remember the few basic rules of explain plans and with some experience, you can read most
plans easily.

Oracle Database 11g: SQL Tuning Workshop 5 - 41


Execution Plan Interpretation: Example 1

SELECT /*+ RULE */ ename,job,sal,dname


FROM emp,dept
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHERE dept.deptno=emp.deptno and not exists(SELECT *


FROM salgrade
WHERE emp.sal between losal and hisal);

--------------------------------------------------
| Id | Operation | Name | FILTER 1
--------------------------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | FILTER | |
|
|
2 |
3 |
NESTED LOOPS
TABLE ACCESS FULL
|
| EMP
|
|
NESTED
LOOPS 2 6 n s e
ice
el
| 4 | TABLE ACCESS BY INDEX ROWID| DEPT |
|* 5 | INDEX UNIQUE SCAN | PK_DEPT |
b l
TABLE ACCESS FULL
a
|* 6 | TABLE ACCESS FULL | SALGRADE |

4fe
r SALGRADE
--------------------------------------------------
3
a n s TABLE ACCESS

n-tr
Predicate Information (identified by operation id): BY ROWID
DEPT

a no ---------------------------------------------------
TABLE ACCESS FULL
EMP
1 - filter( NOT EXISTS
h a s eฺ 5
(SELECT 0 FROM "SALGRADE" "SALGRADE" WHERE
) i d
"HISAL">=:B1 AND "LOSAL"<=:B2))
i r ฺes nt Gu
ฺm ude
5 - access("DEPT"."DEPTNO"="EMP"."DEPTNO")
INDEX UNIQUE SCAN

d p
6 - filter("HISAL">=:B1 AND "LOSAL"<=:B2)
g t PK_DEPT

@ s S
i Oracle. All rights reserved.
a b iaCopyrightt©h2008,
j e
b ia ( to us
n
Execution Plani oAInterpretation: Example 1
t o
n with an example query to illustrate how to interpret an execution plan. The slide shows a
YouAstart
e
Josquery with its associated execution plan and the same plan in the tree format.
The query tries to find employees who have salaries outside the range of salaries in the salary grade
table. The query is a SELECT statement from two tables with a subquery based on another table to
check the salary grades.
See the execution order for this query. Based on the example in the slide, and from the previous
slide, the execution order is 3 – 5 – 4 – 2 – 6 – 1:
• 3: The plan starts with a full table scan of EMP (ID=3).
• 5: The rows are passed back to the controlling nested loops join step (ID=2), which uses them
to execute the lookup of rows in the PK_DEPT index in ID=5.
• 4: The ROWIDs from the index are used to lookup the other information from the DEPT table in
ID=4.
• 2: ID=2, the nested loops join step, is executed until completion.
• 6: After ID=2 has exhausted its row sources, a full table scan of SALGRADE in ID=6 (at the
same level in the tree as ID=2, therefore, its sibling) is executed.
• 1: This is used to filter the rows from ID2 and ID6.

Oracle Database 11g: SQL Tuning Workshop 5 - 42


Execution Plan Interpretation: Example 1 (continued)
Note that children are executed before parents, so although structures for joins must be set up before
the child execution, the children are notated as executed first. Probably, the easiest way is to consider
it as the order in which execution completes, so for the NESTED LOOPS join at ID=2, the two
children {ID=3 and ID=4 (together with its child)} must have completed their execution before
ID=2 can be completed.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 43


Execution Plan Interpretation: Example 1
SQL> alter session set statistics_level=ALL;

Session altered.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> select /*+ RULE to make sure it reproduces 100% */ ename,job,sal,dname


from emp,dept where dept.deptno = emp.deptno and not exists (select * from salgrade
where emp.sal between losal and hisal);

no rows selected

SQL> select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL IOSTATS


LAST'));

SQL_ID 274019myw3vuf, child number 0


n s e
ce
-------------------------------------

eli
Plan hash value: 1175760222
a b l
--------------------------------------------------------------------------------
fer
| Id | Operation | Name | Starts | A-Rows | Buffers |
a n s
tr
--------------------------------------------------------------------------------
|* 1 | FILTER | | 1 | 0 |
o n -
61 |

an
| 2 | NESTED LOOPS | | 1 | 14 | 25 |
| 3 | TABLE ACCESS FULL | EMP | 1 | 14 | 7 |
| 4 | TABLE ACCESS BY INDEX ROWID| DEPT | 14 |
h s
a deฺ14 | 18 |
|* 5 | INDEX UNIQUE SCAN | PK_DEPT |
e )
s Gui14 | 14 | 4 |
|* 6 | TABLE ACCESS FULL
r ฺ
| SALGRADE | 12 | 12 | 36 |

ฺ mi dent
--------------------------------------------------------------------------------
p

d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Execution Plan
o io Interpretation: Example 1 (continued)
TheA nt in the slide is a plan dump from V$SQL_PLAN with STATISTICS_LEVEL set to
example
e This report shows you some important additional information compared to the output of the
JosALL.
EXPLAIN PLAN command:
• A-Rows corresponds to the number of rows produced by the corresponding row source.
• Buffers corresponds to the number of consistent reads done by the row source.
• Starts indicates how many times the corresponding operation was processed.
For each row from the EMP table, the system gets its ENAME, SAL, JOB, and DEPTNO.
Then the system accesses the DEPT table by its unique index (PK_DEPT) to get DNAME using
DEPTNO from the previous result set.
If you observe the statistics closely, the TABLE ACCESS FULL operation on the EMP table (ID=3)
is started once. However, operations from ID 5 and 4 are started 14 times; once for each EMP rows.
At this step (ID=2), the system gets all ENAME, SAL, JOB, and DNAME.
The system now must filter out employees who have salaries outside the range of salaries in the
salary grade table. To do that, for each row from ID=2, the system accesses the SALGRADE table
using a FULL TABLE SCAN operation to check if the employee’s salary is outside the salary range.
This operation only needs to be done 12 times in this case because at run time the system does the
check for each distinct salary, and there are 12 distinct salaries in the EMP table.

Oracle Database 11g: SQL Tuning Workshop 5 - 44


Execution Plan Interpretation: Example 2

SQL> select /*+ USE_NL(d) use_nl(m) */ m.last_name as dept_manager


2 , d.department_name
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 , l.street_address
4 from hr.employees m join
5 hr.departments d on (d.manager_id = m.employee_id)
6 natural join
7 hr.locations l
8 where l.city = 'Seattle';

0 SELECT STATEMENT
1 0 NESTED LOOPS 1
2 1 NESTED LOOPS
n s e
ce
3 2 TABLE ACCESS BY INDEX ROWID LOCATIONS
4 3 INDEX RANGE SCAN LOC_CITY_IX
eli
5 2 TABLE ACCESS BY INDEX ROWID DEPARTMENTS
2 a b
7 l
6 5 INDEX RANGE SCAN DEPT_LOCATION_IX
fe r
7 1 TABLE ACCESS BY INDEX ROWID EMPLOYEES
a n s
8 7 INDEX UNIQUE SCAN EMP_EMP_ID_PK
n -tr
o
s a n3 5 8
) h a deฺ
r ฺ e s Gui
p ฺ mi dent 4 6
d g S t u
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n
Execution Plan
o io Interpretation: Example 2
nt retrieves names, department names, and addresses for employees whose departments are
ThisAquery
e
Joslocated in Seattle and who have managers.
For formatting reasons, the explain plan has the ID in the first column, and PID in the second
column. The position is reflected by the indentation. The execution plan shows two nested loops join
operations.
You follow the steps from the previous example:
1. Start at the top. ID=0
2. Move down the row sources until you get to the one, which produces data, but does not consume
any. In this case, ID 0, 1, 2, and 3 consume data. ID=4 is the first row source that does not
consume any. This is the start row source. ID=4 is executed first. The index range scan
produces ROWIDs, which are used to lookup in the LOCATIONS table in ID=3.
3. Look at the siblings of this row source. These row sources are executed next. The sibling at the
same level as ID=3 is ID=5. Node ID=5 has a child ID=6, which is executed before it. This is
another index range scan producing ROWIDs, which are used to lookup in the DEPARTMENTS
table in ID=5.
4. After the children operation, the parent operation is next. The NESTED LOOPS join at ID=2 is
executed next bringing together the underlying data.

Oracle Database 11g: SQL Tuning Workshop 5 - 45


Execution Plan Interpretation: Example 2 (continued)
5. Now that this parent and its children are completed, walk back up the tree, and look at the
siblings of the parent row source and its parents. Execute as before. The sibling of ID=2 at the
same level in the plan is ID=7. This has a child ID=8, which is executed first. The index unique
scan produces ROWIDs, which are used to lookup in the EMPLOYEES table in ID=7.
6. Move back up the plan until all row sources are exhausted. Finally this is brought together with
the NESTED LOOPS at ID=1, which passes the results back to ID=0.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7. 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, using an index
access on the CITY column. This is because you search for departments in Seattle only.
The result is joined with the DEPARTMENTS table, using the index on the LOCATION_ID join
column; the result of this first join operation is the driving row source for the second nested loops se
join. c e n
l i
e The
The second join probes the index on the EMPLOYEE_ID column of the EMPLOYEES table. b l
f era of
system can do that because it knows (from the first join) the employee ID of allsmanagers
r
departments in Seattle. Note that this is a unique scan because it is based ton n primary key.
athe
-
Finally, the EMPLOYEES table is accessed to retrieve the last name.
a non
h a s eฺ
) i d
i r ฺes nt Gu
g p ฺm ude
d S t
@ s
j a b ia
e thi
b ia ( to us
o A
t o ni
A n
e
Jos

Oracle Database 11g: SQL Tuning Workshop 5 - 46


Execution Plan Interpretation: Example 3

select /*+ ORDERED USE_HASH(b) SWAP_JOIN_INPUTS(c) */ max(a.i)


from t1 a, t2 b, t3 c
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

where a.i = b.i and a.i = c.i;

0 SELECT STATEMENT
1 SORT AGGREGATE 1
2 1 HASH JOIN
3 2 TABLE ACCESS FULL T3
4 2 HASH JOIN
5 4 TABLE ACCESS FULL T1
2
6 4 TABLE ACCESS FULL T2
n s e
i ce
3 4 b l el
fer a
a n s
o n -tr
s an 5 6
) h a deฺ
r ฺ e s Gui
Join order is: T1 - T2ฺm - T3i ent
d p
g Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Execution Plan
o io Interpretation: Example 3
SeeA ntexecution plan in the slide. Try to find the order in which the plan is executed and deduce
the
e is the join order (order in which the system joins tables). Again, ID is in the first column and
Joswhat
PID in the second column. The position is reflected by the indentation. It is important to recognize
what the join order of an execution plan is, to be able to find your plan into a 10053 event trace file.
Here is the interpretation of this plan:
• The system first hashes the T3 table (Operation ID=3) into memory.
• Then it hashes the T1 table (Operation ID=5) into memory.
• Then the scan of the T2 table begins (Operation ID=6).
• The system picks a row from T2 and probes T1 (T1.i=T2.i).
• If the row survives, the system probes T3 (T1.i=T3.i).
• If the row survives, the system sends it to next operation.
• The system outputs the maximum value from the previous result set.
In conclusion, the execution order is : 3 – 5 – 6 – 4 – 2 – 1
The join order is: T1 – T2 – T3
You can also use Enterprise Manager to understand execution plans, especially because it displays
the Order column.
Note: A special hint was used to make sure T3 would be first in the plan.

Oracle Database 11g: SQL Tuning Workshop 5 - 47


Reading More Complex Execution Plans

SELECT owner , segment_name , segment_type


FROM dba_extents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHERE file_id = 1
AND 123213 BETWEEN block_id AND block_id + blocks -1;

n s e
i ce
b l el
fer a
Collapse using indentation a n s
and o n -tr
a nmost resources.
focus on operations consuming
s eฺ
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Reading More
o n ioComplex Execution Plans
TheA nt at the left comes from the query (in the slide) on the data dictionary. It is so long that it is
plan
e
Josvery difficult to apply the previous method to interpret it and locate the first operation.
You can always collapse a plan to make it readable. This is illustrated at the right where you can see
the same plan collapsed. As shown, this is easy to do when using the Enterprise Manager graphical
interface. You can clearly see that this plan is a UNION ALL of two branches. Your knowledge
about the data dictionary enables you to understand that the two branches correspond to dictionary-
managed tablespaces and locally-managed ones. Your knowledge about your database enables you to
know that there are no dictionary-managed tablespaces. So, if there is a problem, it must be on the
second branch. To get confirmation, 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. Then, you just need to
expand the branch you want to investigate (where time is being spent). To use this method, 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. For example, tkprof cumulates for each parent
operation the time it takes to execute itself plus the sum of all its child operation time.

Oracle Database 11g: SQL Tuning Workshop 5 - 48


Reviewing the Execution Plan

• Drive from the table that has most selective filter.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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
n s e
– Views are correctly used
lice
– Unintentional Cartesian products a b le
– Tables accessed efficiently s f er
n a
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Reviewing n
o ioExecution Plan
the
t tune a SQL statement in an online transaction processing (OLTP) environment, the goal is
nyou
When A
e from the table that has the most selective filter. This means that there are fewer rows passed
Jostoto drive
the next step. If the next step is a join, this means fewer rows are joined. Check to see whether the
access paths are optimal. When you examine the optimizer execution plan, look for the following:
• The plan is such that the driving table has the best filter.
• The join order in each step means that the fewest number of rows are returned to the next step
(that is, the join order should reflect going to the best not-yet-used filters).
• The join method is appropriate for the number of rows being returned. For example, nested loop
joins through indexes may not be optimal when many rows are returned.
• Views are used efficiently. Look at the SELECT list to see whether access to the view is
necessary.
• There are any unintentional Cartesian products (even with small tables).
• Each table is being accessed efficiently: Consider the predicates in the SQL statement and the
number of rows in the table. Look for suspicious activity, such as a full table scans on tables
with large number of rows, which have predicates in the WHERE clause. Also, a full table scan
might be more efficient on a small table, or to leverage a better join method (for example,
hash_join) for the number of rows returned.
If any of these conditions are not optimal, consider restructuring the SQL statement or the indexes
available on the tables.

Oracle Database 11g: SQL Tuning Workshop 5 - 49


Looking Beyond Execution Plans

• An execution plan alone cannot tell you whether a plan is


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

good or not.
• May need additional testing and tuning:
– SQL Tuning Advisor
– SQL Access Advisor
– SQL Performance Analyzer e
n s
– SQL Monitoring i ce
– Tracing b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Looking Beyond
o n io Execution Plans
TheA nt
execution plan alone cannot differentiate between well-tuned statements and those that perform
s e
Jo poorly. For example, an EXPLAIN PLAN output that shows that a statement uses an index does not
necessarily mean that the statement runs efficiently. Sometimes indexes can be extremely inefficient.
It is best to use EXPLAIN PLAN to determine an access plan, and then later prove that it is the
optimal plan through testing. When evaluating a plan, you should examine the statement’s actual
resource consumption.
The rest of this course is intended to show you various methods to achieve this.

Oracle Database 11g: SQL Tuning Workshop 5 - 50


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Gather execution plans


• Display execution plans
• Interpret execution plans

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 51


Practice 5: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Using different techniques to extract execution plans


• Using SQL monitoring

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 5 - 52


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Case Study:
Star Transformation

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Define a star schema


• Show a star query plan without transformation
• Define the star transformation requirements
• Show a star query plan after transformation
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 6 - 2


The Star Schema Model
Dimension/Lookup table
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PRODUCTS Fact table TIMES


Keys
PROD_ID TIME_ID

PROD_NAME DAY_NAME
PROD_DESC PROD_ID CALENDAR_YEAR
TIME_ID
CHANNEL_ID

n s e
SALES
i ce
b l el
AMOUNT_SOLD r a
CUSTOMERS QUANTITY_SOLD
sfe
CHANNELS
an
- t r
CUST_ID
on CHANNEL_ID
a n CHANNEL_DESC
Measures
CUST_GENDER
CUST_CITY
h s
a deฺ CHANNEL_CLASS
e )
s Gui
r ฺ
p ฺ m>>i Dimension
Fact
d e nt
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
The Star Schema
o io Model
TheA ntschema is the simplest data warehouse schema. It is called a star schema because the entity-
star
e
Joscenter
relationship diagram of this schema resembles a star, with points radiating from a central table. The
of the star consists of one or more fact tables and the points of the star are the dimension
tables. 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), each of which contains information about the entries for a particular attribute in the fact
table. A star query is a join between a fact table and a number of dimension tables. Each dimension
table is joined to the fact table using a primary key to foreign key join, but the dimension tables are
not joined to each other. The cost-based optimizer (CBO) recognizes star queries and generates
efficient execution plans for them. A typical fact table contains keys and measures. For example, in
the Sales History schema, the sales fact table contains the quantity_sold, amount, and
cost measures, and the cust_id, time_id, prod_id, channel_id, and promo_id keys.
The dimension tables are customers, times, products, channels, and promotions. The
products dimension table, for example, contains information about each product number that
appears in the fact table.
Note: It is easy to generalize this model to include more than one fact table.

Oracle Database 11g: SQL Tuning Workshop 6 - 3


The Snowflake Schema Model
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PRODUCTS TIMES

SALES n s e
i ce
CUSTOMERS
b l el
fer a
a n s
o n -tr
CHANNELS
a n
COUNTRIES h s
a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
The Snowflake n i o Schema Model
n t o
TheA snowflake schema is a more complex data warehouse model than a star schema, and is a type of
s e
Jo star schema. It is called a snowflake schema because the diagram of the schema resembles a snowflake. Snowflake schemas normalize dimensions to eliminate redundancy. That is, the
dimension data has been grouped into multiple tables instead of one large table. For example, a
product dimension table in a star schema might be normalized into a products table, a
product_category table, and a product_manufacturer table in a snowflake schema or,
as shown in the slide, you can normalize the customers table using the countries table. While
this saves space, it increases the number of dimension tables and requires more foreign key joins.
The result is more complex queries and reduced query performance.
Note: It is suggested that you select a star schema over a snowflake schema unless you have a clear
reason not to.

Oracle Database 11g: SQL Tuning Workshop 6 - 4


Star Query: Example

SELECT ch.channel_class, c.cust_city,


t.calendar_quarter_desc,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SUM(s.amount_sold) sales_amount

FROM sales s,times t,customers c,channels ch


0
1
1
0
WHERE s.time_id = t.time_id AND
0
1
1
s.cust_id = c.cust_id AND
0

n s e
ce
0
1
1
0
s.channel_id = ch.channel_id AND eli
a b l
fe r
c.cust_state_province = 'CA' AND
n s
- tra AND
ch.channel_desc IN ('Internet','Catalog')
n
a no
t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')

h a s eฺ
GROUP BY ch.channel_class, c.cust_city, ) i d
t.calendar_quarter_desc; i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
io
Star Query:nExample
o
A nt the star query in the slide. For the star transformation to operate, it is supposed that the
Consider
e
Jossales table of the Sales History schema has bitmap indexes on the time_id, channel_id, and cust_id columns.

Oracle Database 11g: SQL Tuning Workshop 6 - 5


Execution Plan Without Star Transformation
-------------------------------------------
| Id | Operation | Name |
-------------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 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 |
Hash Join | 8 | TABLE ACCESS FULL| SALES |
-------------------------------------------
n s e
i ce
CHANNELS Hash Join
Predicate Information (by operation id):

b
--------------------------------------------l el
r a
2 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
fe
n s
3 - filter("CH"."CHANNEL_DESC"='Catalog' OR
"CH"."CHANNEL_DESC"='Internet')
a
n -tr
4 - access("S"."TIME_ID"="T"."TIME_ID")
5 - filter("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR
TIMES Hash Join o
an
"T"."CALENDAR_QUARTER_DESC"='1999-Q2')
6 - access("S"."CUST_ID"="C"."CUST_ID")
s
a deฺ
7 - filter("C"."CUST_STATE_PROVINCE"='CA')

h
e )
s Gui
CUSTOMERS r
SALES ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Execution Plan
o io Without Star Transformation
t see the benefits of a star transformation, you should review how a join on a star schema is
nyou
A
Before
e
Josprocessed without their benefits.
The fundamental issue with the plan in the slide is that the query always starts joining the SALES
table to a dimension table. This results in a very large number of rows that can only be trimmed
down by the other parent joins in the execution plan.

Oracle Database 11g: SQL Tuning Workshop 6 - 6


Star Transformation

• Create bitmap indexes on fact tables foreign keys.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Set STAR_TRANSFORMATION_ENABLED to TRUE.


• Requires at least two dimensions and one fact table
• Gather statistics on all corresponding objects.
• Carried out in two phases:
– First, identify interesting fact rows using bitmap indexes based
n s e
on dimensional filters.
e l ice
l
– Join them to the dimension tables. rab e
a n sf
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Star Transformation
o
t best possible performance for star queries, it is important to follow some basic guidelines:
nthe
A
To get
e
Jos •• AThebitmap index should be built on each of the foreign key columns of the fact table or tables.
STAR_TRANSFORMATION_ENABLED initialization parameter should be set to TRUE.
This enables an important optimizer feature for star queries. It is set to FALSE by default for
backwards compatibility.
When a data warehouse satisfies these conditions, the majority of the star queries that run in the data
warehouse use a query execution strategy known as star transformation. Star transformation provides
very efficient query performance for star queries.
Star transformation is a powerful optimization technique that relies on implicitly rewriting (or
transforming) the SQL of the original star query. The end user never needs to know any of the details
about the star transformation. The system’s CBO automatically selects star transformation where
appropriate. Oracle processes a star query using two basic phases:
• The first phase retrieves exactly the necessary rows from the fact table (the result set). Because
this retrieval utilizes bitmap indexes, it is very efficient.
• The second phase joins this result set to the dimension tables. This operation is called a
semijoin.
Note: At least three tables are used in the query (two dimensions and one fact).

Oracle Database 11g: SQL Tuning Workshop 6 - 7


Star Transformation: Considerations

• Queries containing bind variables are not transformed.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Queries referring to remote fact tables are not transformed.


• Queries containing antijoined tables are not transformed.
• Queries referring to unmerged nonpartitioned views are not
transformed.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n io
Star Transformation:
o Considerations
n t
StarAtransformation is not supported for tables with any of the following characteristics:
s e
Jo •• Queries with a table hint that is incompatible with a bitmap access path
Queries that contain bind variables
• Tables with too few bitmap indexes. There must be a bitmap index on a fact table column for the
optimizer to generate a subquery for it.
• Remote fact tables. However, remote dimension tables are allowed in the subqueries that are
generated.
• Antijoined tables
• Tables that are already used as a dimension table in a subquery
• Tables that are really unmerged views, which are not view partitions

Oracle Database 11g: SQL Tuning Workshop 6 - 8


Star Transformation: Rewrite Example
Phase 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT s.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','1999-Q2'))
0
n s e
ce
1
1

li
0
AND cust_id IN (SELECT cust_id
FROM customers
a b le
fer
WHERE cust_state_province = 'CA')
s
0
1
- t r an
1
0
AND channel_id IN(SELECT channel_id
no n
FROM channels a
s IN
h a
WHERE channel_desc
) i d eฺ
ฺes('Internet','Catalog'));
Gu ir ent
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n io
Star Transformation:
o Rewrite Example
TheA
t
n processes the query seen earlier in two phases. In the first phase, the system uses the
system
e
Josfilters against the dimensional tables to retrieve the dimensional primary keys, which match those
filters. 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. That is,
the system retrieves the result set from the sales table by using essentially the rewritten query in the
slide.
Note: The SQL in the slide is a theoretical SQL statement that represents what goes on in phase I.

Oracle Database 11g: SQL Tuning Workshop 6 - 9


Retrieving Fact Rows from
One Dimension
Phase 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

BITMAP
MERGE One bitmap
is
Produced.

n s e
BITMAP
i ce
KEY
b l el
ITERATION
fer a
a n s
o n -tr
Dimension a nFact Table
Table
h s
a deBitmap ฺ
) i
Access
i r ฺes nt Gu Access
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
Abia to
Retrieving n
o io Rows from One Dimension
Fact
TheA nt shows retrieval of fact table rows using only one dimension table. Based on the
slide
e
Joscorresponding dimension filter predicates, like in t.calendar_quarter_desc IN ('1999-
Q1','1999-Q2') from the example in the previous slide, the system scans the dimension table,
and for each corresponding row, it probes the corresponding fact bitmap index and fetches the
corresponding bitmap.
BITMAP KEY ITERATION makes each key coming from its left input a lookup key for the index
on its right input, and returns all bitmaps fetched by that index. Note that its left input supplies join
keys from the dimension table in this case.
The last step in this tree merges all fetched bitmaps from the previous steps. 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.
Note: BITMAP_MERGE_AREA_SIZE plays an important role in tuning the performance of this
operation when using the shared server mode. The system does not recommend using the
BITMAP_MERGE_AREA_SIZE parameter unless the instance is configured with the shared server
option. The system recommends that you enable automatic sizing of SQL working areas by setting
PGA_AGGREGATE_TARGET instead. BITMAP_MERGE_AREA_SIZE is retained for backward
compatibility.

Oracle Database 11g: SQL Tuning Workshop 6 - 10


Retrieving Fact Rows from All Dimensions
Intermediate Phase 1
Result Set
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

(IRS)
BITMAP
Conversion
Multiple bitmaps To Rowids
are
ANDed together.

n s e
i ce
BITMAP AND
b l el
fer a
a n s
… …
o n -tr
s a nMERGE n
MERGE 1
MERGE)i h
a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Retrieving n
o io Rows from All Dimensions
Fact
t first phase, the steps mentioned in the previous slide are repeated for each dimension
nthe
A
During
e So each BITMAP MERGE in the plan generates a bitmap for a single dimension table. To
Jostable.
identify all rows from the fact table that are of interest, the system must intersect all generated
bitmaps. This is to eliminate fact rows that join with one dimension, but not with all of them. This is
achieved by performing a very efficient BITMAP AND operation on all the bitmaps generated for
each dimension. 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.
Note: Until now, only fact bitmap indexes and dimension tables were used. To further access the fact
table, the system must convert the generated bitmap to a rowids set.

Oracle Database 11g: SQL Tuning Workshop 6 - 11


Joining the Intermediate Result Set
with Dimensions
Phase 2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

HASH JOIN

Dimension n
Table HASH JOIN
Access e
n s
i ce
Dimension i b l el
Table HASH JOIN fe r a
Access a n s
o n -tr
Dimension 1s a
n Fact Table
Table ) h a deฺ Access
ฺ e
Access
r s Gui From
ฺ i
m den t IRS
d p
g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o io
Joining thenIntermediate Result Set with Dimensions
t
e An
After the result set is determined, the system enters phase 2 of the star transformation algorithm. In
s
Jo tables
this phase, it is needed to join the sales data, corresponding to the result set, with the dimension
data used to group the rows and pertaining to the query’s select list.
Note that the graphic in the slide shows that a hash join is performed between the fact table and its
dimensions. Although a hash join is statistically the most-used technique to join rows in a star query,
this might not be always true, as this is evaluated by the CBO.

Oracle Database 11g: SQL Tuning Workshop 6 - 12


Star Transformation Plan: Example 1
SORT GROUP BY
HASH JOIN
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
BITMAP INDEX RANGE SCAN SALES_CHANNELS_BX
i ce
BITMAP MERGE
b l el
BITMAP KEY ITERATION
fer a
BUFFER SORT
a n s
TABLE ACCESS FULL TIMES n -tr
o
BITMAP INDEX RANGE SCAN SALES_TIMES_BX
s an
… ) h a deฺ
TABLE ACCESS FULL CHANNELS r ฺ e s Gui
TABLE ACCESS FULL TIMES
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
Star Transformation
o Plan: Example 1 io A
t
ThisAisna possible plan to answer the query shown in the “Execution Plan Without Star
e
JosTransformation” section. Note that for formatting purposes, only the channels and times dimensions
are shown. It is easy to generalize the case for n dimensions.
Note: It is supposed that sales is not partitioned.

Oracle Database 11g: SQL Tuning Workshop 6 - 13


Star Transformation: Further Optimization

• In a star transformation execution plan, dimension tables are


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

accessed twice; once for each phase.


• This might be a performance issue in the case of big
dimension tables and low selectivity.
• If the cost is lower, the system might decide to create a
temporary table and use it instead of accessing the same
n s e
dimension table twice.
e l ice
• Temporary table’s creation in the plan: abl fer
a n s
n -tr
LOAD AS SELECT SYS_TEMP_0FD9D6720_BEBDC o
TABLE ACCESS FULL CUSTOMERS
s an
) h a deฺ
s Gui

filter("C"."CUST_STATE_PROVINCE"='CA') r ฺ e
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Star Transformation:
o Further Optimization
t
When
e Anyou look at the previous execution plan, you see that each dimension table is accessed
Jostwice—once during the first phase, where the system determines the necessary fact table rows, and
once when joining the fact rows to each dimension table during the second phase. This might be a
performance issue if the dimension tables are big, and there is no fast access path to them for solving
the problem. In such cases, the system might decide to create temporary tables containing
information needed for both phases. This decision is made if the costs for creating a temporary table,
consisting of the result set for both the predicate and the join columns on the dimension table, is
cheaper than accessing the dimension table twice. In the previous execution plan example, the
TIMES and CHANNELS tables are very small, and accessing them using a full table scan is not a big
deal.
The creation of these temporary tables and the data insertion are shown in the execution plan. The
name of those temporary tables is system-generated and varies. In the slide, you see an extract from
an execution plan using temporary tables for the CUSTOMERS table.
Note: In addition, temporary tables are not be used by star transformation under the following
conditions:
• The database is in read-only mode.
• The star query is part of a transaction that is in serializable mode.

Oracle Database 11g: SQL Tuning Workshop 6 - 14


Using Bitmap Join Indexes

• Volume of data to be joined is reduced


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Can be used to eliminate bitwise operations


• More efficient in storage than MJVs

CREATE BITMAP INDEX sales_q_bjx


ON sales(times.calendar_quarter_desc) n s e
i ce
FROM sales, times
b l el
WHERE sales.time_id = times.time_id
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Using Bitmap
o n ioJoin Indexes
TheA nt of data that must be joined can be reduced if the join indexes used as joins have already
volume
e
Josbeen precalculated.
In addition, the join indexes, which contain multiple dimension tables can eliminate bitwise
operations, which are necessary in the star transformation with existing bitmap indexes.
Finally, bitmap join indexes are much more efficient in storage than materialized join views (MJVs),
which do not compress rowids of the fact tables.
Assume that you have created the additional index structure mentioned in the slide.

Oracle Database 11g: SQL Tuning Workshop 6 - 15


Star Transformation Plan: Example 2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SORT GROUP BY
HASH JOIN
HASH JOIN
TABLE ACCESS BY INDEX ROWID SALES
BITMAP CONVERSION TO ROWIDS
BITMAP AND
BITMAP MERGE
n s e
ce
BITMAP KEY ITERATION
BUFFER SORT
eli
TABLE ACCESS FULL CHANNELS a b l
fer
BITMAP INDEX RANGE SCAN SALES_CHANNELS_BX
a n s
BITMAP OR
n -tr
o
an
BITMAP INDEX SINGLE VALUE SALES_Q_BJX
BITMAP INDEX SINGLE VALUE SALES_Q_BJX
h s
a deฺ
TABLE ACCESS FULL CHANNELS
e )
s Gui
r ฺ
mi dent
TABLE ACCESS FULL TIMES
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
n io
Star Transformation
o Plan: Example 2
TheA n t
processing of the same star query using the bitmap join index is similar to the previous example.
s e
Jo 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.
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. This is
because the join predicate information on times.calendar_quarter_desc can be satisfied
with the sales_q_bjx bitmap join index.
Note that access to the join index is done twice because the corresponding query’s predicate is
t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')

Oracle Database 11g: SQL Tuning Workshop 6 - 16


Star Transformation Hints

• The STAR_TRANSFORMATION hint: Use best plan containing


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

a star transformation, if there is one.


• The FACT(<table_name>) hint: The hinted table should be
considered as the fact table in the context of a star
transformation.
• The NO_FACT (<table_name>) hint: The hinted table should
n s e
not be considered as the fact table in the context of a star
e l ice
transformation. bl
fe r a
• The FACT and NO_FACT hints are useful for star queries
n s
a
containing more than one fact table. n-tr no
a
s eฺ
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io
Star Transformation
o Hints
t
n STAR_TRANSFORMATION hint makes the optimizer use the best plan in which the
• AThe
e
Jos transformation has been used. Without the hint, the optimizer could make a cost-based decision
to use the best plan generated without the transformation, instead of the best plan for the
transformed query. Even if the hint is given, there is no guarantee that the transformation takes
place. The optimizer only generates the subqueries if it seems reasonable to do so. If no
subqueries are generated, there is no transformed query, and the best plan for the untransformed
query is used, regardless of the hint.
• 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.
• 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.
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.

Oracle Database 11g: SQL Tuning Workshop 6 - 17


Bitmap Join Indexes: Join Model 1

c1 pk fk
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

d f e
n s
i ce
CREATE BITMAP INDEX bji ON f(d.c1)
b l el
FROM f, d
fe r a
WHERE d.pk = f.fk;
a n s
o n -tr
SELECT sum(f.facts)
s an
FROM d, f
) h a deฺ
WHERE d.pk = f.fk AND d.c1 ฺ e s =G 1;ui
r
mi dent
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
Bitmap Join n i o A Join Model 1
Indexes:
to three slides, F represents the fact table, D, the dimension table, PK a primary key,
nfollowing
A
In the
e
Josand FK a foreign key.
A bitmap join index can be used in the SELECT statement in the slide to avoid the join operation.
Similar to the materialized join view, a bitmap join index precomputes the join and stores it as a
database object. 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.
Note: C1 is the indexed column in the dimension table.

Oracle Database 11g: SQL Tuning Workshop 6 - 18


Bitmap Join Indexes: Join Model 2

c1 c2 pk fk
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

d f e
n s
CREATE BITMAP INDEX bjx ON f(d.c1,d.c2) lice
FROM f, d a b le
WHERE d.pk = f.fk; s f er
tra n
n -
SELECT sum(f.facts) a no
FROM d, f h a s eฺ
e s ) = 1 uAND id d.c2 = 1;
WHERE d.pk = f.fk AND d.c1 ฺ
ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Bitmap Join n i o A Join Model 2
Indexes:
TheA nto in the slide is an extension of model 1, requiring a concatenated bitmap join index to
model
e
Josrepresent it.
Note that BJX, in this case, can also be used to answer the following select statement:
select sum(f.facts) from d,f where d.pk=f.fk and d.c1=1
This is due to the fact that D.C1 is the leading part of the BJX.

Oracle Database 11g: SQL Tuning Workshop 6 - 19


Bitmap Join Indexes: Join Model 3

c1 pk fk1 fk2 pk c1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

d1 f d2 e
n s
CREATE BITMAP INDEX bjx ON f(d1.c1,d2.c1) lice
FROM f, d1, d2 a b le
r
WHERE d1.pk = f.fk1 AND d2.pk = f.fk2; nsfe
n - tra
SELECT sum(f.sales) a no
FROM d1, f, d2 h a s eฺ
WHERE d1.pk = f.fk1 ANDฺed2.pk s ) u d
if.fk2
i r t G = AND
d1.c1 = p
g
1 ฺm AND d2.c1
u d en = 2;
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Bitmap Join n o
iIndexes: Join Model 3
t o
n also requires the concatenated bitmap join index shown in the slide. In this case, two
ThisAmodel
e
Josdimension tables are used.

Oracle Database 11g: SQL Tuning Workshop 6 - 20


Bitmap Join Indexes: Join Model 4

c1 pk c2 pk fk
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

d1 d2 f e
n s
i ce
CREATE BITMAP INDEX bjx ON f(d1.c1)
b l el
FROM f, d1, d2
fer a
WHERE d1.pk = d2.c2 AND d2.pk = f.fk;
a n s
o n -tr
SELECT sum(f.sales)
s an
FROM d1, d2, f
) h a deฺ
WHERE d1.pk = d2.c2 ANDฺed2.pk
r s G=uif.fk AND
d1.c1 = 1; pฺm
i ent
d g Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Bitmap Joinn i o A Join Model 4
Indexes:
TheA ntoshows a snowflake model that involves joins between two or more dimension tables. It can
slide
e
Josbe expressed by a bitmap join index. The bitmap join index can be either single or concatenated
depending on the number of columns in the dimension tables to be indexed. A bitmap join index on
D1.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.

Oracle Database 11g: SQL Tuning Workshop 6 - 21


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Define a star schema


• Show a star query plan without transformation
• Define the star transformation requirements
• Show a star query plan after transformation
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 6 - 22


Practice 6: Overview

This practice covers using the star transformation technique to


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

optimize your query.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 6 - 23


J
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

o s e
n A
t o n
Aio
b i a
b i
to
(ja use
d p ฺ
a@ this
r ฺ
g Stu
e ) h
mi dent
s
s Gui
an
o
a deฺ
tr n -
a n s fe
r
a b l el
i
ce n s e
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Optimizer Statistics

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to do the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

following:
• Gather optimizer statistics
• Gather system statistics
• Set statistic preferences
• Use dynamic sampling s e
ce n
• Manipulate optimizer statistics eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 7 - 2


Optimizer Statistics

• Describe the database and the objects in the database


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Information used by the query optimizer to estimate:


– Selectivity of predicates
– Cost of each execution plan
– Access method, join order, and join method
– CPU and input/output (I/O) costs e
n s
• Refreshing optimizer statistics whenever they are stale is as li ce
important as gathering them: a b le
r fe
– Automatically gathered by the system
t r a ns
on-
– Manually gathered by the user with DBMS_STATS
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io
Optimizer Statistics
o
A nt statistics describe details about the database and the objects in the database. These
Optimizer
e
Josstatistics are used by the query optimizer to select the best execution plan for each SQL statement.
Because the objects in a database change constantly, statistics must be regularly updated so that they
accurately describe these database objects. Statistics are maintained automatically by Oracle
Database, or you can maintain the optimizer statistics manually using the DBMS_STATS package.

Oracle Database 11g: SQL Tuning Workshop 7 - 3


Types of Optimizer Statistics

• Table statistics: • Column statistics


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Number of rows – Basic: Number of distinct


– Number of blocks values, number of nulls,
– Average row length average length, min, max
– Histograms (data distribution
• Index Statistics:
when the column data is
– B*-tree level skewed)
– Distinct keys – Extended statistics n s e
i ce
– Number of leaf blocks
b l el
– Clustering factor
fer a
• System statistics a n s
n -tr
– I/O performance and o
utilization s an
) h a deฺ
– CPU performance and
r ฺ e s Gui
utilization
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io Statistics
Types of Optimizer
o
A
Most ntthe optimizer statistics are listed in the slide.
of
e
JosStarting with Oracle Database 10g, index statistics are automatically gathered when the index is
created or rebuilt.
Note: The statistics mentioned in this slide are optimizer statistics, which are created for query
optimization and are stored in the data dictionary. These statistics should not be confused with
performance statistics visible through V$ views.

Oracle Database 11g: SQL Tuning Workshop 7 - 4


Table Statistics (DBA_TAB_STATISTICS)

• Used to determine:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Table access cost


– Join cardinality
– Join order
• Some of the statistics gathered are:
– Row count (NUM_ROWS) e
n s
– Block count (BLOCKS) Exact lice
– Empty blocks (EMPTY_BLOCKS) Exact a b le
– Average free space per block (AVG_SPACE) s f er
- t r an
– Number of chained rows (CHAIN_CNT) on
– Average row length (AVG_ROW_LEN) s an
) h a deฺ
– ฺes
Statistics status (STALE_STATS) ui
ฺ m ir ent G
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o n io (DBA_TAB_STATISTICS)
Table Statistics
A nt
NUM_ROWS
e
Josdriving
This is the basis for cardinality computations. Row count is especially important if the table is the
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. Block count in combination with
DB_FILE_MULTIBLOCK_READ_COUNT gives the base table access cost.
EMPTY_BLOCKS
Number of empty (never used) data blocks in the table. This is the blocks between the used data
blocks and the high-water mark.
AVG_SPACE
This is the average amount of free space, in bytes, in a data block allocated to the table.
CHAIN_CNT
This is the number of rows in the table that are chained from one data block to another, or which
have migrated to a new block, requiring a link to preserve the old ROWID.
AVG_ROW_LEN
This is the average length of a row in the table in bytes.
STALE_STATS
This tells you if statistics are valid on the corresponding table.
Oracle Database 11g: SQL Tuning Workshop 7 - 5
Index Statistics (DBA_IND_STATISTICS)

• Used to decide:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Full table scan versus index scan


• Statistics gathered are:
– B*-tree level (BLEVEL) Exact
– Leaf block count (LEAF_BLOCKS)
– Clustering factor (CLUSTERING_FACTOR)
n s e
i ce
– Distinct keys (DISTINCT_KEYS)
b l el
– Average number of leaf blocks in which each distinctfe r in
value a
s
the index appears (AVG_LEAF_BLOCKS_PER_KEY tran ) n-
– Average number of data blocks in the table
a no pointed to by a
h a s eฺ
distinct value in the index (AVG_DATA_BLOCKS_PER_KEY )
)
s Gu i d
– Number of rows in the indexirฺe(NUM_ROWS)
ฺ m dent
d p
g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
nio (DBA_IND_STATISTICS)
Index Statistics
to
An to select an index access, the optimizer requires a predicate on the prefix of the index
In general,
e
Joscolumns. However, in case there is no predicate and all the columns referenced in the query are
present in an index, the optimizer considers using a full index scan versus a full table scan.
BLEVEL
This is used to calculate the cost of leaf block lookups. It indicates the depth of the index from its
root block to its leaf blocks. A depth of “0” indicates that the root block and leaf block are the same.
LEAF_BLOCKS
This is used to calculate the cost of a full index scan.
CLUSTERING_FACTOR
This measures the order of the rows in the table based on the values of the index. If the value is near
the number of blocks, the table is very well ordered. In this case, the index entries in a single leaf
block tend to point to the rows in the same data blocks. If the value is near the number of rows, the
table is very randomly ordered. In this case, it is unlikely that the index entries in the same leaf block
point to rows in the same data blocks.
STALE_STATS
This tells you if the statistics are valid in the corresponding index.

Oracle Database 11g: SQL Tuning Workshop 7 - 6


Index Statistics (DBA_IND_STATISTICS) (continued)
DISTINCT_KEYS
This is the number of distinct indexed values. For indexes that enforce UNIQUE and PRIMARY KEY
constraints, this value is the same as the number of rows in the table.
AVG_LEAF_BLOCKS_PER_KEY
This is the average number of leaf blocks in which each distinct value in the index appears, rounded
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

to the nearest integer. For indexes that enforce UNIQUE and PRIMARY KEY constraints, this value
is always 1 (one).
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. This statistic is the average number of data blocks that contain
rows that contain a given value for the indexed columns.
NUM_ROWS
n s e
It is the number of rows in the index. ice
l
a b le
e r
a n sf
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 7 - 7


Index Clustering Factor

Must read all blocks to retrieve all As


Block 1 Block 2 Block 3
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

A B C A B C A B C

Number of rows (9) A B C


Big clustering factor:
Favor alternative paths e
n s
DBA_IND_STATISTICS.CLUSTERING_FACTOR
lice
Small clustering factor: le
Favor the fera
b
Number of blocks (3)
A B C
ans path
index range scan
-tr
o n
s an
A A A B B B ha C C e Cฺ
) i d
Block 1
i r ฺes2 nt GBlock
Block u 3
Only need to read
g p ฺmoneublockde to retrieve all As
d t
i a @ his S
j ab Copyright
e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Index Clustering
o io Factor
TheA nt performs input/output (I/O) by blocks. Therefore, the optimizer’s decision to use full
system
e scans is influenced by the percentage of blocks accessed, not rows. This is called the index
Jostable
clustering factor. If the blocks contain single rows, the rows accessed and blocks accessed are the
same.
However, most tables have multiple rows in each block. Consequently, the desired number of rows
could be clustered together in a few blocks, or they could be spread out over a larger number of
blocks. In addition to information, such as the level of the B*-tree, the number of leaf blocks, and the
index selectivity, the cost formula of an index range scan also includes the clustering factor. This is
because a lower clustering factor indicates that the individual rows are concentrated within fewer
blocks in the table. A high clustering factor indicates that the individual rows are scattered more
randomly across the blocks in the table. Therefore, 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. In real life scenarios, 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, the clustering factor for one index might be small
while at the same time the clustering factor for another index might be large. 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.
Oracle Database 11g: SQL Tuning Workshop 7 - 8
Index Clustering Factor (continued)
The clustering factor is computed in the CLUSTERING_FACTOR column of the DBA_INDEXES
view when you gather statistics on the index. The way it is computed is relatively easy. You read the
index from left to right, 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. Based on this
algorithm, the smallest possible value for the clustering factor is the number of blocks, and the
highest possible value is the number of rows.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

The example in the slide shows how the clustering factor can affect cost. Assume the following
situation: There is a table with 9 rows, there is a nonunique index on col1 for table, the c1 column
currently stores the values A, B, and C, and the table only has three Oracle blocks.
• 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), 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. n s e
Note: For bitmap indexes, the clustering factor is not applicable and is not used. e l ice
b l
f e ra
n s
n - tra
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 7 - 9


Column Statistics (DBA_TAB_COL_STATISTICS)

• Count of distinct values of the column (NUM_DISTINCT)


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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) n s e
i ce
• Type of histogram (HISTOGRAM)
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o n io (DBA_TAB_COL_STATISTICS)
Column Statistics
A nt
NUM_DISTINCT is used in selectivity calculations, for example, 1/NDV
s e
Jo LOW_VALUE and HIGH_VALUE: The cost-based optimizer (CBO) assumes uniform distribution of
values between low and high values for all data types. These values are used to determine range
selectivity.
NUM_NULLS helps with selectivity of nullable columns and the IS NULL and IS NOT NULL
predicates.
DENSITY is only relevant for histograms. It is used as the selectivity estimate for nonpopular values.
Can be thought of as the probability of finding one particular value in this column. It is calculated
depending on whether there is a histogram on the column, and its type.
NUM_BUCKETS is the number of buckets in histogram for the column.
HISTOGRAM indicates the existence or type of the histogram: NONE, FREQUENCY, HEIGHT
BALANCED

Oracle Database 11g: SQL Tuning Workshop 7 - 10


Histograms

• The optimizer assumes uniform distributions; this may lead


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

to suboptimal access plans in the case of data skew.


• Histograms:
– Store additional column distribution information
– Give better selectivity estimates in the case of nonuniform
distributions
n s e
• With unlimited resources you could store each different ce
value and the number of rows for that value. le li
a b
er
• This becomes unmanageable for a large number nofsfdistinct
a
n o values and a different approach is used: n-tr
– Frequency histogram (#distinct values a
s ≤ #buckets)
– Height-balanced histogram (#buckets ) h a
<i d eฺ
#distinct values)
r ฺ e s Gu
• They are stored in DBA_TAB_HISTOGRAMS.
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Histogramsnio
A nto captures the distribution of different values in a column, so it yields better selectivity
A histogram
e
Josestimates. 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, join methods, and so on.
Without histograms, an even distribution is assumed. If a histogram is available on a column, the
estimator uses it instead of the number of distinct values.
When creating histograms, Oracle Database uses two different types of histogram representations
depending on the number of distinct values found in the corresponding column. When you have a
data set with less than 254 distinct values, and the number of histogram buckets is not specified, the
system creates a frequency histogram. If the number of distinct values is greater than the required
number of histogram buckets, the system creates a height-balanced histogram.
You can find information about histograms in these dictionary views: DBA_TAB_HISTOGRAMS,
DBA_PART_HISTOGRAMS, and DBA_SUBPART_HISTOGRAMS
Note: Gathering histogram statistics is the most resource-consuming operation in gathering statistics.

Oracle Database 11g: SQL Tuning Workshop 7 - 11


Frequency Histograms

10 buckets, 10 distinct values


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Cumulative cardinality
ENDPOINT NUMBER

40000 # rows for column value


30000

20000
n s e
i ce
10000
b l el
f er a
1 3 5 7 10 16 27 32 39 tr49 an s 0

o n -
ENDPOINT VALUE: Column a n
value
h a s eฺ
) i d
Distinct values: 1, 3, 5, 7, 10, 16, 27, 32,
i r ฺes39,n49 t Gu
Number of rows: 40001 g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Frequency n
o io
Histograms
ForA
the
t
nexample in the slide, assume that you have a column that is populated with 40,001 numbers.
s e
Jo You only have ten distinct values: 1, 3, 5, 7, 10, 16, 27, 32, 39, and 49. Value 10 is the most popular
value with 16,293 occurrences.
When the requested number of buckets equals (or is greater than) the number of distinct values, you
can store each different value and record exact cardinality statistics. In this case, in
DBA_TAB_HISTOGRAMS, the ENDPOINT_VALUE column stores the column value and the
ENDPOINT_NUMBER column stores the row count for that column value.
The row counts are stored in cumulative form as this can avoid some calculation for range scans. The
actual number of row counts is shown by the curve in the slide for clarity; only the
ENDPOINT_VALUE and ENDPOINT_NUMBER columns are stored in the data dictionary.

Oracle Database 11g: SQL Tuning Workshop 7 - 12


Viewing Frequency Histograms

BEGIN
DBMS_STATS.gather_table_STATS (OWNNAME=>'OE', TABNAME=>'INVENTORIES',
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

METHOD_OPT => 'FOR COLUMNS SIZE 20 warehouse_id');


END;

SELECT column_name, num_distinct, num_buckets, histogram


FROM USER_TAB_COL_STATISTICS
WHERE table_name = 'INVENTORIES' AND
column_name = 'WAREHOUSE_ID';

COLUMN_NAME NUM_DISTINCT NUM_BUCKETS HISTOGRAM


------------ ------------ ----------- ---------
n s e
ce
WAREHOUSE_ID 9 9 FREQUENCY

eli
SELECT endpoint_number, endpoint_value
a b l
FROM USER_HISTOGRAMS
fer
WHERE table_name = 'INVENTORIES' and column_name = 'WAREHOUSE_ID'
a n s
o n -tr ORDER BY endpoint_number;

ENDPOINT_NUMBER ENDPOINT_VALUE
--------------- -------------- s an
36 1
) h a deฺ
213 2
r ฺ e s Gui
261

3
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i
Viewing Frequencyo A Histograms
TheA nto in the slide shows you how to view a frequency histogram. Because the number of
example
e
Josdistinct values in the WAREHOUSE_ID column of the INVENTORIES table is 9, and the number of requested buckets is 20, the system automatically creates a frequency histogram with 9 buckets. You
can view this information in the USER_TAB_COL_STATISTICS view.
To view the histogram itself, you can query the USER_HISTOGRAMS view. You can see both
ENDPOINT_NUMBER that corresponds to the cumulative frequency of the corresponding and
ENDPOINT_VALUE, which represents, in the case, the actual value of the column data.
Note: The DBMS_STATS package is dealt with later in the lesson.

Oracle Database 11g: SQL Tuning Workshop 7 - 13


Height-Balanced Histograms

5 buckets, 10 distinct values


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

(8000 rows per bucket)

Popular value
ENDPOINT VALUE

1 7 10 10 32 49
Same number n s e
i ce
of rows per bucket
b l el
fer a
a n s
o n -tr 0 1 2 3 4 5

ENDPOINT NUMBER: Bucket


n
a number
h s
a deฺ
Distinct values: 1, 3, 5, 7, 10, 16, 27, 32, e )
s39, 49Gui
r ฺ
Number of rows: 40001 p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i
Height-Balanced o AHistograms
In aA nto
height-balanced histogram, the column values are divided into bands so that each band contains
s e
Jo approximately the same number of rows. The histogram tells you where in the range of values the
endpoints end. In the slide example, assume that you have a column that is populated with 40,001
numbers. You only have ten distinct values: 1, 3, 5, 7, 10, 16, 27, 32, 39, and 49. Value 10 is the
most popular value with 16,293 occurrences. When the number of buckets is less than the number of
distinct values, ENDPOINT_NUMBER records the bucket number and ENDPOINT_VALUE records
the column value that corresponds to this endpoint. In the example, the number of rows per bucket is
1/5th of the total number of rows, that is 8000.
Based on this assumption, value 10 appears between 8000 and 24000 times. So you are sure that
value 10 is a popular value.
This type of histogram is good for equality predicate on popular value, and range predicates.
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. In this example, value 10
is a popular value because it spans multiple endpoint values. The histogram does not actually store
duplicated buckets to save space. In the example in the slide, bucket 2 (with endpoint value 10)
would not be recorded in DBA_TAB_HISTOGRAMS for that reason.
Note: The density statistic in this example would be 1/9 * 4/5 = 0.088 or 8.8% (9=#NPV and
4=#NPB). This value would be used for selectivity calculation for a nonpopular value.

Oracle Database 11g: SQL Tuning Workshop 7 - 14


Viewing Height-Balanced Histograms

BEGIN
DBMS_STATS.gather_table_STATS(OWNNAME =>'OE', TABNAME=>'INVENTORIES',
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

METHOD_OPT => 'FOR COLUMNS SIZE 10 quantity_on_hand');


END;

SELECT column_name, num_distinct, num_buckets, histogram


FROM USER_TAB_COL_STATISTICS
WHERE table_name = 'INVENTORIES' AND column_name = 'QUANTITY_ON_HAND';

COLUMN_NAME NUM_DISTINCT NUM_BUCKETS HISTOGRAM


------------------------------ ------------ ----------- ---------------
QUANTITY_ON_HAND 237 10 HEIGHT BALANCED
n s e
i ce
SELECT endpoint_number, endpoint_value
FROM USER_HISTOGRAMS
b l el
WHERE table_name = 'INVENTORIES' and column_name = 'QUANTITY_ON_HAND'
fer a
ORDER BY endpoint_number;
a n s
n -tr
ENDPOINT_NUMBER ENDPOINT_VALUE
o
--------------- --------------
s an
0 0
) h a deฺ
s Gui
1 27
2 42
r ฺ e

3 57
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Viewing Height-Balanced
o Histograms
TheA
t
n in the slide shows you how to view a height-balanced histogram. Because the number
example
e
Josof distinct values in the QUANTITY_ON_HAND column of the INVENTORIES table is 237, and the
number of requested buckets is 10, the system automatically creates a height-balanced histogram
with 10 buckets. You can view this information in the USER_TAB_COL_STATISTICS view.
To view the histogram itself, you can query the USER_HISTOGRAMS view. You can see that the
ENDPOINT_NUMBER corresponds to the bucket number, and ENDPOINT_VALUE corresponds to
values of the endpoints end.
Note: The DBMS_STATS package is dealt with later in the lesson.

Oracle Database 11g: SQL Tuning Workshop 7 - 15


Histogram Considerations

• Histograms are useful when you have a high degree of skew


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

in the column distribution.


• 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 e
n s
• The maximum number of buckets is the least (254,# distinct lice
values). a b le
f e r
s
• Do not use histograms unless they substantiallyaimprove
n
- t r
performance. on
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Histogram n
o io
Considerations
A nt are useful only when they reflect the current data distribution of a given column. The
Histograms
e in the column can change as long as the distribution remains constant. If the data distribution of
Josdata
a column changes frequently, you must recompute its histogram frequently.
Histograms are useful when you have a high degree of data skew in the columns for which you want
to create histograms.
However, there is no need to create histograms for columns which do not appear in a WHERE clause
of a SQL statement. Similarly, there is no need to create histograms for columns with uniform
distribution.
In addition, for columns declared as UNIQUE, histograms are useless because the selectivity is
obvious. Also, the maximum number of buckets is 254, which can be lower depending on the actual
number of distinct column values. Histograms can affect performance and should be used only when
they substantially improve query plans. For uniformly distributed data, the optimizer can make fairly
accurate guesses about the cost of executing a particular statement without the use of histograms.
Note: Character columns have some exceptional behavior as histogram data is stored only for the
first 32 bytes of any string.

Oracle Database 11g: SQL Tuning Workshop 7 - 16


Multicolumn Statistics: Overview
VEHICLE
MAKE MODEL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

S(MAKE Λ MODEL)=S(MAKE)xS(MODEL)
select
dbms_stats.create_extended_stats('jfv','vehicle','(make,model)')
2
from dual;
n s e
exec dbms_stats.gather_table_stats('jfv','vehicle',-
li3 ce
method_opt=>'for all columns size 1 for columns (make,model) size 3'); l e
fe rab
a n s
DBA_STAT_EXTENSIONS VEHICLE
o n -tr
SYS_STUF3GLKIOP5F4B0BTTCFTMX0W

MAKE MODEL
s an 4
) h a deฺ
r ฺ e s Gui
S(MAKE ΛpMODELฺ mi )=S( d e nt
MAKE,MODEL )
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Multicolumn n o
iStatistics: Overview
t o
n Database 10g, the query optimizer takes into account the correlation between columns
With
e AOracle
Joswhen 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,
and the predicates are equalities used in equijoins, then the optimizer uses the number of distinct
keys (NDK) in the index for estimating selectivity, as 1/NDK.
• When DYNAMIC_SAMPLING is set to level 4, the query optimizer uses dynamic sampling to
estimate the selectivity of complex predicates involving several columns from the same table.
However, the sample size is very small and increases parsing time. As a result, the sample is
likely to be statistically inaccurate and may cause more harm than good.
In all other cases, the optimizer assumes that the values of columns used in a complex predicate are
independent of each other. It estimates the selectivity of a conjunctive predicate by multiplying the
selectivity of individual predicates. This approach results in underestimation of the selectivity if there
is a correlation between the columns. To circumvent this issue, Oracle Database 11g allows you to
collect, store, and use the following statistics to capture functional dependency between two or more
columns (also called groups of columns): number of distinct values, number of nulls, frequency
histograms, and density.

Oracle Database 11g: SQL Tuning Workshop 7 - 17


Multicolumn Statistics: Overview (continued)
For example, consider a VEHICLE table in which you store information about cars. The MAKE and
MODEL columns are highly correlated, in that MODEL determines MAKE. This is a strong
dependency, and both columns should be considered by the optimizer as highly correlated. You can
signal that correlation to the optimizer by using the CREATE_EXTENDED_STATS function as
shown in the example in the slide, and then compute the statistics for all columns (including the ones
for the correlated groups that you created).
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

The optimizer uses only multicolumn statistics with equality predicates.


Note
• The CREATE_EXTENDED_STATS function returns a virtual hidden column name, such as
SYS_STUW_5RHLX443AN1ZCLPE_GLE4.
• Based on the example in the slide, the name can be determined by using the following SQL:
select
n s e
dbms_stats.show_extended_stats_name('jfv','vehicle','(make,model
e l ice
)') from dual l
• After you create the statistics extensions, you can retrieve them by using the rab
e
ALL|DBA|USER_STAT_EXTENSIONS views.
ansf
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 7 - 18


Expression Statistics: Overview
CREATE INDEX upperidx ON VEHICLE(upper(MODEL))
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

VEHICLE
MODEL

VEHICLE
MODEL Still possible

Recommended

n s e
VEHICLE DBA_STAT_EXTENSIONS
ice
S(upper( MODEL))=0.01 MODEL
b l el
fe r a
SYS_STU3FOQ$BDH0S_14NGXFJ3TQ50

a n s
o n -tr
s an
h a deฺ
select dbms_stats.create_extended_stats('jfv','vehicle','(upper(model))')
)
from dual;
i
i r ฺes nt Gu
exec dbms_stats.gather_table_stats('jfv','vehicle',-

p ฺmsize 1 forecolumns (upper(model)) size 3');


g tud
method_opt=>'for all columns
d
@ his S
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
i o A Overview
ExpressionnStatistics:
A ntoinvolving expressions on columns are a significant issue for the query optimizer. When
Predicates
e
Joscomputing selectivity on predicates of the form function(Column) = constant, the optimizer assumes a static selectivity value of 1 percent. This approach is wrong because it may cause the
optimizer to produce suboptimal plans.
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.
Further enhancements have been made to evaluate built-in functions during query optimization to
derive better selectivity using dynamic sampling. Finally, the optimizer collects statistics on virtual
columns created to support function-based indexes.
However, these solutions are either limited to a certain class of functions or work only for
expressions used to create function-based indexes. By using expression statistics in Oracle Database
11g, you can use a more general solution that includes arbitrary user-defined functions and does not
depend on the presence of function-based indexes. As shown in the example in the slide, this feature
relies on the virtual column infrastructure to create statistics on expressions of columns.

Oracle Database 11g: SQL Tuning Workshop 7 - 19


Gathering System Statistics

• System statistics enable the CBO to use CPU and I/O


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

characteristics.
• System statistics must be gathered on a regular basis; this
does not invalidate cached plans.
• Gathering system statistics equals analyzing system activity
for a specified period of time:
n s e
• Procedures: i ce
– DBMS_STATS.GATHER_SYSTEM_STATS b l el
fe r a
– DBMS_STATS.SET_SYSTEM_STATS
a n s
o n -tr – DBMS_STATS.GET_SYSTEM_STATS
• GATHERING_MODE: s an
) h a deฺ
– NOWORKLOAD|INTERVAL ฺes
r G ui
– START|STOP p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
Gathering Systemi o A Statistics
o
ntstatistics
e A
System allow the optimizer to consider a system’s I/O and CPU performance, and
s
Jo utilization. For each candidate plan, the optimizer computes estimates for I/O and CPU costs. It is
important to know the system characteristics to select the most efficient plan with optimal proportion
between I/O and CPU cost. System CPU and I/O characteristics depend on many factors and do not
stay constant all the time. Using system statistics management routines, you can capture statistics in
the interval of time when the system has the most common workload. For example, database
applications can process online transaction processing (OLTP) transactions during the day and run
OLAP reports at night. You can gather statistics for both states and activate appropriate OLTP or
OLAP statistics when needed. This allows the optimizer to generate relevant costs with respect to the
available system resource plans. When the system generates system statistics, it analyzes system
activity in a specified period of time. Unlike the table, index, or column statistics, the system does
not invalidate already parsed SQL statements when system statistics get updated. All new SQL
statements are parsed using new statistics.
It is highly recommended that you gather system statistics. System statistics are gathered in a user-
defined time frame with the DBMS_STATS.GATHER_SYSTEM_STATS routine. You can also set system
statistics values explicitly using DBMS_STATS.SET_SYSTEM_STATS. Use
DBMS_STATS.GET_SYSTEM_STATS to verify system statistics.

Oracle Database 11g: SQL Tuning Workshop 7 - 20


Gathering System Statistics (continued)
When you use the GATHER_SYSTEM_STATS procedure, you should specify the
GATHERING_MODE parameter:
• NOWORKLOAD: This is the default. This mode captures characteristics of the I/O system.
Gathering may take a few minutes and depends on the size of the database. During this period
the system estimates the average read seek time and transfer speed for the I/O system. This
mode is suitable for all workloads. It is recommended that you run GATHER_SYSTEM_STATS
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

('noworkload') after you create the database and tablespaces.


• 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. You should
provide an interval value in minutes, after which system statistics are created or updated in the
dictionary or a staging table. You can use GATHER_SYSTEM_STATS
(gathering_mode=>'STOP') to stop gathering earlier than scheduled.
• START | STOP: Captures system activity during specified start and stop times, and refreshes these
c e n
dictionary or a staging table with statistics for the elapsed period.
e l i
b l
Note: Since Oracle Database 10g, Release 2, the system automatically gathers essential
f e ra parts of
system statistics at startup.
a ns
o n -tr
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 7 - 21


Gathering System Statistics: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EXECUTE DBMS_STATS.GATHER_SYSTEM_STATS(
First day interval => 120,
stattab => 'mystats', statid => 'OLTP');

EXECUTE DBMS_STATS.GATHER_SYSTEM_STATS(
First night interval => 120,
stattab => 'mystats', statid => 'OLAP');
n s e
li ce
b l e
Next days
EXECUTE DBMS_STATS.IMPORT_SYSTEM_STATS(
e r a
stattab => 'mystats', statid => 'OLTP');
a n sf
o n -tr
Next nights EXECUTE DBMS_STATS.IMPORT_SYSTEM_STATS( a n
stattab => 'mystats',
h s
a statid eฺ => 'OLAP');
) i d
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io Statistics: Example
Gathering System
o
TheA nt in the slide shows database applications processing OLTP transactions during the day
example
e
Josand running reports at night.
First, system statistics must be collected during the day. In this example, gathering ends after 120
minutes and is stored in the mystats table.
Then, system statistics are collected during the night. Gathering ends after 120 minutes and is stored
in the mystats table.
Generally, the syntax in the slide is used to gather system statistics. Before invoking the
GATHER_SYSTEM_STATS procedure with the INTERVAL parameter specified, you must activate job
processes using a command, such as SQL> alter system set job_queue_processes = 1;. You
can also invoke the same procedure with different arguments to enable manual gathering instead of
using jobs.
If appropriate, you can switch between the statistics gathered. Note that it is possible to automate this
process by submitting a job to update the dictionary with appropriate statistics. During the day, a job
may import the OLTP statistics for the daytime run, and during the night, another job imports the
online analytical processing (OLAP) statistics for the nighttime run.

Oracle Database 11g: SQL Tuning Workshop 7 - 22


Gathering System Statistics: Example

• Start manual system statistics collection in the data


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

dictionary:
SQL> EXECUTE DBMS_STATS.GATHER_SYSTEM_STATS( -
2 gathering_mode => 'START');

• Generate the workload.


• End the collection of system statistics: s e
ce n
SQL> EXECUTE DBMS_STATS.GATHER_SYSTEM_STATS( - ble
li
2 gathering_mode => 'STOP'); fera s
- t r an
n
no
a
s eฺ
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io Statistics: Example (continued)
Gathering System
o
TheA nt in the previous slide shows how to collect system statistics with jobs by using the
example
e parameter of the DBMS_STATS.GATHER_SYSTEM_STATS procedure. To collect system statistics
Josinternal
manually, another parameter of this procedure can be used as shown in the slide.
First, you must start the system statistics collection, and then you can can end the collection process
at any time after you are certain that a representative workload has been generated on the instance.
The example collects system statistics directly in the data dictionary.

Oracle Database 11g: SQL Tuning Workshop 7 - 23


Mechanisms for Gathering Statistics

• Automatic statistics gathering


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– gather_stats_prog automated task


• Manual statistics gathering
– DBMS_STATS package
• Dynamic sampling
• When statistics are missing: s e
Selectivity:
ce n
el i
Equality
a b l
1%
Inequality
fer 5%
Other predicates
a n s 5%
tr
Table row length
on- 20

s an
# of index leaf blocks 25

h a valuesdeฺ
# of distinct
) 100

r ฺ e s cardinality
Table
G ui 100

p ฺ mi Remoted e t cardinality
ntable 2000

d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Mechanisms
o n iofor Gathering Statistics
t
nDatabase
e A
Oracle provides several mechanisms to gather statistics. These are discussed in more detail
s
Jo in the subsequent slides. It is recommended that you use automatic statistics gathering for objects.
Note: When the system encounters a table with missing statistics, it dynamically gathers the
necessary statistics needed by the optimizer. However, for certain types of tables, it does not perform
dynamic sampling. These include remote tables and external tables. In those cases and also when
dynamic sampling has been disabled, the optimizer uses default values for its statistics.

Oracle Database 11g: SQL Tuning Workshop 7 - 24


Statistic Preferences: Overview
Optimizer Statement level
statistics
gathering Table level
DBA_TAB_STAT_PREFS
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

task
Schema level

Database level

Global level
CASCADE DEGREE
ESTIMATE_PERCENT METHOD_OPT
NO_INVALIDATE GRANULARITY
PUBLISH INCREMENTAL
n s e
ce
STALE_PERCENT

set_global_prefs
eli
a b l
set_database_prefs
DB
MS
_S
fe r
se
t|
ex get a
TA
n sTS

o n -tr set_schema_prefs po | d
rt | ele
im te
set_table_prefs
a n po
rt
gather_*_stats a
h s eฺ
) i d
i r ฺes nt Gu
g p ฺm ude
exec dbms_stats.set_table_prefs('SH','SALES','STALE_PERCENT','13');
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io
Statistic Preferences:
o Overview
TheA n t
automated statistics-gathering feature was introduced in Oracle Database 10g, Release 1 to
s e
Jo reduce the burden of maintaining optimizer statistics. However, there were cases where you had to
disable it and run your own scripts instead. One reason was the lack of object-level control.
Whenever you found a small subset of objects for which the default gather statistics options did not
work well, you had to lock the statistics and analyze them separately by using your own options. For
example, 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. The only way to get around this issue was to manually specify
the sample size in your own script.
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. You can use the DBMS_STATS package to manage the gathering statistics
options shown in the slide.

Oracle Database 11g: SQL Tuning Workshop 7 - 25


Statistic Preferences: Overview (continued)
You can set, get, delete, export, and import those preferences at the table, schema, database, and
global levels. Global preferences are used for tables that do not have preferences, whereas database
preferences are used to set preferences on all tables. The preference values that are specified in
various ways take precedence from the outer circles to the inner ones (as shown in the slide).
In the graphic in the slide, the last three highlighted options are new in Oracle Database 11g,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Release 1:
• CASCADE gathers statistics on the indexes as well. Index statistics gathering is not parallelized.
• ESTIMATE_PERCENT is the estimated percentage of rows used to compute statistics (Null
means all rows): The valid range is [0.000001,100]. Use the constant
DBMS_STATS.AUTO_SAMPLE_SIZE to have the system determine the appropriate sample
size for good statistics. This is the recommended default.
• NO_INVALIDATE invalidates or not dependent cursors. It does not invalidate the dependent
e
cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set toens
FALSE. Use DBMS_STATS.AUTO_INVALIDATE to have the system decide whenlto e lic
invalidate dependent cursors. This is the default. r a b
• PUBLISH is used to decide whether to publish the statistics to the dictionary
e
sfor to store them in
a n
a pending area before.
o n -tr
• STALE_PERCENT is used to determine the threshold level atn which an object is considered to
s a
have stale statistics. The value is a percentage of rowsamodifiedesince ฺ the last statistics
h to 13idpercent
gathering. The example changes the 10 percent s
e )
default u for SH.SALES only.
• DEGREE determines the degree of parallelism r ฺ
i used G
ntot compute statistics. The default for degree
p ฺ m d e
d g S
is null, which means use the table default tuconstant
value specified by the DEGREE clause in the CREATE
TABLE or ALTER TABLEastatement.
i @ his Use the DBMS_STATS.DEFAULT_DEGREE to
specify the default valuej b t
a basedsone the initialization parameters. The AUTO_DEGREE value
( u
ia oftoparallelism
determines thebdegree automatically. This is either 1 (serial execution) or
io A
DEFAULT_DEGREE (the system default value based on the number of CPUs and initialization
o n
A nt
parameters), depending on the size of the object.
is a SQL string used to collect histogram statistics. The default value is FOR
o s e• METHOD_OPT
J ALL COLUMNS SIZE AUTO.
• GRANULARITY is the granularity of statistics to collect for partitioned tables.
• INCREMENTAL is used to gather global statistics on partitioned tables in an incremental way.

It is important to note that you can change default values for the above parameters using the
DBMS_STATS.SET_PARAM procedure.
Note: You can describe all the effective statistics preference settings for all relevant tables by using
the DBA_TAB_STAT_PREFS view.

Oracle Database 11g: SQL Tuning Workshop 7 - 26


When to Gather Statistics Manually

• Rely mostly on automatic statistics collection:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Change the frequency of automatic statistics collection to


meet your needs.
– Remember that STATISTICS_LEVEL should be set to
TYPICAL or ALL for automatic statistics collection to work
properly.
• Gather statistics manually for: n s e
i ce
– Objects that are volatile
b l el
– Objects modified in batch operations f e ra
– External tables, system statistics, fixed objects t r a ns
o n -
– Objects modified in batch operations: a n statistics as part
Gather
of the batch operation. h s
a deฺ
e )
srightG ui object creation.
i r
– New objects: Gather statisticsฺ t after
g p ฺm uden
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
io Statistics Manually Ab
n
When to Gather
o
TheA nt
automatic statistics gathering mechanism keeps all the statistics current. It is very important to
s e
Jo you
determine when and how often to gather new statistics. The default gathering interval is nightly, but
can change this interval to suit your business needs. You can do so by changing the
characteristics of your maintenance windows. However, manual statistics gathering may be required
in certain cases. For example, the statistics on tables that are significantly modified during the day
may become stale. 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, statistics are not collected during GATHER_SCHEMA_STATS,
GATHER_DATABASE_STATS, and automatic optimizer statistics collection processing. However,
you can collect statistics on an individual external table using GATHER_TABLE_STATS. Sampling
on external tables is not supported so the ESTIMATE_PERCENT option should be explicitly set to
null. Because data manipulation is not allowed against external tables, it is sufficient to analyze
external tables when the corresponding file changes. Other areas in which statistics need to be
manually gathered are the system statistics and fixed objects, such as the dynamic performance
tables. These statistics are not automatically gathered.

Oracle Database 11g: SQL Tuning Workshop 7 - 27


Manual Statistics Gathering

You can use Enterprise Manager and the DBMS_STATS


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

package to:
• Generate and manage statistics for use by the optimizer:
– Gather/Modify
– View/Name
– Export/Import e
n s
– Delete/Lock i ce
• Gather statistics on: b l el
fe r a
– Indexes, tables, columns, partitions a n s
o n -tr – Object, schema, or database
• Gather statistics either serially or inaparallel s an
) h i d eฺ
• Gather/Set system statisticsir(currently ฺes nt Gunot possible in EM)
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io Gathering
Manual Statistics
o
A
Both nt
Enterprise Manager and the DBMS_STATS package enable you to manually generate and
s e
Jo manage statistics for the optimizer. You can use the DBMS_STATS package to gather, modify, view,
export, import, lock, and delete statistics. You can also use this package to identify or name gathered
statistics. You can gather statistics on indexes, tables, columns, and partitions at various granularity:
object, schema, and database level.
DBMS_STATS gathers only statistics needed for optimization; it does not gather other statistics. For
example, the table statistics gathered by DBMS_STATS include the number of rows, number of
blocks currently containing data, and average row length, but not the number of chained rows,
average free space, or number of unused data blocks.
Note: Do not use the COMPUTE and ESTIMATE clauses of the ANALYZE statement to collect
optimizer statistics. These clauses are supported solely for backward compatibility and may be
removed in a future release. The DBMS_STATS package collects a broader, more accurate set of
statistics, and gathers statistics more efficiently. 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

Oracle Database 11g: SQL Tuning Workshop 7 - 28


Manual Statistics Collection: Factors

• Monitor objects for DMLs.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Determine the correct sample sizes.


• Determine the degree of parallelism.
• Determine if histograms should be used.
• Determine the cascading effects on indexes.
• Procedures to use in DBMS_STATS: n s e
i ce
– GATHER_INDEX_STATS
b l el
– GATHER_TABLE_STATS fer a
a n s
– GATHER_SCHEMA_STATS tr
o n -
– GATHER_DICTIONARY_STATS
s an
– GATHER_DATABASE_STATS
s) ha ideฺ
– GATHER_SYSTEM_STATSirฺe G u
t
d g p ฺm u
t den
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
io Collection: Factors Ab
o n
Manual Statistics
t manually gather optimizer statistics, you must pay special attention to the following
WhenA nyou
e
Josfactors:
• 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

Oracle Database 11g: SQL Tuning Workshop 7 - 29


Managing Statistics Collection: Example

dbms_stats.gather_table_stats
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

('sh' -- schema
,'customers' -- table
, null -- partition
, 20 -- sample size(%)
, false -- block sample?
,'for all columns' -- column spec
n s e
, 4 -- degree of parallelism
i ce
,'default' -- granularity
b l el
, true ); -- cascade to indexes fer a
a n s
n - tr
dbms_stats.set_param('CASCADE', o
s an
'DBMS_STATS.AUTO_CASCADE');
) h a deฺ
r ฺ e s Gui
dbms_stats.set_param('ESTIMATE_PERCENT','5');
i nt
dbms_stats.set_param('DEGREE','NULL');
ฺm ude
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
io Ab
t o n
Managing Statistics Collection: Example
TheA n example uses the DBMS_STATS package to gather statistics on the CUSTOMERS table of
first
e
osthe SH schema. It uses some of the options discussed in the previous slides.
J
Setting Parameter Defaults
You can use the SET_PARAM procedure in DBMS_STATS to set default values for parameters of all
DBMS_STATS procedures. The second example in the slide shows this usage. You can also use the
GET_PARAM function to get the current default value of a parameter.
Note: Granularity of statistics to collect is pertinent only if the table is partitioned. This parameter
determines at which level statistics should be gathered. This can be at the partition, subpartition, or
table level.

Oracle Database 11g: SQL Tuning Workshop 7 - 30


Optimizer Dynamic Sampling: Overview

• Dynamic sampling can be done for tables and indexes:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Without statistics
– Whose statistics cannot be trusted
• Used to determine more accurate statistics when estimating:
– Table cardinality
– Predicate selectivity e
n s
• Feature controlled by: i ce
– The OPTIMIZER_DYNAMIC_SAMPLING parameter b l el
f er a
– The OPTIMIZER_FEATURES_ENABLE parameter an s
- t r
– The DYNAMIC_SAMPLING hint n on
a
– as hint ฺ
The DYNAMIC_SAMPLING_EST_CDN
) h de
s u i
m i rฺe nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io Sampling: Overview
Optimizer Dynamic
o
A nt sampling improves server performance by determining more accurate selectivity and
Dynamic
e
Joscardinality estimates that allow the optimizer to produce better performing plans. For example,
although it is recommended that you collect statistics on all of your tables for use by the CBO, you
may not gather statistics for your temporary tables and working tables used for intermediate data
manipulation. In these cases, the CBO provides a value through a simple algorithm that can lead to a
suboptimal execution plan. 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.
The DYNAMIC_SAMPLING and DYNAMIC_SAMPLING_EST_CDN hints can be used to further control
dynamic sampling.
Note: The OPTIMIZER_FEATURES_ENABLE initialization parameter turns off dynamic sampling if set
to a version prior to 9.2.

Oracle Database 11g: SQL Tuning Workshop 7 - 31


Optimizer Dynamic Sampling at Work

• Sampling is done at compile time.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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, predicates are applied to the e
sample to determine selectivity. n s
lice
• Use dynamic sampling when: a b le
r e
sf
– Sampling time is a small fraction of the executionntime
a
– Query is executed many times o n -tr
– You believe a better plan can be found
s an
a ฺ h de
s ) u i
m i rฺe nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io Sampling at Work
Optimizer Dynamic
o
TheA nt performance attribute is compile time. The system determines at compile time whether
primary
equery would benefit from dynamic sampling. If so, a recursive SQL statement is issued to scan a
Josasmall random sample of the table’s blocks, and to apply the relevant single table predicates to
estimate predicate selectivities.
Depending on the value of the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter, a certain
number of blocks is read by the dynamic sampling query.
For a query that normally completes quickly (in less than a few seconds), you do not want to incur
the cost of dynamic sampling. However, dynamic sampling can be beneficial under any of the
following conditions:
• A better plan can be found using dynamic sampling.
• The sampling time is a small fraction of total execution time for the query.
• The query is executed many times.
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 Database 11g: SQL Tuning Workshop 7 - 32


OPTIMIZER_DYNAMIC_SAMPLING

• Dynamic session or system parameter


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Can be set to a value from “0” to “10”


• “0” turns off dynamic sampling
• “1” samples all unanalyzed tables, if an unanalyzed table:
– Is joined to another table or appears in a subquery or
nonmergeable view e
n s
– Has no indexes i ce
– Has more than 32 blocks b l el
fer a
• “2” samples all unanalyzed tables a n s
o n -tr
• The higher the value the more aggressiven application of
sampling s a
) h a
i d eฺ
s Gupdateu
irฺe if no
• Dynamic sampling is repeatable tฺm uden
activity
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
Ab
nio
OPTIMIZER_DYNAMIC_SAMPLING
to
n dynamic sampling with the OPTIMIZER_DYNAMIC_SAMPLING parameter, which can be set
YouAcontrol
e
Josto a value from “0” to “10.” A value of “0” means dynamic sampling is not done.
A value of “1” (the default) 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.
• This unanalyzed table is joined to another table or appears in a subquery or nonmergeable view.
• This unanalyzed table has no indexes.
• This unanalyzed table has more blocks than the default number of blocks that would be used for
dynamic sampling of this table. This default number is 32.
The default value is “2” if OPTIMIZER_FEATURES_ENABLE is set to 10.0.0 or higher. At this
level, the system applies dynamic sampling to all unanalyzed tables. The number of blocks sampled
is two times the default number of dynamic sampling blocks (32).
Increasing the value of the parameter results in more aggressive application of dynamic sampling, in
terms of both the type of tables sampled (analyzed or unanalyzed) and the amount of I/O spent on
sampling.
Note: Dynamic sampling is repeatable if no rows have been inserted, deleted, or updated in the table
being sampled since the previous sample operation.

Oracle Database 11g: SQL Tuning Workshop 7 - 33


Locking Statistics

• Prevents automatic gathering


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Is mainly used for volatile tables:


– Lock without statistics implies dynamic sampling.
BEGIN
DBMS_STATS.DELETE_TABLE_STATS('OE','ORDERS');
DBMS_STATS.LOCK_TABLE_STATS('OE','ORDERS');
END;
n s e
– Lock with statistics for representative values. i ce
b l el
BEGIN
fe ra
DBMS_STATS.GATHER_TABLE_STATS('OE','ORDERS');
a n s
-tr
DBMS_STATS.LOCK_TABLE_STATS('OE','ORDERS');
o n END;

s an
• The FORCE argument overrides statistics
) h a dlocking.
i eฺ
i r ฺ es t Gu
SELECT stattype_locked FROM
g p u d en
ฺm dba_tab_statistics;
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io
Locking Statistics
o
A ntwith Oracle Database 10g, you can lock statistics on a specified table with the
Starting
e
JosLOCK_TABLE_STATS procedure of the DBMS_STATS package. 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. 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.
You can also lock statistics at the schema level by using the LOCK_SCHEMA_STATS procedure.
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.
You can use the UNLOCK_TABLE_STATS procedure to unlock the statistics on a specified table.
You can set the value of the FORCE parameter to TRUE to overwrite the statistics even if they are
locked. The FORCE argument is found in the following DBMS_STATS procedures:
DELETE_*_STATS, IMPORT_*_STATS, RESTORE_*_STATS, and SET_*_STATS.
Note: When you lock the statistics on a table, all the dependent statistics are considered locked. This
includes table statistics, column statistics, histograms, and dependent index statistics.

Oracle Database 11g: SQL Tuning Workshop 7 - 34


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Collect optimizer statistics


• Collect system statistics
• Set statistic preferences
• Use dynamic sampling
• Manipulate optimizer statistics n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 7 - 35


Practice 7: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Using system statistics


• Using automatic statistics gathering

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 7 - 36


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Using Bind Variables

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• List the benefits of using bind variables


• Use bind peeking
• Use adaptive cursor sharing

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 8 - 2


Cursor Sharing and Different Literal Values

SELECT * FROM jobs WHERE min_salary > 12000;


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM jobs WHERE min_salary > 18000;

SELECT * FROM jobs WHERE min_salary > 7500;

n s e
i ce
Cursor sharing b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
Library cache r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Cursor Sharing
o io and Different Literal Values
t SQL statements where literal values are provided for the WHERE clause conditions, you
nuse
A
If you
e results in many versions of almost identical SQL to be stored in the library cache. As each SQL
Josget
statement is submitted with a different value, the statement is not found in the library cache, and so
you must perform all steps for processing a new SQL statement. Not only does this incur typically
unnecessary statement parsing, it may also cause the library cache to fill up quickly because of all the
different statements stored in it.
When coded this way, you are not taking advantage of cursor sharing.
However, depending on the literal value provided, different execution plans might be generated by
the optimizer. For example, there might be a lot of JOBS, where MIN_SALARY is greater than
12000. On the other hand, there might be very few JOBS that have a MIN_SALARY greater than
18000. 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. This is illustrated in the slide. As
you can see, the first and third queries use the same execution plan, but the second query uses a
different one.
From a performance perspective, it is good to have separate cursors. However, this is not very
economic because you could have shared cursors for the first and last queries in this example.
Note: In the case of the slide example, V$SQL.PLAN_HASH_VALUE is identical for the first and
third query.

Oracle Database 11g: SQL Tuning Workshop 8 - 3


Cursor Sharing and Bind Variables

12000 SELECT * FROM jobs WHERE min_salary > :min_sal;


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

18000 SELECT * FROM jobs WHERE min_salary > :min_sal;

17500 SELECT * FROM jobs WHERE min_salary > :min_sal;

n s e
i ce
Cursor sharing b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
Library cache r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Cursor Sharing
o io and Bind Variables
A nt of issuing different statements for each literal, you use a bind variable, then that extra
If, instead
e activity is eliminated (in theory). This is because the optimizer recognizes that the statement is
Josparse
already parsed and decides to reuse the same execution plan even though you specified different bind
values the next time you reexecute the same statement.
In the example in the slide, the bind variable is called min_sal. It is to be compared with the
MIN_SALARY column of the JOBS table. Instead of issuing three different statements, issue a single
statement that uses a bind variable. At execution time, the same execution plan is used, the given
value is substituted for the variable.
However, from a performance perspective, this is not the best situation because you get best
performance two times out of three. On the other hand, this is very economic because you just need
one shared cursor in the library cache to execute all the three statements.

Oracle Database 11g: SQL Tuning Workshop 8 - 4


Bind Variables in SQL*Plus

SQL> variable job_id varchar2(10)


SQL> exec :job_id := 'SA_REP';
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PL/SQL procedure successfully completed.

SQL> select count(*) from employees where job_id = :job_id;

COUNT(*)
----------
30
n s e
i ce
SQL> exec :job_id := 'AD_VP';
b l el
fe r a
PL/SQL procedure successfully completed.
a n s
o n -tr
SQL> select count(*) from employees where job_id
a n = :job_id;
h a s eฺ
COUNT(*)
e s ) u id
---------- ฺ
ir ent G
ฺ m
2
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
Bind Variablesi o inASQL*Plus
A
Bind nto can be used in SQL*Plus sessions. In SQL*Plus, use the VARIABLE command to
variables
e
Josdefine a bind variable. Then, you can assign values to the variable by executing an assignment
statement with the EXEC[UTE] command. Any references to that variable from then on use the value
you assigned.
In the example in the slide, the first count is selected while SA_REP is assigned to the variable. The
result is 30. Then, AD_VP is assigned to the variable, and the resulting count is 2.

Oracle Database 11g: SQL Tuning Workshop 8 - 5


Bind Variables in Enterprise Manager
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Bind Variables
o n io in Enterprise Manager
On A
t
thenSQL Worksheet page of Enterprise Manager (see the SQL Worksheet link in the Related
e region of the Database Home page), you can specify that a SQL statement should use bind
JosLinks
variables. You can do this by selecting the “Use bind variables for execution” check box. When you
select that, several fields are generated, where you can enter bind variable values. Refer to these
values in the SQL statement using variable names that begin with a colon. The order in which
variables are referred to defines which variable gets which value. The first variable referred to gets
the first value, the second variable gets the second value, and so on. If you change the order in which
variables are referenced in the statement, you may need to change the value list to match that order.

Oracle Database 11g: SQL Tuning Workshop 8 - 6


Bind Variable Peeking

SELECT * FROM jobs WHERE min_salary > :min_sal 12000


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

First time
you execute
Plan A

n s e
ice
b l el
fe r a
min_sal=12000
a n s
o n -tr Next time
s an you execute
) h a deฺ
r ฺ e s Gui > :min_sal 18000
mi dent
SELECT * FROM jobs WHERE min_salary
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
i A
oPeeking
Bind Variablen
to are used in a query, those literal values can be used by the optimizer to decide on the
A
When nliterals
e
Josbest plan. However, when bind variables are used, the optimizer still needs to select the best plan
based on the values of the conditions in the query, but cannot see those values readily in the SQL
text. That means, as a SQL statement is parsed, the system needs to be able to see the value of the
bind variables, to ensure that a good plan that would suit those values is selected. The optimizer does
this by peeking at the value in the bind variable. When the SQL statement is hard parsed, the
optimizer evaluates the value for each bind variable, and uses that as input in determining the best
plan. After the execution is determined the first time you parsed the query, it is reused when you
execute the same statement regardless of the bind values used.
This feature was introduced in Oracle9i Database, Release 2. Oracle Database 11g changes this
behavior.

Oracle Database 11g: SQL Tuning Workshop 8 - 7


Bind Variable Peeking

SELECT * FROM jobs WHERE min_salary > :min_sal


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1 min_sal=12000

2 min_sal=18000
n s e
i ce
3 b l el
min_sal=7500
fe r a
a n s
o n -tr
s an
) h a deฺ
Plan A
r ฺ e s Gui
One plan not palways ฺ mi dappropriate
e nt for all bind values
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Bind Variable i oPeeking (continued)
t o
Ansome conditions, bind variable peeking can cause the optimizer to select the suboptimal plan.
Under
e
JosThis occurs because the first value of the bind variable is what is used to determine the plan for all
subsequent executions of the query. Therefore, even though subsequent executions provide different
bind values, the same plan is used. It is possible that a different plan would be better for executions
that have different bind variable values. An example is where the selectivity of a particular index
varies extremely depending on the column value. For low selectivity, a full table scan may be faster.
For high selectivity, an index range scan may be more appropriate. As shown in the slide, plan A
may be good for the first and third values of min_sal, but it may not be the best for the second one.
Suppose there are very few MIN_SALARY values that are above 18000, and plan A is a full table
scan. It is probable that a full table scan is not a good plan for the second execution, in that case.
So bind variables are beneficial in that they cause more cursor sharing to happen, and thus reduce
parsing of SQL. But, as in this case, it is possible that they cause a suboptimal plan to be chosen for
some of the bind variable values. This is a good reason for not using bind variables for decision
support system (DSS) environments, where the parsing of the query is a very small percentage of the
work done when submitting a query. The parsing may take fractions of a second, but the execution
may take minutes or hours. To execute with a slower plan is not worth the savings gained in parse
time.

Oracle Database 11g: SQL Tuning Workshop 8 - 8


Cursor Sharing Enhancements

• Oracle8i introduced the possibility of sharing SQL


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

statements that differ only in literal values.


• Oracle9i extends this feature by limiting it to similar
statements only, instead of forcing it.
• Similar: Regardless of the literal value, same execution plan
SQL> SELECT * FROM employees
n s e
2 WHERE employee_id = 153; i ce
b l el
• Not similar: Possible different execution plans for different
f er a
s
n -t
literal values ran
o
SQL> SELECT * FROM employees
2 WHERE department_id = 50; as
an
) h i d eฺ
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n
Cursor Sharing
o io Enhancements
A nt introduced the possibility of sharing SQL statements that differ only in literal values. Rather
Oracle8i
e
Josthan 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.
Because only one execution plan is used instead of potential different ones, this feature should be
tested against your applications before you decide to enable it or not. That is why Oracle9i extends
this feature by sharing only statements considered as similar. That is, only when the optimizer has the
guarantee that the execution plan is independent of the literal value used. For example, consider a
query, where EMPLOYEE_ID is the primary key:
SQL> SELECT * FROM employees WHERE employee_id = 153;
The substitution of any value would produce the same execution plan. It would, therefore, be safe for
the optimizer to generate only one plan for different occurrences of the same statement executed with
different literal values.
On the other hand, assume that the same EMPLOYEES table has a wide range of values in its
DEPARTMENT_ID column. For example, department 50 could contain over one third of all employees
and department 70 could contain just one or two.

Oracle Database 11g: SQL Tuning Workshop 8 - 9


Cursor Sharing Enhancements (continued)
See the two queries:
SQL> SELECT * FROM employees WHERE department_id = 50;
SQL> SELECT * FROM employees WHERE department_id = 70;
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. In this case, depending on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

which statement was executed first, the execution plan could contain a full table (or fast full index)
scan, or it could use a simple index range scan.

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 8 - 10


The CURSOR_SHARING Parameter

• The CURSOR_SHARING parameter values:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– FORCE
– EXACT (default)
– SIMILAR
• CURSOR_SHARING can be changed using:
– ALTER SYSTEM e
n s
– ALTER SESSION i ce
– Initialization parameter files b l el
fer a
• The CURSOR_SHARING_EXACT hint a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio
The CURSOR_SHARING
to
Parameter
TheA n of the CURSOR_SHARING initialization parameter determines how the optimizer processes
value
e
Josstatements 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. It causes the optimizer to
examine the statement to ensure that replacement occurs only for safe literals. In doing this, it can
use information about the nature of any available index (unique or nonunique) and statistics collected
on the index or underlying table, including histograms.
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.
The CURSOR_SHARING_EXACT hint causes the system to execute the SQL statement without any
attempt to replace literals by bind variables.

Oracle Database 11g: SQL Tuning Workshop 8 - 11


Forcing Cursor Sharing: Example

SQL> alter session set cursor_sharing = FORCE;


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT * FROM jobs WHERE min_salary > 12000;


SELECT * FROM jobs WHERE min_salary > 18000;
SELECT * FROM jobs WHERE min_salary > 7500;

n s e
SELECT * FROM jobs WHERE min_salary > :"SYS_B_0" i ce
b l el
fe r a
a n s
o n -tr
s a n System-generated
) h a deฺ bind variable
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Forcing Cursor
o io Sharing: Example
A ntyou forced cursor sharing with the ALTER SESSION command, all your queries that differ
Because
e with literal values are automatically rewritten to use the same system-generated bind variable
Josonly
called SYS_B_0 in the example in the slide. As a result, you end up with only one child cursor
instead of three.
Note: Adaptive cursor sharing may also apply, and might generate a second child cursor in this case.

Oracle Database 11g: SQL Tuning Workshop 8 - 12


Adaptive Cursor Sharing: Overview

Adaptive cursor sharing:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Allows for intelligent cursor sharing only for statements that


use bind variables
• Is used to compromise between cursor sharing and
optimization
• Has the following benefits: e
n s
– Automatically detects when different executions would benefitli ce
from different execution plans a b le
s fer
– Limits the number of generated child cursors to anminimum
– Automated mechanism that cannot be turned n - ra
toff
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Adaptive Cursor
o io Sharing: Overview
BindA nt
variables were designed to allow the Oracle Database to share a single cursor for multiple SQL
s e
Jo statements to reduce the amount of shared memory used to parse SQL statements. However, cursor
sharing and SQL optimization are conflicting goals. Writing a SQL statement with literals provides
more information for the optimizer and naturally leads to better execution plans, while increasing
memory and CPU overhead caused by excessive hard parses. Oracle9i Database was the first attempt
to introduce a compromising solution by allowing similar SQL statements using different literal
values to be shared. For statements using bind variables, Oracle9i also introduced the concept of bind
peeking. To benefit from bind peeking, it is assumed that cursor sharing is intended and that different
invocations of the statement are supposed to use the same execution plan. If different invocations of
the statement would significantly benefit from different execution plans, bind peeking is of no use in
generating good execution plans.
To address this issue as much as possible, Oracle Database 11g introduces adaptive cursor sharing.
This feature is a more sophisticated strategy designed to not share the cursor blindly, 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, because the purpose of using bind
variables is to share cursors in memory, a compromise must be found regarding the number of child
cursors that need to be generated.

Oracle Database 11g: SQL Tuning Workshop 8 - 13


Adaptive Cursor Sharing: Architecture
Bind-sensitive cursor
System
observes
11
SELECT * FROM emp WHERE sal = :1 and dept = :2
statement
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

for a while. Bind-aware cursor


Initial selectivity cube Same selectivity cube No need
Initial plan
for new plan
GB S
o GB
HJ f
t HJ
HJ
0.0025 . P
a
0.003 . HJ
2
r
s
e
n s e
ce
0.15 0.18

:1=A & :2=B ⇒ S(:1)=0.15 ∧ S(:2)=0.0025 :1=C & :2=D ⇒ S(:1)=0.18 ∧ S(:2)=0.003
eli
a b l
Merged selectivity cubes No need Second selectivity cube
fe r
for new plan
a n s
Need new plan

-tr
H
a GB GB H
a 0.009 . GB GB
r
HJ HJ
r
o n HJ

an
d d HJ

4 P 0.004 . HJ HJ
P HJ HJ 3
a
r
s Cubes merged h r s
a deฺ
a
e
e )
s Gui
s
e

r ฺ
mi de:1=E
0.28

nt& :2=F ⇒ S(:1)=0.3 ∧ S(:2)=0.009


0.3

p ฺ
:1=G & :2=H ⇒ S(:1)=0.28 ∧ S(:2)=0.004
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
io Sharing: Architecture A
o n
Adaptive Cursor
t use adaptive cursor sharing, the following steps take place in the scenario illustrated in the
WhenA nyou
e
Josslide:
1. The cursor starts its life with a hard parse, as usual. If bind peeking takes place, and a histogram
is used to compute selectivity of the predicate containing the bind variable, then the cursor is
marked as a bind-sensitive cursor. In addition, some information is stored about the predicate
containing the bind variables, including the predicate selectivity. In the slide example, the
predicate selectivity that would be stored is a cube centered around (0.15,0.0025). Because of
the initial hard parse, an initial execution plan is determined using the peeked binds. After the
cursor is executed, 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, and finds the matching cursor for execution. At the end of execution,
execution statistics are compared with the ones currently stored in the cursor. 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.

Oracle Database 11g: SQL Tuning Workshop 8 - 14


Adaptive Cursor Sharing: Architecture (continued)
2. On the next soft parse of this query, if the cursor is now bind aware, bind-aware cursor matching
is used. Suppose the selectivity of the predicate with the new set of bind values is now
(0.18,0.003). Because selectivity is used as part of bind-aware cursor matching, and because the
selectivity is within an existing cube, the statement uses the existing child cursor’s execution
plan to run.
3. On the next soft parse of this query, suppose that the selectivity of the predicate with the new set
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

of bind values is now (0.3,0.009). Because that selectivity is not within an existing cube, no
child cursor match is found. So the system does a hard parse, which generates a new child cursor
with a second execution plan in that case. In addition, the new selectivity cube is stored as part
of the new child cursor. After the new child cursor executes, the system stores the bind values
and execution statistics in the cursor.
4. On the next soft parse of this query, suppose the selectivity of the predicate with the new set of
bind values is now (0.28,0.004). Because that selectivity is not within one of the existing cubes, n s e
the system does a hard parse. Suppose that this time, the hard parse generates the same lice
execution plan as the first one. Because the plan is the same as the first child cursor,a b le child
both
cursors are merged. That is, both cubes are merged into a new bigger cube,sand f erone of the child
cursors is deleted. The next time there is a soft parse, if the selectivity
- t r an within the new cube,
falls
the child cursor matches. on
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 8 - 15


Adaptive Cursor Sharing: Views

The following views provide information about adaptive cursor


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

sharing usage:

V$SQL Two new columns show whether a


cursor is bind sensitive or bind aware.

V$SQL_CS_HISTOGRAM Shows the distribution of the execution


count across the execution history
n s e
histogram i ce
b l el
V$SQL_CS_SELECTIVITY r a
Shows the selectivity cubes stored for
fe
n s
every predicate containing a bind
a
o n -tr variable and whose selectivity is used in

an
the cursor sharing checks

Shows h s
aexecution eฺstatistics of a cursor
V$SQL_CS_STATISTICS
)
s different i d
u bind sets
i r ฺeusing t G
g p ฺm uden
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io Sharing: Views
Adaptive Cursor
o
t determine whether a query is bind aware or not, and is handled automatically, without
nviews
A
These
e
Josany user input. However, information about what goes on is exposed through V$ views so that you
can diagnose problems, if any. New columns have been added to V$SQL:
• IS_BIND_SENSITIVE: Indicates if a cursor is bind sensitive; value YES | NO. 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. A cursor in the cursor
cache that has been marked to use bind-aware cursor sharing is called bind aware.
• V$SQL_CS_HISTOGRAM: Shows the distribution of the execution count across a three-bucket
execution history histogram.
• 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. It
contains the text of the predicates and the selectivity range low and high values.

Oracle Database 11g: SQL Tuning Workshop 8 - 16


Adaptive Cursor Sharing: Views (continued)
• V$SQL_CS_STATISTICS: Adaptive cursor sharing monitors execution of a query and
collects information about it for a while, and uses this information to decide whether to switch to
using bind-aware cursor sharing for the query. This view summarizes the information that it
collects to make this decision. For a sample of executions, it keeps track of the rows processed,
buffer gets, and CPU time. The PEEKED column has the value YES if the bind set was used to
build the cursor, and NO otherwise.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 8 - 17


Adaptive Cursor Sharing: Example

SQL> variable job varchar2(6)


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> exec :job := 'AD_ASST'


SQL> select count(*), max(salary) from emp where job_id=:job;

Selectivity
Plan A Plan B

n s e
i ce
b l el
fe ra
a n s
o n -tr
s an
) h a deฺ'AD_ASST'
r ฺ e s Gui
'SA_REP' ฺmi e nt
d p
g Stu d
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Adaptive Cursor
o io Sharing: Example
A nt the data in the slide. There are histogram statistics on the JOB_ID column, showing that
Consider
e are many thousands times more occurrences of SA_REP than AD_ASST. In this case, if literals
Joswere
there
used instead of a bind variable, the query optimizer would see that the AD_ASST value occurs
in less than 1% of the rows, whereas the SA_REP value occurs in approximately a third of the rows.
If the table has over a million rows in it, the execution plans are different for each of these values’
queries. The AD_ASST query results in an index range scan because there are so few rows with that
value. The SA_REP query results in a full table scan because so many of the rows have that value, it
is more efficient to read the entire table. But, as it is, using a bind variable causes the same execution
plan to be used for both of the values, at first. So, even though there exist different and better plans
for each of these values, they use the same plan.
After several executions of this query using a bind variable, the system considers the query bind
aware, at which point it changes the plan based on the bound value. This means the best plan is used
for the query, based on the bind variable value.

Oracle Database 11g: SQL Tuning Workshop 8 - 18


Interacting with Adaptive Cursor Sharing

• CURSOR_SHARING:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– If CURSOR_SHARING <> EXACT, statements containing


literals may be rewritten using bind variables.
– If statements are rewritten, adaptive cursor sharing may apply
to them.
• SQL Plan Management (SPM):
– If OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES is set to n s e
TRUE, only the first generated plan is used. lice
a b le
– As a workaround, set this parameter to FALSE, and run
s f er your
application until all plans are loaded in the cursor
- t r ancache.
– Manually load the cursor cache into the n on
corresponding plan
a
baseline. has eฺ s ) id
ฺ e u
ฺ m ir ent G
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Interactingn
o io Adaptive Cursor Sharing
with
nt cursor sharing is independent of the CURSOR_SHARING parameter. The setting of
• AAdaptive
e
Jos this parameter determines whether literals are replaced by the system-generated bind
variables. If they are, adaptive cursor sharing behaves just as it would if the user supplied binds
to begin with.
• When using the SPM automatic plan capture, the first plan captured for a SQL statement with
bind variables is marked as the corresponding SQL plan baseline. If another plan is found for
that same SQL statement (which maybe the case with adaptive cursor sharing), it is added to the
SQL statements plan history and marked for verification: It will not be used immediately. So
even though adaptive cursor sharing has come up with a new plan based on a new set of bind
values, SPM does not let it be used until the plan has been verified. Thus reverting to10g
behavior, only the plan generated based on the first set of bind values is used by all subsequent
executions of the statement. One possible workaround is to run the system for some time with
automatic plan capture set to False, and after the cursor cache has been populated with all the
plans a SQL statement with bind has, load the entire plan directly from the cursor cache into the
corresponding SQL plan baseline. By doing this, all the plans for a single SQL statement are
marked as SQL baseline plans by default. This course does not tackle SQL Plan Management.
Refer to the SQL Performance Tuning course or the New Features for Administrators course.

Oracle Database 11g: SQL Tuning Workshop 8 - 19


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• List the benefits of using bind variables


• Use bind peeking
• Use adaptive cursor sharing

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 8 - 20


Practice 8: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Using adaptive cursor sharing and bind peeking


• Using the CURSOR_SHARING initialization parameter

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 8 - 21


J
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

o s e
n A
t o n
Aio
b i a
b i
to
(ja use
d p ฺ
a@ this
r ฺ
g Stu
e ) h
mi dent
s
s Gui
an
o
a deฺ
tr n -
a n s fe
r
a b l el
i
ce n s e
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Using Optimizer Hints

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to :


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Use hints when appropriate


• Specify hints for:
– Optimizer mode
– Query transformation
– Access path s e
– Join orders ce n
eli
– Join methods a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 9 - 2


Optimizer Hints: Overview

Optimizer hints:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Influence optimizer decisions


• Example:
SELECT /*+ INDEX(e empfirstname_idx) skewed col */ *
FROM employees e
WHERE first_name='David'
n s e
• HINTS SHOULD ONLY BE USED AS A LAST RESORT. le lice
r a b
• When you use a hint, it is good practice to also addsa f e
a n
comment about that hint. -tr on
a n
h a s eฺ
) i d
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io Overview
Optimizer Hints:
o
t you to influence decisions made by the optimizer. Hints provide a mechanism to direct
nenable
A
Hints
e
Josthe optimizer to select a certain query execution plan based on the specific criteria.
For example, you might know that a certain index is more selective for certain queries. Based on this
information, you might be able to select a more efficient execution plan than the plan that the
optimizer recommends. In such a case, use hints to force the optimizer to use the optimal execution
plan. This is illustrated in the slide example where you force the optimizer to use the
EMPFIRSTNAME_IDX index to retrieve the data. As you can see, you can use comments in a SQL
statement to pass instructions to the optimizer.
The plus sign (+) causes the system to interpret the comment as a list of hints. The plus sign must
follow immediately after the comment delimiter. No space is permitted.
Hints should be used sparingly, and only after you have collected statistics on the relevant tables and
evaluated the optimizer plan without hints using the EXPLAIN PLAN statement. 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.
In addition, the use of hints involves extra code that must be managed, checked, and controlled.

Oracle Database 11g: SQL Tuning Workshop 9 - 3


Types of Hints
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
Statement hints Apply to the entire SQL statement i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Types of Hints
o io
A nt Single-table hints are specified on one table or view. INDEX and USE_NL are
Single-table:
e
Josexamples of single-table hints.
Multitable: Multitable hints are like single-table hints, except that the hint can specify one or more
tables or views. LEADING is an example of a multitable hint.
Query block: Query block hints operate on single query blocks. STAR_TRANSFORMATION and
UNNEST are examples of query block hints.
Statement: Statement hints apply to the entire SQL statement. ALL_ROWS is an example of a
statement hint.
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 Database 11g: SQL Tuning Workshop 9 - 4


Specifying Hints

Hints apply to the optimization of only one statement block:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• A self-contained DML statement against a table


• A top-level DML or a subquery
MERGE
SELECT
/*+ */
INSERT hint
n s e
i ce
el
UPDATE comment
text
a b l
DELETE
fer
MERGE
a n s
n -tr
o
an
SELECT
INSERT --+
h
hint s
a deฺ
e )
s comment ui
UPDATE
r ฺ G
p ฺ mi dentext t
DELETE
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Specifying n
o io
Hints
t to the optimization of only the block of the statement in which they appear. A statement
napply
A
Hints
e is:
Josblock
• A simple MERGE, SELECT, INSERT, UPDATE, or DELETE statement
• A parent statement or a subquery of a complex statement
• A part of a compound query using set operators (UNION, MINUS, INTERSECT)
For example, a compound query consisting of two component queries combined by the UNION
operator has two blocks, one for each component query. For this reason, hints in the first component
query apply only to its optimization, not to the optimization of the second component query.
Optimizer Hint Syntax
Enclose hints within the comments of a SQL statement. You can use either style of comment. The
hint delimiter (+) must come immediately after the comment delimiter. If you separate them by a
space, the optimizer does not recognize that the comment contains hints.

Oracle Database 11g: SQL Tuning Workshop 9 - 5


Rules for Hints

• Place hints immediately after the first SQL keyword of a


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

statement block.
• Each statement block can have only one hint comment, but
it can contain multiple hints.
• Hints apply to only the statement block in which they
appear. e
n s
• If a statement uses aliases, hints must reference the aliases lice
rather than the table names. a b le
s
• The optimizer ignores hints specified incorrectlyawithoutf er
t r n
raising errors. on -
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Rules for Hints
o io
nt must place the hint comment immediately after the first keyword (MERGE, SELECT,
• AYou
e
Jos • AINSERT, DELETE, or UPDATE) of a SQL statement block.
statement block can have only one comment containing hints, but it can contain many hints
inside that comment separated by spaces.
• Hints apply to only the statement block in which they appear and override instance- or session-
level parameters.
• If a SQL statement uses aliases, hints must reference the aliases rather than the table names.
The Oracle server ignores incorrectly specified hints. However, be aware of the following situations:
• You never get an error message.
• Other (correctly) specified hints in the same comment are considered.
• The Oracle server also ignores combinations of conflicting hints.

Oracle Database 11g: SQL Tuning Workshop 9 - 6


Hint Recommendations

• Use hints carefully because they imply a high-maintenance


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

load.
• Be aware of the performance impact of hard-coded hints
when they become less valid.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Hint Recommendations
o
nt hints as a last remedy when tuning SQL statements.
• AUse
e
Jos •• Hints may prevent the optimizer from using better execution plans.
Hints may become less valid (or even invalid) when the database structure or contents change.

Oracle Database 11g: SQL Tuning Workshop 9 - 7


Optimizer Hint Syntax: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

UPDATE /*+ INDEX(p PRODUCTS_PROD_CAT_IX)*/


products p
SET p.prod_min_price =
(SELECT
(pr.prod_list_price*.95)
FROM products pr
n s e
WHERE p.prod_id = pr.prod_id) lice
WHERE p.prod_category = 'Men'
a b le
AND p.prod_status = 'available, on stock' sfer
/ tran n -
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Optimizer Hint
o io Syntax: Example
TheA nt shows an example with a hint that advises the cost-based optimizer (CBO) to use the
slide
e The execution plan is as follows:
osindex.
J Execution Plan
----------------------------------------------------------
0 UPDATE STATEMENT Optimizer=ALL_ROWS (Cost=3 …)
1 0 UPDATE OF 'PRODUCTS'
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' (TABLE) (Cost…)
3 2 INDEX (RANGE SCAN) OF 'PRODUCTS_PROD_CAT_IX' (INDEX)
(cost…)
4 1 TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' (TABLE) (Cost…)
5 4 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.

Oracle Database 11g: SQL Tuning Workshop 9 - 8


Hint Categories

There are hints for:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Optimization approaches and goals


• Access paths
• Query transformations
• Join orders
• Join operation n s e
i ce
• Parallel execution
b l el
• Additional hints fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
Hint Categories
o
MostA ntthese hints are discussed in the following slides. Many of these hints accept the table and
of
e
Josindex names as arguments.
Note: Hints for parallel execution is not covered in this course.

Oracle Database 11g: SQL Tuning Workshop 9 - 9


Optimization Goals and Approaches
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ALL_ROWS Selects a cost-based approach with a goal


of best throughput

FIRST_ROWS(n) Instructs the Oracle server to optimize an


n s e
individual SQL statement for fast response i ce
b l el
fe r a
a n s
o n -tr
s an
Note: The ALTER SESSION...es ) haOPTIMIZER_MODE
i d eฺ
i r ฺ nt Gu
SET
statement does not affect pSQL
g ฺm that u d eis run from within PL/SQL.
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n
Optimization
o ioGoals and Approaches
A nt The ALL_ROWS hint explicitly selects the cost-based approach to optimize a statement
ALL_ROWS:
e
Josblock with a goal of best throughput. That is, minimum total resource consumption.
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. It instructs the server to
select the plan that returns the first n rows most efficiently. The FIRST_ROWS hint, which optimizes
for the best plan to return the first single row, is retained for backward compatibility and plan
stability. The optimizer ignores this hint SELECT statement blocks that include any blocking
operations, such as sorts or groupings. 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. If you specify this hint in any such statement, the database optimizes for best throughput.
If you specify either the ALL_ROWS or the FIRST_ROWS(n) hint in a SQL statement, and if the
data dictionary does not have statistics about tables accessed by the statement, then the optimizer
uses default statistical values to estimate the missing statistics and to subsequently select an
execution plan.
If you specify hints for access paths or join operations along with either the ALL_ROWS or
FIRST_ROWS(n) hint, the optimizer gives precedence to the access paths and join operations
specified by the hints.
Note: The FIRST_ROWS hints are probably the most useful hints.

Oracle Database 11g: SQL Tuning Workshop 9 - 10


Hints for Access Paths
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

FULL Performs a full table scan


CLUSTER Accesses table by cluster scan
HASH Accesses table by hash scan
ROWID Accesses a table by ROWID e
n s
i ce
INDEX Scans an index in the ascending order
b l el
e r a
t ra
INDEX_ASC
nsf
Scans an index in the ascending order
INDEX_COMBINE Explicitly chooses a bitmapoaccess path n -
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i
Hints for Accesso APaths
A nto one of these hints causes the optimizer to choose the specified access path only if the
Specifying
e
Josaccess path is available based on the existence of an index and on the syntactic constructs of the SQL
statement. If a hint specifies an unavailable access path, the optimizer ignores it. You must specify
the table to be accessed exactly as it appears in the statement. If the statement uses an alias for the
table, use the alias rather than the table name in the hint. The table name in the hint should not
include the schema name if the schema name is present in the statement.
FULL: The FULL hint explicitly selects a full table scan for the specified table. For example:
SELECT /*+ FULL(e) */ employee_id, last_name
FROM hr.employees e WHERE last_name LIKE 'K%';
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.
CLUSTER: The CLUSTER hint instructs the optimizer to use a cluster scan to access the specified
table. This hint applies only to clustered tables.
HASH: The HASH hint instructs the optimizer to use a hash scan to access the specified table. This
hint applies only to tables stored in a table cluster.
ROWID: The ROWID hint explicitly chooses a table scan by ROWID for the specified table.

Oracle Database 11g: SQL Tuning Workshop 9 - 11


Hints for Access Paths (continued)
INDEX: The INDEX hint explicitly chooses an index scan for the specified table. You can use the
INDEX hint for domain, B*-tree, bitmap, and bitmap join indexes. However, it is better if you use
INDEX_COMBINE rather than INDEX for bitmap indexes because it is a more versatile hint. This
hint can optionally specify one or more indexes.
If this hint specifies a single available index, the optimizer performs a scan on this index. The
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

optimizer does not consider a full table scan or a scan on another index on the table.
If this hint specifies a list of available indexes, the optimizer considers the cost of a scan on each
index in the list and then performs the index scan with the lowest cost. The optimizer can also choose
to scan multiple indexes from this list and merge the results, if such an access path has the lowest
cost. The optimizer does not consider a full table scan or a scan on an index not listed in the hint.
If this hint specifies no indexes, 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. The optimizer can also choose to se
scan multiple indexes and merge the results, if such an access path has the lowest cost. The optimizer c e n
el i
does not consider a full table scan. b l
INDEX_ASC: The INDEX_ASC hint explicitly chooses an index scan for the specified f e ra table. If the
statement uses an index range scan, the Oracle server scans the index entries t r a nsascending order of
in
their indexed values. Because the server’s default behavior for a range o n -
scan is to scan index entries in
the ascending order of their indexed values, this hint does notsspecify a n anything more than the INDEX
hint. However, you might want to use the INDEX_ASC ) h
hint i d eฺ ascending range scans
a to specify
explicitly, should the default behavior change. irฺes t Gu
INDEX_DESC: The INDEX_DESC hint p
instructs
n
ฺm uthedeoptimizer to use a descending index scan for
the specified table. If the statement d g S t
i a @uses anh i index range scan and the index is ascending, the system
s
scans the index entries in the
j b
a descending t
e order of their indexed values. In a partitioned index, the
(
ia to
results are in the descending orderu s
within each partition. For a descending index, this hint effectively
A b
cancels out theodescending order, resulting in a scan of the index entries in the ascending order.
n i
n to
INDEX_COMBINE: The INDEX_COMBINE hint explicitly chooses a bitmap access path for the
e A
table. If no indexes are given as arguments for the INDEX_COMBINE hint, the optimizer uses a
o s
J Boolean combination of bitmap indexes that has the best cost estimate for the table. If certain indexes
are given as arguments, the optimizer tries to use some Boolean combination of those particular
bitmap 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';
Note: For INDEX, INDEX_FFS, and INDEX_SS, there are counter hints, NO_INDEX,
NO_INDEX_FFS, and NO_INDEX_SS, respectively to avoid using those paths.

Oracle Database 11g: SQL Tuning Workshop 9 - 12


Hints for Access Paths
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

INDEX_JOIN Instructs the optimizer to use an index


join as an access path
INDEX_DESC Selects an index scan for the specified
table
INDEX_FFS Performs a fast-full index scan s e
ce n
INDEX_SS Performs an index skip scan
eli
a b l
NO_INDEX Does not allow using a set of indexesfer
ns
AND_EQUAL -tra
Merges single-column indexes
on
a n
h a s eฺ
) i d
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n io Paths (continued)
Hints for Access
o
A nt
INDEX_JOIN: The INDEX_JOIN hint explicitly instructs the optimizer to use an index join as an
s e
Jo access path. For the hint to have a positive effect, a sufficiently small number of indexes must exist
that contain all the columns required to resolve the query.
For example, the following query uses an index join to access the employee_id and
department_id columns, both of which are indexed in the employees table:
SELECT /*+index_join(employees emp_emp_id_pk emp_department_ix)*/
employee_id, department_id
FROM hr.employees WHERE department_id > 50;
INDEX_DESC: The INDEX_DESC hint explicitly chooses an index scan for the specified table. If
the statement uses an index range scan, the Oracle server scans the index entries in the descending
order of their indexed values. In a partitioned index, the results are in the descending order within
each partition.
For example:
SELECT /*+ INDEX_DESC(a ord_order_date_ix) */ a.order_date,
a.promotion_id, a.order_id
FROM oe.orders a WHERE a.order_date < '01-jan-1985';

Oracle Database 11g: SQL Tuning Workshop 9 - 13


Hints for Access Paths (continued)
INDEX_FFS: The INDEX_FFS hint causes a fast-full index scan to be performed rather than a full
table scan.
For example:
SELECT /*+ INDEX_FFS ( o order_pk ) */ COUNT(*)
FROM order_items l, orders o
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHERE l.order_id > 50 AND l.order_id = o.order_id;


INDEX_SS: The INDEX_SS hint instructs the optimizer to perform an index skip scan for the
specified indexes of the specified table. If the statement uses an index range scan, the system scans
the index entries in the ascending order of their indexed values. In a partitioned index, the results are
in the ascending order within each partition. There are also INDEX_SS_ASC and
INDEX_SS_DESC hints.
NO_INDEX: The NO_INDEX hint explicitly disallows a set of indexes for the specified table.
• If this hint specifies a single available index, the optimizer does not consider a scan on this en
se
index. Other indexes that are not specified are still considered. l e lic
• If this hint specifies a list of available indexes, the optimizer does not considereara b on any of
scan
n
the specified indexes. Other indexes that are not specified in the list are still
f
sconsidered.
• If this hint specifies no indexes, the optimizer does not consider anscan
a
-tr on any index on the
table. This behavior is the same as a NO_INDEX hint that specifies o
n a list of all available indexes
s a
for the table.
) h a deฺ
The NO_INDEX hint applies to function-based, B*-tree,
r ฺ e s bitmap,
G uior domain indexes. If a NO_INDEX
hint and an index hint (INDEX, INDEX_ASC, ฺ i ent INDEX_COMBINE, or
mINDEX_DESC,
INDEX_FFS) both specify the same d
p
g then
indexes, d the NO_INDEX hint and the index hint are
tuboth
aand is S
@ thehoptimizer
ignored for the specified indexes
a b i t considers the specified indexes.
j e
For example:
b i a( to us
i o A
SELECT /*+NO_INDEX(employees emp_empid)*/ employee_id

n t on FROM employees WHERE employee_id > 200;


A
AND_EQUAL:
e The AND_EQUAL hint explicitly chooses an execution plan that uses an access path
s
Jo that merges the scans on several single-column indexes, where you can specify: • The name or alias of the table that is associated with the indexes to be merged
• The indexes on which an index scan is to be performed. You must specify at least two indexes,
but no more than five.

Oracle Database 11g: SQL Tuning Workshop 9 - 14


The INDEX_COMBINE Hint: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT --+INDEX_COMBINE(CUSTOMERS)
cust_last_name
FROM SH.CUSTOMERS
WHERE ( CUST_GENDER= 'F' AND
CUST_MARITAL_STATUS = 'single')
OR CUST_YEAR_OF_BIRTH BETWEEN '1917'
n s e
i ce
el
AND '1920';
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
The INDEX_COMBINE
o n io Hint: Example
TheA n t
INDEX_COMBINE hint is designed for bitmap index operations. Remember the following:
s e• If certain indexes are given as arguments for the hint, the optimizer tries to use some
Jo combination of those particular bitmap indexes.
• If no indexes are named in the hint, all indexes are considered to be hinted.
• The optimizer always tries to use hinted indexes, whether or not it considers them to be cost
effective.
In the example in the slide, suppose that all the three columns that are referenced in the WHERE
predicate of the statement in the slide (CUST_MARITAL_STATUS, CUST_GENDER, and
CUST_YEAR_OF_BIRTH) have a bitmap index. When you enable AUTOTRACE, the execution plan
of the statement might appear as shown in the next slide.

Oracle Database 11g: SQL Tuning Workshop 9 - 15


The INDEX_COMBINE Hint: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Execution Plan
---------------------------------------------------
| 0 | SELECT STATEMENT |
| 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS
| 2 | BITMAP CONVERSION TO ROWIDS |
| 3 | BITMAP OR |
| 4 | BITMAP MERGE | n s e
i ce
| 5 | BITMAP INDEX RANGE SCAN | CUST_YOB_BIX
b l el
| 6 | BITMAP AND |
fer a
| 7 | BITMAP INDEX SINGLE VALUE| CUST_MARITAL_BIX
a n s
| 8 | BITMAP INDEX SINGLE VALUE| CUST_GENDER_BIX
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio
The INDEX_COMBINE
to
Hint: Example (continued)
Anexample in the slide, the following bitmap row sources are used:
In the
e
Jos
Bitmap Row Source Description
BITMAP CONVERSION Converts bitmaps into ROWIDs to access a table
TO ROWIDS: 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
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)

Oracle Database 11g: SQL Tuning Workshop 9 - 16


Hints for Query Transformation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

NO_QUERY_TRANSFORMATION Skips all query transformation


USE_CONCAT Rewrites OR into UNION ALL and
disables INLIST processing
NO_EXPAND Prevents OR expansions
REWRITE Rewrites query in terms of e
materialized views n s
i ce
NO_REWRITE Turns off query rewrite b l el
f er a
UNNEST Merges subquery bodies
a n sinto
tr
n-block
surrounding query
n o
NO_UNNEST Turns off sunnesting a
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n
Hints for Queryi oA
Transformation
t o
An
NO_QUERY_TRANSFORMATION:
e The NO_QUERY_TRANSFORMATION hint instructs the
s
Jo optimizer to skip all query transformations, including but not limited to OR-expansion, viewmerging, subquery unnesting, star transformation, and materialized view rewrite.
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. Generally, this
transformation occurs only if the cost of the query using the concatenations is cheaper than the cost
without them. The USE_CONCAT hint disables IN-list processing.
NO_EXPAND: The NO_EXPAND hint prevents the cost-based optimizer from considering OR-
expansion for queries having OR conditions or IN-lists in the WHERE clause. Usually, the optimizer
considers using OR expansion and uses this method if it decides that the cost is lower than not
using it.
REWRITE: The REWRITE hint instructs the optimizer to rewrite a query in terms of materialized
views, when possible, without cost consideration. Use the REWRITE hint with or without a view list.
This course does not deal with Materialized Views.
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, allowing the optimizer to consider them together
when evaluating access paths and joins.

Oracle Database 11g: SQL Tuning Workshop 9 - 17


Hints for Query Transformation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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 e
n s
li used ce
FACT Indicates that the hinted table should b l e
e r a
be considered as a fact table
a n sf
NO_FACT Indicates that the hinted n r should
-ttable
o
not be considered
s a nas a fact table
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
n i
Hints for Query oA
Transformation (continued)
t o
An The MERGE hint lets you merge a view for each query. If a view’s query contains a GROUP
MERGE:
e
JosBY clause or a DISTINCT operator in the SELECT list, then the optimizer can merge the view’squery into the accessing statement only if complex view merging is enabled. This is the case by
default, but you can disable this mechanism using the NO_MERGE hint for example. Complex
merging can also be used to merge an IN subquery into the accessing statement if the subquery is not
correlated.
When the MERGE hint is used without an argument, it should be placed in the view query block.
When MERGE is used with the view name as an argument, it should be placed in the surrounding
query.
NO_MERGE: The NO_MERGE hint causes the Oracle server not to merge views that can be merged.
This hint gives the user more influence over the way in which the view is accessed. When the
NO_MERGE hint is used without an argument, it should be placed in the view query block. When
NO_MERGE is used with the view name as an argument, it should be placed in the surrounding query.

Oracle Database 11g: SQL Tuning Workshop 9 - 18


Hints for Query Transformation (continued)
STAR_TRANSFORMATION: The STAR_TRANSFORMATION hint causes the optimizer to use the
best plan in which the transformation has been used. Without the hint, the optimizer could make a
cost-based decision to use the best plan that is generated without the transformation, instead of the
best plan for the transformed query.
Even if the hint is given, there is no guarantee that the transformation will take place. The optimizer
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

generates the subqueries only if it seems reasonable to do so. If no subqueries are generated, there is
no transformed query, and the best plan for the untransformed query is used regardless of the hint.
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.
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.
n s e
i ce
b l el
fe r a
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 9 - 19


Hints for Join Orders
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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 e
n s
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Hints for Join
o ioOrders
TheA nt
following hints are used to suggest join orders:
s e
Jo ORDERED: The ORDERED hint causes the Oracle server to join tables in the order in which they
appear in the FROM clause. If you omit the ORDERED hint from a SQL statement performing a join,
the optimizer selects the order in which to join the tables. 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. With a nested loops example, the most precise method is to
order the tables in the FROM clause in the order of the keys in the index, with the large table at the
end. Then use the following hints:
/*+ ORDERED USE_NL(FACTS) INDEX(facts fact_concat) */
Here, facts is the table and fact_concat is the index. A more general method is to use the
STAR hint.
LEADING: The LEADING hint instructs the optimizer to use the specified set of tables as the prefix
in the execution plan. The LEADING hint is ignored if the tables specified cannot be joined first in
the order specified because of dependencies in the join graph. If you specify two or more LEADING
hints on different tables, all the hints are ignored. If you specify the ORDERED hint, it overrides all
LEADING hints.

Oracle Database 11g: SQL Tuning Workshop 9 - 20


Hints for Join Operations

USE_NL Joins the specified table using a nested loop join


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

NO_USE_NL Does not use nested loops to perform the join


USE_NL_WITH_INDEX Similar to USE_NL, 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 nse
join e l ice
l b
USE_HASH Joins the specified table using a hash ra
fejoin
ans
NO_USE_HASH Does not use hash join n - t r
n o
DRIVING_SITE Instructs the optimizer a
s to execute the query at a
h a e ฺ
different site than
e s ) thatuselected id by the database

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Hints for Joini oOperations
A
Each ntodescribed here suggests a join operation for a table. In the hint, you must specify a table
hint
e
Josexactly the same way as it appears in the statement. If the statement uses an alias for the table, you
must use the alias rather than the table name in the hint. However, the table name in the hint should
not include the schema name if the schema name is present in the statement. 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; the hints are ignored if the
referenced table is the outer table.
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 the specified table as the inner table. 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, rather than for best throughput, then you can force the optimizer to
select a nested loop join by using the USE_NL hint.
USE_NL_WITH_INDEX: The USE_NL_WITH_INDEX hint is similar to the USE_NL hint.
However, if no index is specified, the optimizer must be able to use some index with at least one join
predicate as the index key. If an index is specified, the optimizer must be able to use that index with
at least one join predicate as the index key.
NO_USE_NL: The NO_USE_NL hint causes the optimizer to exclude the nested loops join.
However, in some cases tables can only be joined using nested loops. In such cases, the optimizer
ignores the hint for those tables.
Oracle Database 11g: SQL Tuning Workshop 9 - 21
Hints for Join Operations (continued)
In many cases, a nested loop join returns the first row faster than a sort-merge join does. 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. 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

In the following statement in which a nested loop is forced through a hint, orders is accessed
through a full table scan and the l.order_id = h.order_id filter condition is applied to
every row. For every row that meets the filter condition, order_items is accessed through the
index order_id.
SELECT /*+ USE_NL(l h) */ h.customer_id, l.unit_price * l.quantity
FROM oe.orders h ,oe.order_items l
WHERE l.order_id = h.order_id;
Adding an INDEX hint to the query could avoid the full table scan on orders, resulting in an n s e
lic e
execution plan similar to one that is used on larger systems, even though it might not be particularly
b l e
efficient here.
e r a
USE_MERGE: The USE_MERGE hint causes the Oracle server to join each specified
a n sf table with
no SELECT /*+USE_MERGE(employees departments)*/ * FROM employees,
n-tr
another row source by using a sort-merge join, as in the following example:
a
s eฺ
h a departments WHERE employees.department_id = departments.department_id;
NO_USE_MERGE: The NO_USE_MERGE hint causes e s u id to exclude the sort-merge join to
)the optimizer
ฺ t G table as the inner table.
r thenspecified
iusing

join each specified table to another row source m e
USE_HASH: The USE_HASH hint causes d gpthe Oracle
S tudserver to join each specified table with another
@ his example:
row source using a hash join,basiain the following
j a e t
SELECT /*+USE_HASH(l
i (
a to u s l2) */ l.order_date, l.order_id,
A b
l2.product_id, SUM(l2.unit_price*quantity)
i o
noe.orders l, oe.order_items l2
FROM
n t o
e AWHERE l.order_id = l2.order_id
Jo s GROUP BY l2.product_id, l.order_date, l.order_id;
Here is another example:
SELECT /*+use_hash(employees departments)*/ *
FROM hr.employees, hr.departments
WHERE employees.department_id = departments.department_id;
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.
DRIVING_SITE: This hint instructs the optimizer to execute the query at a different site than that
selected by the database. This hint is useful if you are using distributed query optimization to decide
on which site a join should be executed.

Oracle Database 11g: SQL Tuning Workshop 9 - 22


Additional Hints

APPEND Enables direct-path INSERT


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

NOAPPEND Enables regular INSERT


ORDERED_PREDICATES Forces the optimizer to preserve
the order of predicate evaluation
CURSOR_SHARING_EXACT Prevents replacing literals with
bind variables
n s e
CACHE Overrides the default caching ce
el i
specification of the table
a b l
PUSH_PRED fe
Pushes join predicate into sview
r
- t r an
Evaluates nonmerged n subqueries PUSH_SUBQ
first n o
s a
DYNAMIC_SAMPLING Controls ) h a
dynamic i d eฺsampling to
i r ฺes nserver
improve t Gu performance
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Additional n
o io
Hints
A nt The APPEND hint lets you enable direct-path INSERT if your database runs in serial
APPEND:
e
Josmode. Your database is in serial mode if you are not using Enterprise Edition. Conventional INSERT
is the default in serial mode, and direct-path INSERT is the default in parallel mode. In direct-path
INSERT, data is appended to the end of the table rather than using existing space currently allocated
to the table. As a result, direct-path INSERT can be considerably faster than the conventional
INSERT.
NOAPPEND: The NOAPPEND hint enables direct-path INSERT by disabling parallel mode for the
duration of the INSERT statement. (Conventional INSERT is the default in serial mode, and direct-
path INSERT is the default in parallel mode.)
ORDERED_PREDICATES: The ORDERED_PREDICATES hint forces the optimizer to preserve the
order of predicate evaluation, except for predicates that are used as index keys. Use this hint in the
WHERE clause of SELECT statements.

Oracle Database 11g: SQL Tuning Workshop 9 - 23


Additional Hints (continued)
If you do not use the ORDERED_PREDICATES hint, the Oracle server evaluates all predicates in the
following order:
1. Predicates without user-defined functions, type methods, or subqueries are evaluated first, in the
order specified in the WHERE clause.
2. Predicates with user-defined functions and type methods that have user-computed costs are
evaluated next, in the order of increasing cost.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3. Predicates with user-defined functions and type methods without user-computed costs are
evaluated next, in the order specified in the WHERE clause.
4. Predicates that are not specified in the WHERE clause (for example, predicates that are
transitively generated by the optimizer) are evaluated next.
5. Predicates with subqueries are evaluated last, in the order specified in the WHERE clause.
CURSOR_SHARING_EXACT: The Oracle server can replace literals in SQL statements with bind
variables if it is safe to do so. This is controlled with the CURSOR_SHARING startup parameter. The n s e
CURSOR_SHARING_EXACT hint causes this behavior to be disabled. In other words, the Oracle
e l ice
a
server executes the SQL statement without any attempt to replace literals with bind variables.
r bl
CACHE: The CACHE hint instructs the optimizer to place the blocks retrievedn s
for
e table in the
fthe
-
corresponding hot part of the buffer cache when a full table scan is performed.
n tra This hint is useful for
small lookup tables. a no
The CACHE and NOCACHE hints affect system statistics h a s scanse(long
table ฺ tables) and table scans
) i d
i r es t G
(short tables), as shown in the V$SYSSTAT data ฺdictionary u
view.
PUSH_PRED: The PUSH_PRED hint instructs
g p d en to push a join predicate into the view.
ฺmtheuoptimizer
PUSH_SUBQ: The PUSH_SUBQ @ hintdinstructsSthet optimizer to evaluate nonmerged subqueries at the
earliest possible step in thea b ia plan.
execution
s
thiGenerally, subqueries that are not merged are executed as
j e
ia ( tplan.
the last step in the execution
b o uIfs the subquery is relatively inexpensive and reduces the number
i o A evaluating the subquery earlier can improve performance. This hint has no
of rows significantly,
n
o subquery is applied to a remote table or one that is joined using a merge join.
effect iftthe
A n
o s e
DYNAMIC_SAMPLING: The DYNAMIC_SAMPLING hint lets you control dynamic sampling to
J improve server performance by determining more accurate selectivity and cardinality estimates. You
can set the value of DYNAMIC_SAMPLING to a value from 0 to 10. The higher the level, the more
effort the compiler puts into dynamic sampling and the more broadly it is applied. Sampling defaults
to the cursor level unless you specify a table.
Consider the following example:
SELECT /*+ dynamic_sampling(1) */ * FROM ...
This example enables dynamic sampling if all the following conditions are true:
• There is more than one table in the query.
• At least one table has not been analyzed and has no indexes.
• The optimizer determines that a relatively expensive table scan is required for the table that has
not been analyzed.

Oracle Database 11g: SQL Tuning Workshop 9 - 24


Additional Hints
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

MONITOR Forces real-time query monitoring


NO_MONITOR Disables real-time query
monitoring
RESULT_CACHE Caches the result of the query or
query fragment
n s e
NO_RESULT_CACHE Disables result caching for the lice
query or query fragment a b le
r fe
Sets initialization parameter n s
OPT_PARAM
n - t ra for
query duration no
s a
) h a
i d eฺ
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
Abia to
Additional n
o io (continued)
Hints
A nt The MONITOR hint forces real-time SQL monitoring for the query, even if the statement
MONITOR:
enot long running. This hint is valid only when the CONTROL_MANAGEMENT_PACK_ACCESS
Josisparameter is set to DIAGNOSTIC+TUNING.
NO_MONITOR: The NO_MONITOR hint disables real-time SQL monitoring for the query.
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_RESULT_CACHE: The optimizer caches query results in the result cache if the
RESULT_CACHE_MODE initialization parameter is set to FORCE. In this case, the
NO_RESULT_CACHE hint disables such caching for the current query.
OPT_PARAM: The OPT_PARAM hint lets you set an initialization parameter for the duration of the
current query only. This hint is valid only for the following parameters:
OPTIMIZER_DYNAMIC_SAMPLING, OPTIMIZER_INDEX_CACHING,
OPTIMIZER_INDEX_COST_ADJ, OPTIMIZER_SECURE_VIEW_MERGING, and
STAR_TRANSFORMATION_ENABLED

Oracle Database 11g: SQL Tuning Workshop 9 - 25


Hints and Views

• Do not use hints in views.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Use view-optimization techniques:


– Statement transformation
– Results accessed like a table
• Hints can be used on mergeable views and nonmergeable
views. e
n s
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n
Hints and Views
o io
nt not use hints in or on views because views can be defined in one context and used in
YouAshould
e
Josdifferently
another; such hints can result in unexpected plans. In particular, hints in views are handled
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. 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.
• Push the predicate of the referencing query block inside the view.
When these transformations are impossible, the view’s query is executed and the result is accessed as
if it were a table. This appears as a VIEW step in execution plans.

Oracle Database 11g: SQL Tuning Workshop 9 - 26


Hints and Views (continued)
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, UNION ALL, INTERSECT, MINUS)
• The CONNECT BY clause
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• The ROWNUM pseudocolumn


• Group functions (AVG, COUNT, MAX, MIN, 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, that hint is used regardless of any such hints in the
views.
• If there is no top-level optimizer-mode hint, mode hints in referenced views are used as long as se
all mode hints in the views are consistent. c e n
eareli
• If two or more mode hints in the referenced views conflict, all mode hints in the views b l
discarded and the session mode is used, whether default or user specified. fera
a s
ncontains
Access-method and join hints on referenced views are ignored unless the tview
n - r a single table
(or references another view with a single table). For such single-table
n oviews, an access-method hint
or a join hint on the view applies to the table in the view. s a
) h a deฺ
Access-method and join hints can also appear in a view
e sthe FROM i
definition:
uclause
r ฺ
i ent
• If the view is a subquery (that is, if it appears in G of a SELECT statement), all
access-method and join hints in thegview ฺ m
p aretpreserved
d when the view is merged with the top-
level query. d S u
• For views that are not a b ia@ access-method
subqueries, t h is and join hints in the view are preserved only if
( j u s eother tables or views
b ia to
the top-level query references no (that is, if the FROM clause of the
SELECTo A contains only the view).
statement
Hintsn to Nonmergeable Views
and
ni
A
e nonmergeable views, optimizer-mode hints in the view are ignored. The top-level query
Josdecides the optimization mode.
With

Because nonmergeable views are optimized separately from the top-level query, access-method and
join hints in the view are always preserved. For the same reason, access-method hints on the view in
the top-level query are ignored.
However, join hints on the view in the top-level query are preserved because (in this case) a
nonmergeable view is similar to a table.

Oracle Database 11g: SQL Tuning Workshop 9 - 27


Global Table Hints

• Extended hint syntax enables specifying for tables that


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

appear in views
• References a table name in the hint with a recursive dot
notation
CREATE view city_view AS
SELECT *
n s e
FROM customers c i ce
WHERE cust_city like 'S%'; b l el
fer a
a n s
n
SELECT /*+ index(v.c cust_credit_limit_idx)
o -tr */
an
v.cust_last_name, v.cust_credit_limit
s
FROM city_view v
) h a deฺ
WHERE cust_credit_limitirฺ>es5000; G ui
t ฺm uden
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.
ia to
ioHints Ab
Global Table
o n
A
Hints nthatt specify a table generally refer to tables in the DELETE, SELECT, or UPDATE query block
ewhich the hint occurs, rather than to tables inside any views that are referenced by the statement.
JosinWhen you want to specify hints for tables that appear inside views, it is recommended that you use
global hints instead of embedding the hint in the view.
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. In addition, an optional query
block name can precede the table specification.
For example, by using the global hint structure, you can avoid the modification of a view with the
specification of an index hint in the body of view.
Note: If a global hint references a table name or alias that is used twice in the same query (for
example, in a UNION statement), the hint applies to only the first instance of the table (or alias).

Oracle Database 11g: SQL Tuning Workshop 9 - 28


Specifying a Query Block in a Hint

explain plan for


select /*+ FULL(@strange dept) */ ename
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

from emp e, (select /*+ QB_NAME(strange) */ *


from dept where deptno=10) d
where e.deptno = d.deptno and d.loc = 'C';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ALL'));

Plan hash value: 615168685


---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost(%CPU)|
n s e
---------------------------------------------------------------
i ce
| 0 | SELECT STATEMENT | | 1 | 41 | 7 (15)|
b l el
|* 1 | HASH JOIN | | 1 | 41 | 7 (15)|
fer a
|* 2 | TABLE ACCESS FULL| DEPT | 1 | 21 | 3 (0)|
a n s
|* 3 | TABLE ACCESS FULL| EMP | 3 | 60 | 3 (0)|
n -tr
---------------------------------------------------------------
o
Query Block Name / Object Alias (identified by operation id):
s an
) h a deฺ
-------------------------------------------------------------
1 - SEL$DB579D14
r ฺ e s Gui
mi dent
2 - SEL$DB579D14 / DEPT@STRANGE
3 - SEL$DB579D14 / E@SEL$1
p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Specifying n o Block in a Hint
aiQuery
o
nt specify an optional query block name in many hints to specify the query block to which the
YouAcan
e
Joshint applies. This syntax lets you specify in the outer query a hint that applies to an inline view.
The syntax of the query block argument is of the @queryblock form, where queryblock is an
identifier that specifies a query block in the query. The queryblock identifier can either be
system-generated or user-specified. When you specify a hint in the query block itself to which the
hint applies, you do not have to specify the @queryblock syntax.
The slide gives you an example. You can see that the SELECT statement uses an inline view. The
corresponding query block is given the name strange through the use of the QB_NAME hint.
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. However, because you specify
the FULL hint to apply to the strange query block in the main query block, the optimizer does not
use the index in question. You can see that the execution plan exhibits a full table scan on the DEPT
table. In addition, the output of the plan clearly shows the system-generated names for each query
block in the original query.

Oracle Database 11g: SQL Tuning Workshop 9 - 29


Specifying a Full Set of Hints
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT /*+ LEADING(e2 e1) USE_NL(e1)


INDEX(e1 emp_emp_id_pk) USE_MERGE(j) FULL(j) */
e1.first_name, e1.last_name, j.job_id,
sum(e2.salary) total_sal
FROM hr.employees e1, hr.employees e2,
hr.job_history j
n s e
WHERE e1.employee_id = e2.manager_id
lice
AND e1.employee_id = j.employee_id
a b le
AND e1.hire_date = j.start_date s f er
- tran
GROUP BY e1.first_name, e1.last_name, nj.job_id
o
ORDER BY total_sal; an s eฺ
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Specifying n o Set of Hints
aiFull
o
t hints, you might sometimes need to specify a full set of hints to ensure the optimal
nusing
When
e A
Josexecution plan. For example, if you have a very complex query consisting of many table joins, and if
you specify only the INDEX hint for a given table, then the optimizer needs to determine the
remaining access paths to be used as well as the corresponding join methods. Therefore, even though
you gave the INDEX hint, 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.
In the example, the LEADING hint specifies the exact join order to be used. The join methods to be
used on the different tables are also specified.

Oracle Database 11g: SQL Tuning Workshop 9 - 30


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Set the optimizer mode


• Use optimizer hint syntax
• Determine access-path hints
• Analyze hints and their impact on views
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Summary nio
to you should have learned about additional optimizer settings and hints.
nlesson,
A
In this
e
JosBy using hints, you can influence the optimizer at the statement level. Use hints as a last remedy
when tuning SQL statements. There are several hint categories, one of which includes hints for
access-path methods.
To specify a hint, use the hint syntax in the SQL statement.

Oracle Database 11g: SQL Tuning Workshop 9 - 31


Practice 9: Overview

This practice covers using various hints to influence execution


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

plans.

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 9 - 32


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Application Tracing

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to do the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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 s e
ce n
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 10 - 2


End-to-End Application Tracing Challenge

Client Client Client Clients


Client JF/Session 6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Client OE Client OE Client C4

CRM ERP CRM CRM ERP CRM

Dedicated Dedicated Dedicated Shared Shared Shared


server server server server server server

Trace Trace Trace n s e


Trace Trace Trace
ice
file file file file file file
b l el
fe r a
• I want to retrieve traces from CRM service. tr a n s
-
• I want to retrieve traces from client C4. non
s a
• I want to retrieve traces from sessiona ฺ
) h 6. ide
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
io Ab
End-to-EndnApplication Tracing Challenge
t o
AnDatabase implements tracing by generating a trace file for each server process when you
Oracle
e
Josenable the tracing mechanism.
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. However, in a shared server configuration,
a client 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 for you
to get a complete picture of the life cycle of a session.
Moreover, 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.

Oracle Database 11g: SQL Tuning Workshop 10 - 3


End-to-End Application Tracing

• Simplifies the process of diagnosing performance problems


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

in multitier environments by allowing application workloads


to be seen by:
– Service
– Module
– Action
– Session n s e
i ce
– Client
b l el
• End-to-End Application Tracing tools: fer a
a n s
– Enterprise Manager
o n -tr
– DBMS_APPICATION_INFO, DBMS_SERVICE,
s an
DBMS_MONITOR, DBMS_SESSION
) h a deฺ
– SQL Trace and TRCSESS r ฺ e s Gui
utility
p ฺ mi dent
– tkprof dg tu
i a @ his S
j e t© 2008, Oracle. All rights reserved.
ab Copyright
(
ia to u s
A b
o io
End-to-EndnApplication Tracing
t
An Application Tracing simplifies the process of diagnosing performance problems in
End-to-End
e
Josmultitier environments. In multitier environments, a request from an end client is routed to different
database sessions by the middle tier, making it difficult to track a client across different database
sessions. End-to-End Application Tracing uses a client identifier to uniquely trace a specific end
client through all tiers to the database server.
You can used End-to-End Application Tracing to identify the source of an excessive workload, such
as a high-load SQL statement. Also, you can identify what a user’s session does at the database level
to resolve user performance problems.
End-to-End Application Tracing also simplifies management of application workloads by tracking
specific modules and actions in a service. Workload problems can be identified by End-to-End
Application Tracing for:
• Client identifier: Specifies an end user based on the logon Id, such as HR.
• Service: Specifies a group of applications with common attributes, service level thresholds, and
priorities; or a single application
• Module: Specifies a functional block within an application
• Action: Specifies an action, such as an INSERT or an UPDATE operation, 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. Other tools listed
in the slide are discussed later in this lesson.
Oracle Database 11g: SQL Tuning Workshop 10 - 4
Location for Diagnostic Traces
DIAGNOSTIC_DEST

Diagnostic Data Previous Location ADR Location


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Foreground process USER_DUMP_DEST $ADR_HOME/trace


traces

Background process BACKGROUND_DUMP_DEST $ADR_HOME/trace


traces

Alert log data BACKGROUND_DUMP_DEST $ADR_HOME/alert&trace

n s e
i ce
Core dumps CORE_DUMP_DEST $ADR_HOME/cdump
b l el
fe r a
Incident dumps USER|BACKGROUND_DUMP_DES
a n s
$ADR_HOME/incident/incdir_n

-tr
T

o n
s a nV$DIAG_INFO
) h a deฺ
ADR trace = Oracle s G
Database
r ฺ e 10g ui– critical error trace
trace

p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Location for n o
iDiagnostic Traces
t o
An Diagnostic Repository (ADR) is a file-based repository for database diagnostic data such
Automatic
e
Josas traces, incident dumps, packages, the alert log, Health Monitor reports, core dumps, and so on.
Starting with Oracle Database 11g, Release 1, the traditional …_DUMP_DEST initialization
parameters are ignored. The ADR root directory is known as the ADR base. Its location is set by the
DIAGNOSTIC_DEST initialization parameter. 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. With Oracle Database 11g, there is no distinction between foreground and
background trace files. Both types of files go into the $ADR_HOME/trace directory. You can use
V$DIAG_INFO to list some important ADR locations.
All nonincident traces are stored inside the TRACE subdirectory. This is the main difference from
previous releases, where critical error information is dumped into the corresponding process trace
files instead of incident dumps. Incident dumps are placed in files separated from the normal process
trace files starting with Oracle Database 11g.
Note: The main difference between a trace and a dump is that a trace is more of a continuous output,
such as when SQL tracing is turned on, and a dump is a one-time output in response to an event, such
as an incident. Also, a core is a binary memory dump that is port specific.
In the slide, $ADR_HOME is used to denote the ADR Home directory defined by the
DIAGNOSTIC_DEST initialization parameter. However, there is no official environment variable
called ADR_HOME.
Oracle Database 11g: SQL Tuning Workshop 10 - 5
What Is a Service?

• Is a means of grouping sessions that perform the same kind


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

of work
• Provides a single-system image instead of a multiple-
instances image
• Is a part of the regular administration tasks that provide
dynamic service-to-instance allocation
n s e
• Is the base for High Availability of connections
e l ice
• Provides a performance-tuning dimension r a bl
n s fe
• Is a handle for capturing trace information ra n -t
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
n io
What Is a Service?
o
TheA nt of a service was first introduced in Oracle8i as a means for the listener to perform
concept
e
Josconnection load balancing between nodes and instances of a cluster. However, the concept,
definition, and implementation of services have been dramatically expanded. A service organizes
work execution within the database to make it more manageable, measurable, tunable, and
recoverable. A service is a grouping of related tasks within the database with common functionality,
quality expectations, and priority relative to other services. A service provides a single-system image
for managing competing applications that run within a single instance and across multiple instances
and databases.
Using standard interfaces, Enterprise Manager, and SRVCTL, services can be configured,
administered, enabled, disabled, and measured as a single entity.
Services provide availability. Following outages, a service is recovered quickly and automatically at
surviving instances.
Services provide a dimension to performance tuning. With services, workloads are visible and
measurable. Tuning by “service and SQL” replaces tuning by “session and SQL” in the majority of
systems where sessions are anonymous and shared.
From a tracing point of view, a service provides a handle that permits capturing trace information by
service name regardless of the session.

Oracle Database 11g: SQL Tuning Workshop 10 - 6


Use Services with Client Applications
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ERP=(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=mynode)(PORT=1521))
(CONNECT_DATA=(SERVICE_NAME=ERP)))

url="jdbc:oracle:oci:@ERP"
n s e
i ce
b l el
fer a
a n s
url="jdbc:oracle:thin:@(DESCRIPTION= n -tr
o
(ADDRESS=(PROTOCOL=TCP)(HOST=mynode)(PORT=1521))
s an
(CONNECT_DATA=(SERVICE_NAME=ERP)))"
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Use Services
o n iowith Client Applications
A nt and middle-tier connection pools select a service by using the Transparent Network
Applications
e
JosSubstrate (TNS) connection descriptor.
The selected service must match the service that has been created.
The first example in the slide shows the TNS connect descriptor that can be used to access the ERP
service.
The second example shows the thick Java Database Connectivity (JDBC) connection description
using the previously defined TNS connect descriptor.
The third example shows the thin JDBC connection description using the same TNS connect
descriptor.

Oracle Database 11g: SQL Tuning Workshop 10 - 7


Tracing Services

• Applications using services can be further qualified by:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– MODULE
– ACTION
– CLIENT_IDENTIFIER
• Set using the following PL/SQL packages:
– DBMS_APPLICATION_INFO
– DBMS_SESSION n s e
i ce
• Tracing can be done at all levels:
b l el
– CLIENT_IDENTIFIER
f er a
– an s
SESSION_ID
- t r
– SERVICE_NAMES n on
– s a
MODULE
) h a
i d eฺ
– ACTION
i r ฺes nt Gu
– pฺm de MODULE, ACTION
Combination of SERVICE_NAME,
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io
Tracing Services
o
An A nt
application can qualify a service by MODULE and ACTION names to identify the important
s e
Jo 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 for the user. 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 user-recognizable name 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).
SESSION_ID is automatically set by the database when a session is created, and
CLIENT_IDENTIFIER can be set using the DBMS_SESSION.SET_IDENTIFIER procedure.

Oracle Database 11g: SQL Tuning Workshop 10 - 8


Tracing Services (continued)
The traditional method of tracing each session produces trace files with SQL commands that can
span workloads. This results in a hit-or-miss approach to diagnose problematic SQL. 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Note: DBA_ENABLED_TRACES displays information about enabled traces.

n s e
i ce
b l el
fe ra
a n s
n - tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
n t
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 10 - 9


Use Enterprise Manager to Trace Services
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n
Use Enterprise
o io Manager to Trace Services
On A
t
thenPerformance page, you can click the Top Consumers link. The Top Consumers page is
e
Josdisplayed.
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.

Oracle Database 11g: SQL Tuning Workshop 10 - 10


Service Tracing: Example

• Trace on service, module, and action:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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: n s e


lice
exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE
a b le
r
sfe
(client_id=>'C4', waits => TRUE, binds => FALSE);
n
n - tra
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n
Service Tracing:
o io Example
t code box, all sessions that log in under the AP service are traced. A trace file is created for
nfirst
A
In the
e session that uses the service, regardless of the module and action. To be precise, you can trace
Joseach
only 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 define this task are recorded in the trace file. This complements collecting statistics by service,
module, and action because relevant wait events for an 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');
Note: CLIENT_IDENTIFIER can be set using the DBMS_SESSION.SET_IDENTIFIER
procedure.
Oracle Database 11g: SQL Tuning Workshop 10 - 11
Session Level Tracing: Example

• For all sessions in the database:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EXEC dbms_monitor.DATABASE_TRACE_ENABLE(TRUE,TRUE);

EXEC dbms_monitor.DATABASE_TRACE_DISABLE();

• For a particular session: n s e


lice
EXEC dbms_monitor.SESSION_TRACE_ENABLE(session_id=>
a b le
fer
27, serial_num=>60, waits=>TRUE, binds=>FALSE);
s
- t r an
no
EXEC dbms_monitor.SESSION_TRACE_DISABLE(session_id n
a
=>27, serial_num=>60); as ฺ
) h de
s u i
m i rฺe nt G
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
Session Level
o n ioTracing: Example
nt use tracing to debug performance problems. Trace-enabling procedures have been
YouAcan
e
Josimplemented
a database.
as part of the DBMS_MONITOR package. These procedures enable tracing globally for

You can use the DATABASE_TRACE_ENABLE procedure to enable instance-wide session-level


SQL tracing. 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.
Use the SESSION_TRACE_DISABLE procedure to disable the trace for a given database session
identifier and serial number.
Note: SQL Trace involves some overhead, so you usually do not want to enable SQL Trace at the
instance level.

Oracle Database 11g: SQL Tuning Workshop 10 - 12


Trace Your Own Session

• Enabling trace:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

EXEC DBMS_SESSION.SESSION_TRACE_ENABLE(waits =>


TRUE, binds => FALSE);

• Disabling trace:
n s e
EXEC DBMS_SESSION.SESSION_TRACE_DISABLE();
i ce
b l el
• Easily identifying your trace files: fe r a
a n s
o n -tr alter session set
n tracefile_identifier='mytraceid';a
h s
a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Trace YournOwni o ASession
A
WhilenthetoDBMS_MONITOR package can be invoked only by a user with the DBA role, any user can
e SQL tracing for his or her own session by using the DBMS_SESSION package. The
Josenable
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.
TRACEFILE_IDENTIFIER is an initialization parameter that 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 is written to a trace file, which has 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
Note: The SQL_TRACE initialization parameter is deprecated as of Oracle Database 10g. You can
obtain the complete list of deprecated parameters by using the following statement:
SELECT name FROM v$parameter WHERE isdeprecated = 'TRUE'

Oracle Database 11g: SQL Tuning Workshop 10 - 13


The trcsess Utility
Client Client Client Clients
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CRM ERP CRM CRM ERP CRM

Dedicated Dedicated Dedicated Shared Shared Shared


server server server server server server

Trace Trace Trace Trace Trace Trace


n s e
file file file file file file
i ce
b l el
fe r a
TRCSESS TRCSESS
a n s
o n -tr
Trace file
a nTrace file
for CRM service tkprof
h a s eonefor
ฺ client

e s ) u id
r ฺ
Report
ifile n tG
ฺ m e
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
The trcsess
o n ioUtility
TheA nt
trcsess utility consolidates trace output from selected trace files on the basis of several
s e
Jo 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.

Oracle Database 11g: SQL Tuning Workshop 10 - 14


Invoking the trcsess Utility

trcsess [output=output_file_name]
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[session=session_id]
[clientid=client_identifier]
[service=service_name]
[action=action_name]
[module=module_name]
[<trace file names>]
n s e
i ce
Trace Trace Trace
b l el
file file file
fer a
a n s
n -tr
o
an
TRCSESS

s
a deฺ
Consolidated) h
r
trace ฺ e
file
s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Invoking the n o
itrcsess Utility
t o
n for the trcsess utility is shown in the slide, where:
TheAsyntax
e
Jos • output
output specifies the file where the output is generated. If this option is not specified, standard
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.
Note: One of the session, clientid, service, action, or module options must be
specified. If there are more than one options specified, the trace files, which satisfy all the criteria
specified are consolidated into the output file.

Oracle Database 11g: SQL Tuning Workshop 10 - 15


The trcsess Utility: Example

exec dbms_session.set_identifier('HR session');


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

First session Second 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;


n s e
li … ce
b l e
r
select * from departments;
e a

a n sf
o n -tr
exec DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE(
s a -n
client_id => 'HR session');
) h a deฺ
r ฺ e s Gsession' ui
p ฺ mi dent
trcsess output=mytrace.trc clientid='HR
$ORACLE_BASE/diag/rdbms/orcl/orcl/trace/*.trc
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
A Example
The trcsessn i oUtility:
TheA nto in the slide illustrates a possible use of the trcsess utility. The example assumes that
example
e
Josyou 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.

Oracle Database 11g: SQL Tuning Workshop 10 - 16


SQL Trace File Contents

• Parse, execute, and fetch counts


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• CPU and elapsed times


• Physical reads and logical reads
• Number of rows processed
• Misses on the library cache
• Username under which each parse occurred n s e
• Each commit and rollback e l ice
r a bl
• Wait event and bind data for each SQL statement sfe
• Row operations showing the actual execution - t r
plan an of each
n o n
SQL statement a
h a s eฺ physical writes,
• Number of consistent reads, physical
) d
reads,
i
and time elapsed for each i r ฺes nt G
operation on
u
a row
ฺm gp Stude
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
b ia to u
io Contents A
SQL Trace n File
o
ntalready, a SQL trace file provides performance information on individual SQL statements. It
A
As seen
e
Josgenerates 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.
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.

Oracle Database 11g: SQL Tuning Workshop 10 - 17


SQL Trace File Contents: Example

*** [ Unix process pid: 19687 ]


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

*** 2008-02-25 15:49:19.820


*** 2008-02-25 15:49:19.820
*** 2008-02-25 15:49:19.820
*** 2008-02-25 15:49:19.820

====================
PARSING IN CURSOR #4 len=23 dep=0 uid=82 oct=3 lid=82 tim=1203929332521849
hv=4069246757 ad='34b6f730' sqlid='f34thrbt8rjt5'
select * from employees
END OF STMT
PARSE #4:c=49993,e=67123,p=28,cr=403,cu=0,mis=1,r=0,dep=0,og=1,tim=1203929332521845
n s e
EXEC #4:c=0,e=16,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1203929332521911
i ce
FETCH #4:c=1000,e=581,p=6,cr=6,cu=0,mis=0,r=1,dep=0,og=1,tim=1203929332522553
FETCH #4:c=0,e=45,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,tim=1203929332522936
b l el

fer a
FETCH #4:c=0,e=49,p=0,cr=1,cu=0,mis=0,r=1,dep=0,og=1,tim=1203929333649241
a n s
pr=6 pw=6 time=0 us cost=3 size=7276 card=107)'
n -tr
STAT #4 id=1 cnt=107 pid=0 pos=1 obj=70272 op='TABLE ACCESS FULL EMPLOYEES (cr=15

o
an
*** [ Unix process pid: 19687 ]
*** 2008-02-25 15:49:19.820
*** 2008-02-25 15:49:19.820
h s
a deฺ
*** 2008-02-25 15:49:19.820
e )
s Gui
r ฺ
mi dent
*** 2008-02-25 15:49:19.820

p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
SQL Trace n
o io Contents: Example
File
A
There naret multiple types of trace files that can be generated by the Oracle Database. The one that is
e
Josreferred 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 files, you can find (for each statement that were 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.

Oracle Database 11g: SQL Tuning Workshop 10 - 18


Formatting SQL Trace Files: Overview

Use the tkprof utility to format your SQL trace files:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Sort raw trace file to exhibit top SQL statements


• Filter dictionary statements
Trace
Trace
file
Trace Trace Trace Trace Trace
fileTrace
file file file file file
Trace
file
file n s e
i ce
TRCSESS
b l el
f er a
Consolidated n s
aConcatenated
trace file tkprof
n -tr trace file
n o
s a
Report
) h a deฺ
r ฺ e s Gui
file

p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
FormattingnSQL
o io Trace Files: Overview
A
tkprof nt is an executable that parses SQL trace files to produce more readable output. Remember
e all the information in tkprof is available from the raw trace file. You can invoke a number of
Josthat
sort functions using tkprof. There is a huge number of sort options that can be accessed by
entering tkprof at the command prompt. A useful starting point is the fchela sort option, which
orders the output by elapsed time fetching. The resultant .prf file contains the most time-
consuming SQL statement at the start of the file. Another useful parameter is sys. 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 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.
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.
Oracle Database 11g: SQL Tuning Workshop 10 - 19
Invoking the tkprof Utility
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

tkprof inputfile outputfile [waits=yes|no]


[sort=option]
[print=n]
[aggregate=yes|no]
[insert=sqlscritfile]
n s e
[sys=yes|no]
lice
[table=schema.table]ble
f e ra
ns
[explain=user/password]
a
n -tr
[record=statementfile]
o
n
sa
[width=n]
h a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Invoking the n o
itkprof Utility
t o
Anyou enter the tkprof command without any arguments, it generates a usage message
When
e
Jostogether 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.
• aggregate: If set to NO, tkprof does not aggregate multiple users of the same SQL text.

Oracle Database 11g: SQL Tuning Workshop 10 - 20


Invoking the tkprof Utility (continued)
• insert: Creates a SQL script to store the trace file statistics in the database. tkprof creates
this script with the name you specify for sqlscritfile. 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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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, n s e
and DELETE statements against the table. If the table does not already exist, the user must l e
icalso
be able to issue the CREATE TABLE and DROP TABLE statements. This option allows bl e
multiple individuals to run tkprof concurrently with the same user in thesEXPLAIN fe r a value.
These individuals can specify different TABLE values and avoid destructively
t ra n interfering with
each other’s processing on the temporary plan table. If you use o n -
the EXPLAIN parameter without
the TABLE parameter, tkprof uses the PROF$PLAN_TABLE a n table in the schema of the user
specified by the EXPLAIN parameter. If you use )thehTABLE as dparameter
e ฺ without the EXPLAIN
s u i
parameter, tkprof ignores the TABLE parameter.
m i rฺe nIft noGplan table exists, tkprof creates the
PROF$PLAN_TABLE table and thenpdrops
g ฺ ituatdthe
e end.
d t
• explain: Determines the execution planSfor each SQL statement in the trace file and writes
these execution plans to b i
the
@ file.
aoutput h istkprof determines execution plans by issuing the
t
EXPLAIN PLANastatement
i (ja uafter seconnecting to the system with the user and password specified
A
in this parameter. to user must have CREATE SESSION system privileges.
b The specified
o
ni takes longer to process a large trace file if the EXPLAIN option is used.
tkprof
n t o
• Arecord: Creates a SQL script with the specified file name statementfile with all the
e
Jos nonrecursive
the trace file.
SQL statements in the trace file. This can be used to replay the user events from

• 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.

Oracle Database 11g: SQL Tuning Workshop 10 - 21


tkprof Sorting Options

Sort Option Description


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
Number of misses in the library cache during parse i ce
el
prsmis
execnt Number of executes that were called a b l
fe r
execpu CPU time spent executing
a n s
exeela Elapsed time executing
o n -tr
exedsk Number of disk reads during
s a n execute
) h a deฺ read during execute
exeqry Number of buffers
r ฺ e s Gui for consistent
Number iof buffers
execu
p ฺ m dentfor current read during execute
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
o n io Options
tkprof Sorting
TheA nt lists all the sort options you can use with the sort argument of tkprof.
table
e
Jos

Oracle Database 11g: SQL Tuning Workshop 10 - 22


tkprof Sorting Options

Sort Option Description


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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
n s e
Number of buffers for consistent read during fetch i ce
el
fchqry
fchcu Number of buffers for current read during fetchabl
s f er
fchrow Number of rows fetched
- t r an
User ID of user that parsed then
userid
n o cursor
s a
) h a
i d eฺ
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 10 - 23


Output of the tkprof Command

• Text of the SQL statement


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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 n s e


(This step modifies the data for the INSERT, UPDATE, lice
e
and DELETE statements.) abl fer
a n s
FETCH Retrieves the rows returned by a query -tr
(Fetches are performed only for the n on
SELECT
s a
statements.)
) h a
i d eฺ
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
Output of theiotkprof Command Ab
o n
TheA nt command output lists the statistics for a SQL statement by the SQL processing step.
tkprof
e step for each row that contains statistics is identified by the value of the call column.
osThe
J
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, columns, and other referenced objects.
EXECUTE This step is the actual execution of the statement by the Oracle server.
For the INSERT, UPDATE, and DELETE statements, this step modifies the
data (including sorts when needed). For the SELECT statements, this step
identifies the selected rows.
FETCH This step retrieves rows returned by a query and sorts them when needed.
Fetches are performed only for the SELECT statements.
Note: The PARSE value includes both hard and soft parses. A hard parse refers to the development
of the execution plan (including optimization); it is subsequently stored in the library cache. A soft
parse means that a SQL statement is sent for parsing to the database, but the database finds it in the
library cache and only needs to verify things, such as access rights. Hard parses can be expensive,
particularly due to the optimization. A soft parse is mostly expensive in terms of library cache
activity.

Oracle Database 11g: SQL Tuning Workshop 10 - 24


Output of the tkprof Command

There are seven categories of trace statistics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Count Number of times the procedure was executed


CPU Number of seconds to process
Elapsed Total number of seconds to execute
n s e
i ce
Number of physical blocks read
el
Disk
a b l
r
an
Query
sfe
Number of logical buffers read for consistent read
- t r
Current Number of logical buffers read in current
n on mode
s a
Rows Number of rows processedhby the fetch
) a
i d eฺ or execute
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Output of the
o n iotkprof Command (continued)
TheA nt is explained on the following page.
output
e
JosSample output is as follows:
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ---Parse
1 0.03 0.06 0 0 0 0
Execute 1 0.06 0.30 1 3 0 0
Fetch 2 0.00 0.46 0 0 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ---total
4 0.09 0.83 1 3 0 1

Oracle Database 11g: SQL Tuning Workshop 10 - 25


Output of the tkprof Command (continued)
Next to the CALL column, tkprof displays the following statistics for each statement:
• Count: Number of times a statement was parsed, executed, 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, tkprof aggregates identical statement executions into one
summary table.)
• CPU: Total CPU time in seconds for all parse, execute, or fetch calls
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Elapsed: Total elapsed time in seconds for all parse, execute, or fetch calls
• Disk: Total number of data blocks physically read from the data files on disk for all parse,
execute, or fetch calls
• Query: Total number of buffers retrieved in consistent mode for all parse, execute, or fetch
calls (Buffers are usually retrieved in consistent mode for queries.)
• Current: Total number of buffers retrieved in current mode (Buffers typically are retrieved in
current mode for data manipulation language statements. However, segment header blocks arense
always retrieved in current mode.)
e l ice
• Rows: Total number of rows processed by the SQL statement (This total does not include
r a bl rows
processed by subqueries of the SQL statement. For SELECT statements, the s e
fnumber of rows
returned appears for the fetch step. For the UPDATE, DELETE, and INSERT n
tra statements, the
n -
number of rows processed appears for the execute step.)
a no
Note
h a s eฺ
• DISK is equivalent to physical reads from
e s )
v$sysstat
u id or AUTOTRACE.
• QUERY is equivalent to consistent gets ฺ
ir from n tG
v$sysstat or AUTOTRACE.
ฺ m e
• CURRENT is equivalent to db block
d gp gets dfrom v$sysstat or AUTOTRACE.
S tu
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 10 - 26


Output of the tkprof Command

The tkprof output also includes the following:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Recursive SQL statements


• Library cache misses
• Parsing user ID
• Execution plan
• Optimizer mode or hint n s e
i ce
• Row source operation
b l el
fer a
...
a n s
-tr
Misses in library cache during parse: 1

o n Optimizer mode: ALL_ROWS

an
Parsing user id: 61

Rows Row Source Operation


------- ---------------------------------------------------
h s
a pr=0dpw=0 eฺ time=129 us)
24 TABLE ACCESS BY INDEX ROWID EMPLOYEES)(cr=9 i
24 INDEX RANGE SCAN SAL_IDX (cr=3 pr=0
i r ฺespw=0 t G u us)(object id …
time=1554

g p ฺm uden
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Output of the
o n iotkprof Command (continued)
A nt Calls
Recursive
e
JosTo execute a SQL statement issued by a user, the Oracle server must occasionally issue additional
statements. Such statements are called recursive SQL statements. For example, if you insert a row in
a table that does not have enough space to hold that row, the Oracle server makes recursive calls to
allocate the space dynamically. Recursive calls are also generated when data dictionary information
is not available in the data dictionary cache and must be retrieved from disk.
If recursive calls occur while the SQL Trace facility is enabled, tkprof marks them clearly as
recursive SQL statements in the output file. You can suppress the listing of recursive calls in the
output file by setting the SYS=NO command-line parameter. Note that the statistics for recursive
SQL statements are always included in the listing for the SQL statement that caused the recursive
call.
Library Cache Misses
tkprof also lists the number of library cache misses resulting from parse and execute steps for each
SQL statement. These statistics appear on separate lines following the tabular statistics.

Oracle Database 11g: SQL Tuning Workshop 10 - 27


Output of the tkprof Command (continued)
Row Source Operations
These provide the number of rows processed for each operation executed on the rows and additional
row source information, such as physical reads and writes; cr = consistent reads, w = physical writes,
r = physical reads, time = time (in microseconds).
Parsing User ID
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

This is the ID of the last user to parse the statement.


Row Source Operation
The row source operation shows the data sources for execution of the SQL statement. This is
included only if the cursor has been closed during tracing. If the row source operation does not
appear in the trace file, you may then want to see EXPLAIN PLAN.
Execution Plan e
n s
If you specify the EXPLAIN parameter on the tkprof command line, tkprof uses the EXPLAIN
l i ce
PLAN command to generate the execution plan of each SQL statement traced. tkprofbalso e
l displays
the number of rows processed by each step of the execution plan. fe r a
n s
n - tra command is run
Note: Be aware that the execution plan is generated at the time that the tkprof
n o
and not at the time the trace file was produced. This could make a difference if, for example, an
index has been created or dropped since tracing the statements. a
s eฺ
h a
Optimizer Mode or Hint
e s ) u id
This indicates the optimizer hint that is usedm
ฺ t G of the statement. If there is no hint,
ir theenexecution
during

dgp tud
it shows the optimizer mode that is used.
@ his S
i a
( j ab se t
b ia to u
o A
t o ni
A n
e
Jos

Oracle Database 11g: SQL Tuning Workshop 10 - 28


tkprof Output with No Index: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

...
select max(cust_credit_limit)
from customers
where cust_city ='Paris'

call count cpu elapsed disk query current rows


------- ------ -------- ---------- ---------- ---------- ---------- -------
Parse 1 0.02 0.02 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.10 0.09 1408 1459 0 1
------- ------ -------- ---------- ---------- ---------- ---------- -------
n s e
ce
total 4 0.12 0.11 1408 1459 0 1

eli
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
a b l
Parsing user id: 61
fer
a n s
Rows
-------
Row Source Operation
---------------------------------------------------
n -tr
1 SORT AGGREGATE (cr=1459 pr=1408 pw=0 time=93463 us)
o
77
an
TABLE ACCESS FULL CUSTOMERS (cr=1459 pr=1408 pw=0 time=31483 us)
s
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio with No Index: Example
tkprof Output
to
TheA n in the slide shows that the aggregation of results across several executions (rows) is
example
e
Josbeing fetched from the CUSTOMERS table. It requires .12 second of CPU fetch time. The statement is
executed through a full table scan of the CUSTOMERS table, as you can see in the row source
operation of the output.
The statement must be optimized.
Note: If CPU or elapsed values are 0, timed_statistics is not set.

Oracle Database 11g: SQL Tuning Workshop 10 - 29


tkprof Output with Index: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

...
select max(cust_credit_limit) from customers
where cust_city ='Paris'

call count cpu elapsed disk query current rows


------- ------ -------- ---------- ---------- ---------- ---------- ---------
Parse 1 0.01 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 77 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ---------
total 4 0.01 0.00 0 77 0 1

Misses in library cache during parse: 1


n s e
Optimizer mode: ALL_ROWS
i ce
Parsing user id: 61
b l el
Rows Row Source Operation
fer a
-------
1
---------------------------------------------------
SORT AGGREGATE (cr=77 pr=0 pw=0 time=732 us)
a n s
77
-tr
TABLE ACCESS BY INDEX ROWID CUSTOMERS (cr=77 pr=0 pw=0 time=1760 us)
n
77 INDEX RANGE SCAN CUST_CUST_CITY_IDX (cr=2 pr=0 pw=0 time=100
o
an
us)(object id 55097)

h s
a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
nio with Index: Example
tkprof Output
to
TheA n shown in the slide indicate that CPU time was reduced to .01 second when an index was
results
e
Josuses
created on the CUST_CITY column. These results may have been achieved because the statement
the index to retrieve the data. Additionally, because this example reexecutes the same statement,
most of the data blocks are already in memory. You can achieve significant improvements in
performance by indexing sensibly. Identify areas for potential improvement using the SQL Trace
facility.
Note: Indexes should not be built unless required. Indexes do slow down the processing of the
INSERT, UPDATE, and DELETE commands because references to rows must be added, changed, or
removed. Unused indexes should be removed. However, instead of processing all the application
SQL through EXPLAIN PLAN, you can use index monitoring to identify and remove any indexes
that are not used.

Oracle Database 11g: SQL Tuning Workshop 10 - 30


Summary

In this lesson, you should have learned how to:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• 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
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Summary nio
nto introduced you to the SQL Trace facility. By using SQL Trace, you can evaluate the
ThisAlesson
e
Joson
performance of SQL statements. After SQL Trace is enabled, you view execution plans and statistics
SQL statements that are generated in a trace file.
To use SQL Trace, follow these steps:
• Identify the appropriate initialization parameters that you want to use.
• Enable SQL Trace.
• Use tkprof to format the trace file that is generated.
• Interpret the output of tkprof.

Oracle Database 11g: SQL Tuning Workshop 10 - 31


Practice 10: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Creating a service
• Tracing your application using services
• Interpreting trace information using trcsess and tkprof

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 10 - 32


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Automating SQL Tuning

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s
Objectives

After completing this lesson, you should be able to do the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

following:
• Describe statement profiling
• Use SQL Tuning Advisor
• Use SQL Access Advisor
• Use Automatic SQL Tuning s e
ce n
eli
a b l
fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 11 - 2


Tuning SQL Statements Automatically

• Tuning SQL statements automatically eases the entire SQL


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

tuning process and replaces manual SQL tuning.


• Optimizer modes:
– Normal mode
– Tuning mode or Automatic Tuning Optimizer (ATO)
• SQL Tuning Advisor is used to access tuning mode. e
n s
• You should use tuning mode only for high-load SQL i ce
statements. b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Tuning SQL n o
iStatements Automatically
t o
AnSQL statements automatically is the capability of the query optimizer to automate the entire
Tuning
e
Josrepetitive,
SQL tuning process. This automatic process replaces manual SQL tuning, which is a complex,
and time-consuming function. The SQL Tuning Advisor exposes the features of SQL
tuning to the user. The enhanced query optimizer has two modes:
• In the normal mode, the optimizer compiles SQL and generates an execution plan. The normal
mode of the optimizer generates a reasonable execution plan for the vast majority of SQL
statements. In the normal mode, the optimizer operates with very strict time constraints, usually
a fraction of a second, during which it must find a good execution plan.
• In the tuning mode, the optimizer performs additional analysis to check whether the execution
plan produced under the normal mode can be further improved. The output of the query
optimizer in the tuning mode is not an execution plan, but a series of actions, along with their
rationale and expected benefit (for producing a significantly superior plan). When called under
tuning mode, the optimizer is referred to as Automatic Tuning Optimizer (ATO). The tuning
performed by ATO is called system SQL tuning.
Under the tuning mode, the optimizer can take several minutes to tune a single statement. ATO is
meant to be used for complex and high-load SQL statements that have a nontrivial impact on the
entire system.

Oracle Database 11g: SQL Tuning Workshop 11 - 3


Application Tuning Challenges
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

How can I
tune my I can do
high-load it for you!
SQL?
SQL workload
n s e
i ce
b l el
fe ra
a n s
ADDM
o n -tr
s an
High-load
) h a deฺ
SQL
r ฺ e s Gui SQL Tuning
i
ฺm uden t Advisor
d g p t
@ i s S
a b iaCopyrightt©h2008,
( j u s e Oracle. All rights reserved.

A bia to
io Challenges
ApplicationnTuning
o
TheA nt of identifying high-load SQL statements and tuning them is very challenging even for an
process
e
Josdatabase
expert. SQL tuning is not only one of the most critical aspects of managing the performance of a
server, but also one of the most difficult tasks to accomplish. Starting with Oracle Database
10g, the task of identifying high-load SQL statements has been automated by Automatic Database
Diagnostic Monitor (ADDM). 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, the task of
tuning them is still highly complex and requires a high level of expertise.
Also, the SQL tuning activity is a continuous task because the SQL workload can change relatively,
often when new application modules are deployed.
SQL Tuning Advisor, introduced with Oracle Database 10g, is designed to replace the manual tuning
of SQL statements. SQL statements that consume high resources (such as CPU, I/O, and temporary
space) are good candidates for SQL Tuning Advisor. The advisor receives one or more SQL
statements as input and then provides advice on how to optimize the execution plan, a rationale for
the advice, estimated performance benefits, and the actual command to implement the advice. You
accept the advice, thereby tuning the SQL statements. With the introduction of SQL Tuning Advisor,
you can now let the Oracle optimizer tune the SQL code for you.

Oracle Database 11g: SQL Tuning Workshop 11 - 4


SQL Tuning Advisor: Overview
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Statistics Check
optimization Detect stale or
mode missing statistics.

Plan Tuning
optimization Plan tuning
mode (SQL Profile).

n s e
Access Analysis
i ce
optimization
mode
Add missing index.
Run Access Advisor. b l el
fe r a
a n s
o n -tr SQL Analysis
optimization
SQL Tuning
Advisor Restructure SQL.
s an mode

) h a deฺ
r ฺ e s Gui Comprehensive
Automatic Tuning
Optimizer p ฺ mi dent SQL tuning
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
SQL Tuning n i o A Overview
Advisor:
nto Advisor is primarily the driver of the tuning process. It calls Automatic Tuning
SQLATuning
e
JosOptimizer (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. 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. It also collects auxiliary information in the form of customized optimizer
settings, such as first rows and all rows, based on the past execution history of the SQL
statement. It builds a SQL Profile using the auxiliary information and makes a recommendation
to create it. When a SQL Profile is created, the profile enables the query optimizer, under
normal mode, to generate a well-tuned plan.
• Access Path Analysis: ATO explores whether a new index can be used to significantly improve
access to each table in the query, and when appropriate makes recommendations to create such
indexes.
• SQL Structure Analysis: Here, ATO tries to identify SQL statements that lend themselves to
bad plans, and makes relevant suggestions to restructure them. The suggested restructuring can
be syntactic as well as semantic changes to the SQL code.

Oracle Database 11g: SQL Tuning Workshop 11 - 5


Stale or Missing Object Statistics

• Object statistics are key inputs to the optimizer.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• ATO verifies object statistics for each query object.


• ATO uses dynamic sampling and generates:
– Auxiliary object statistics to compensate for missing or stale
object statistics
– Recommendations to gather object statistics where e
appropriate: n s
i ce
b l el
fe ra
DBMS_STATS.GATHER_TABLE_STATS(
n s
ownname=>'SH', tabname=>'CUSTOMERS',
n - tra
no
estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE);
a
h a s eฺ
) i d
i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
ia to u s
io Object Statistics Ab
n
Stale or Missing
o
TheA nt optimizer relies on object statistics to generate execution plans. If these statistics are stale
query
e missing, the optimizer does not have the necessary information it needs and can generate
Josor
suboptimal execution plans.
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, and statistic
adjustment factor for objects with stale statistics
• Recommendations to gather relevant statistics for objects with stale or no statistics
For optimal results, you gather statistics when recommended and then rerun Automatic Tuning
Optimizer. However, you may be hesitant to accept this recommendation immediately because of the
impact it could have on other queries in the system.

Oracle Database 11g: SQL Tuning Workshop 11 - 6


SQL Statement Profiling

• Statement statistics are key inputs to the optimizer.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• ATO verifies statement statistics such as:


– Predicate selectivity
– Optimizer settings (FIRST_ROWS versus ALL_ROWS)
• Automatic Tuning Optimizer uses:
– Dynamic sampling s e
– Partial execution of the statement ce n
el i
– Past execution history statistics of the statement a b l
s f er
• ATO builds a profile if statistics were generated:
- t r an
n on
exec :profile_name := -a
h a s- ideฺ
dbms_sqltune.accept_sql_profile( )
task_name =>'my_sql_tuning_task'); i r ฺes nt Gu
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
n
SQL Statement
o io Profiling
TheA nt verification step during SQL Profiling is the verification of the query optimizer’s own
main
e
Josestimates of cost, selectivity, and cardinality for the statement that is tuned.
During SQL Profiling, ATO performs verification steps to validate its own estimates. The validation
consists of taking a sample of data and applying appropriate predicates to the sample. The new
estimate is compared to the regular estimate, and if the difference is large enough, a correction factor
is applied. 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. ATO picks the appropriate estimate validation
method.
ATO also uses the past execution history of the SQL statement to determine correct settings. For
example, if the execution history indicates that a SQL statement is only partially executed the
majority of times, ATO uses the FIRST_ROWS optimization as opposed to ALL_ROWS.
ATO builds a SQL Profile if it has generated auxiliary information either during Statistics Analysis
or during SQL Profiling. When a SQL Profile is built, it generates a user recommendation to create a
SQL Profile.
In this mode, ATO can recommend the acceptance of the generated SQL Profile to activate it.

Oracle Database 11g: SQL Tuning Workshop 11 - 7


Plan Tuning Flow and SQL Profile Creation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Submit Create
Optimizer
(Tuning mode)

SQL Tuning SQL


Advisor Profile

Use n s e
i ce
b l el
fe r a
No application
a n s
o n -tr code change Optimizer Output
(Normal mode)
s an
) h a deฺ
Database
r ฺ e s Gui Well-tuned
users
ฺ m i ent plan

d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
Plan Tuning n i o A and SQL Profile Creation
Flow
to is a collection of auxiliary information that is built during automatic tuning of a SQL
nProfile
A
A SQL
e
Josstatement. Thus, a SQL Profile is to a SQL statement what statistics are to a table or index. After it is
created, a SQL Profile is used in conjunction with the existing statistics by the query optimizer, in
normal mode, to produce a well-tuned plan for the corresponding SQL statement. A SQL Profile is
stored persistently in the data dictionary. However, the SQL profile information is not exposed
through regular dictionary views. After creation of a SQL Profile, every time the corresponding SQL
statement is compiled in normal mode, the query optimizer uses the SQL Profile to produce a well-
tuned plan.
The slide shows the process flow of the creation and use of a SQL Profile. The process consists of
two separate phases. They are system SQL tuning phase and regular optimization phase. During the
system SQL tuning phase, 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
invokes ATO to generate tuning recommendations, possibly with a SQL Profile. If a SQL Profile is
built, you can accept it. When it is accepted, the SQL Profile is stored in the data dictionary. In the
next phase, when an end user issues the same SQL statement, the query optimizer (under normal
mode) uses the SQL Profile to build a well-tuned plan. The use of the SQL Profile remains
completely transparent to the end user and does not require changes to the application source code.

Oracle Database 11g: SQL Tuning Workshop 11 - 8


SQL Tuning Loop
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Workload

Generate
profiles

n s e
SQL Tuning
i ce
High load Advisor
b l el
fer a
a n s
n -tr
o
s an
ADDM
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Tuning n o
iLoop
o
nt information contained in a SQL Profile is stored in such a way that it stays relevant
TheA auxiliary
e database changes, such as addition or removal of indexes, growth in the size of tables, and
Josafter
periodic collection of database statistics. Therefore, when a profile is created, the corresponding plan
is not frozen (as when outlines are used).
However, a SQL Profile may not adapt to massive changes in the database or changes that have
accumulated over a long period of time. In such cases, a new SQL Profile needs to be built to replace
the old one.
For example, when a SQL Profile becomes outdated, the performance of the corresponding SQL
statement may become noticeably worse. In such a case, the corresponding SQL statement may start
showing up as high-load or top SQL, thus becoming again a target for system SQL Tuning. In such a
situation, ADDM again captures the statement as high-load SQL. If that happens, you can decide to
re-create a new profile for that statement.

Oracle Database 11g: SQL Tuning Workshop 11 - 9


Access Path Analysis

SQL Tuning
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Advisor

Significant
performance
SQL e
gain Workload n s
Access
i ce
Advisor
b l el
Comprehensive
r
fea
n s
n - ra
tindex
a
Indexes no analysis
h a s eฺ
) i d
i r ฺesonnJFV.TEST("C");
t Gu
Indexes
CREATE INDEX JFV.IDX$_00002
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
A bia to
Access Path n o
iAnalysis
o
nt provides advice on indexes. Effective indexing is a well-known tuning technique that can
ATO
e A also
Jossignificantly improve the performance of SQL statements by reducing the need for full table scans.
Any index recommendations generated by ATO are specific to the SQL statement being tuned.
Therefore, 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, it recommends running the Access Advisor on the SQL statement along with a
representative SQL workload. The Access Advisor collects advice given on each statement of a SQL
workload and consolidates it into global advice for the entire SQL workload.
The Access Path Analysis can make the following recommendations:
• Create new indexes if they provide significantly superior performance.
• Run SQL Access Advisor to perform a comprehensive index analysis based on application
workload.

Oracle Database 11g: SQL Tuning Workshop 11 - 10


SQL Structure Analysis

How can I
rewrite it?
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Poorly written
SQL statement

Restructured n s e
SQL statement i ce
SQL constructs b l el
fer a
Type mismatch and a n s
indexes n -tr
n o
Design mistakess a
SQL Tuning ) h a deฺ
Advisor r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
SQL Structure
o n Analysis io A
TheA
t
n of the SQL Structure Analysis is to help you identify poorly written SQL statements as well
goal
e
Josas to advise you on how to restructure them.
There are certain syntax variations that are known to have a negative impact on performance. In this
mode, ATO evaluates statements against a set of rules, identifying less efficient coding techniques,
and providing recommendations for an alternative statement where possible. The recommendation
may be very similar, but not precisely equivalent to the original query. For example, the NOT
EXISTS and NOT IN constructors are similar, but not exactly the same. Therefore, you have to
decide whether the recommendation is valid. For this reason, ATO does not automatically rewrite the
query, but gives advice instead.
The following categories of problems are detected by the SQL Structure Analysis:
• Use of SQL constructors such as NOT IN instead of NOT EXISTS, 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)

Oracle Database 11g: SQL Tuning Workshop 11 - 11


SQL Tuning Advisor: Usage Model

Automatic selection
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

AWR

ADDM High-load SQL


SQL
Sources Tuning
Manual Selection Advisor
AWR e
n s
i ce
b l el
fer a
a n s
o n -tr
a n Filter
Cursor cache

h s
a deฺ
e )
s Gui
r ฺ
Custom
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
b
SQL Tuningn i o A Usage Model
Advisor:
nto Advisor takes one or more SQL statements as input. The input can come from different
SQLATuning
e
Jossources: • 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. This can be done using snapshots or baselines.
• Custom workload: A user can create a custom workload consisting of statements of interest to
the user. These may be statements that are not in cursor cache and are not high-load to be
captured by ADDM or AWR. For such statements, a user can create a custom workload and tune
it using the advisor.
SQL statements from cursor cache, AWR, and custom workload can be filtered and ranked before
they are input to SQL Tuning Advisor.
For a multistatement input, a new object called SQL Tuning Set (STS) is provided. 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.

Oracle Database 11g: SQL Tuning Workshop 11 - 12


Database Control and SQL Tuning Advisor
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
n io and SQL Tuning Advisor
Database Control
o
TheA nt way to access the SQL Tuning Advisor from Enterprise Manager is on the Advisor
easiest
e page. On the Home page, click the Advisor Central link located in the Related Links section
JosCentral
to open the Advisor Central page.
On the Advisor Central page, click the SQL Advisors link. On the SQL Advisors page, click the SQL
Tuning Advisor link. This takes you to the Schedule SQL Tuning Advisor page. On this page, you
find links to various other pages. You click the Top Activity link to open the Top Activity page.

Oracle Database 11g: SQL Tuning Workshop 11 - 13


Running SQL Tuning Advisor: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Running SQL
o n ioTuning Advisor: Example
nt use Database Control to identify the high-load or top SQL statements. There are several
YouAcan
e
Joslocations in Database Control from where SQL Tuning Advisor can be launched with the identified
SQL statement or statements, or an STS:
• Tuning ADDM-identified SQL statements: The ADDM Finding Details page shows high-load
SQL statements identified by ADDM. Each of these high-load SQL statements is known to
consume a significant proportion of one or more system resources such as CPU time, buffer
gets, disk reads, and so on. You can use this page to launch SQL Tuning Advisor on a selected
high-load SQL statement.
• Tuning top SQL statements: Another SQL source is the list of top SQL statements. 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. The user can select one or
more top SQL statements identified by their SQL IDs and launch SQL Tuning Advisor on them.
• Tuning a SQL Tuning Set: It is also possible to look at various STSs created by different users.
An STS could have been created from a list of top SQL statements, by selecting SQL statements
from a range of snapshots created by AWR, or by selecting customized SQL statements.

Oracle Database 11g: SQL Tuning Workshop 11 - 14


Implementing Recommendations
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Implementing
o n ioRecommendations
t Tuning Advisor is launched, Enterprise Manager automatically creates a tuning task,
nSQL
A
After
e
Josprovided the user has the appropriate ADVISOR privilege to do so. Enterprise Manager shows the
tuning task with automatic defaults on the Schedule SQL Tuning Advisor page shown in the previous
slide. On this page, the user can change the automatic defaults pertaining to a tuning task.
One of the important options is to select the scope of the tuning task. If you select the Limited option,
SQL Tuning Advisor produces recommendations based on statistics check, access path analysis, and
SQL structure analysis. No SQL Profile recommendation is generated with Limited scope. If you
select the Comprehensive option, SQL Tuning Advisor performs all recommendations under Limited
scope, and invokes the optimizer under SQL Profiling mode to build a SQL Profile if applicable.
With the Comprehensive option, you can also specify a time limit for the tuning task, which by
default is 30 minutes. Another useful option is to run the tuning task immediately or schedule it to be
run at a later time.
You can configure your tuning task on the Schedule Advisor page. To do this, select the Schedule
SQL Tuning Advisor action and click Go. Navigating back to the Top Activity page, you can click
the tuned statement to open the SQL Details page, where you can view the Tuning Information.
This shows you the completed tuning task. By clicking the task, you can see its general SQL Tuning
Results. By clicking the View button, you can see its details. As shown, a SQL Profile has been
created; you can implement it if you want, after you view the new plan.

Oracle Database 11g: SQL Tuning Workshop 11 - 15


SQL Access Advisor: Overview

What
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

partitions, indexes,
and MVs do I need SQL
to optimize Solution Access
my entire Advisor
workload?

n s e
i ce
b l el
fer a
No expertise a n s
Workload required o n -tr Component
n
aProvides of CBO

h s eฺ
aimplementation
) i d
i r ฺes nt Gu script
g p ฺm ude
d S t
@ i s
iaCopyrightt©h2008,
( j a b e Oracle. All rights reserved.
u s
Abia to
SQL Access n o
iAdvisor: Overview
t o
An appropriate access structures to optimize SQL queries has always been a concern for the
Defining
e
Josdeveloper. As a result, there have been many papers and scripts written as well as high-end tools
developed to address the matter. In addition, with the development of partitioning and materialized
view technology, deciding on access structures has become even more complex.
As part of the manageability improvements in Oracle Database 10g and 11g, SQL Access Advisor
has been introduced to address this very critical need.
SQL Access Advisor identifies and helps resolve performance problems relating to the execution of
SQL statements by recommending which indexes, materialized views, materialized view logs, or
partitions to create, drop, or retain. It can be run from Database Control or from the command line by
using PL/SQL procedures.
SQL Access Advisor takes an actual workload as input, or the Advisor can derive a hypothetical
workload from the schema. It then recommends the access structures for faster execution path. 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
• Provides simple, user-friendly GUI wizards
• Generates scripts for implementation of recommendations

Oracle Database 11g: SQL Tuning Workshop 11 - 16


SQL Access Advisor: Usage Model

SQL Access
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Advisor

SQL cache
Workload
Hypothetical

STS n s e
i ce
Filter
Options b l el
fe ra
a n s
o n -tr
s an
) h a deฺ
s Gui Materialized
Indexes irฺeMaterialized
p ฺ m deviews nt views log
Partitioned
objects
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Usage Model
t o
n Advisor takes as input a workload that can be derived from multiple sources:
SQLAAccess
e
Jos •• Hypothetical,
SQL cache, to take the current content of V$SQL
to generate a likely workload from your dimensional model. This option is
interesting when your system is being initially designed.
• SQL Tuning Sets, from the workload repository
SQL Access Advisor also provides powerful workload filters that you can use to target the tuning.
For example, a user can specify that the advisor should look at only the 30 most resource-intensive
statements in the workload, based on optimizer cost. For the given workload, the advisor then does
the following:
• Simultaneously considers index solutions, materialized view solutions, partition solutions, 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, indexes, and materialized views
• Combines similar indexes into a single index
• Generates recommendations that support multiple workload queries

Oracle Database 11g: SQL Tuning Workshop 11 - 17


Possible Recommendations

Recommendation Comprehensive Limited


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Add new (partitioned) index on table or materialized view. YES YES


Drop an unused index. YES NO
Modify an existing index by changing the index type. YES NO
Modify an existing index by adding columns at the end. YES YES
Add a new (partitioned) materialized view. YES YES
n s e
Drop an unused materialized view (log). YES NO i ce
Add a new materialized view log. YES b l
YES
el
fer a
Modify an existing materialized view log to add new columns YES
a n s YES
or clauses.
n-tr
no
Partition an existing unpartitioned table or index. a
s eฺ
YES YES
h a
e s ) u id

ir ent G
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
(
ia to u s
A b
n io
Possible Recommendations
o
nt Advisor carefully considers the overall impact of recommendations and makes
SQLAAccess
e
Josrecommendations by using only the known workload and supplied information. Two workload
analysis methods are available:
• Comprehensive: With this approach, SQL Access Advisor addresses all aspects of tuning
partitions, materialized views, indexes, and materialized view logs. It assumes that the workload
contains a complete and representative set of application SQL statements.
• Limited: Unlike the comprehensive workload approach, a limited workload approach assumes
that the workload contains only problematic SQL statements. Thus, advice is sought for
improving the performance of a portion of an application environment.
When comprehensive workload analysis is chosen, SQL Access Advisor forms a better set of global
tuning adjustments, but the effect may be a longer analysis time. As shown in the table, the chosen
workload approach determines the type of recommendations made by the advisor.
Note: Partition recommendations can work on only those tables that have at least 10,000 rows, and
workloads that have some predicates and joins on columns of the NUMBER or DATE type.
Partitioning advices can be generated only on these types of columns. In addition, partitioning
advices can be generated only for single-column interval and hash partitions. Interval partitioning
recommendations can be output as range syntax, but interval is the default. Hash partitioning is done
to leverage only partitionwise joins.

Oracle Database 11g: SQL Tuning Workshop 11 - 18


SQL Access Advisor Session: Initial Options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Access n o
iAdvisor Session: Initial Options
t o
n few slides describe a typical SQL Access Advisor session. You can access the SQL Access
TheAnext
e
JosAdvisor 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. 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.
On the SQL Access Advisor: Initial Options page, you can select a template or task from which to
populate default options before starting the wizard. You can click Continue to start the wizard or
Cancel to go back to the Advisor Central page.
Note: The SQL Access Advisor may be interrupted while generating recommendations, thereby
allowing the results to be reviewed.
For general information about using SQL Access Advisor, see the “Overview of the SQL Access
Advisor” section in the lesson titled “SQL Access Advisor” of the Oracle Data Warehousing Guide.

Oracle Database 11g: SQL Tuning Workshop 11 - 19


SQL Access Advisor Session: Initial Options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Access n o
iAdvisor Session: Initial Options (continued)
t o
Anselect the “Inherit Options from a Task or Template” option on the Initial Options page, you
If you
e
Joscan select an existing task, or an existing template to inherit SQL Access Advisor’s options. By
default, the SQLACCESS_EMTASK template is used.
You can view the various options defined by a task or a template by selecting the corresponding
object and clicking View Options.

Oracle Database 11g: SQL Tuning Workshop 11 - 20


SQL Access Advisor: Workload Source
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Workload Source
t o
n select your workload source from three different sources:
YouAcan
e
Jos • cached
Current and Recent SQL Activity: This source corresponds to SQL statements that are still
in your System Global Area (SGA).
• Use an existing SQL Tuning Set: You also have the possibility of creating and using a SQL
Tuning Set that holds your statements.
• Hypothetical Workload: This option provides a schema that allows the advisor to search for
dimension tables and produce a workload. This is very useful to initially design your schema.
Using the Filter Options section, you can further filter your workload source. Filter options are:
• Resource Consumption: Number of statements ordered by Optimizer Cost, Buffer Gets, CPU
Time, Disk Reads, Elapsed Time, Executions
• Users
• Tables
• SQL Text
• Module IDs
• Actions

Oracle Database 11g: SQL Tuning Workshop 11 - 21


SQL Access Advisor: Recommendation Options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Recommendation Options
t o
On AthenRecommendations Options page, you can select whether to limit the SQL Access Advisor to
e
Josrecommendations based on a single access method. You can select the type of structures to be
recommended by the advisor. If none of the three possible ones are chosen, the advisor evaluates
existing structures instead of trying to recommend new ones.
You can use the Advisor Mode section to run the advisor in one of the two modes. These modes
affect the quality of recommendations as well as the length of time required for processing. In
Comprehensive Mode, the Advisor searches a large pool of candidates resulting in recommendations
of the highest quality. In Limited Mode, the advisor performs quickly, limiting the candidate
recommendations by working on the highest-cost statements only.
Note: You can click Advanced Options to show or hide options that allow you to set space
restrictions, tuning options, and default storage locations.

Oracle Database 11g: SQL Tuning Workshop 11 - 22


SQL Access Advisor: Schedule and Review
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Schedule and Review
t o
n then schedule and submit your new analysis by specifying various parameters to the
YouAcan
e
osscheduler. The possible options are shown in the screenshots in the slide.
J

Oracle Database 11g: SQL Tuning Workshop 11 - 23


SQL Access Advisor: Results
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Results
t o
From
e Anthe Advisor Central page, you can retrieve the task details for your analysis. By selecting the
JosSummary
task name in the Results section of the Advisor Central page, you can access the Results for Task
page, 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.

Oracle Database 11g: SQL Tuning Workshop 11 - 24


SQL Access Advisor: Results and Implementation
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
SQL Access n o
iAdvisor: Results and Implementation
t o
Another aspects of the results for the Access Advisor task, click one of the three other tabs on the
To see
e
Jospage, Recommendations, SQL Statements, or Details.
On the Recommendations page, you can drill down to each of the recommendations. For each of
them, you see important information in the Select Recommendations for Implementation table. You
can then select one or more recommendations and schedule their implementation.
If you click the ID for a particular recommendation, you are taken to the Recommendations page that
displays all actions for the specified recommendation and, optionally, to modify the tablespace name
of the statement. When you complete any changes, click OK to apply the changes. On the
Recommendations page, you can view the full text of an action by clicking the link in the Action
field for the specified action. You can view the SQL for all actions in the recommendation by
clicking Show SQL.
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. The top SQL statement is
improved the most by implementing its associated recommendation.
The Details page shows you the workload and task options that were used when the task was created.
This page also gives you all journal entries that were logged during the task execution.
You can also schedule the implementation of the recommendations by clicking the Schedule
Implementation button.

Oracle Database 11g: SQL Tuning Workshop 11 - 25


SQL Tuning Loop

1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Workload

Accept profiles.
SQL Tuning n s
i ce
Automatic High load Advisor
b l e4 l
fe
Generate r a
n s
SQL profiles.
a
o n -tr
2
ADDM s an
) h a deฺ 3
e s u i

ir ent G Run SQL Tuning Advisor.
ฺ m
d gp Stud
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
SQL Tuning n o
iLoop
t o
nDatabase
e A
Oracle 10g introduced SQL Tuning Advisor to help application developers improve the
s
Jo performance of SQL statements. The advisor targets the problem of poorly written SQL, in which
SQL statements have not been designed in the most efficient fashion. 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, the advisor makes
specific suggestions for speeding up SQL performance, but it leaves the responsibility of
implementing the recommendations to the user.
In addition to SQL Tuning Advisor, Oracle Database 10g has an automated process to identify high-
load SQL statements in your system. This is done by ADDM, which automatically identifies high-
load SQL statements that are good candidates for tuning.
However, major issues still remain: Although it is true that ADDM identifies some SQL that should
be tuned, users must manually look at ADDM reports and run SQL Tuning Advisor on the reports for
tuning.

Oracle Database 11g: SQL Tuning Workshop 11 - 26


Automatic SQL Tuning

AWR
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Top SQL 2

Workload Auto matic

3
n s e
1 i ce
b l el
fer a
SQL Tuning s
Reports
tr a n
4 o n -
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Automatic n
o io Tuning
SQL
t
nDatabase
e A
Oracle 11g further automates the SQL Tuning process by identifying problematic SQL
s
Jo recommendations
statements, running SQL Tuning Advisor on them, and implementing the resulting SQL profile
to tune the statement without requiring user intervention. Automatic SQL Tuning
uses the AUTOTASK framework through a new task called “Automatic SQL Tuning” that runs every
night by default. 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, any day in the past week, any hour in the past week, or single response
time), Automatic SQL Tuning targets for automatic tuning.
• Steps 2 and 3: While the Automatic SQL Tuning task executes during the maintenance window,
the previously identified SQL statements are automatically tuned by invoking SQL Tuning
Advisor. As a result, SQL profiles are created for them if needed. However, before making any
decision, the new profile is carefully tested.
• Step 4: At any point in time, you can request a report about these automatic tuning activities.
You then have the option of checking the tuned SQL statements to validate or remove the
automatic SQL profiles that were generated.

Oracle Database 11g: SQL Tuning Workshop 11 - 27


Automatic Tuning Process
Not considered for
auto implementation
Considered for
auto implementation
New
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Restructure
SQL. SQL profile

Existing N 3X Y
benefit? Accept profile.
Indexes profile?

N n s e
ice
Y
b l el
3X N
f
Ignore a
er new profile.
Stale
benefit?
an s
stats
Y
o n -tr
s an
) h a deฺReplace profile.

r ฺ e s Gui
mi dent
GATHER_STATS_JOB

p ฺ
g Stu
d
@ his
b i aCopyright
t© 2008, Oracle. All rights reserved.
( j a s e
A bia to u
Automatic n
o io Process
Tuning
t tuning process, all the recommendation types are considered and reported, but only SQL
nthe
A
During
e can be implemented automatically (when the ACCEPT_SQL_PROFILES task parameter is
Josset
profiles
to TRUE). Otherwise, only the recommendation to create a SQL profile is reported in the
automatic SQL tuning reports.
In Oracle Database 11g, the performance improvement factor has to be at least three before a SQL
profile is implemented. As we have already mentioned, the Automatic SQL Tuning process
implements only SQL profile recommendations automatically. Other recommendations (to create
new indexes, refresh stale statistics, or restructure SQL statements) are generated as part of the SQL
tuning process, but are not implemented. These are left for you to review and implement manually,
as appropriate.
Here is a short description of the automatic tuning process in general:
Tuning is performed on a per-statement basis. Because only SQL profiles can be implemented, there
is no need to consider the effect of such recommendations on the workload as a whole. For each
statement (in the order of importance), the tuning process carries out each of the following steps:
1. Tune the statement by using SQL Tuning Advisor. Look for a SQL profile and, if it is found,
verify that the base optimizer statistics are current for it.

Oracle Database 11g: SQL Tuning Workshop 11 - 28


Automatic Tuning Process (continued)
2. If a SQL profile is recommended, perform the following:
1. Test the new SQL profile by executing the statement with and without it.
2. When a SQL profile is generated and it causes the optimizer to pick a different execution
plan for the statement, the advisor must decide whether to implement the SQL profile. It
makes its decision according to the flowchart in the slide. Although the benefit thresholds
here apply to the sum of CPU and input/output (I/O) time, SQL profiles are not accepted
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

when there is degradation in either statistic. So the requirement is that there is a three-
times improvement in the sum of CPU and I/O time, with neither statistic becoming worse.
In this way, the statement runs faster than it would without the profile, even with
contention in CPU or I/O.
3. If stale or missing statistics are found, make this information available to
GATHER_STATS_JOB.
Automatic implementation of tuning recommendations is limited to SQL profiles because they have n s e
fewer risks. It is easy for you to revert the implementation. lic e
l e
btracked
a
Note: All SQL profiles are created in the standard EXACT mode. They are matched rand
e
a sf
according to the current value of the CURSOR_SHARING parameter. You arenresponsible for setting
CURSOR_SHARING appropriately for your workload. n- t r
a no
h a s eฺ
e s ) u id

ir ent G
ฺ m
d gp Stud
b i a@ this
i a (ja use
Ab to
nio
n t o
e A
Jos

Oracle Database 11g: SQL Tuning Workshop 11 - 29


Automatic SQL Tuning Controls

• Autotask configuration:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– On/off switch
– Maintenance windows running tuning task
– CPU resource consumption of tuning task
• Task parameters:
– SQL profile implementation automatic/manual switch e
n s
– Global time limit for tuning task
e l ice
– Per-SQL time limit for tuning task r a bl
– Test-execute mode disabled to save time n s fe
n - t ra
– Maximum number of SQL profiles automatically
o implemented
per execution as well as overall a n
h a s eฺ
s)
– Task execution expiration period uid
rฺe nt G
m i
d gpฺ Stude
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Automatic n
o io Tuning Controls
SQL
t
e Aisna PL/SQL control example for the Automatic SQL Tuning task:
Here
JosBEGIN
dbms_sqltune.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK',
'LOCAL_TIME_LIMIT', 1400);
dbms_sqltune.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK',
'ACCEPT_SQL_PROFILES', 'TRUE');
dbms_sqltune.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK',
'MAX_SQL_PROFILES_PER_EXEC', 50);
dbms_sqltune.set_tuning_task_parameter('SYS_AUTO_SQL_TUNING_TASK',
'MAX_AUTO_SQL_PROFILES', 10002);
END;
The last three parameters in this example are supported only for the Automatic SQL Tuning task.
You can also use parameters such as LOCAL_TIME_LIMIT, or TIME_LIMIT, which are valid
parameters for the traditional SQL tuning tasks. One important example is to disable test-execute
mode (to save time) and to use only execution plan costs to decide about the performance by using
the TEST_EXECUTE parameter.
In addition, you can control when the Automatic SQL Tuning task runs and the CPU resources that it
is allowed to use.

Oracle Database 11g: SQL Tuning Workshop 11 - 30


Automatic SQL Tuning Task
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Automatic n
o SQLio Tuning Task
A nt stated, Automatic SQL Tuning is implemented as an automated maintenance task that is
As already
e called Automatic SQL Tuning. You can see some high-level information about the last runs of
Jositself
the Automatic SQL Tuning task on the Automated Maintenance Tasks page. To open this page, on
your Database Control Home page, click the Server tab. On the Server tabbed page that opens, click
the Automated Maintenance Tasks link in the Tasks section.
On the Automated Maintenance Tasks page, you see the predefined tasks. You then access each task
by clicking the corresponding link to get more information about the task itself (illustrated in the
slide). When you click either the Automatic SQL Tuning link or the latest execution icon (the green
area on the timeline), the Automatic SQL Tuning Result Summary page opens.

Oracle Database 11g: SQL Tuning Workshop 11 - 31


Configuring Automatic SQL Tuning
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Configuring n o
iAutomatic SQL Tuning
t o
n configure various Automatic SQL Tuning parameters by using the Automatic SQL Tuning
YouAcan
e
JosSettings page.
To navigate to that page, click the Configure button on the Automated Maintenance Tasks page. You
see the Automated Maintenance Tasks Configuration page, on which you see the various
maintenance windows that are delivered with Oracle Database 11g.
By default, Automatic SQL Tuning executes on all predefined maintenance windows in
MAINTENANCE_WINDOW_GROUP. You can disable it for specific days in the week. On this page,
you can also edit each Window to change its characteristics. You can do so by clicking Edit Window
Group.
To navigate to the Automatic SQL Tuning Settings page, click the Configure button on the line
corresponding to Automatic SQL Tuning in the Task Settings section.
On the Automatic SQL Tuning Settings page, you can specify the parameters shown in the slide. By
default, “Automatic Implementation of SQL Profiles” is not selected.
Note: If you set STATISTICS_LEVEL to BASIC, turn off the AWR snapshots by using
DBMS_WORKLOAD_REPOSITORY, or if AWR retention is less than seven days, you also stop
Automatic SQL Tuning.

Oracle Database 11g: SQL Tuning Workshop 11 - 32


Automatic SQL Tuning: Result Summary
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Automatic n
o io Tuning: Result Summary
SQL
A nt the Automatic SQL Tuning Result Summary page contains various summary graphs so
In addition,
e you can control the Automatic SQL Tuning task. An example is given in the slide. The first chart
Josthat
in the Overall Task Statistics section shows you the breakdown by finding types for the designated
period of time. 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. In the example, Customized is used; it shows you the
latest run. You can select All to cover all executions of the task so far. Users can request it for any
time period over the past month, since that is the amount of time for which the advisor persists its
tuning history. You then generate the report by clicking View Report.
On the “Breakdown by Finding Type” chart, you can clearly see that only SQL profiles can be
implemented. Although many more profiles were recommended, not all of them were automatically
implemented for the reasons that have already been explained. Similarly, recommendations for index
creation and other types are not implemented. However, the advisor keeps historical information
about all the recommendations if you want to implement them later.
In the Profile Effect Statistics section, you can see the Tuned SQL DB Time Benefit chart, which
shows you the before-and-after DB Time for implemented profiles and other recommendations.

Oracle Database 11g: SQL Tuning Workshop 11 - 33


Automatic SQL Tuning: Result Details
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
Automatic n
o io Tuning: Result Details
SQL
On A
t
thenAutomatic SQL Tuning Result Details page, you can also see important information for each
e
Josautomatically tuned SQL statement, including its SQL text and SQL ID, the type of recommendation
that was done by SQL Tuning Advisor, the verified benefit percentage, whether a particular
recommendation was automatically implemented, and the date of the recommendation.
From this page, you can either drill down to the SQL statement itself by clicking its corresponding
SQL ID link, 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.
Note: The benefit percentage shown for each recommendation is calculated using the formula
bnf% = (time_old - time_new)/(time_old). With this formula, you can see that a three-time benefit
(for example, time_old = 100, time_new = 33) corresponds to 66%. So the system implements any
profiles with benefits over 66%. According to this formula, 98% is a 50-times benefit.

Oracle Database 11g: SQL Tuning Workshop 11 - 34


Automatic SQL Tuning Result Details: Drilldown
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Automatic n
o io Tuning Result Details: Drilldown
SQL
On A
t
then“Recommendations for SQL ID” page, you can see the corresponding recommendations and
e
Josimplement them manually.
By clicking the SQL Test link, you access the SQL Details page, where you see the tuning history as
well as the plan control associated with your SQL statement.
In the slide, you see that the statement was tuned by Automatic SQL Tuning and that the associated
profile was automatically implemented.

Oracle Database 11g: SQL Tuning Workshop 11 - 35


Automatic SQL Tuning Considerations

• SQL not considered for Automatic SQL Tuning:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

– Ad hoc or rarely repeated SQL


– Parallel queries
– Long-running queries after profiling
– Recursive SQL statements
– DML and DDL e
n s
• These statements can still be manually tuned by using SQL lice
e
Tuning Advisor. abl fer
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
A bia to u
Automatic n
o io Tuning Considerations
SQL
A nt SQL Tuning does not seek to solve every SQL performance issue occurring on a system.
Automatic
e not consider the following types of SQL.
JosIt •does
Ad hoc or rarely repeated SQL: If a SQL is not executed multiple times in the same form, the
advisor ignores it. SQL that do not repeat within a week are also not considered.
• Parallel queries.
• Long-running queries (postprofile): If a query takes too long to run after being SQL profiled, it
is not practical to test-execute and, therefore, it is ignored by the advisor. Note that this does not
mean that the advisor ignores all long-running queries. If the advisor can find a SQL profile that
causes a query that once took hours to run in minutes, it could be accepted because test-
execution is still possible. The advisor would execute the old plan just long enough to determine
that it is worse than the new one, and then would terminate test-execution without waiting for
the old plan to finish, thereby switching the order of execution.
• Recursive SQL statements
• DMLs, such as INSERT SELECT or CREATE TABLE AS SELECT
With the exception of truly ad hoc SQL, these limitations apply to Automatic SQL Tuning only.
Such statements can still be tuned by manually running SQL Tuning Advisor.

Oracle Database 11g: SQL Tuning Workshop 11 - 36


Summary

In this lesson, you should have learned the following:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Statement profiling
• SQL Tuning Advisor
• SQL Access Advisor
• Automatic SQL Tuning
n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 11 - 37


Practice 11: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• Using ADDM and SQL Tuning Advisor to tune your SQL


statements
• Using SQL Access Advisor to change your schema
• Using Automatic SQL Tuning to tune your statements

n s e
i ce
b l el
fer a
a n s
n -tr
o
s an
) h a deฺ
r ฺ e s Gui
p ฺ mi dent
d g Stu
i @ his
aCopyright
j a b e t© 2008, Oracle. All rights reserved.
( s
Abia to u
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop 11 - 38

You might also like