D52163GC10 sg1
D52163GC10 sg1
D52163GC10 sg1
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.
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ฺ
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ฺ
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ฺ
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ฺ
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ฺ
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ฺ
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ฺ
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ฺ
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ฺ
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
Instance
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
SGA
User
• Connection: Bidirectional network process
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
ฺ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.
SGA
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
• 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.
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.
• 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.
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.
– Recovery Manager
s an
Response Request
– Shared server h a d e ฺ
s ) u i
queue queue
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.
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
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.
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.
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.
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
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.
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.
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.
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
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
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
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
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
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.
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
Services
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
Home
ASH Run
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
Report ADDM
Performance
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.
• 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.
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.
• 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.
• 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.
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.
– 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.
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.
• 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.
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
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
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
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ฺ
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
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.
Client
Server
Server Server Server Server Server
process process process process process
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.
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).
1. Create a cursor.
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.
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.
• Execute:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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
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.
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
Query to optimize
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
Query to optimize
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
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.
SELECT *
FROM emp
WHERE job = 'CLERK' OR deptno = 10;
• 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.
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.
SELECT *
FROM emp, dept
WHERE emp.deptno = 20 AND emp.deptno = dept.deptno;
• 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.
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).
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).
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
• 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.
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.
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
• 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.
OPTIMIZER_FEATURES_ENABLED
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
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
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
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
• 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.
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.
(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.
• No suitable index
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.
---------------------------------------------------------------------
| 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.
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
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.
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.
• 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.
) 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.
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.
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.
---------------------------------------------------------------------
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.
---------------------------------------------------------------------
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.
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).
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
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.
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.
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
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.
alter table emp modify (SAL not null, ENAME not null);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
---------------------------------------------------------------------
| 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.
---------------------------------------------------------------------
| 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.
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.
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.
Indexed Accessing
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
---------------------------------------------------------------------
| 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.
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
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')
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
---------------------------------------------------------------------
| 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.
• 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.
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.”
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.
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.
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
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
• Index cluster:
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.
• SORT operator:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
---------------------------------------------------------------------
| 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.
deptno=1 deptno=2
---------------------------------------------------------------------
| 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 count(*)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
---------------------------------------------------------------------
| 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.
---------------------------------------------------------------------
| 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.
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.
NL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
TAF TAR
Driving
IRS
Inner
NL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
NL TAR
TAF IRS
Driving Inner
) 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.
Sorted
Sorted
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
) 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 >=.
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.
--------------------------------------------------------------------
| 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.
--------------------------------------- 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.
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
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.
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.
• 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
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
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.
--------------------------------------------------------------------
| 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.
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.
--------------------------------------------------------------
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.
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
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
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
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
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
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.
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.
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;
• PLAN_TABLE:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
SQL> EXPLAIN PLAN SET STATEMENT_ID = 'demo01' FOR SELECT * FROM emp
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
Explained.
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.
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
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
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).
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.
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.
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
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.
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.
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.
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
-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;
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
• Creating snapshots:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
• 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.
• 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.
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------
SQL_ID 454rug2yva18w
--------------------
select /* example */ * from hr.employees natural join hr.departments
----------------------------------------------------------------------------------
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 |
----------------------------------------------------------------------------------
SQL> @$ORACLE_HOME/rdbms/admin/awrsqrpt
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
+
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.
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
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.
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).
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
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.
If you remember the few basic rules of explain plans and with some experience, you can read most
plans easily.
--------------------------------------------------
| 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.
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
Session altered.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
no rows selected
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.
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.
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.
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.
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.
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
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
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
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.
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.
SUM(s.amount_sold) sales_amount
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.
| 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.
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
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.
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.
(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.
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.
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.
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')
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.
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.
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.
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.
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
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
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
• Used to determine:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
• Used to decide:
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
A B C A B C A B C
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
Cumulative cardinality
ENDPOINT NUMBER
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.
BEGIN
DBMS_STATS.gather_table_STATS (OWNNAME=>'OE', TABNAME=>'INVENTORIES',
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
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.
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
BEGIN
DBMS_STATS.gather_table_STATS(OWNNAME =>'OE', TABNAME=>'INVENTORIES',
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.
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',-
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.
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.
dictionary:
SQL> EXECUTE DBMS_STATS.GATHER_SYSTEM_STATS( -
2 gathering_mode => 'START');
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.
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.
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.
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
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.
– 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.
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.
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
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
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
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.
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.
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.
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.
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.
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.
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
– 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.
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.
: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
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.
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
sharing usage:
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.
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
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.
• CURSOR_SHARING:
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
o n io
nt
e A
J o s
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
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ฺ
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
Optimizer hints:
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.
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.
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.
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.
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)
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
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.
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.
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.
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).
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
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
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
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?
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.
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.
– 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.
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
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.
exec DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE('AP');
exec DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE(-
'AP', 'PAYMENTS', 'QUERY_DELINQUENT');
EXEC dbms_monitor.DATABASE_TRACE_ENABLE(TRUE,TRUE);
EXEC dbms_monitor.DATABASE_TRACE_DISABLE();
• Enabling trace:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
• 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'
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.
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.
exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE( -
client_id=>'HR session', waits => FALSE, binds => FALSE);
Third session
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.
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ฺ
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.
• 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
an
Parsing user id: 61
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.
...
select max(cust_credit_limit)
from customers
where cust_city ='Paris'
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.
...
select max(cust_credit_limit) from customers
where cust_city ='Paris'
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.
• 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
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
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
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.
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.
Submit Create
Optimizer
(Tuning mode)
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.
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.
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.
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)
Automatic selection
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
AWR
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.
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.
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.
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.
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
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
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.
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.
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
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.
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
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.
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.
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.
AWR
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ
Top SQL 2
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.
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.
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
• 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.
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.
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.
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.
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.
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.
• 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
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