D52163GC10 sg2

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

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

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

ir ent G
ฺ m
d gp SVolume
tud
II • 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
D54948
Author Copyright © 2008, Oracle. All rights reserved.

Jean-François Verrier Disclaimer

This document contains proprietary information and is protected by copyright and


Technical Contributors other intellectual property laws. You may copy and print this document solely for your
own use in an Oracle training course. The document may not be modified or altered in
and Reviewers any way. Except where your use constitutes "fair use" under copyright law, you may
Muriel Fry (Special thanks) not use, share, download, upload, copy, print, display, perform, reproduce, publish,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

license, post, transmit, or distribute this document in whole or in part without the
Joel Goodman express authorization of Oracle.
Harald van Breederode
The information contained in this document is subject to change without notice. If you
Jörn Bartels find any problems in the document, please report them in writing to: Oracle University,
500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
Pekka Siltala warranted to be error-free.
Bernard Soleillant
Restricted Rights Notice

James Spiller If this documentation is delivered to the United States Government or anyone using
the documentation on behalf of the United States Government, the following notice is
n s e
Clay Fuller applicable:
ice
Ira Singer
b l el
Christopher Andrews
U.S. GOVERNMENT RIGHTS

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

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

1 Exploring the Oracle Database Architecture


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

J o s SYSTEM and SYSAUX Tablespaces 1-24


Summary 1-25
Practice 1: Overview 1-26

2 Introduction to SQL Tuning


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

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

Table Design 2-19


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

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

J o s SQL Statement Parsing: Overview 3-14


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

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

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

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

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

Buffer Sort Operator 4-49


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

5 Interpreting Execution Plans


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

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

Using the V$SQL_PLAN View 5-20


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

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

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

Ab to
Looking Beyond Execution Plans 5-50

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

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

Bitmap Join Indexes: Join Model 2 6-19


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

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

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

Bind Variables in SQL*Plus 8-5


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

t o ni Hint Recommendations 9-7


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

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

Location for Diagnostic Traces 10-5


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

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

SQL Access Advisor Session: Initial Options 11-19


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

Jos

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

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

xii
r ฺ
g Stu
e ) h
mi dent
s
s Gui
an
o
a deฺ
tr n -
a n s fe
r
a b l el
i
ce n s e
Appendix A
Practices and Solutions
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s
Table of Contents
Practices for Lesson 1 ......................................................................................................... 3
Practice 1-1: Exploring the Database Architecture......................................................... 4
Practices for Lesson 2 ....................................................................................................... 12
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Practice 2-1: Avoiding Common Mistakes................................................................... 13


Practices for Lesson 3 ....................................................................................................... 32
Practice 3-1: Understanding Optimizer Decisions........................................................ 33
Practices for Lesson 4 ....................................................................................................... 57
Practice 4-1: Using Different Access Paths .................................................................. 58
Practice 4-2: Using the Result Cache.......................................................................... 121
Practices for Lesson 5 ..................................................................................................... 144
Practice 5-1: Extracting Execution Plans.................................................................... 145 n s e
Practices for Lesson 6 ..................................................................................................... 179 ice
Practice 6-1: Star Schema Tuning............................................................................... 180 b l el
fe ra
Practices for Lesson 7 ..................................................................................................... 196
n s
Practice 7-1: Using System Statistics ......................................................................... 197
a
- tr
Practice 7-2: Automatic Statistics Gathering.............................................................. 212
n
o
an
Practices for Lesson 8 ..................................................................................................... 240
s
) h a deฺ
Practice 8-1: Understanding Adaptive Cusrsor Sharing ............................................. 241

r ฺ e s Gui
Practice 8-2: Understanding CURSOR_SHARING................................................... 260

p ฺ mi dent
Practices for Lesson 9 ..................................................................................................... 273
Practice 9-1: Using Hints ............................................................................................ 274
d g Stu
Practices for Lesson 10 ................................................................................................... 318
i a@ this
Practice 10-1: Tracing Applications ........................................................................... 319
b
i a (ja use
Practices for Lesson 11 ................................................................................................... 363
b to
Practice 11-1: Proactively Tuning High-Load SQL Statements................................. 364
A
o n io
Practice 11-2: Using SQL Access Advisor................................................................. 385
ntPractice 11-3: Using Automatic SQL Tuning............................................................. 415
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 2


Practices for Lesson 1
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 3


Practice 1-1: Exploring the Database Architecture
Fill in the blanks with the correct answers:
1) The two main components of Oracle RDBMS are
_________________________ and _______________________.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2) An instance consists of _____________________and _____________________


processes.

3) A session is a connection between the _______________ process and either the


______________ process or the ________________ process. e
n s
i ce
4) Name some components of the System Global Area (SGA). 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
• ____________________________
A b to
o
ni some background processes:
5)toList
An
e •
Jos
___________________________
• ___________________________
• ___________________________
• ___________________________
• ___________________________
• ___________________________

6) The _______________________ process writes the dirty buffers to the data files.

7) The _______________________ process writes the redo logs to the log files.

8) Name some files associated with an Oracle database.

Oracle Database 11g: SQL Tuning Workshop A - 4


Practice 1-1: Exploring the Database Architecture (continued)
• ___________________________
• ___________________________
• ___________________________
• ___________________________
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

• ___________________________
• ___________________________
• ____________________________

9) Some of the logical storage structures of an Oracle database are: e


n s
• ___________________________ i ce
b l el
• ___________________________
fer a
• ___________________________ a n s
• ___________________________ o n -tr
• ___________________________ s an
) h a deฺ
• ___________________________ ฺes
r G ui

• ____________________________
p mi dent
d g Stu
b i a@ this
i a (ja useprocess copies the redo log files to an archive
10) The ___________________
b
destination.
A to
ni o
t o
e An11) The _____________________ contains data and control information for a server or a
Jos background process.

12) The logical tablespace structure is associated with the physical


____________________ files on disk.

13) State whether the following statements are true or false.


a) The SGA includes the Database buffer cache and the Redo log buffer. ____
b) Each server process and background process has its own Program Global Area
(PGA). ____
c) User processes run the application or Oracle tool code. ____
d) Oracle Database processes include server processes and background processes.
____.

Oracle Database 11g: SQL Tuning Workshop A - 5


Practice 1-1: Exploring the Database Architecture (continued)
14) From a terminal session connected as the oracle user, execute the
processes.sh script located in your
$HOME/solutions/Database_Architecture directory. What does this
script show you?
a) It shows you all the database instance processes currently running on your
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

machine. This includes both background processes and foreground processes.


[oracle@edrsr33p1-orcl Database_Architecture]$ ./processes.sh
oracle 9537 1 0 01:00 ? 00:00:00 ora_w000_orcl
oracle 12132 22002 0 Mar26 pts/4 00:01:08
/u01/app/oracle/product/11.1.0/db_1/jdk/bin/java -server -Xmx256M -
XX:MaxPermSize=200m -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40
-DORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1 -
Doracle.home=/u01/app/oracle/product/11.1.0/db_1/oc4j -
n s e
ce
Doracle.oc4j.localhome=/u01/app/oracle/product/11.1.0/db_1/edrsr33p1
.us.oracle.com_orcl/sysman -
eli
DEMSTATE=/u01/app/oracle/product/11.1.0/db_1/edrsr33p1.us.oracle.com
a b l
_orcl -Doracle.j2ee.dont.use.memory.archive=true -
fe r
Djava.protocol.handler.pkgs=HTTPClient -
a n s
- tr
Doracle.security.jazn.config=/u01/app/oracle/product/11.1.0/db_1/oc4
n
o
j/j2ee/OC4J_DBConsole_edrsr33p1.us.oracle.com_orcl/config/jazn.xml -
an
Djava.security.policy=/u01/app/oracle/product/11.1.0/db_1/oc4j/j2ee/
s
) h a deฺ
OC4J_DBConsole_edrsr33p1.us.oracle.com_orcl/config/java2.policy -

onfig/OCMTrustedCerts.txt-
r ฺ s Gui
Djavax.net.ssl.KeyStore=/u01/app/oracle/product/11.1.0/db_1/sysman/c
e
ฺ mi dent
Djava.security.properties=/u01/app/oracle/product/11.1.0/db_1/oc4j/j
p
g Stu
2ee/home/config/jazn.security.props -
d
a@ this
DEMDROOT=/u01/app/oracle/product/11.1.0/db_1/edrsr33p1.us.oracle.com
b i
_orcl -Dsysman.md5password=true -

i a (ja use
Drepapi.oracle.home=/u01/app/oracle/product/11.1.0/db_1 -
b to
Ddisable.checkForUpdate=true -
A
io
Doracle.sysman.ccr.ocmSDK.websvc.keystore=/u01/app/oracle/product/11

t o n
.1.0/db_1/jlib/emocmclnt.ks -

A n Dice.pilots.html4.ignoreNonGenericFonts=true -

o s e Djava.awt.headless=true -jar
J /u01/app/oracle/product/11.1.0/db_1/oc4j/j2ee/home/oc4j.jar -config
/u01/app/oracle/product/11.1.0/db_1/oc4j/j2ee/OC4J_DBConsole_edrsr33
p1.us.oracle.com_orcl/config/server.xml
oracle 12225 1 0 Mar26 ? 00:00:00 ora_pmon_orcl
oracle 12227 1 0 Mar26 ? 00:00:00 ora_vktm_orcl
oracle 12231 1 0 Mar26 ? 00:00:00 ora_diag_orcl
oracle 12233 1 0 Mar26 ? 00:00:00 ora_dbrm_orcl
oracle 12235 1 0 Mar26 ? 00:00:00 ora_psp0_orcl
oracle 12239 1 0 Mar26 ? 00:00:00 ora_dia0_orcl
oracle 12241 1 0 Mar26 ? 00:00:00 ora_mman_orcl
oracle 12243 1 0 Mar26 ? 00:00:21 ora_dbw0_orcl
oracle 12245 1 0 Mar26 ? 00:00:48 ora_lgwr_orcl
oracle 12247 1 0 Mar26 ? 00:00:01 ora_ckpt_orcl
oracle 12249 1 0 Mar26 ? 00:00:04 ora_smon_orcl
oracle 12251 1 0 Mar26 ? 00:00:00 ora_reco_orcl
oracle 12253 1 0 Mar26 ? 00:00:05 ora_mmon_orcl
oracle 12255 1 0 Mar26 ? 00:00:01 ora_mmnl_orcl
oracle 12257 1 0 Mar26 ? 00:00:00 ora_d000_orcl
oracle 12259 1 0 Mar26 ? 00:00:00 ora_s000_orcl
oracle 12282 1 0 Mar26 ? 00:00:00 ora_fbda_orcl

Oracle Database 11g: SQL Tuning Workshop A - 6


Practice 1-1: Exploring the Database Architecture (continued)
oracle 12284 1 0 Mar26 ? 00:00:00 ora_smco_orcl
oracle 12290 1 0 Mar26 ? 00:00:00 ora_qmnc_orcl
oracle 12302 1 0 Mar26 ? 00:00:03 oracleorcl
(LOCAL=NO)
oracle 12325 1 0 Mar26 ? 00:00:00 ora_q000_orcl
oracle 12329 1 0 Mar26 ? 00:01:59 oracleorcl
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

(LOCAL=NO)
oracle 12331 1 0 Mar26 ? 00:00:12 oracleorcl
(LOCAL=NO)
oracle 12333 1 0 Mar26 ? 00:00:05 oracleorcl
(LOCAL=NO)
oracle 12335 1 0 Mar26 ? 00:00:37 oracleorcl
(LOCAL=NO)
oracle 12340 1 0 Mar26 ? 00:01:51 oracleorcl
(LOCAL=NO)
oracle 12346 1 0 Mar26 ? 00:00:01 oracleorcl
n s e
(LOCAL=NO)
i ce
oracle 12362 1 0 Mar26 ? 00:00:00 ora_q001_orcl
b l el
oracle
oracle
12570
20119
1 0 Mar26 ?
1 0 14:50 ?
00:00:01 ora_cjq0_orcl
00:00:00 oracleorcl
fe r a
(LOCAL=NO)
a n s
oracle 20482 20480 0 14:56 pts/2 -tr
00:00:00 grep orcl
o n
an
oracle 22002 1 0 Mar26 pts/4 00:00:08
/u01/app/oracle/product/11.1.0/db_1/perl/bin/perl
h s
a deฺ
/u01/app/oracle/product/11.1.0/db_1/bin/emwd.pl dbconsole
)
s Gui
/u01/app/oracle/product/11.1.0/db_1/edrsr33p1.us.oracle.com_orcl/sys
e
r ฺ
mi dent
man/log/emdb.nohup

p ฺ
[oracle@edrsr33p1-orcl Database_Architecture]$
g Stu
d
b i a@ this
--------------------------------------------------------------

#!/bin/bash (ja se
b i a to u
i o
ps -efA| grep orcl

nton
e A 15) From a terminal session connected as the oracle user, execute the files.sh
J o s script located in your $HOME/solutions/Database_Architecture
directory. What does this script show you?
a) This script shows you the location and names of all database files, the
initialization file, the password file, and trace files.
[oracle@edrsr33p1-orcl Database_Architecture]$ ./files.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Thu Mar 27 17:58:56


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

Oracle Database 11g: SQL Tuning Workshop A - 7


Practice 1-1: Exploring the Database Architecture (continued)
SQL>
SQL> col name format a45
SQL>
SQL> select name from v$controlfile;

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

---------------------------------------------
/u01/app/oracle/oradata/orcl/control01.ctl
/u01/app/oracle/oradata/orcl/control02.ctl
/u01/app/oracle/oradata/orcl/control03.ctl

SQL>
SQL>
SQL> col member format a45
SQL>
SQL> select group#,member from v$logfile;
n s e
i ce
GROUP# MEMBER
b l el
----------
3
---------------------------------------------
/u01/app/oracle/oradata/orcl/redo03.log
fer a
2 /u01/app/oracle/oradata/orcl/redo02.log
a n s
1 /u01/app/oracle/oradata/orcl/redo01.log
o n -tr
SQL>
s an
SQL> col tablespace_name format a20 ) h
SQL> a deฺ
SQL> col file_name format a45 rฺe
s Gui
SQL> ฺ m i ent
d p tud from dba_data_files;
g file_name
SQL> select tablespace_name,
@ his S
TABLESPACE_NAME bia FILE_NAME
t
( j a
-------------------- s e
---------------------------------------------
USERS bia
t o u /u01/app/oracle/oradata/orcl/users01.dbf
i oA
UNDOTBS1
n
/u01/app/oracle/oradata/orcl/undotbs01.dbf

ntoSYSTEM
SYSAUX /u01/app/oracle/oradata/orcl/sysaux01.dbf

e A /u01/app/oracle/oradata/orcl/system01.dbf

J o s EXAMPLE /u01/app/oracle/oradata/orcl/example01.dbf

SQL>
SQL> select tablespace_name, file_name from dba_temp_files;

TABLESPACE_NAME FILE_NAME
-------------------- ---------------------------------------------
TEMP /u01/app/oracle/oradata/orcl/temp01.dbf

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
-rw-rw---- 1 oracle oinstall 1544 Aug 22 2007
/u01/app/oracle/product/11.1.0/db_1/dbs/hc_orcl.dat
-rw-r----- 1 oracle oinstall 1536 Mar 26 22:03
/u01/app/oracle/product/11.1.0/db_1/dbs/orapworcl
-rw-r----- 1 oracle oinstall 2560 Mar 27 03:13
/u01/app/oracle/product/11.1.0/db_1/dbs/spfileorcl.ora

Oracle Database 11g: SQL Tuning Workshop A - 8


Practice 1-1: Exploring the Database Architecture (continued)
alert cdump hm incident incpkg ir lck metadata stage sweep
trace
-rw-r--r-- 1 oracle oinstall 557386 Mar 27 13:00
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
[oracle@edrsr33p1-orcl Database_Architecture]$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Database_Architecture

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

n s e
export
i ce
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
b l el
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
fe r a
a n s
sqlplus / as sysdba @files.sql
o n -tr
ls -l $ORACLE_HOME/dbs/*orcl*
s an
) h a deฺ
ls /u01/app/oracle/diag/rdbms/orcl/orcl
r ฺ e s Gui
ฺ mi dent
ls -l /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert*
p
d g Stu
b i a@ this
--------------------------------------------------------------

set echo on (ja se


i a o
b format ta45 u
o A
ni name from v$controlfile;
col name

ntoselect
e A
J o s
col member format a45

select group#,member from v$logfile;

col tablespace_name format a20


col file_name format a45

select tablespace_name, file_name from dba_data_files;

select tablespace_name, file_name from dba_temp_files;

exit;

16) From a terminal session connected as the oracle user, execute the sga.sh script
located in your $HOME/solutions/Database_Architecture directory.
What does this script show you?

Oracle Database 11g: SQL Tuning Workshop A - 9


Practice 1-1: Exploring the Database Architecture (continued)
a) This script prints the various pools held in your SGA.
[oracle@edrsr33p1-orcl Database_Architecture]$ ./sga.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Thu Mar 27 18:15:02


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

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

n s e
SQL>
i ce
SQL> select * from v$sgainfo;
b l el
NAME BYTES RES
fer a
-------------------------------- ---------- ---
a n s
Fixed SGA Size 1303188 No
n - tr
Redo Buffers 5181440 No o
Buffer Cache Size 335544320 Yes
s an
Shared Pool Size a deฺ
276824064 Yes
) h
Large Pool Size
r ฺ e s Gui
4194304 Yes

mi dent
Java Pool Size 12582912 Yes
Streams Pool Size
p ฺ
g Stu
0 Yes
Shared IO Pool Size
d 0 Yes

a@ this
Granule Size 4194304 No
Maximum SGA Size
b i 845348864 No

i a (ja use
Startup overhead in Shared Pool 46137344 No

Ab to
o n io
NAME BYTES RES
-------------------------------- ---------- ---
nt
e A Free SGA Memory Available 209715200

J o s 12 rows selected.

SQL>
SQL> col component format a30
SQL>
SQL> select component,current_size,min_size,max_size from
v$sga_dynamic_components;

COMPONENT CURRENT_SIZE MIN_SIZE MAX_SIZE


------------------------------ ------------ ---------- ----------
shared pool 276824064 226492416 276824064
large pool 4194304 4194304 4194304
java pool 12582912 12582912 12582912
streams pool 0 0 0
DEFAULT buffer cache 335544320 335544320 385875968
KEEP buffer cache 0 0 0
RECYCLE buffer cache 0 0 0
DEFAULT 2K buffer cache 0 0 0
DEFAULT 4K buffer cache 0 0 0
DEFAULT 8K buffer cache 0 0 0

Oracle Database 11g: SQL Tuning Workshop A - 10


Practice 1-1: Exploring the Database Architecture (continued)
DEFAULT 16K buffer cache 0 0 0

COMPONENT CURRENT_SIZE MIN_SIZE MAX_SIZE


------------------------------ ------------ ---------- ----------
DEFAULT 32K buffer cache 0 0 0
Shared IO Pool 0 0 0
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ASM Buffer Cache 0 0 0

14 rows selected.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
[oracle@edrsr33p1-orcl Database_Architecture]$
i ce
b l el
--------------------------------------------------------------
fer a
a n s
#!/bin/bash
o n -tr
cd /home/oracle/solutions/Database_Architecture
s an
) h a deฺ
export ORACLE_SID=orcl
r ฺ e s Gui
ฺ mi dent
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
p
d g Stu
export
b i a@ this
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

i a (ja use
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

A b to
ni o
ntosqlplus / as sysdba @sga.sql

e A
J o s --------------------------------------------------------------

set echo on

select * from v$sgainfo;

col component format a30

select component,current_size,min_size,max_size from


v$sga_dynamic_components;

exit;

Oracle Database 11g: SQL Tuning Workshop A - 11


Practices for Lesson 2
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 12


Practice 2-1: Avoiding Common Mistakes
In this practice, you examine some common mistakes in writing SQL statements. You
have to find a workaround to enhance performance.
1) You analyze a correlated subquery first. Before executing the culprit statement,
execute the correlation_setup.sh script to set up the environment for this
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

example. Make sure you run the script from a terminal session connected as the
oracle user. You can find the scripts for all the following cases in your
$HOME/solutions/Common_Mistakes directory.
[oracle@edrsr33p1-orcl Common_Mistakes]$ ./correlation_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Mar 26 20:21:50


2008
n s e
i ce
el
Copyright (c) 1982, 2007, Oracle. All rights reserved.

a b l
fe r
Connected to:
a n s
Production
o n -tr
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -

an
With the Partitioning, Oracle Label Security, OLAP, Data Mining
s
and Real Application Testing options
) h a deฺ
SQL> SQL>
r ฺ e s Gui
User altered.
p ฺ mi dent
SQL> SQL> d g Stu
Grant succeeded. ia@ is
b t h
SQL> SQL>ia (ja usefrom Oracle Database 11g Enterprise Edition
Disconnected
A
Release to - Production
b11.1.0.6.0
o
ni Real
With the Partitioning, Oracle Label Security, OLAP, Data Mining
ntoand Application Testing options
e A [oracle@edrsr33p1-orcl Common_Mistakes]$

J o s
--------------------------------------------------------------
#!/bin/bash

cd /home/oracle/solutions/Common_Mistakes

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba <<EOF

alter user sh identified by sh account unlock;

grant dba to sh;


EOF

Oracle Database 11g: SQL Tuning Workshop A - 13


Practice 2-1: Avoiding Common Mistakes (continued)
2) Connected as the SH user from a SQL*Plus session (stay connected to that session
until the end of this case), make sure you execute the following command:
set timing on
@flush
The goal of the first command is to tell you how long the next command takes to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

execute. The flush.sql script flushes both the shared pool and the buffer cache to
avoid most caching effects so that you can have good comparisons between two
executions. Note: You should not use commands found in this script on a production
system.
[oracle@edrsr33p1-orcl Common_Mistakes]$ sqlplus sh/sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Mar 26 20:37:49


2008
n s e
i ce
Copyright (c) 1982, 2007, Oracle. All rights reserved.
b l el
fe r a
Connected to:
a n s
-
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
n tr
Production
o
an
With the Partitioning, Oracle Label Security, OLAP, Data Mining
s
and Real Application Testing options
) h a deฺ
SQL> set timing on
r ฺ e s Gui
SQL> @flush
p ฺ mi dent
d g Stu
a@ this
System altered.
b i
i a (ja use
Elapsed: 00:00:00.21

A b to
ni o
System altered.

ntoElapsed: 00:00:00.49
e A SQL>
J o s
--------------------------------------------------------------

alter system flush shared_pool;


alter system flush buffer_cache;

3) From the same SQL*Plus session, execute the following statement and note the time
it takes to execute (You can use the correlation.sql script.):
SELECT COUNT(*)
FROM products p
WHERE prod_list_price < 1.15 * (SELECT avg(unit_cost)
FROM costs c
WHERE c.prod_id = p.prod_id);
SQL> @correlation
SQL>
SQL> SELECT COUNT(*)
2 FROM products p
3 WHERE prod_list_price < 1.15 * (SELECT avg(unit_cost)

Oracle Database 11g: SQL Tuning Workshop A - 14


Practice 2-1: Avoiding Common Mistakes (continued)
4 FROM costs c
5 WHERE c.prod_id = p.prod_id);

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

Elapsed: 00:00:01.21
SQL>

--------------------------------------------------------------

set timing on
set echo on

SELECT COUNT(*)
n s e
FROM products p
i ce
WHERE prod_list_price < 1.15 * (SELECT avg(unit_cost)
b l el
FROM costs c
WHERE c.prod_id = p.prod_id);
fe r a
a n s
n - tr
o
4) Before trying to fix the previous statement, flush your environment again using the
flush.sql script from the SQL*Plus session.
s an
) h a deฺ
SQL> @flush
r ฺ e s Gui
ฺ m i ent
SQL> alter system flush shared_pool;

System altered.
d p
g Stud
b i
Elapsed: 00:00:00.10 a@ this
(ja flush
SQL> alter system
i a u se buffer_cache;
A b to
i o
System altered.

nton
e A Elapsed: 00:00:00.17

J o s SQL>

5) How do you rewrite this statement to enhance performance? Test your solution. You
should discuss this with your instructor.
SQL> @nocorrelation
SQL>
SQL> set timing on
SQL> set echo on
SQL>
SQL> SELECT COUNT(*)
2 FROM products p, (SELECT prod_id, AVG(unit_cost) ac FROM
costs GROUP BY prod_id) c
3 WHERE p.prod_id = c.prod_id AND
4 p.prod_list_price < 1.15 * c.ac;

COUNT(*)
----------
46

Elapsed: 00:00:00.16

Oracle Database 11g: SQL Tuning Workshop A - 15


Practice 2-1: Avoiding Common Mistakes (continued)
SQL>

--------------------------------------------------------------

set timing on
set echo on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SELECT COUNT(*)
FROM products p, (SELECT prod_id, AVG(unit_cost) ac FROM costs
GROUP BY prod_id) c
WHERE p.prod_id = c.prod_id AND
p.prod_list_price < 1.15 * c.ac;

6) Exit from your SQL*Plus session.


n s e
ce
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release
eli
11.1.0.6.0 - Production
a b l
With the Partitioning, Oracle Label Security, OLAP, Data Mining
fe r
and Real Application Testing options
a n s
[oracle@edrsr33p1-orcl Common_Mistakes]$
o n -tr
s an
7) From your terminal session, execute the correlation_cleanup.sh script to
clean up your environment.
) h a deฺ
[oracle@edrsr33p1-orcl Common_Mistakes]$
r ฺ e G ui
s ./correlation_cleanup.sh
ฺ m i ent
SQL*Plus: Release 11.1.0.6.0
d p
g Stud - Production on Wed Mar 26 20:49:22

ia@
2008
b t h is
Copyright (c)ja e
( 1982,
u s
2007, Oracle. All rights reserved.

A bia to
o n io
Connected to:
t
An Production
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
e
Jos With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> SQL>
Revoke succeeded.

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Common_Mistakes]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Common_Mistakes

export ORACLE_SID=orcl

Oracle Database 11g: SQL Tuning Workshop A - 16


Practice 2-1: Avoiding Common Mistakes (continued)
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

sqlplus / as sysdba <<EOF

revoke dba from sh;

EOF

8) Before continuing, execute the setup_rest.sh script to set up the environment


for all the examples that follow. Make sure you run the script from a terminal session
connected as the oracle user. You can find the scripts for all the following cases in n s e
ice
your $HOME/solutions/Common_Mistakes directory.
b l el
[oracle@edrsr33p1-orcl Common_Mistakes]$ ./setup_rest.sh
f er a
n s
a20:59:53
SQL*Plus: Release 11.1.0.6.0 - Production on Wed Mar 26
- t r
2008
n on
s a reserved.
a eฺ
Copyright (c) 1982, 2007, Oracle. All rights
) h i d
i r ฺes nt Gu
Connected to:
g p
Oracle Database 11g Enterprise ฺm Edition
u d e Release 11.1.0.6.0 -
Production d S t
i a @ Oracle
h i s
With the Partitioning,
b t Label Security, OLAP, Data Mining

i a (ja use
and Real Application Testing options

A
SQL> SQL> to drop user jfv cascade
b SQL> SQL>
o
ni at line *
ntoERROR 1:
e A ORA-01918: user 'JFV' does not exist

J o s
SQL> SQL>
User created.

SQL> SQL>
Grant succeeded.

SQL> SQL> Connected.


SQL> SQL> drop table orders purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> SQL>
Table created.

SQL> SQL> 2 3 4 5 6 7
PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 17


Practice 2-1: Avoiding Common Mistakes (continued)
SQL> SQL> 2 3 4 5 6 7
PL/SQL procedure successfully completed.

SQL> SQL> drop table employees purge


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

ERROR at line 1:
ORA-00942: table or view does not exist

SQL> drop table job_history purge


*
ERROR at line 1:
ORA-00942: table or view does not exist

n s e
SQL> SQL>
i ce
Table created.
b l el
SQL> SQL> 2 3 4 5 6 7
fer a
PL/SQL procedure successfully completed.
a n s
n -tr
o
an
SQL> SQL>
Table created.
s
a deฺ
)7h
SQL> SQL> 2 3 4 5
r
6
ฺ e s Gui
ฺ m i ent
PL/SQL procedure successfully completed.

SQL> SQL>
d g p t u d
Index created.
a@ this S
b i
SQL> SQL> SQL>
i a (jadroputable
se old purge
b
at line 1:t
ERROR A
* o
ni o
ntoORA-00942: table or view does not exist

e A
J o s SQL> drop table new purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> SQL>
Table created.

SQL>
Table created.

SQL> SQL> 2 3 4 5 6 7
PL/SQL procedure successfully completed.

SQL> SQL> 2 3 4 5 6 7
PL/SQL procedure successfully completed.

SQL> SQL> SQL> Disconnected from Oracle Database 11g Enterprise


Edition Release 11.1.0.6.0 - Production

Oracle Database 11g: SQL Tuning Workshop A - 18


Practice 2-1: Avoiding Common Mistakes (continued)
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Common_Mistakes]$

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

#!/bin/bash

cd /home/oracle/solutions/Common_Mistakes

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
n s e
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
i ce
b l el
sqlplus / as sysdba <<EOF
fer a
set echo on
a n s
o n -tr
a n users temporary
drop user jfv cascade;

create user jfv identified by jfv default


h s
a deฺ
tablespace
tablespace temp;
e )
s Gui
grant connect, resource, dbaฺm
r ฺ
toi jfv; ent
d g p t u d
connect jfv/jfv
a@ this S
b i
i a (ja purge;
drop table orders
u se
A b to (order_id_char varchar2(50) primary key,
ni o
create table orders

ntoorder_total number, customer_name varchar2(300));

e A
J o s begin
for i in 1..500000 loop
insert into orders
values(i,100,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaa');
end loop;
commit;
end;
/

begin
for i in 1..500000 loop
insert into orders
values(500000+i,100,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
end loop;
commit;

Oracle Database 11g: SQL Tuning Workshop A - 19


Practice 2-1: Avoiding Common Mistakes (continued)
end;
/

drop table employees purge;


drop table job_history purge;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create table employees (employee_id number primary key, name


varchar2(500));

begin
for i in 1..500000 loop
insert into employees
values(i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa');
n s e
end loop;
i ce
commit;
b l el
end;
/
fer a
a n s
o n -tr
create table job_history (employee_id number, job varchar2(500));

begin
s an
for i in 1..500000 loop
) h a deฺ
insert into job_history
r ฺ e s Gui
ฺ m i ent
values(mod(i,1000),'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
d g p t u d
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

a@ this
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa');S
end loop; b i
commit;
i a (ja use
end;
A b to
o
ni index job_history_empid_indx on job_history(employee_id);
/

ntocreate
e A
J o s
drop table old purge;
drop table new purge;

create table old(name varchar2(10), other varchar2(500));


create table new(name varchar2(10), other varchar2(500));

begin
for i in 1..500000 loop
insert into old
values(i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa');
end loop;
commit;
end;
/

begin

Oracle Database 11g: SQL Tuning Workshop A - 20


Practice 2-1: Avoiding Common Mistakes (continued)
for i in 1..500000 loop
insert into new
values(500000+i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa');
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

end loop;
commit;
end;
/

EOF

9) Connect as the JFV user from a SQL*Plus session and stay connected in that session
n s e
until further notice. In the session, set SQL*Plus timings on and flush your
ice
environment again before starting the second case. You can use the set timing
b l el
on command and the flush.sql script for this.
f er a
an s
[oracle@edrsr33p1-orcl Common_Mistakes]$ sqlplus jfv/jfv
- t r
n 26 21:06:33
n oMar
SQL*Plus: Release 11.1.0.6.0 - Production on Wed
s a
a eฺ
2008
) h i d
Copyright (c) 1982, 2007, Oracle. es
i r t Gu reserved.
ฺ Allnrights
g p ฺm ude
Connected to: d S t
i a @ h i s
Oracle Database 11g
b t
Enterprise Edition Release 11.1.0.6.0 -
Production
a (ja useOracle Label Security, OLAP, Data Mining
With the iPartitioning,
A
and Real to Testing options
b Application
o
ni set timing on
ntoSQL>
e A SQL> @flush

J o s
System altered.

Elapsed: 00:00:00.33

System altered.

Elapsed: 00:00:07.12
SQL>

--------------------------------------------------------------

alter system flush shared_pool;


alter system flush buffer_cache;

Oracle Database 11g: SQL Tuning Workshop A - 21


Practice 2-1: Avoiding Common Mistakes (continued)
10) The second case you analyze is a join case. From your SQL*Plus session, execute the
following query and note the time it takes to complete:
SELECT count(*)
FROM job_history jh, employees e
WHERE
substr(to_char(e.employee_id),1)=substr(to_char(jh.employee_id),1);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> @join
SQL>
SQL> SELECT count(*)
2 FROM job_history jh, employees e
3 WHERE substr(to_char(e.employee_id),1) =
substr(to_char(jh.employee_id),1);

COUNT(*)
----------
n s e
499500
i ce
b l el
Elapsed: 00:00:03.03
SQL> fe r a
a n s
n
--------------------------------------------------------------
o -tr
set timing on a n
set echo on
h s
a deฺ
e )
s Gui
r ฺ
mi e dent
SELECT count(*)
FROM
p
job_history jh, employees ฺ
g Stu
d
WHERE substr(to_char(e.employee_id),1) =

b i a@ this
substr(to_char(jh.employee_id),1);

i a (ja use
b to fixtthe
11) Before trying
A o previous statement, flush your environment again using the
i o
flush.sql
n script from your SQL*Plus session.
t o
e An SQL> @flush
Jos
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.12
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:00.72
SQL>

--------------------------------------------------------------

alter system flush shared_pool;


alter system flush buffer_cache;

12) How would you rewrite the previous query for better performance? Test your
solution. You should discuss this with your instructor.

Oracle Database 11g: SQL Tuning Workshop A - 22


Practice 2-1: Avoiding Common Mistakes (continued)
SQL> @better_join
SQL>
SQL> set timing on
SQL> set echo on
SQL>
SQL> SELECT count(*)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2 FROM job_history jh, employees e


3 WHERE e.employee_id = jh.employee_id;

COUNT(*)
----------
499500

Elapsed: 00:00:00.70
SQL>
n s e
--------------------------------------------------------------
ice
b l el
set timing on
fe r a
set echo on
a n s
SELECT count(*)
o n -tr
an
FROM job_history jh, employees e
WHERE e.employee_id = jh.employee_id;
h s
a deฺ
e )
s Guagain i
r
13) Before analyzing the third case, flush yourฺ
i ent environment using the flush.sql
ฺ m
gp Stud
script from your SQL*Plus session.
d
SQL> @flush
i @ shared_pool;
aflush h is
b t
(ja use
SQL> alter system

b i a to
A
System altered.
o
t o ni
Elapsed: 00:00:00.11
A n SQL> alter system flush buffer_cache;
e
Jos System altered.

Elapsed: 00:00:00.23
SQL>

--------------------------------------------------------------

alter system flush shared_pool;


alter system flush buffer_cache;

14) The third case you analyze is a simple predicate case. Still connected as the JFV user
from your SQL*Plus session, execute the following query and note the time it takes
to complete:
SELECT * FROM orders WHERE order_id_char = 1205;
SQL> @simple_predicate
SQL>
SQL> set timing on
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 23


Practice 2-1: Avoiding Common Mistakes (continued)
SQL>
SQL> SELECT * FROM orders WHERE order_id_char = 1205;

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

--------------------------------------------------------------------
------------
1205 100
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

n s e
Elapsed: 00:00:07.62
i ce
SQL>
b l el
--------------------------------------------------------------
fe r a
a n s
set timing on
o n -tr
an
set echo on

SELECT * FROM orders WHERE order_id_chara=s1205; ฺ


s ) h
u i de
m i rฺein step
n Gflush your environment again
t 14,

15) Before trying to fix the SELECT statement
gp Stud e
using the flush.sql script. d
b i a@ this
(ja flush
SQL> @flush
a
SQL> alter system
i u se shared_pool;
A b to
ni o
System altered.
t o
e An Elapsed: 00:00:00.13
Jos
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:00.20
SQL>

16) How would you rewrite the previous statement for better performance? Test your
solution. You should discuss this with your instructor.
SQL> @better_predicate
SQL>
SQL> set timing on
SQL> set echo on
SQL>
SQL> SELECT * FROM orders WHERE order_id_char = '1205';

ORDER_ID_CHAR ORDER_TOTAL
-------------------------------------------------- -----------
CUSTOMER_NAME

Oracle Database 11g: SQL Tuning Workshop A - 24


Practice 2-1: Avoiding Common Mistakes (continued)
--------------------------------------------------------------------
------------
1205 100
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

aaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Elapsed: 00:00:00.04
SQL>

--------------------------------------------------------------

set timing on
n s e
set echo on
i ce
b l el
SELECT * FROM orders WHERE order_id_char = '1205';
fe ra
a n s
n - tr
17) Before proceeding with the next analysis, flush your environment again using the
flush.sql script. o
s an
SQL> alter system flush shared_pool; ) h
SQL> @flush a deฺ
r ฺ e s Gui
System altered.
p ฺ mi dent
Elapsed: 00:00:00.11@d
g Stu
b
SQL> alter system i t h is
aflush buffer_cache;
i a (ja use
b
System altered.
A to
ni o
ntoElapsed: 00:00:00.11

e A SQL>

J o s --------------------------------------------------------------

alter system flush shared_pool;


alter system flush buffer_cache;

18) The fourth case is a UNION case. Execute the following query and note the time it
takes to complete:
select count(*)
from (select name from old union select name from new);
SQL> @union
SQL>
SQL> set timing on
SQL> set echo on
SQL>
SQL> select count(*)
2 from (select name from old union select name from new);

COUNT(*)

Oracle Database 11g: SQL Tuning Workshop A - 25


Practice 2-1: Avoiding Common Mistakes (continued)
----------
1000000

Elapsed: 00:00:06.39
SQL>

19) Before investigating a better solution, flush your environment again using the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

flush.sql script.
SQL> @flush
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.10
SQL> alter system flush buffer_cache;
n s e
i ce
System altered.
b l el
Elapsed: 00:00:00.09
fe r a
SQL>
a n s
n -tr
20) How would you rewrite the previous statement for better performance? Test your
o
an
solution. You should discuss this with your instructor.
s
) h a deฺ
SQL> @better_union
SQL>
r ฺ e s Gui
SQL> set timing on
p ฺ mi dent
SQL> set echo on
d g Stu
a@ this
SQL>
b i
SQL> select count(*)

i a (ja name
2 from (select
u sefrom old union all select name from new);
A b
COUNT(*) to
i o
ton 1000000
----------

A n
o s e
J Elapsed: 00:00:00.42
SQL>

21) Execute the multiple_setup.sql script to set up the environment for this case.
SQL> @multiple_setup
SQL> create table myemp as select * from hr.employees;

Table created.

SQL> insert into myemp select * from myemp;


/
/
/
/
/
/
/
/
/
/

Oracle Database 11g: SQL Tuning Workshop A - 26


Practice 2-1: Avoiding Common Mistakes (continued)
/
/
/
/
/
commit;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

107 rows created.

SQL>
214 rows created.

SQL>
428 rows created.

SQL>
n s e
856 rows created.
i ce
b l el
SQL>
1712 rows created.
fer a
a n s
SQL>
n -tr
o
an
3424 rows created.

SQL>
h s
a deฺ
6848 rows created.
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m
13696 rows created.
d g Stud
SQL> b i a@ this
i a (ja use
27392 rows created.

SQL> A
b to
o
ni rows created.
nto54784
e A
J o s SQL>
109568 rows created.

SQL>
219136 rows created.

SQL>
438272 rows created.

SQL>
876544 rows created.

SQL>
1753088 rows created.

SQL>

3506176 rows created.

SQL>
Commit complete.

Oracle Database 11g: SQL Tuning Workshop A - 27


Practice 2-1: Avoiding Common Mistakes (continued)

SQL> insert into myemp select * from myemp;


commit;

7012352 rows created.


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

SQL>
Commit complete.

SQL>

22) Execute the multiple1.sql script and note the total time it takes to execute.
SQL> @multiple1
SQL> set timing on
n s e
i ce
el
SQL>
SQL>
a b l
SQL> SELECT COUNT (*)
fe r
FROM myemp
a n s
WHERE salary < 2000;
n - tr
2 3
o
s an
) h a deฺ
COUNT(*)
r ฺ e s Gui
----------
p ฺ mi dent
0
d g Stu
b
Elapsed: 00:00:15.49 i a@ this
( j
SQL> SELECT COUNT a (*)se
FROM myemp
WHERE A salary to u 2000 AND 4000;
bia BETWEEN
o n i2o 3
nt COUNT(*)
e A ----------
J o s 5636096

Elapsed: 00:00:17.14
SQL> SELECT COUNT (*)
FROM myemp
WHERE salary>4000;
2 3
COUNT(*)
----------
8388608

Elapsed: 00:00:18.12
SQL>

23) How would you rewrite the statements found in multiple1.sql script for better
performance?
SQL> @multiple2
SQL> SELECT COUNT (CASE WHEN salary < 2000
2 THEN 1 ELSE null END) count1,

Oracle Database 11g: SQL Tuning Workshop A - 28


Practice 2-1: Avoiding Common Mistakes (continued)
3 COUNT (CASE WHEN salary BETWEEN 2001 AND 4000
4 THEN 1 ELSE null END) count2,
5 COUNT (CASE WHEN salary > 4000
6 THEN 1 ELSE null END) count3
7 FROM myemp;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

COUNT1 COUNT2 COUNT3


---------- ---------- ----------
0 5636096 8388608

Elapsed: 00:00:18.19
SQL>

24) Exit from your SQL*Plus session.


SQL> exit
n s e
ce
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
eli
With the Partitioning, Oracle Label Security, OLAP, Data Mining
a b l
and Real Application Testing options
fe r
[oracle@edrsr33p1-orcl Common_Mistakes]$
a n s
n -tr
25) You now analyze a fifth case that deals with database connections. Execute the
o
an
bad_connect.sh script from your terminal window connected as the oracle
s
user. Note the time it takes to complete.
) h a deฺ
r ฺ e
[oracle@edrsr33p1-orcl Common_Mistakes]$ G ui
s ./bad_connect.sh

Wed Mar 26 21:56:42 GMT-7 2008
p mi dent
Wed Mar 26 21:57:07 GMT-7
[oracle@edrsr33p1-orcl d
g Stu
2008

a@ this
Common_Mistakes]$
b i
i a (ja use
--------------------------------------------------------------

A b
#!/bin/bash to
o
ni /home/oracle/solutions/Common_Mistakes
ntocd
e A
J o s export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

STREAM_NUM=0
MAX_STREAM=500

date

while [ $STREAM_NUM -lt $MAX_STREAM ]; do

# one more
let STREAM_NUM="STREAM_NUM+1"

# start one more stream

Oracle Database 11g: SQL Tuning Workshop A - 29


Practice 2-1: Avoiding Common Mistakes (continued)
sqlplus -s jfv/jfv @select.sql >> /tmp/bad_connect.log 2>&1

done

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

--------------------------------------------------------------

select count(*) from dba_users;


exit;

26) Analyze the bad_connect.sh script and try to find a better solution to enhance
the performance of that application. Test your solution. You should discuss this with
your instructor. n s e
i ce
b l el
[oracle@edrsr33p1-orcl Common_Mistakes]$ ./better_connect.sh
fe r a
Wed Mar 26 22:00:48 GMT-7 2008
a n s
-tr
Wed Mar 26 22:00:50 GMT-7 2008
[oracle@edrsr33p1-orcl Common_Mistakes]$
o n
s an
--------------------------------------------------------------

) h a deฺ
#!/bin/bash
r ฺ e s Gui
p ฺ mi dent
cd /home/oracle/solutions/Common_Mistakes
d g Stu
i a@ this
export ORACLE_SID=orcl
b
i a (ja use
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
A b to
ni o
export

ntoPATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
e A l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

J o s
date

sqlplus -s jfv/jfv @select2.sql >> /tmp/better_connect.log 2>&1

date

--------------------------------------------------------------

declare
c number;
begin
for i in 1..500 loop
select count(*) into c from dba_users;
end loop;
end;
/
exit;

Oracle Database 11g: SQL Tuning Workshop A - 30


Practice 2-1: Avoiding Common Mistakes (continued)
27) Clean up your environment by executing the cleanup_rest.sh script from your
terminal session.
[oracle@edrsr33p1-orcl Common_Mistakes]$ ./cleanup_rest.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Mar 26 22:03:10


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

2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
i ce
SQL> SQL> SQL> SQL>
b l el
User dropped.
f er a
SQL> SQL> Disconnected from Oracle Database 11g Enterprise s
an Edition
- t r
on Data Mining
Release 11.1.0.6.0 - Production
n
With the Partitioning, Oracle Label Security, OLAP,
a
[oracle@edrsr33p1-orcl Common_Mistakes]$as
and Real Application Testing options

) h i d eฺ
i r ฺes nt Gu
--------------------------------------------------------------

g p ฺm ude
#!/bin/bash d S t
@ s
a b ia
e thi
cd /home/oracle/solutions/Common_Mistakes
j
b ia ( to us
o AORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
export ORACLE_SID=orcl

n i
ntoexport

e A
J o s export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba <<EOF

set echo on

drop user jfv cascade;

EOF

Oracle Database 11g: SQL Tuning Workshop A - 31


Practices for Lesson 3
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 32


Practice 3-1: Understanding Optimizer Decisions
In this practice, you try to understand optimizer decisions relating to which execution
plan to use. All the scripts needed for this practice can be found in your
$HOME/solutions/Trace_Event directory.
1) Execute the te_setup.sh script. This script executes the following query and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

generates a trace file that contains all optimizer decisions.


SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sh.sales s,sh.times t,sh.customers c,sh.channels ch
WHERE s.time_id = t.time_id AND
s.cust_id = c.cust_id AND
s.channel_id = ch.channel_id AND
c.cust_state_province = 'CA' AND
ch.channel_desc IN ('Internet','Catalog') AND
n s e
i ce
el
t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;
a b l
[oracle@edrsr33p1-orcl Trace_Event]$ ./te_setup.sh
fe r
a n s
o n -tr
SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 9 22:16:53 2008

Copyright (c) 1982, 2007, Oracle. All rights n


a reserved.
h s
a deฺ
e )
s Gui
r ฺ
miEdition t
Connected to:
p
Oracle Database 11g Enterpriseฺ d e nRelease 11.1.0.6.0 -
Production g
dOracle S t u
@ s
thi options
ia Testing
With the Partitioning, Label Security, OLAP, Data Mining
a b
and Real Application
j e
SQL> b ia ( to us
i o A system flush shared_pool;
SQL> alter
n
nto
e A System altered.

J o s SQL>
SQL> alter user sh identified by sh account unlock;

User altered.

SQL>
SQL> connect sh/sh
Connected.
SQL>
SQL> ALTER SESSION SET TRACEFILE_IDENTIFIER = 'MYOPTIMIZER';

Session altered.

SQL>
SQL> alter session set events '10053 trace name context forever,
level 1';

Session altered.

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 33


Practice 3-1: Understanding Optimizer Decisions (continued)
SQL> SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
2 FROM sh.sales s,sh.times t,sh.customers c,sh.channels ch
3 WHERE s.time_id = t.time_id AND
4 s.cust_id = c.cust_id AND
5 s.channel_id = ch.channel_id AND
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

6 c.cust_state_province = 'CA' AND


7 ch.channel_desc IN ('Internet','Catalog') AND
8 t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')
9 GROUP BY ch.channel_class, c.cust_city,
t.calendar_quarter_desc;

no rows selected

SQL>
SQL> exit
n s e
Disconnected from Oracle Database 11g Enterprise Edition Release
ice
11.1.0.6.0 - Production
b l el
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
fe ra
orcl_ora_1801_MYOPTIMIZER.trc
a n s
orcl_ora_1801_MYOPTIMIZER.trm
o n -tr
aandn interpret the important
[oracle@edrsr33p1-orcl Trace_Event]$

2) With the help of your instructor, see the generatedatraces eฺ purposes. Do not use it
hdemonstration
)
parts of the trace file. Note: This lab is onlysfor u i d
i
on your production system unless explicitly r ฺe askedt G
by Oracle Support Services.
a) The 10053 trace output is g p ฺmdownudinto
broken
ena number of sections that broadly
t through in evaluating a plan. These
doptimizerSgoes
@
reflect the stages that the i s
ia query,thparameters
( a b
stages are asjfollows: e used by the optimizer, base statistical
ia to
information, base u s
table access cost, join order and method computations,
b
A for special features, such as query transformations.
n i orecosting
to[oracle@edrsr33p1-orcl Trace_Event]$ cat myoptimizer.trc
e An
Jos Trace file
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_32643_MYOPTIMIZE
R.trc
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
ORACLE_HOME = /u01/app/oracle/product/11.1.0/db_1
System name: Linux
Node name: edrsr33p1.us.oracle.com
Release: 2.6.9-55.0.0.0.2.ELsmp
Version: #1 SMP Wed May 2 14:59:56 PDT 2007
Machine: i686
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 18
Unix process pid: 32643, image: [email protected] (TNS
V1-V3)

Oracle Database 11g: SQL Tuning Workshop A - 34


Practice 3-1: Understanding Optimizer Decisions (continued)
*** 2008-04-09 21:46:51.950
*** SESSION ID:(134.35423) 2008-04-09 21:46:51.950
*** CLIENT ID:() 2008-04-09 21:46:51.950
*** SERVICE NAME:(SYS$USERS) 2008-04-09 21:46:51.950
*** MODULE NAME:(SQL*Plus) 2008-04-09 21:46:51.950
*** ACTION NAME:() 2008-04-09 21:46:51.950
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Registered qb: SEL$1 0xb7e0905c (PARSER)


---------------------
QUERY BLOCK SIGNATURE
---------------------
signature (): qb_name=SEL$1 nbfros=4 flg=0
fro(0): flg=4 objn=72254 hint_alias="C"@"SEL$1"
fro(1): flg=4 objn=72252 hint_alias="CH"@"SEL$1"
fro(2): flg=4 objn=72192 hint_alias="S"@"SEL$1"
fro(3): flg=4 objn=72250 hint_alias="T"@"SEL$1"
n s e
i ce
SPM: statement not found in SMB
b l el
DOP: Automatic degree of parallelism is disabled: Parameter.
PM: Considering predicate move-around in query block SEL$1 (#0)
fer a
**************************
a n s
Predicate Move-Around (PM)
o n -tr
an
**************************
OPTIMIZER INFORMATION
h s
a deฺ
******************************************
e )
s session i
u(sql_id=70fqjd9u1zk7c)
----- Current SQL Statement for rthisฺ
i ent G
-----
p ฺ m
SELECT ch.channel_class, gc.cust_city, d t.calendar_quarter_desc,
d S t u
FROM sh.sales b a@ thit,sh.customers
SUM(s.amount_sold) sales_amount
i
s,sh.times
s c,sh.channels ch
j a e
ia ( to= uc.cust_id
s
WHERE s.time_id = t.time_id AND

A b
s.cust_id AND

o n io c.cust_state_province = 'CA' AND


s.channel_id = ch.channel_id AND

nt
e A ch.channel_desc IN ('Internet','Catalog') AND

J o s t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc
*******************************************
Legend
The following abbreviations are used by optimizer trace.
CBQT - cost-based query transformation
JPPD - join predicate push-down
OJPPD - old-style (non-cost-based) JPPD
FPD - filter push-down
PM - predicate move-around
CVM - complex view merging
SPJ - select-project-join
SJC - set join conversion
SU - subquery unnesting
OBYE - order by elimination
ST - star transformation
CNT - count(col) to count(*) transformation
JE - Join Elimination
qb - query block
LB - leaf blocks
DK - distinct keys

Oracle Database 11g: SQL Tuning Workshop A - 35


Practice 3-1: Understanding Optimizer Decisions (continued)
LB/K - average number of leaf blocks per key
DB/K - average number of data blocks per key
CLUF - clustering factor
NDV - number of distinct values
Resp - response cost
Card - cardinality
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Resc - resource cost


NL - nested loops (join)
SM - sort merge (join)
HA - hash (join)
CPUSPEED - CPU Speed
IOTFRSPEED - I/O transfer speed
IOSEEKTIM - I/O seek time
SREADTIM - average single block read time
MREADTIM - average multiblock read time
MBRC - average multiblock read count
n s e
MAXTHR - maximum I/O system throughput
i ce
SLAVETHR - average slave I/O throughput
b l el
dmeth - distribution method
1: no partitioning required
fer a
2: value partitioned
a n s
4: right is random (round-robin)
n -tr
o
an
128: left is random (round-robin)
8: broadcast right and partition left
16: broadcast left and partition right
h s
a deฺ
)
s Gui
32: partition left using partitioning of right
e
r ฺ
mi dent
64: partition right using partitioning of left
256: run the join in serial
p ฺ
g Stu
d
0: invalid distribution method
sel - selectivity
ptn - partition b i a@ this
i a (ja use
***************************************
b to
PARAMETERS USED BY THE OPTIMIZER
A
o n io
********************************
*************************************
nt
e A PARAMETERS WITH ALTERED VALUES

J o s ******************************
Compilation Environment Dump
_smm_min_size = 204 KB
_smm_max_size = 40960 KB
_smm_px_max_size = 102400 KB
Bug Fix Control Environment

*************************************
PARAMETERS WITH DEFAULT VALUES
******************************
Compilation Environment Dump
optimizer_mode_hinted = false
optimizer_features_hinted = 0.0.0
parallel_execution_enabled = true
parallel_query_forced_dop = 0
parallel_dml_forced_dop = 0
parallel_ddl_forced_degree = 0
parallel_ddl_forced_instances = 0
_query_rewrite_fudge = 90
optimizer_features_enable = 11.1.0.6

Oracle Database 11g: SQL Tuning Workshop A - 36


Practice 3-1: Understanding Optimizer Decisions (continued)
_optimizer_search_limit = 5

optimizer_use_invisible_indexes = false
flashback_data_archive_internal_cursor = 0
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

_optimizer_extended_stats_usage_control = 240
Bug Fix Control Environment
fix 3834770 = 1
fix 3746511 = enabled
fix 4519016 = enabled
fix 3118776 = enabled
fix 4488689 = enabled
fix 2194204 = disabled

fix 6133948 = enabled
n s e
fix 6239909 = enabled
i ce
b l el
***************************************
fer a
PARAMETERS IN OPT_PARAM HINT
a n s
****************************
o n -tr
an
***************************************
Column Usage Monitoring is ON: tracking level = 1
***************************************
h s
a deฺ
e )
s Gui
r ฺ
i on equery
nt block SEL$1 (#0)

Considering Query Transformations
p m d
**************************
d g Stu
Query transformations
b i a@ this
**************************
(QT)

CBQT: Validity
i a (jachecks
u sepassed for 70fqjd9u1zk7c.

SEL$1 A
b
CSE: Considering
tocommon sub-expression elimination in query block

o n io (#0)
*************************
nt Common Subexpression elimination (CSE)
e A
J o s *************************
CSE: CSE not performed on query block SEL$1 (#0).
OBYE: Considering Order-by Elimination from view SEL$1 (#0)
***************************
Order-by elimination (OBYE)
***************************
OBYE: OBYE bypassed: no order by to eliminate.
JE: Considering Join Elimination on query block SEL$1 (#0)
*************************
Join Elimination (JE)
*************************
SQL:******* UNPARSED QUERY IS *******
SELECT "CH"."CHANNEL_CLASS" "CHANNEL_CLASS","C"."CUST_CITY"
"CUST_CITY","T"."CALENDAR_QUARTER_DESC"
"CALENDAR_QUARTER_DESC",SUM("S"."AMOUNT_SOLD") "SALES_AMOUNT" FROM
"SH"."SALES" "S","SH"."TIMES" "T","SH"."CUSTOMERS"
"C","SH"."CHANNELS" "CH" WHERE "S"."TIME_ID"="T"."TIME_ID" AND
"S"."CUST_ID"="C"."CUST_ID" AND "S"."CHANNEL_ID"="CH"."CHANNEL_ID"
AND "C"."CUST_STATE_PROVINCE"='CA' AND
("CH"."CHANNEL_DESC"='Internet' OR "CH"."CHANNEL_DESC"='Catalog')
AND ("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR

Oracle Database 11g: SQL Tuning Workshop A - 37


Practice 3-1: Understanding Optimizer Decisions (continued)
"T"."CALENDAR_QUARTER_DESC"='1999-Q2') GROUP BY
"CH"."CHANNEL_CLASS","C"."CUST_CITY","T"."CALENDAR_QUARTER_DESC"
Query block SEL$1 (#0) unchanged
CNT: Considering count(col) to count(*) on query block SEL$1 (#0)
*************************
Count(col) to Count(*) (CNT)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

*************************
CNT: COUNT() to COUNT(*) not done.
JE: Considering Join Elimination on query block SEL$1 (#0)
*************************
Join Elimination (JE)
*************************
SQL:******* UNPARSED QUERY IS *******
SELECT "CH"."CHANNEL_CLASS" "CHANNEL_CLASS","C"."CUST_CITY"
"CUST_CITY","T"."CALENDAR_QUARTER_DESC"
"CALENDAR_QUARTER_DESC",SUM("S"."AMOUNT_SOLD") "SALES_AMOUNT" FROM
n s e
"SH"."SALES" "S","SH"."TIMES" "T","SH"."CUSTOMERS"
i ce
"C","SH"."CHANNELS" "CH" WHERE "S"."TIME_ID"="T"."TIME_ID" AND
b l el
"S"."CUST_ID"="C"."CUST_ID" AND "S"."CHANNEL_ID"="CH"."CHANNEL_ID"
AND "C"."CUST_STATE_PROVINCE"='CA' AND
fer a
n s
("CH"."CHANNEL_DESC"='Internet' OR "CH"."CHANNEL_DESC"='Catalog')
a
AND ("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR
n -tr
o
an
"T"."CALENDAR_QUARTER_DESC"='1999-Q2') GROUP BY
"CH"."CHANNEL_CLASS","C"."CUST_CITY","T"."CALENDAR_QUARTER_DESC"
Query block SEL$1 (#0) unchanged
h s
a deฺ
query block SEL$1 (#0) unchanged
e )
s Gui
r ฺ
mi dent
Considering Query Transformations on query block SEL$1 (#0)
**************************
p ฺ
g Stu
d
Query transformations (QT)

b i a@ this
**************************
CSE: Considering common sub-expression elimination in query block
SEL$1 (#0)
i a (ja use
b to
*************************
A
o n io
Common Subexpression elimination (CSE)
*************************
nt
e A CSE: CSE not performed on query block SEL$1 (#0).

J o s query block SEL$1 (#0) unchanged


apadrv-start sqlid=8087006336042125548
:
call(in-use=62016, alloc=81864), compile(in-use=64064,
alloc=66868), execution(in-use=3376, alloc=4060)

*******************************************
Peeked values of the binds in SQL statement
*******************************************

CBQT: Considering cost-based transformation on query block SEL$1


(#0)
********************************
COST-BASED QUERY TRANSFORMATIONS
********************************
FPD: Considering simple filter push (pre rewrite) in query block
SEL$1 (#0)
FPD: Current where clause predicates "S"."TIME_ID"="T"."TIME_ID"
AND "S"."CUST_ID"="C"."CUST_ID" AND
"S"."CHANNEL_ID"="CH"."CHANNEL_ID" AND
"C"."CUST_STATE_PROVINCE"='CA' AND ("CH"."CHANNEL_DESC"='Internet'

Oracle Database 11g: SQL Tuning Workshop A - 38


Practice 3-1: Understanding Optimizer Decisions (continued)
OR "CH"."CHANNEL_DESC"='Catalog') AND
("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR "T"."C

OBYE: Considering Order-by Elimination from view SEL$1 (#0)


***************************
Order-by elimination (OBYE)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

***************************
OBYE: OBYE bypassed: no order by to eliminate.
Considering Query Transformations on query block SEL$1 (#0)
**************************
Query transformations (QT)
**************************
CSE: Considering common sub-expression elimination in query block
SEL$1 (#0)
*************************
Common Subexpression elimination (CSE)
n s e
*************************
i ce
CSE: CSE not performed on query block SEL$1 (#0).
b l el
kkqctdrvTD-start on query block SEL$1 (#0)
kkqctdrvTD-start: :
fer a
n
call(in-use=62016, alloc=81864), compile(in-use=105980,
a s
alloc=109196), execution(in-use=3376, alloc=4060)
o n -tr
kkqctdrvTD-cleanup: transform(in-use=0, alloc=0)
s an:
h a deฺ
call(in-use=62016, alloc=81864), compile(in-use=106488,
alloc=109196), execution(in-use=3376,) alloc=4060)
r ฺ e s Gui
kkqctdrvTD-end: ฺ m i ent
d g p
call(in-use=62016, alloc=81864),
t u d compile(in-use=106808,
a@ this S
alloc=109196), execution(in-use=3376, alloc=4060)
b i
i a (ja set-join
SJC: Considering
u se conversion in query block SEL$1 (#1)
to (SJC)
b Conversion
*************************
A
i o
Set-Join
n
nto*************************
e A SJC: not performed

J o s CNT: Considering count(col) to count(*) on query block SEL$1 (#1)


*************************
Count(col) to Count(*) (CNT)
*************************
CNT: COUNT() to COUNT(*) not done.
JE: Considering Join Elimination on query block SEL$1 (#1)
*************************
Join Elimination (JE)
*************************
SQL:******* UNPARSED QUERY IS *******
SELECT "CH"."CHANNEL_CLASS" "CHANNEL_CLASS","C"."CUST_CITY"
"CUST_CITY","T"."CALENDAR_QUARTER_DESC"
"CALENDAR_QUARTER_DESC",SUM("S"."AMOUNT_SOLD") "SALES_AMOUNT" FROM
"SH"."SALES" "S","SH"."TIMES" "T","SH"."CUSTOMERS"
"C","SH"."CHANNELS" "CH" WHERE "S"."TIME_ID"="T"."TIME_ID" AND
"S"."CUST_ID"="C"."CUST_ID" AND "S"."CHANNEL_ID"="CH"."CHANNEL_ID"
AND "C"."CUST_STATE_PROVINCE"='CA' AND
("CH"."CHANNEL_DESC"='Internet' OR "CH"."CHANNEL_DESC"='Catalog')
AND ("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR
"T"."CALENDAR_QUARTER_DESC"='1999-Q2') GROUP BY
"CH"."CHANNEL_CLASS","C"."CUST_CITY","T"."CALENDAR_QUARTER_DESC"

Oracle Database 11g: SQL Tuning Workshop A - 39


Practice 3-1: Understanding Optimizer Decisions (continued)
Query block SEL$1 (#1) unchanged
PM: Considering predicate move-around in query block SEL$1 (#1)
**************************
Predicate Move-Around (PM)
**************************
PM: PM bypassed: Outer query contains no views.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PM: PM bypassed: Outer query contains no views.


kkqctdrvTD-start on query block SEL$1 (#1)
kkqctdrvTD-start: :
call(in-use=81660, alloc=98240), compile(in-use=109508,
alloc=113320), execution(in-use=3376, alloc=4060)

kkqctdrvTD-cleanup: transform(in-use=0, alloc=0) :


call(in-use=81660, alloc=98240), compile(in-use=109984,
alloc=113320), execution(in-use=3376, alloc=4060)

n s e
kkqctdrvTD-end:
i ce
call(in-use=81660, alloc=98240), compile(in-use=110304,
b l el
alloc=113320), execution(in-use=3376, alloc=4060)
fer a
kkqctdrvTD-start on query block SEL$1 (#1)
a n s
kkqctdrvTD-start: :
o n -tr
an
call(in-use=81660, alloc=98240), compile(in-use=110304,
alloc=113320), execution(in-use=3376, alloc=4060)
h s
a deฺ
Registered qb: SEL$1 0xb7e01c6c (COPY) SEL$1)
r ฺ e s Gui
---------------------
QUERY BLOCK SIGNATURE ฺ m i ent
---------------------
d g p t u d
signature(): NULL @
a is S
b i t h
***********************************
(ja ByuPlacement
Cost-Based Group
i a se
b to
***********************************
A
n i o
GBP: Checking validity of GBP for query block SEL$1 (#1)

nto(#1)
GBP: Checking validity of group-by placement for query block SEL$1

e A
J o s GBP: Bypassed: QB has disjunction.
kkqctdrvTD-cleanup: transform(in-use=9008, alloc=9460) :
call(in-use=81684, alloc=98240), compile(in-use=129100,
alloc=132448), execution(in-use=3376, alloc=4060)

kkqctdrvTD-end:
call(in-use=81684, alloc=98240), compile(in-use=119884,
alloc=132448), execution(in-use=3376, alloc=4060)

GBP: Applying transformation directives


GBP: Checking validity of group-by placement for query block SEL$1
(#1)
GBP: Bypassed: QB has disjunction.
JPPD: Considering Cost-based predicate pushdown from query block
SEL$1 (#1)
************************************
Cost-based predicate pushdown (JPPD)
************************************
kkqctdrvTD-start on query block SEL$1 (#1)
kkqctdrvTD-start: :

Oracle Database 11g: SQL Tuning Workshop A - 40


Practice 3-1: Understanding Optimizer Decisions (continued)
call(in-use=81708, alloc=98240), compile(in-use=122632,
alloc=132448), execution(in-use=3376, alloc=4060)

kkqctdrvTD-cleanup: transform(in-use=0, alloc=0) :


call(in-use=81708, alloc=98240), compile(in-use=123108,
alloc=132448), execution(in-use=3376, alloc=4060)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

kkqctdrvTD-end:
call(in-use=81708, alloc=98240), compile(in-use=123428,
alloc=132448), execution(in-use=3376, alloc=4060)

JPPD: Applying transformation directives


query block SEL$1 (#1) unchanged
FPD: Considering simple filter push in query block SEL$1 (#1)
"S"."TIME_ID"="T"."TIME_ID" AND "S"."CUST_ID"="C"."CUST_ID" AND
"S"."CHANNEL_ID"="CH"."CHANNEL_ID" AND
n s e
"C"."CUST_STATE_PROVINCE"='CA' AND ("CH"."CHANNEL_DESC"='Internet'
i ce
OR "CH"."CHANNEL_DESC"='Catalog') AND
b l el
("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR "T"."C
try to generate transitive predicate from check constraints for
fe r a
query block SEL$1 (#1)
a n s
o n -tr
finally: "S"."TIME_ID"="T"."TIME_ID" AND "S"."CUST_ID"="C"."CUST_ID"

an
AND "S"."CHANNEL_ID"="CH"."CHANNEL_ID" AND
"C"."CUST_STATE_PROVINCE"='CA' AND ("CH"."CHANNEL_DESC"='Internet'
OR "CH"."CHANNEL_DESC"='Catalog') AND
h s
a deฺ
)
s Gui
("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR "T"."C
e
r ฺ
kkoqbc: optimizing query block
p ฺ miSEL$1de(#1)
nt
d g Stu
:
b i
call(in-use=81716, a@ alloc=98240),
t h is compile(in-use=124640,
alloc=132448),( j a s e
execution(in-use=3376, alloc=4060)
b i a to u
i o A
kkoqbc-subheap (create addr=0xb7d47960)
n
toQUERY BLOCK TEXT
****************
A n
o s e ****************
J SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sh.sales s,sh.times t,sh.customers c,sh.channels ch
WHERE s.time_id = t.time_id AND
s.cust_id = c.cust_id AND
s.channel_id = ch.channel_id
---------------------
QUERY BLOCK SIGNATURE
---------------------
signature (optimizer): qb_name=SEL$1 nbfros=4 flg=0
fro(0): flg=0 objn=72254 hint_alias="C"@"SEL$1"
fro(1): flg=0 objn=72252 hint_alias="CH"@"SEL$1"
fro(2): flg=0 objn=72192 hint_alias="S"@"SEL$1"
fro(3): flg=0 objn=72250 hint_alias="T"@"SEL$1"

-----------------------------
SYSTEM STATISTICS INFORMATION
-----------------------------
Using NOWORKLOAD Stats
CPUSPEED: 1263 millions instructions/sec

Oracle Database 11g: SQL Tuning Workshop A - 41


Practice 3-1: Understanding Optimizer Decisions (continued)
IOTFRSPEED: 4096 bytes per millisecond (default is 4096)
IOSEEKTIM: 10 milliseconds (default is 10)

***************************************
BASE STATISTICAL INFORMATION
***********************
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Table Stats::
Table: CHANNELS Alias: CH
#Rows: 5 #Blks: 4 AvgRowLen: 40.00
Index Stats::
Index: CHANNELS_PK Col#: 1
LVLS: 0 #LB: 1 #DK: 5 LB/K: 1.00 DB/K: 1.00 CLUF: 1.00
***********************
Table Stats::
Table: CUSTOMERS Alias: C
#Rows: 55500 #Blks: 1486 AvgRowLen: 180.00
n s e
Index Stats::
i ce
Index: CUSTOMERS_GENDER_BIX Col#: 4
b l el
LVLS: 1 #LB: 3 #DK: 2 LB/K: 1.00 DB/K: 2.00 CLUF: 5.00
Index: CUSTOMERS_MARITAL_BIX Col#: 6
fer a
LVLS: 1 #LB: 5 #DK: 11 LB/K: 1.00 DB/K: 1.00 CLUF: 18.00
a n s
Index: CUSTOMERS_PK Col#: 1
n -tr
o
an
LVLS: 1 #LB: 115 #DK: 55500 LB/K: 1.00 DB/K: 1.00 CLUF:
54405.00
Index: CUSTOMERS_YOB_BIX Col#: 5
h s
a deฺ
)
s Gui
LVLS: 1 #LB: 19 #DK: 75 LB/K: 1.00 DB/K: 1.00 CLUF: 75.00
e
r ฺ
mi dent
***********************
Table Stats::
p ฺ
g Stu
Table: TIMES Alias: T
d
Index Stats:: b i a@ this
#Rows: 1826 #Blks: 59 AvgRowLen: 197.00

i a (ja use
Index: TIMES_PK Col#: 1
b to
LVLS: 1 #LB: 5 #DK: 1826 LB/K: 1.00 DB/K: 1.00 CLUF: 53.00
A
o n io
***********************
Table Stats::
nt
e A Table: SALES Alias: S (Using composite stats)

J o s #Rows: 918843 #Blks: 1769 AvgRowLen: 29.00


Index Stats::
Index: SALES_CHANNEL_BIX Col#: 4
USING COMPOSITE STATS
LVLS: 1 #LB: 47 #DK: 4 LB/K: 11.00 DB/K: 23.00 CLUF: 92.00
Index: SALES_CUST_BIX Col#: 2
USING COMPOSITE STATS
LVLS: 1 #LB: 475 #DK: 7059 LB/K: 1.00 DB/K: 5.00 CLUF:
35808.00
Index: SALES_PROD_BIX Col#: 1
USING COMPOSITE STATS
LVLS: 1 #LB: 32 #DK: 72 LB/K: 1.00 DB/K: 14.00 CLUF:
1074.00
Index: SALES_PROMO_BIX Col#: 5
USING COMPOSITE STATS
LVLS: 1 #LB: 30 #DK: 4 LB/K: 7.00 DB/K: 13.00 CLUF: 54.00
Index: SALES_TIME_BIX Col#: 3
USING COMPOSITE STATS
LVLS: 1 #LB: 59 #DK: 1460 LB/K: 1.00 DB/K: 1.00 CLUF:
1460.00
Access path analysis for SALES

Oracle Database 11g: SQL Tuning Workshop A - 42


Practice 3-1: Understanding Optimizer Decisions (continued)
***************************************
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for SALES[S]
Table: SALES Alias: S
Card: Original: 918843.000000 Rounded: 918843 Computed:
918843.00 Non Adjusted: 918843.00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Access Path: TableScan


Cost: 498.20 Resp: 498.20 Degree: 0
Cost_io: 481.00 Cost_cpu: 260685437
Resp_io: 481.00 Resp_cpu: 260685437
****** trying bitmap/domain indexes ******
Access Path: index (FullScan)
Index: SALES_CHANNEL_BIX
resc_io: 75.00 resc_cpu: 552508
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 75.04 Resp: 75.04 Degree: 0
n s e
Access Path: index (FullScan)
i ce
Index: SALES_CUST_BIX
b l el
resc_io: 503.00 resc_cpu: 10743684
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
fer a
Cost: 503.71 Resp: 503.71 Degree: 0
a n s
Access Path: index (FullScan)
n -tr
o
an
Index: SALES_PROD_BIX
resc_io: 60.00 resc_cpu: 642086
h s
a deฺ
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 60.04 Resp: 60.04 Degree: 0
e )
s Gui
r ฺ
mi dent
Access Path: index (FullScan)
Index: SALES_PROMO_BIX
p ฺ
g Stu
d
resc_io: 58.00 resc_cpu: 423844

b i a@ this
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 58.03 Resp: 58.03 Degree: 0

i a (ja use
Access Path: index (FullScan)
b to
Index: SALES_TIME_BIX
A
o n io
resc_io: 60.00 resc_cpu: 719286
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
nt
e A Cost: 60.05 Resp: 60.05 Degree: 0

J o s Access Path: index (FullScan)


Index: SALES_PROMO_BIX
resc_io: 58.00 resc_cpu: 423844
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 58.03

Access path: Bitmap index - accepted


Cost: 2895.306181 Cost_io: 2869.400000 Cost_cpu:
392576474.936000 Sel: 1.000000
Not Believed to be index-only
****** finished trying bitmap/domain indexes ******
Best:: AccessPath: TableScan
Cost: 498.20 Degree: 1 Resp: 498.20 Card: 918843.00
Bytes: 0

Access path analysis for TIMES


***************************************
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for TIMES[T]
Table: TIMES Alias: T

Oracle Database 11g: SQL Tuning Workshop A - 43


Practice 3-1: Understanding Optimizer Decisions (continued)
Card: Original: 1826.000000 Rounded: 183 Computed: 182.60 Non
Adjusted: 182.60
Access Path: TableScan
Cost: 18.15 Resp: 18.15 Degree: 0
Cost_io: 18.00 Cost_cpu: 2314640
Resp_io: 18.00 Resp_cpu: 2314640
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

****** trying bitmap/domain indexes ******


Access Path: index (FullScan)
Index: TIMES_PK
resc_io: 6.00 resc_cpu: 407929
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 6.03 Resp: 6.03 Degree: 0
****** finished trying bitmap/domain indexes ******
Best:: AccessPath: TableScan
Cost: 18.15 Degree: 1 Resp: 18.15 Card: 182.60 Bytes: 0

n s e
Access path analysis for CUSTOMERS
i ce
***************************************
b l el
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for CUSTOMERS[C]
fer a
Table: CUSTOMERS Alias: C
a n s
-tr
Card: Original: 55500.000000 Rounded: 383 Computed: 382.76
n
o
an
Non Adjusted: 382.76
Access Path: TableScan
Cost: 406.16 Resp: 406.16 Degree: 0
h s
a deฺ
Cost_io: 404.00 Cost_cpu: 32782460
e )
s Gui
r ฺ
mi dent
Resp_io: 404.00 Resp_cpu: 32782460

p ฺ
****** trying bitmap/domain indexes ******
g Stu
d
Access Path: index (FullScan)

b i a@ this
Index: CUSTOMERS_GENDER_BIX
resc_io: 4.00 resc_cpu: 29486

i a (ja use
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
b to
Cost: 4.00 Resp: 4.00 Degree: 0
A
o n io
Access Path: index (FullScan)
Index: CUSTOMERS_MARITAL_BIX
nt
e A resc_io: 6.00 resc_cpu: 46329

J o s ix_sel: 1.000000 ix_sel_with_filters: 1.000000


Cost: 6.00 Resp: 6.00 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_PK
resc_io: 116.00 resc_cpu: 11926087
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 116.79 Resp: 116.79 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_YOB_BIX
resc_io: 20.00 resc_cpu: 157429
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 20.01 Resp: 20.01 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_GENDER_BIX
resc_io: 4.00 resc_cpu: 29486
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 4.00 Resp: 4.00 Degree: 0
Access path: Bitmap index - accepted
Cost: 2367.447569 Cost_io: 2364.560000 Cost_cpu: 43757572.166400
Sel: 1.000000
Not Believed to be index-only

Oracle Database 11g: SQL Tuning Workshop A - 44


Practice 3-1: Understanding Optimizer Decisions (continued)
****** finished trying bitmap/domain indexes ******
Best:: AccessPath: TableScan
Cost: 406.16 Degree: 1 Resp: 406.16 Card: 382.76 Bytes:
0

Access path analysis for CHANNELS


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

***************************************
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for CHANNELS[CH]
Table: CHANNELS Alias: CH
Card: Original: 5.000000 Rounded: 2 Computed: 2.00 Non
Adjusted: 2.00
Access Path: TableScan
Cost: 3.00 Resp: 3.00 Degree: 0
Cost_io: 3.00 Cost_cpu: 29826
Resp_io: 3.00 Resp_cpu: 29826
n s e
****** trying bitmap/domain indexes ******
i ce
Access Path: index (FullScan)
b l el
Index: CHANNELS_PK
resc_io: 1.00 resc_cpu: 8121
fe r a
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
a n s
Cost: 1.00 Resp: 1.00 Degree: 0
n - tr
o
an
****** finished trying bitmap/domain indexes ******
Best:: AccessPath: TableScan
h s
a deฺ
Cost: 3.00 Degree: 1 Resp: 3.00 Card: 2.00 Bytes: 0

e )
s Gu2 i
r ฺ
i ent 286
Grouping column cardinality [CHANNEL_CL]
Grouping column cardinality ฺ[mCUST_CITY]
d g
Grouping column cardinalityp t u d
[CALENDAR_Q] 2

a@ this S
***************************************
b i
i a (ja usANDe
A b
OPTIMIZER
to
STATISTICS COMPUTATIONS

o n io PLANS
***************************************
GENERAL
nt ***************************************
e A
J o s Considering cardinality-based initial join order.
Permutations for Starting Table :0
Join order[1]: CHANNELS[CH]#0 TIMES[T]#1 CUSTOMERS[C]#2
SALES[S]#3

***************
Now joining: TIMES[T]#1
***************
NL Join
Outer table: Card: 2.00 Cost: 3.00 Resp: 3.00 Degree: 1 Bytes:
21
Access path analysis for TIMES
Inner table: TIMES Alias: T
Access Path: TableScan
NL Join: Cost: 37.31 Resp: 37.31 Degree: 1
Cost_io: 37.00 Cost_cpu: 4659106
Resp_io: 37.00 Resp_cpu: 4659106
****** trying bitmap/domain indexes ******
Access Path: index (FullScan)
Index: TIMES_PK
resc_io: 6.00 resc_cpu: 407929

Oracle Database 11g: SQL Tuning Workshop A - 45


Practice 3-1: Understanding Optimizer Decisions (continued)
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 6.03 Resp: 6.03 Degree: 0
****** finished trying bitmap/domain indexes ******

Best NL cost: 37.31


resc: 37.31 resc_io: 37.00 resc_cpu: 4659106
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

resp: 37.31 resp_io: 37.00 resc_cpu: 4659106


Join Card: 365.200000 = = outer (2.000000) * inner (182.600000) *
sel (1.000000)
Join Card - Rounded: 365 Computed: 365.20
Grouping column cardinality [CHANNEL_CL] 2
Grouping column cardinality [ CUST_CITY] 286
Grouping column cardinality [CALENDAR_Q] 2
Best:: JoinMethod: NestedLoop
Cost: 37.31 Degree: 1 Resp: 37.31 Card: 365.20 Bytes: 37

n s e
***************
i ce
Now joining: CUSTOMERS[C]#2
b l el
***************
NL Join
fer a
n
Outer table: Card: 365.20 Cost: 37.31 Resp: 37.31 Degree: 1
a s
Bytes: 37
n -tr
o
an
Access path analysis for CUSTOMERS
Inner table: CUSTOMERS Alias: C
Access Path: TableScan
h s
a deฺ
)
s Gui
NL Join: Cost: 147725.92 Resp: 147725.92 Degree: 1
e
r ฺ
mi dent
Cost_io: 146936.00 Cost_cpu: 11970256947

p ฺ
Resp_io: 146936.00 Resp_cpu: 11970256947
g Stu
d
****** trying bitmap/domain indexes ******

b i a@ this
Access Path: index (FullScan)
Index: CUSTOMERS_GENDER_BIX

i a (ja use
resc_io: 4.00 resc_cpu: 29486
b to
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
A
o n io
Cost: 4.00 Resp: 4.00 Degree: 0
Access Path: index (FullScan)
nt
e A Index: CUSTOMERS_MARITAL_BIX

J o s resc_io: 6.00 resc_cpu: 46329


ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 6.00 Resp: 6.00 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_PK
resc_io: 116.00 resc_cpu: 11926087
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 116.79 Resp: 116.79 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_YOB_BIX
resc_io: 20.00 resc_cpu: 157429
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
Cost: 20.01 Resp: 20.01 Degree: 0
Access Path: index (FullScan)
Index: CUSTOMERS_GENDER_BIX
resc_io: 4.00 resc_cpu: 29486
ix_sel: 1.000000 ix_sel_with_filters: 1.000000
NL Join : Cost: 1498.02 Resp: 1498.02 Degree: 1
Cost_io: 1497.00 Cost_cpu: 15421408
Resp_io: 1497.00 Resp_cpu: 15421408
Access path: Bitmap index - accepted

Oracle Database 11g: SQL Tuning Workshop A - 46


Practice 3-1: Understanding Optimizer Decisions (continued)
Cost: 864192.977522 Cost_io: 863138.400000 Cost_cpu:
15980832052.096001 Sel: 1.000000
Not Believed to be index-only
****** finished trying bitmap/domain indexes ******

Best NL cost: 147725.92


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

resc: 147725.92 resc_io: 146936.00 resc_cpu: 11970256947


resp: 147725.92 resp_io: 146936.00 resc_cpu: 11970256947
Join Card: 139783.448276 = = outer (365.200000) * inner
(382.758621) * sel (1.000000)
Join Card - Rounded: 139783 Computed: 139783.45
Grouping column cardinality [CHANNEL_CL] 2
Grouping column cardinality [ CUST_CITY] 286
Grouping column cardinality [CALENDAR_Q] 2
Best:: JoinMethod: NestedLoop
Cost: 147725.92 Degree: 1 Resp: 147725.92 Card: 139783.45
n s e
Bytes: 63
i ce
b l el
***************
Now joining: SALES[S]#3
fer a
***************
a n s
NL Join
n -tr
o
an
Outer table: Card: 139783.45 Cost: 147725.92 Resp: 147725.92
Degree: 1 Bytes: 63
Access path analysis for SALES
h s
a deฺ
Inner table: SALES Alias: S
e )
s Gui
r ฺ
mi dent
Access Path: TableScan

p ฺ
NL Join: Cost: 2625413.78 Resp: 2625413.78 Degree: 1
g Stu
d
Cost_io: 2538743.82 Cost_cpu: 1313377131608

b i a@ this
Resp_io: 2538743.82 Resp_cpu: 1313377131608
****** trying bitmap/domain indexes ******

i a (ja use
Access Path: index (AllEqJoinGuess)
b to
Index: SALES_CHANNEL_BIX
A
o n io
resc_io: 11.00 resc_cpu: 83786
ix_sel: 0.250000 ix_sel_with_filters: 0.250000
nt
e A NL Join : Cost: 1686111.78 Resp: 1686111.78 Degree: 1

J o s Cost_io: 1684549.00 Cost_cpu: 23682093020


Resp_io: 1684549.00 Resp_cpu: 23682093020
Access Path: index (AllEqJoinGuess)
Index: SALES_CUST_BIX
resc_io: 1.00 resc_cpu: 9171
ix_sel: 0.000142 ix_sel_with_filters: 0.000142
NL Join : Cost: 287593.52 Resp: 287593.52 Degree: 1
Cost_io: 286719.00 Cost_cpu: 13252268345
Resp_io: 286719.00 Resp_cpu: 13252268345
Access Path: index (AllEqJoinGuess)
Index: SALES_TIME_BIX
resc_io: 1.00 resc_cpu: 8171
ix_sel: 0.000685 ix_sel_with_filters: 0.000685
NL Join : Cost: 287584.29 Resp: 287584.29 Degree: 1
Cost_io: 286719.00 Cost_cpu: 13112485345
Resp_io: 286719.00 Resp_cpu: 13112485345
Access path: Bitmap index - accepted
Cost: 723020.120275 Cost_io: 720497.059076 Cost_cpu:
38233905490.990532 Sel: 0.000000
Not Believed to be index-only
****** finished trying bitmap/domain indexes ******

Oracle Database 11g: SQL Tuning Workshop A - 47


Practice 3-1: Understanding Optimizer Decisions (continued)
Best NL cost: 723020.12
resc: 723020.12 resc_io: 720497.06 resc_cpu: 38233905491
resp: 723020.12 resp_io: 720497.06 resc_cpu: 38233905491
Join Card: 3115.595241 = = outer (139783.448276) * inner
(918843.000000) * sel (0.000000)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Join Card - Rounded: 3116 Computed: 3115.60


Grouping column cardinality [CHANNEL_CL] 2
Grouping column cardinality [ CUST_CITY] 286
Grouping column cardinality [CALENDAR_Q] 2
Outer table: CUSTOMERS Alias: C
resc: 147725.92 card 139783.45 bytes: 63 deg: 1 resp:
147725.92
Inner table: SALES Alias: S
resc: 498.20 card: 918843.00 bytes: 21 deg: 1 resp: 498.20
using dmeth: 2 #groups: 1
n s e
SORT ressource Sort statistics
i ce
Sort width: 238 Area size: 208896 Max Area size:
b l el
41943040
Degree: 1
fer a
Blocks to Sort: 1370 Row size: 80 Total Rows:
a n s
139783
n -tr
o
an
Initial runs: 2 Merge passes: 1 IO Cost / pass: 744
Total IO sort cost: 2114 Total CPU sort cost: 156539686
Total Temp space used: 21423000
h s
a deฺ
SORT ressource )
s Gui
Sort statistics
e
r ฺ
mi dent
Sort width: 238 Area size: 208896 Max Area size:
41943040
p ฺ
g Stu
Degree:
d 1

918843 b i a@ this
Blocks to Sort: 3825 Row size: 34 Total Rows:

i a (ja use
Initial runs: 2 Merge passes: 1 IO Cost / pass: 2074
b to
Total IO sort cost: 5899
A
Total CPU sort cost: 929421655

o n io Total Temp space used: 66626000


SM join: Resc: 156308.78 Resp: 156308.78 [multiMatchCost=0.00]
nt
e A SM Join

J o s SM cost: 156308.78
resc: 156308.78 resc_io: 155430.00 resc_cpu: 13316903726
resp: 156308.78 resp_io: 155430.00 resp_cpu: 13316903726
Outer table: CUSTOMERS Alias: C
resc: 147725.92 card 139783.45 bytes: 63 deg: 1 resp:
147725.92
Inner table: SALES Alias: S
resc: 498.20 card: 918843.00 bytes: 21 deg: 1 resp: 498.20
using dmeth: 2 #groups: 1
Cost per ptn: 1944.36 #ptns: 1
hash_area: 124 (max=10240) buildfrag: 1280 probefrag: 3702
ppasses: 1
Hash join: Resc: 150168.48 Resp: 150168.48 [multiMatchCost=0.00]
HA Join
HA cost: 150168.48
resc: 150168.48 resc_io: 149346.00 resc_cpu: 12463693737
resp: 150168.48 resp_io: 149346.00 resp_cpu: 12463693737
GROUP BY sort
GROUP BY adjustment factor: 0.500000
GROUP BY cardinality: 572.000000, TABLE cardinality: 3116.000000
SORT ressource Sort statistics

Oracle Database 11g: SQL Tuning Workshop A - 48


Practice 3-1: Understanding Optimizer Decisions (continued)
Sort width: 238 Area size: 208896 Max Area size:
41943040
Degree: 1
Blocks to Sort: 40 Row size: 103 Total Rows:
3116
Initial runs: 1 Merge passes: 0 IO Cost / pass: 0
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Total IO sort cost: 0 Total CPU sort cost: 16783070


Total Temp space used: 0
Best:: JoinMethod: Hash
Cost: 150169.59 Degree: 1 Resp: 150169.59 Card: 3115.60
Bytes: 84
***********************
Best so far: Table#: 0 cost: 3.0020 card: 2.0000 bytes: 42
Table#: 1 cost: 37.3075 card: 365.2000 bytes: 13505
Table#: 2 cost: 147725.9191 card: 139783.4483
bytes: 8806329
n s e
Table#: 3 cost: 150169.5886 card: 3115.5952 bytes:
i ce
261744
b l el
***********************
Join order[2]: CHANNELS[CH]#0 TIMES[T]#1 SALES[S]#3
fe r a
CUSTOMERS[C]#2
a n s
n - tr
o
an
***************
Now joining: SALES[S]#3
***************
h s
a deฺ
NL Join
e )
s Gui
r ฺ
mi dent
Outer table: Card: 365.20 Cost: 37.31 Resp: 37.31 Degree: 1
Bytes: 37
p ฺ
g Stu
d
Access path analysis for SALES

b i a@ this
Inner table: SALES Alias: S
Access Path: TableScan

i a (ja use
NL Join: Cost: 6507.09 Resp: 6507.09 Degree: 1
b to
n

i oA
nto***********************
e A
J o s Join order[22]:
CHANNELS[CH]#0
SALES[S]#3 CUSTOMERS[C]#2 TIMES[T]#1

***************
Now joining: TIMES[T]#1
***************
NL Join
Outer table: Card: 49822.22 Cost: 910.93 Resp: 910.93 Degree: 1
Bytes: 47
Access path analysis for TIMES
Inner table: TIMES Alias: T
Access Path: TableScan
NL Join: Cost: 804636.92 Resp: 804636.92 Degree: 1
Cost_io: 797001.00 Cost_cpu: 115712978623
Resp_io: 797001.00 Resp_cpu: 115712978623
Access Path: index (UniqueScan)
Index: TIMES_PK
resc_io: 1.00 resc_cpu: 10059
ix_sel: 0.000548 ix_sel_with_filters: 0.000548
NL Join : Cost: 50766.00 Resp: 50766.00 Degree: 1
Cost_io: 50707.00 Cost_cpu: 894143044

Oracle Database 11g: SQL Tuning Workshop A - 49


Practice 3-1: Understanding Optimizer Decisions (continued)
Resp_io: 50707.00 Resp_cpu: 894143044
Access Path: index (AllEqUnique)
Index: TIMES_PK
resc_io: 1.00 resc_cpu: 10059
ix_sel: 0.000548 ix_sel_with_filters: 0.000548
NL Join : Cost: 50766.00 Resp: 50766.00 Degree: 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Cost_io: 50707.00 Cost_cpu: 894143044


Resp_io: 50707.00 Resp_cpu: 894143044
****** trying bitmap/domain indexes ******
****** finished trying bitmap/domain indexes ******

Best NL cost: 50766.00


resc: 50766.00 resc_io: 50707.00 resc_cpu: 894143044
resp: 50766.00 resp_io: 50707.00 resc_cpu: 894143044
Join Card: 6231.190483 = = outer (49822.224013) * inner
(182.600000) * sel (0.000685)
n s e
Join Card - Rounded: 6231 Computed: 6231.19
i ce
Grouping column cardinality [CHANNEL_CL] 2
b l el
Grouping column cardinality [ CUST_CITY]
Grouping column cardinality [CALENDAR_Q]
286
2
fer a
Outer table: CUSTOMERS Alias: C
a n s
-tr
resc: 910.93 card 49822.22 bytes: 47 deg: 1 resp: 910.93
n
o
an
Inner table: TIMES Alias: T
resc: 18.15 card: 182.60 bytes: 16 deg: 1 resp: 18.15
using dmeth: 2 #groups: 1
h s
a deฺ
SORT ressource )
s Gui
Sort statistics
e
r ฺ
mi dent
Sort width: 238 Area size: 208896 Max Area size:
41943040
p ฺ
g Stu
Degree:
d 1

49822 b i a@ this
Blocks to Sort: 379 Row size: 62 Total Rows:

i a (ja use
Initial runs: 2 Merge passes: 1 IO Cost / pass: 206
b to
Total IO sort cost: 585
A
Total CPU sort cost: 59514576

o n io Total Temp space used: 6022000


SORT ressource Sort statistics
nt
e A Sort width: 238 Area size: 208896 Max Area size:

J o s 41943040
Degree: 1
Blocks to Sort: 1 Row size: 28 Total Rows: 183
Initial runs: 1 Merge passes: 0 IO Cost / pass: 0
Total IO sort cost: 0 Total CPU sort cost: 15215743
Total Temp space used: 0
SM join: Resc: 1519.02 Resp: 1519.02 [multiMatchCost=0.00]
SM Join
SM cost: 1519.02
resc: 1519.02 resc_io: 1488.00 resc_cpu: 470031494
resp: 1519.02 resp_io: 1488.00 resp_cpu: 470031494
Outer table: CUSTOMERS Alias: C
resc: 910.93 card 49822.22 bytes: 47 deg: 1 resp: 910.93
Inner table: TIMES Alias: T
resc: 18.15 card: 182.60 bytes: 16 deg: 1 resp: 18.15
using dmeth: 2 #groups: 1
Cost per ptn: 141.09 #ptns: 1
hash_area: 124 (max=10240) buildfrag: 359 probefrag: 1
ppasses: 1
Hash join: Resc: 1070.22 Resp: 1070.22 [multiMatchCost=0.04]
Outer table: TIMES Alias: T

Oracle Database 11g: SQL Tuning Workshop A - 50


Practice 3-1: Understanding Optimizer Decisions (continued)
resc: 18.15 card 182.60 bytes: 16 deg: 1 resp: 18.15
Inner table: CUSTOMERS Alias: C
resc: 910.93 card: 49822.22 bytes: 47 deg: 1 resp: 910.93
using dmeth: 2 #groups: 1
Cost per ptn: 0.83 #ptns: 1
hash_area: 124 (max=10240) buildfrag: 1 probefrag: 359
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ppasses: 1
Hash join: Resc: 929.92 Resp: 929.92 [multiMatchCost=0.00]
HA Join
HA cost: 929.92 swapped
resc: 929.92 resc_io: 903.00 resc_cpu: 407887714
resp: 929.92 resp_io: 903.00 resp_cpu: 407887714
Best:: JoinMethod: Hash
Cost: 929.92 Degree: 1 Resp: 929.92 Card: 6231.19 Bytes:
63

n s e
***************
i ce
Now joining: CHANNELS[CH]#0
b l el
***************
NL Join
f a
er 1
Outer table: Card: 6231.19 Cost: 929.92 Resp: 929.92
an s
Degree:
Bytes: 63
n -tr
o
an
Access path analysis for CHANNELS
Inner table: CHANNELS Alias: CH
Access Path: TableScan
h s
a deฺ
)
s Gui
NL Join: Cost: 7694.18 Resp: 7694.18 Degree: 1
e
r ฺ
mi dent
Cost_io: 7655.00 Cost_cpu: 593732024

p ฺ
Resp_io: 7655.00 Resp_cpu: 593732024
g Stu
d
Access Path: index (UniqueScan)
Index: CHANNELS_PK
b i a@ this
resc_io: 1.00 resc_cpu: 8451

i a (ja use
ix_sel: 0.200000 ix_sel_with_filters: 0.200000
b to
NL Join : Cost: 7164.39 Resp: 7164.39 Degree: 1
A
o n io Cost_io: 7134.00 Cost_cpu: 460548636
Resp_io: 7134.00 Resp_cpu: 460548636
nt
e A Access Path: index (AllEqUnique)

J o s Index: CHANNELS_PK
resc_io: 1.00 resc_cpu: 8451
ix_sel: 0.200000 ix_sel_with_filters: 0.200000
NL Join : Cost: 7164.39 Resp: 7164.39 Degree: 1
Cost_io: 7134.00 Cost_cpu: 460548636
Resp_io: 7134.00 Resp_cpu: 460548636
****** trying bitmap/domain indexes ******
****** finished trying bitmap/domain indexes ******

Best NL cost: 7164.39


resc: 7164.39 resc_io: 7134.00 resc_cpu: 460548636
resp: 7164.39 resp_io: 7134.00 resc_cpu: 460548636
Join Card: 3115.595241 = = outer (6231.190483) * inner (2.000000) *
sel (0.250000)
Join Card - Rounded: 3116 Computed: 3115.60
Grouping column cardinality [CHANNEL_CL] 2
Grouping column cardinality [ CUST_CITY] 286
Grouping column cardinality [CALENDAR_Q] 2
Outer table: TIMES Alias: T
resc: 929.92 card 6231.19 bytes: 63 deg: 1 resp: 929.92
Inner table: CHANNELS Alias: CH

Oracle Database 11g: SQL Tuning Workshop A - 51


Practice 3-1: Understanding Optimizer Decisions (continued)
resc: 3.00 card: 2.00 bytes: 21 deg: 1 resp: 3.00
using dmeth: 2 #groups: 1
SORT ressource Sort statistics
Sort width: 238 Area size: 208896 Max Area size:
41943040
Degree: 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Blocks to Sort: 62 Row size: 80 Total Rows: 6231


Initial runs: 2 Merge passes: 1 IO Cost / pass: 36
Total IO sort cost: 98 Total CPU sort cost: 20219323
Total Temp space used: 926000
SORT ressource Sort statistics
Sort width: 238 Area size: 208896 Max Area size:
41943040
Degree: 1
Blocks to Sort: 1 Row size: 34 Total Rows: 2
Initial runs: 1 Merge passes: 0 IO Cost / pass: 0
n s e
Total IO sort cost: 0 Total CPU sort cost: 15153867
i ce
Total Temp space used: 0
b l el
SM join: Resc: 1033.25 Resp: 1033.25 [multiMatchCost=0.00]
SM Join
fer a
SM cost: 1033.25
a n s
-
resc: 1033.25 resc_io: 1004.00 resc_cpu: 443290729
n tr
o
an
resp: 1033.25 resp_io: 1004.00 resp_cpu: 443290729
Outer table: TIMES Alias: T
h s
a deฺ
resc: 929.92 card 6231.19 bytes: 63 deg: 1 resp: 929.92
Inner table: CHANNELS Alias: CH
e )
s Gui
r ฺ
mi dent
resc: 3.00 card: 2.00 bytes: 21 deg: 1 resp: 3.00
using dmeth: 2 #groups: 1
p ฺ
g Stu
d
Cost per ptn: 0.56 #ptns: 1

1 b i a@ this
hash_area: 124 (max=10240) buildfrag: 58 probefrag: 1 ppasses:

i a (ja use
Hash join: Resc: 933.50 Resp: 933.50 [multiMatchCost=0.02]
b to
Outer table: CHANNELS Alias: CH
A
o n io
resc: 3.00 card 2.00 bytes: 21 deg: 1 resp: 3.00
Inner table: TIMES Alias: T
nt
e A resc: 929.92 card: 6231.19 bytes: 63 deg: 1 resp: 929.92

J o s using dmeth: 2 #groups: 1


Cost per ptn: 0.54 #ptns: 1
hash_area: 124 (max=10240) buildfrag: 1 probefrag: 58 ppasses:
1
Hash join: Resc: 933.46 Resp: 933.46 [multiMatchCost=0.00]
HA Join
HA cost: 933.46 swapped
resc: 933.46 resc_io: 906.00 resc_cpu: 416117828
resp: 933.46 resp_io: 906.00 resp_cpu: 416117828
GROUP BY sort
GROUP BY adjustment factor: 0.500000
GROUP BY cardinality: 572.000000, TABLE cardinality: 3116.000000
SORT ressource Sort statistics
Sort width: 238 Area size: 208896 Max Area size:
41943040
Degree: 1
Blocks to Sort: 40 Row size: 103 Total Rows:
3116
Initial runs: 1 Merge passes: 0 IO Cost / pass: 0
Total IO sort cost: 0 Total CPU sort cost: 16783070
Total Temp space used: 0

Oracle Database 11g: SQL Tuning Workshop A - 52


Practice 3-1: Understanding Optimizer Decisions (continued)
Join order aborted: cost > best plan cost
***********************
(newjo-stop-1) k:0, spcnt:0, perm:22, maxperm:2000

*********************************
Number of join permutations tried: 22
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

*********************************
(newjo-save) [1 3 2 0 ]
GROUP BY adjustment factor: 0.500000
GROUP BY cardinality: 572.000000, TABLE cardinality: 3116.000000
SORT ressource Sort statistics
Sort width: 238 Area size: 208896 Max Area size:
41943040
Degree: 1
Blocks to Sort: 40 Row size: 103 Total Rows:
3116
n s e
Initial runs: 1 Merge passes: 0 IO Cost / pass: 0
i ce
Total IO sort cost: 0 Total CPU sort cost: 16783070
b l el
Total Temp space used: 0
Trying or-Expansion on query block SEL$1 (#1)
fer a
Transfer Optimizer annotations for query block SEL$1 (#1)
a n s
id=0 frofand predicate="C"."CUST_STATE_PROVINCE"='CA'
n -tr
o
an
id=0 frofkksm[i] (sort-merge/hash)
predicate="S"."CUST_ID"="C"."CUST_ID"
h s
a deฺ
id=0 frosand (sort-merge/hash) predicate="S"."CUST_ID"="C"."CUST_ID"
id=0 frofkksm[i] (sort-merge/hash)
e )
s Gui
r ฺ
mi dent
predicate="S"."TIME_ID"="T"."TIME_ID"

p ฺ
id=0 frosand (sort-merge/hash) predicate="S"."TIME_ID"="T"."TIME_ID"
g Stu
d
id=0 frofand predicate="T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR

b i a@ this
"T"."CALENDAR_QUARTER_DESC"='1999-Q2'
id=0 frofkksm[i] (sort-merge/hash)

i a (ja use
predicate="S"."CHANNEL_ID"="CH"."CHANNEL_ID"
b to
id=0 frosand (sort-merge/hash)
A
o n io
predicate="S"."CHANNEL_ID"="CH"."CHANNEL_ID"
id=0 frofand predicate="CH"."CHANNEL_DESC"='Catalog' OR
nt
e A "CH"."CHANNEL_DESC"='Internet'

J o s GROUP BY adjustment factor: 1.000000


Final cost for query block SEL$1 (#1) - All Rows Plan:
Best join order: 16
Cost: 934.5672 Degree: 1 Card: 3116.0000 Bytes: 261744
Resc: 934.5672 Resc_io: 906.0000 Resc_cpu: 432900898
Resp: 934.5672 Resp_io: 906.0000 Resc_cpu: 432900898
kkoqbc-subheap (delete addr=0xb7d47960, in-use=105564, alloc=107812)
kkoqbc-end:
:
call(in-use=124380, alloc=246544), compile(in-use=132500,
alloc=136572), execution(in-use=3376, alloc=4060)

kkoqbc: finish optimizing query block SEL$1 (#1)


apadrv-end
:
call(in-use=124380, alloc=246544), compile(in-use=133196,
alloc=136572), execution(in-use=3376, alloc=4060)

Starting SQL statement dump

Oracle Database 11g: SQL Tuning Workshop A - 53


Practice 3-1: Understanding Optimizer Decisions (continued)
user_id=92 user_name=SH module=SQL*Plus action=
sql_id=70fqjd9u1zk7c plan_hash_value=1647000731 problem_type=3
----- Current SQL Statement for this session (sql_id=70fqjd9u1zk7c)
-----
SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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


WHERE s.time_id = t.time_id AND
s.cust_id = c.cust_id AND
s.channel_id = ch.channel_id AND
c.cust_state_province = 'CA' AND
ch.channel_desc IN ('Internet','Catalog') AND
t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc
sql_text_length=473
sql=SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
n s e
SUM(s.amount_sold) sales_amount
i ce
FROM sh.sales s,sh.times t,sh.customers c,sh.channels ch
b l el
WHERE s.time_id = t.time_id AND
s.cust_id = c.cust_id AND
fer a
s.channel_id = ch.channel_id
a n s
sql=AND
o n -tr
an
c.cust_state_province = 'CA' AND
ch.channel_desc IN ('Internet','Catalog') AND
h s
a deฺ
t.calendar_quarter_desc IN ('1999-Q1','1999-Q2')
)
s Gui
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc
e
r ฺ
mi dent
----- Explain Plan Dump -----
----- Plan Table -----
p ฺ
g Stu
d
============
Plan Table b i a@ this
============(ja se
b i a to u
----------------------------------------------------+---------------
o A
i | Operation
--------------------+---------------+

nto||n Id | Name | Rows | Bytes

e A Cost | Time | Pstart| Pstop |

J o s ----------------------------------------------------+---------------
--------------------+---------------+
| 0 | SELECT STATEMENT | | |
| 935 | | | |
| 1 | HASH GROUP BY | | 572 | 47K
| 935 | 00:00:12 | | |
| 2 | HASH JOIN | | 3116 | 256K
| 933 | 00:00:12 | | |
| 3 | TABLE ACCESS FULL | CHANNELS | 2 | 42
| 3 | 00:00:01 | | |
| 4 | HASH JOIN | | 6231 | 383K
| 930 | 00:00:12 | | |
| 5 | PART JOIN FILTER CREATE | :BF0000 | 183 | 2928
| 18 | 00:00:01 | | |
| 6 | TABLE ACCESS FULL | TIMES | 183 | 2928
| 18 | 00:00:01 | | |
| 7 | HASH JOIN | | 49K | 2287K
| 911 | 00:00:11 | | |
| 8 | TABLE ACCESS FULL | CUSTOMERS| 383 | 9958
| 406 | 00:00:05 | | |

Oracle Database 11g: SQL Tuning Workshop A - 54


Practice 3-1: Understanding Optimizer Decisions (continued)
| 9 | PARTITION RANGE JOIN-FILTER | | 897K | 18M
| 498 | 00:00:06 | :BF0000| :BF0000|
| 10 | TABLE ACCESS FULL | SALES | 897K | 18M
| 498 | 00:00:06 | :BF0000| :BF0000|
----------------------------------------------------+---------------
--------------------+---------------+
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Predicate Information:
----------------------
2 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
3 - filter(("CH"."CHANNEL_DESC"='Catalog' OR
"CH"."CHANNEL_DESC"='Internet'))
4 - access("S"."TIME_ID"="T"."TIME_ID")
6 - filter(("T"."CALENDAR_QUARTER_DESC"='1999-Q1' OR
"T"."CALENDAR_QUARTER_DESC"='1999-Q2'))
7 - access("S"."CUST_ID"="C"."CUST_ID")
8 - filter("C"."CUST_STATE_PROVINCE"='CA')
n s e
i ce
Content of other_xml column
b l el
===========================
db_version : 11.1.0.6
fer a
parse_schema : SH
a n s
plan_hash : 1647000731
n -tr
o
an
plan_hash_2 : 94088042
Outline Data:
/*+
h s
a deฺ
BEGIN_OUTLINE_DATA
e )
s Gui
r ฺ
mi dent
IGNORE_OPTIM_EMBEDDED_HINTS

p ฺ
OPTIMIZER_FEATURES_ENABLE('11.1.0.6')
g Stu
d
DB_VERSION('11.1.0.6')
ALL_ROWS
b i a@ this
OUTLINE_LEAF(@"SEL$1")

i a (ja use
FULL(@"SEL$1" "C"@"SEL$1")
b to
FULL(@"SEL$1" "S"@"SEL$1")
A
o n io FULL(@"SEL$1" "T"@"SEL$1")
FULL(@"SEL$1" "CH"@"SEL$1")
nt
e A LEADING(@"SEL$1" "C"@"SEL$1" "S"@"SEL$1" "T"@"SEL$1"

J o s "CH"@"SEL$1")
USE_HASH(@"SEL$1" "S"@"SEL$1")
USE_HASH(@"SEL$1" "T"@"SEL$1")
USE_HASH(@"SEL$1" "CH"@"SEL$1")
SWAP_JOIN_INPUTS(@"SEL$1" "T"@"SEL$1")
SWAP_JOIN_INPUTS(@"SEL$1" "CH"@"SEL$1")
USE_HASH_AGGREGATION(@"SEL$1")
END_OUTLINE_DATA
*/

Optimizer state dump:


Compilation Environment Dump
optimizer_mode_hinted = false
optimizer_features_hinted = 0.0.0
parallel_execution_enabled = true
parallel_query_forced_dop = 0
parallel_dml_forced_dop = 0
parallel_ddl_forced_degree = 0
parallel_ddl_forced_instances = 0
_query_rewrite_fudge = 90
optimizer_features_enable = 11.1.0.6

Oracle Database 11g: SQL Tuning Workshop A - 55


Practice 3-1: Understanding Optimizer Decisions (continued)

Bug Fix Control Environment


fix 3834770 = 1
fix 3746511 = enabled
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

fix 4519016 = enabled


fix 3118776 = enabled
fix 4488689 = enabled
fix 2194204 = disabled
fix 2660592 = enabled

n s e
Query Block Registry:
i ce
SEL$1 0xb7e0905c (PARSER) [FINAL]
b l el
:
fe r a
call(in-use=143844, alloc=246544), compile(in-use=166732,
a n s
alloc=224788), execution(in-use=13796, alloc=16288)
o n -tr
End of Optimizer State Dump n
a ======================
====================== END SQL StatementaDump s eฺ
[oracle@edrsr33p1-orcl Trace_Event]$ ) h i d
i r ฺes nt Gu
3) Execute the te_cleanup.sh g p ฺm clean
script totu deup your environment for this lab.
d S ./te_cleanup.sh
@ Trace_Event]$
i a i s
ab se th
[oracle@edrsr33p1-orcl

SQL*Plus:ia
( j u
A b t o
Release 11.1.0.6.0 - Production on Wed Apr 9 22:18:01 2008

o n io
Copyright (c) 1982, 2007, Oracle. All rights reserved.
t
e An
Jos Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Trace_Event]$

Oracle Database 11g: SQL Tuning Workshop A - 56


Practices for Lesson 4
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 57


Practice 4-1: Using Different Access Paths
In this practice, you explore various access paths the optimizer can use, and compare
them. You have the possibility of exploring 16 different scenarios, each of which are self-
contained. All scripts needed for this lab can be found in your
$HOME/solutions/Access_Paths directory.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1) Case 1: Connected as the oracle user from a terminal session, execute the
ap_setup.sh script.
[oracle@edrsr33p1-orcl Access_Paths]$ ./ap_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 9 14:24:51 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
ice
Connected to:
b l el
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
fe ra
Production
a n s
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n - tr
o
SQL>
s an
SQL> alter user sh identified by sh account
) h i d eฺ
a unlock;
User altered. i r ฺes nt Gu
g p ฺm ude
SQL> d S t
@ s
thi
SQL> grant dba to a
a b i sh;
j e
ia ( to us
Grant succeeded.
b
i o A
n
SQL>
toSQL> exit;
A n Disconnected from Oracle Database 11g Enterprise Edition Release
o s e 11.1.0.6.0 - Production
J With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Access_Paths]$

2) In the same terminal session, connect as the SH user in the SQL*Plus session.
[oracle@edrsr33p1-orcl Access_Paths]$ sqlplus sh/sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 9 14:24:58 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 58


Practice 4-1: Using Different Access Paths (continued)
3) Unless otherwise notified, stay connected to your SQL*Plus session as the SH user.
Execute the idx_setup.sql script to set up your environment for case 1.
SQL> @idx_setup
SQL>
SQL> drop table mysales purge;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

drop table mysales purge


*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table mysales as select * from sh.sales;

Table created.
n s e
i ce
SQL>
b l el
SQL> insert into mysales select * from mysales;
fe r a
a n s
918843 rows created.
n - tr
o
an
SQL> commit;

Commit complete. h s
a deฺ
e )
s Gui
r ฺ
SQL>
SQL>
p ฺ mi dent
d
SQL> insert into mysales gselectS*tufrom mysales;
b i a@ this
(ja use
1837686 rows created.

b i a to
A
SQL> commit;
o
ni complete.
toCommit
A n
o s e SQL>
J SQL> insert into mysales select * from mysales;

3675372 rows created.

SQL> commit;

Commit complete.

SQL>
SQL> insert into mysales select * from mysales;

7350744 rows created.

SQL> commit;

Commit complete.

SQL>
SQL> insert into mysales select * from mysales;

Oracle Database 11g: SQL Tuning Workshop A - 59


Practice 4-1: Using Different Access Paths (continued)
14701488 rows created.

SQL> commit;

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

SQL>
SQL> insert into mysales values (0,0,sysdate,0,0,0,0);

1 row created.

SQL> commit;

Commit complete.

SQL>
n s e
SQL> exec dbms_stats.gather_schema_stats('SH');
ice
b l el
PL/SQL procedure successfully completed.
fe ra
SQL>
a n s
o n
4) Set up your session with the following commands: set timing on, set
-tr
an
autotrace trace only, and set linesize 200. After this, execute the
s
h a deฺ
select * from mysales where prod_id=0; query. What do you observe?
)
r ฺ e s Gtable. ui
p ฺ mi dent
a) Basically, there are no indexes on the MYSALES
b) .The only possibility ford goptimizer
the
S tuis to use the full table scan to retrieve only
one row. You can a see@ s takes a long time.
that the iscan
b i t h
i a (jaon use
SQL> @with_and_without_index
SQL> set
SQL> A
b echo
to
o
ni set timing on
ntoSQL>
e A SQL> set autotrace traceonly

J o s SQL> set linesize 200 pagesize 1000


SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.61
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:15.04
SQL>
SQL> select * from mysales where prod_id=0;

Elapsed: 00:00:28.85

Execution Plan
----------------------------------------------------------
Plan hash value: 3597614299

Oracle Database 11g: SQL Tuning Workshop A - 60


Practice 4-1: Using Different Access Paths (continued)
--------------------------------------------------------------------
---------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
---------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 0 | SELECT STATEMENT | | 402K| 11M| 40249 (2)|


00:08:03 |
|* 1 | TABLE ACCESS FULL| MYSALES | 402K| 11M| 40249 (2)|
00:08:03 |
--------------------------------------------------------------------
---------

Predicate Information (identified by operation id):


---------------------------------------------------

n s e
1 - filter("PROD_ID"=0)
i ce
b l el
Statistics
fe r a
----------------------------------------------------------
a n s
421 recursive calls
n - tr
o
an
0 db block gets
141606 consistent gets
141529 physical reads
h s
a deฺ
0 redo size
e )
s Gui
r ฺ
mi dent
790 bytes sent via SQL*Net to client

p ฺ
420 bytes received via SQL*Net from client
g Stu
d
2 SQL*Net roundtrips to/from client

b i a@ this
5 sorts (memory)
0 sorts (disk)

i a (ja use
1 rows processed

Ab to
o n io
SQL>
SQL> set timing off
nt
e A SQL> set autotrace off

J o s SQL>

5) How do you enhance the performance of the query in step 4? Implement your
solution.
SQL> @create_mysales_index
SQL> set echo on
SQL>
SQL> create index mysales_prodid_idx on mysales(prod_id) nologging
compute statistics;

Index created.

SQL>

6) Execute the same query again and verify that performance is enhanced now.
a) After implementing the index, the optimizer can use it to speed up the query
execution time. You can see a dramatic improvement in performance.
SQL> @with_and_without_index
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 61


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> set timing on
SQL> set autotrace traceonly
SQL> set linesize 200 pagesize 1000
SQL>
SQL> alter system flush shared_pool;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

System altered.

Elapsed: 00:00:00.29
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:01.71
SQL>
n s e
SQL> select * from mysales where prod_id=0;
i ce
b l el
Elapsed: 00:00:00.88
fer a
Execution Plan
a n s
o n -tr
----------------------------------------------------------

an
Plan hash value: 3009203711

h s
a deฺ
--------------------------------------------------------------------
------------------------------
e )
s| NameGui
Bytes | Cost (%CPU)| Time ฺm|i
| Id | Operation r ฺ n t | Rows |

d g p tude
--------------------------------------------------------------------

i a @ his S |
------------------------------
|
11M| 6079 (j(1)|ab 00:01:13
0 | SELECT STATEMENT
e t | | 402K|

a ACCESS
1 | biTABLE u sBY INDEX ROWID| MYSALES
|
11M| A
o
t 00:01:13 | | 402K|

o n
|* io2 | INDEX RANGE SCAN
6079 (1)|
| MYSALES_PRODID_IDX | 402K|
nt | 822 (1)| 00:00:10 |
e A
J o s --------------------------------------------------------------------
------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("PROD_ID"=0)

Statistics
----------------------------------------------------------
496 recursive calls
0 db block gets
104 consistent gets
21 physical reads
0 redo size
794 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)

Oracle Database 11g: SQL Tuning Workshop A - 62


Practice 4-1: Using Different Access Paths (continued)
1 rows processed

SQL>
SQL> set timing off
SQL> set autotrace off
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7) Clean up your environment for case 1 by executing the idx_cleanup.sql script.


SQL> @idx_cleanup
SQL> set echo on
SQL>
SQL> drop table mysales purge;

Table dropped.

n s e
ce
SQL>
SQL>
eli
b l
8) Case 2: Drop all indexes currently created on the CUSTOMERS table except its
f e ra
primary key index.
t r a ns
SQL> @drop_customers_indexes
o n -
SQL> set termout off
s an
SQL>
) h a deฺ
ฺ e s Gui
--------------------------------------------------------------
r
p ฺ mi dent
set termout off
store set sqlplus_settings d g replace
S tu
buffer.sqlia @ his
save
timingja b replace
e t off verify off autotrace off feedback off
set
( off
s
heading
bia to u
spool Adait.sql

o n io
t SELECT 'drop index '||i.index_name||';'
e An FROM user_indexes i
Jos WHERE
AND
i.table_name = 'CUSTOMERS'
NOT EXISTS
(SELECT 'x'
FROM user_constraints c
WHERE c.index_name = i.index_name
AND c.table_name = i.table_name
AND c.status = 'ENABLED');

spool off
@dait

get buffer.sql nolist


@sqlplus_settings
set termout on

Oracle Database 11g: SQL Tuning Workshop A - 63


Practice 4-1: Using Different Access Paths (continued)
9) Execute the following query:
SELECT /*+ FULL(c) */ c.*
FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
AND cust_credit_limit = 10000;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

What do you observe?


a) The optimizer uses a full table scan and the cost for this query is relatively high.
SQL> @query00
SQL> set echo on
SQL>
SQL> set timing on
SQL>
SQL>
set autotrace traceonly
set linesize 200 pagesize 1000
n s e
i ce
el
SQL>
SQL> SELECT /*+ FULL(c) */ c.*
a b l
2 FROM customers c
fe r
3 WHERE cust_gender = 'M'
a n s
-tr
4 AND cust_postal_code = 40804
5 AND cust_credit_limit = 10000
o n
6 /
s an
6 rows selected.
) h a deฺ
r ฺ e s Gui
Elapsed: 00:00:00.29
p ฺ mi dent
Execution Plan d g Stu
i a@ this
----------------------------------------------------------
b
i a (ja use
Plan hash value: 2008213504

A b to
--------------------------------------------------------------------
i o
to|n Id | Operation
-----------
| Name | Rows | Bytes | Cost (%CPU)|
A n Time |
o s e --------------------------------------------------------------------
J -----------
| 0 | SELECT STATEMENT | | 6 | 1080 | 407 (1)|
00:00:05 |
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 6 | 1080 | 407 (1)|
00:00:05 |
--------------------------------------------------------------------
-----------

Predicate Information (identified by operation id):


---------------------------------------------------

1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804 AND
"CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')

Statistics
----------------------------------------------------------
1067 recursive calls
0 db block gets
1722 consistent gets

Oracle Database 11g: SQL Tuning Workshop A - 64


Practice 4-1: Using Different Access Paths (continued)
1461 physical reads
116 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
27 sorts (memory)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 sorts (disk)
6 rows processed

SQL>
SQL> set timing off
SQL> set autotrace off
SQL>

10) Create three B*-tree indexes on the following CUSTOMERS table columns:
cust_gender
n s e
cust_postal_code i ce
cust_credit_limit
b l el
fe ra
SQL> @create_cust_gender_index
a n s
SQL>
SQL>
set echo on
n - tr
o
an
SQL> CREATE INDEX cust_cust_gender_idx
2
3
ON customers(cust_gender)
NOLOGGING COMPUTE STATISTICS; h s
a deฺ
e )
s Gui
r ฺ
Index created.
p ฺ mi dent
SQL> d g Stu
SQL> set echo onbi
a@ this
SQL> @create_cust_postal_code_index

SQL>
i a (ja use
A b INDEXtocust_cust_postal_code_idx
SQL> CREATE

n o
i3 NOLOGGING
2 ON customers(cust_postal_code)

nt o COMPUTE STATISTICS;

e A
J o s Index created.

SQL>
SQL> @create_cust_credit_limit_index
SQL> set echo on
SQL>
SQL> CREATE INDEX cust_cust_credit_limit_idx
2 ON customers(cust_credit_limit)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>
SQL> @list_customers_indexes
SQL> set echo on
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT ui.table_name
2 , decode(ui.index_type
3 ,'NORMAL', ui.uniqueness
4 ,ui.index_type) AS index_type

Oracle Database 11g: SQL Tuning Workshop A - 65


Practice 4-1: Using Different Access Paths (continued)
5 , ui.index_name
6 FROM user_indexes ui
7 WHERE ui.table_name = 'CUSTOMERS'
8 ORDER BY ui.table_name
9 , ui.uniqueness desc;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

TABLE_NAME INDEX_TYPE
INDEX_NAME
------------------------------ --------------------------- ---------
---------------------
CUSTOMERS UNIQUE
CUSTOMERS_PK
CUSTOMERS NONUNIQUE
CUST_CUST_GENDER_IDX
CUSTOMERS NONUNIQUE
CUST_CUST_CREDIT_LIMIT_IDX
n s e
CUSTOMERS NONUNIQUE
i ce
CUST_CUST_POSTAL_CODE_IDX
b l el
SQL>
fe ra
a n s
11) Start monitoring all the CUSTOMERS indexes.
o n -tr
SQL> @start_monitoring_indexes
s an
SQL> set echo on
) h a deฺ
ui
SQL>
r ฺ
SQL> ALTER INDEX CUSTOMERS_PK MONITORINGe s USAGE;
G
p ฺ mi dent
Index altered.
d g Stu
SQL>
b i a@ this
( j
SQL> ALTER INDEXa s e
CUST_CUST_POSTAL_CODE_IDX MONITORING USAGE;
bia to u
Index A
altered.

o n io
nt SQL>
e A SQL> ALTER INDEX CUST_CUST_GENDER_IDX MONITORING USAGE;
J o s
Index altered.

SQL>
SQL> ALTER INDEX CUST_CUST_CREDIT_LIMIT_IDX MONITORING USAGE;

Index altered.

SQL>
SQL> @show_index_usage
SQL> set echo on
SQL>
SQL> set linesize 200
SQL>
SQL> select * from v$object_usage;

INDEX_NAME TABLE_NAME MON


USE START_MONITORING END_MONITORING
------------------------------ ------------------------------ --- --
- ------------------- -------------------

Oracle Database 11g: SQL Tuning Workshop A - 66


Practice 4-1: Using Different Access Paths (continued)
CUSTOMERS_PK CUSTOMERS YES NO
04/09/2008 14:52:43
CUST_CUST_POSTAL_CODE_IDX CUSTOMERS YES NO
04/09/2008 14:52:43
CUST_CUST_GENDER_IDX CUSTOMERS YES NO
04/09/2008 14:52:43
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CUST_CUST_CREDIT_LIMIT_IDX CUSTOMERS YES NO


04/09/2008 14:52:43

SQL>

12) Execute the following query:


SELECT /*+ INDEX(c) */ c.*
FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
n s e
AND cust_credit_limit = 10000;
i ce
What do you observe?
b l el
fe r a
a) The optimizer chooses to use only one index to do a fast full scan. The cost is
a n s
lower than the full table scan.
n - tr
o
an
SQL> @query01
SQL> set echo on
SQL> h s
a deฺ
SQL> set timing on
e )
s Gui
r ฺ
SQL> set autotrace traceonly
p
SQL> set linesize 200 pagesize ฺ mi1000dent
SQL> d g Stu
i a@c t*/
SQL> SELECT /*+ INDEX(c)
b h isc.*
2 FROM
3 WHERE (
a ja use = 'M'
customers
i cust_gender
to
b cust_postal_code
o
i6 /
5 A
4 AND
AND cust_credit_limit
= 40804
= 10000
t o n
A n
o s e 6 rows selected.
J
Elapsed: 00:00:00.04

Execution Plan
----------------------------------------------------------
Plan hash value: 1928091631

--------------------------------------------------------------------
-------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-------------------------------------
| 0 | SELECT STATEMENT | |
6 | 1080 | 218 (1)| 00:00:03 |
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
6 | 1080 | 218 (1)| 00:00:03 |
|* 2 | INDEX FULL SCAN | CUST_CUST_POSTAL_CODE_IDX |
89 | | 134 (1)| 00:00:02 |

Oracle Database 11g: SQL Tuning Workshop A - 67


Practice 4-1: Using Different Access Paths (continued)
--------------------------------------------------------------------
-------------------------------------

Predicate Information (identified by operation id):


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

1 - filter("CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')


2 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)

Statistics
----------------------------------------------------------
395 recursive calls
3 db block gets
384 consistent gets
132 physical reads
n s e
604 redo size
i ce
2570 bytes sent via SQL*Net to client
b l el
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
fe r a
6 sorts (memory)
a n s
0 sorts (disk)
n - tr
o
an
6 rows processed

SQL>
h s
a deฺ
SQL> set timing off
e )
s Gui
r ฺ
mi dent
SQL> set autotrace off
SQL>
p ฺ
g Stu
d
13) Execute the following query:
b i a@ this*/ c.*
SELECT /*+ INDEX_COMBINE(c)
FROM (ja c us=e'M'
customers
i a
b
WHERE cust_gender
to
AND Acust_postal_code
n i o = 40804

to AND cust_credit_limit = 10000;

e An What do you observe?


Jos a) This time the optimizer uses multiple indexes and combines them to access the
table. However, the cost is higher than that from the previous step as well as the
full table scan.
SQL> @query02
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> SELECT /*+ INDEX_COMBINE(c) */ c.*
2 FROM customers c
3 WHERE cust_gender = 'M'
4 AND cust_postal_code = 40804
5 AND cust_credit_limit = 10000
6 /

6 rows selected.

Oracle Database 11g: SQL Tuning Workshop A - 68


Practice 4-1: Using Different Access Paths (continued)
Elapsed: 00:00:00.06

Execution Plan
----------------------------------------------------------
Plan hash value: 4093665856
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
-------------------------------------------
| Id | Operation | Name
| Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-------------------------------------------
| 0 | SELECT STATEMENT |
| 6 | 1080 | 466 (1)| 00:00:06 |
|* 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS
| 6 | 1080 | 466 (1)| 00:00:06 |
n s e
| 2 | BITMAP CONVERSION TO ROWIDS |
i ce
| | | | |
b l el
|
|
3 |
|
BITMAP AND
| | |
|
fer a
| 4 | BITMAP CONVERSION FROM ROWIDS|
a n s
| | | | |
n -tr
o
an
|* 5 | INDEX RANGE SCAN |
CUST_CUST_CREDIT_LIMIT_IDX | | | 14 (0)| 00:00:01 |
| 6 |
h
BITMAP CONVERSION FROM ROWIDS| s
a deฺ
| | | |
e )
s Gui
|
r ฺ
mi dent
|* 7 | INDEX RANGE SCAN | CUST_CUST_GENDER_IDX
| | | 51 ฺ
(0)| 00:00:01 |
p
g Stu
d
--------------------------------------------------------------------

b i a@ this
-------------------------------------------

i a (ja use(identified by operation id):


Predicate Information
b to
---------------------------------------------------
A
i o
nton 15 -- filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)
e A access("CUST_CREDIT_LIMIT"=10000)

J o s 7 - access("CUST_GENDER"='M')

Statistics
----------------------------------------------------------
3 recursive calls
7 db block gets
894 consistent gets
81 physical reads
1020 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 69


Practice 4-1: Using Different Access Paths (continued)
14) Confirm the list of indexes that were accessed in this case.
SQL> @show_index_usage
SQL> set echo on
SQL>
SQL> set linesize 200
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> select * from v$object_usage;

INDEX_NAME TABLE_NAME MON


USE START_MONITORING END_MONITORING
------------------------------ ------------------------------ --- --
- ------------------- -------------------
CUSTOMERS_PK CUSTOMERS YES NO
04/09/2008 14:52:43
CUST_CUST_POSTAL_CODE_IDX CUSTOMERS YES
n s e
YES 04/09/2008 14:52:43
i ce
CUST_CUST_GENDER_IDX
YES 04/09/2008 14:52:43
CUSTOMERS YES
b l el
CUST_CUST_CREDIT_LIMIT_IDX CUSTOMERS
fe a
rYES
YES 04/09/2008 14:52:43
a n s
SQL> o n -tr
SQL>
s an
15) Case 3: Drop all the CUSTOMERS indexes except ) h aits primary
i d eฺkey index. After this,
r
make sure you create a concatenated index
s the following
ฺeon G u CUSTOMERS columns,
i en t
and in the order mentioned here:pฺm d
cust_gender d g S t u
@ s
cust_credit_limit
j a b ia
e thi
b i a ( to us
cust_postal_code

i A termout off
SQL> @drop_customers_indexes
o set
to n
SQL>

e An SQL>
SQL> @create_gender_limit_code_index
Jos SQL>
SQL>
set echo on

SQL> CREATE INDEX cust_gender_limit_code_idx


2 ON customers(cust_gender,cust_credit_limit,cust_postal_code)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>

16) Execute the following query:


SELECT /*+ INDEX(c) */ c.*
FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
AND cust_credit_limit = 10000;
What do you observe?
a) The optimizer uses your concatenated index, and the resulting cost is by far the
best compared to the previous steps.

Oracle Database 11g: SQL Tuning Workshop A - 70


Practice 4-1: Using Different Access Paths (continued)
SQL> @query01
SQL> set echo on
SQL>
SQL> set timing on
SQL> set autotrace traceonly
SQL> set linesize 200 pagesize 1000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> SELECT /*+ INDEX(c) */ c.*
2 FROM customers c
3 WHERE cust_gender = 'M'
4 AND cust_postal_code = 40804
5 AND cust_credit_limit = 10000
6 /

6 rows selected.
n s e
Elapsed: 00:00:00.01
i ce
b l el
Execution Plan
fer a
----------------------------------------------------------
Plan hash value: 2871279522
a n s
n -tr
o
an
--------------------------------------------------------------------
--------------------------------------
| Id | Operation | Name
h s
a deฺ |
Rows | Bytes | Cost (%CPU)| Time |
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------
p ฺ
--------------------------------------
g Stu
| 0 | SELECT STATEMENT
d | |
7 | 1260 | 18
b a@ this
(0)| 00:00:01 |
i
(ja use
| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
7 | 1260 |
|* 2 | b i a18 (0)| 00:00:01 |
to
INDEX RANGE SCAN | CUST_GENDER_LIMIT_CODE_IDX |
6 |
io A | 12 (0)| 00:00:01 |
t o n
--------------------------------------------------------------------
A n --------------------------------------

o s e
J Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("CUST_GENDER"='M' AND "CUST_CREDIT_LIMIT"=10000)


filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
22 consistent gets
14 physical reads
0 redo size
2981 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

Oracle Database 11g: SQL Tuning Workshop A - 71


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> set timing off
SQL> set autotrace off
SQL>

17) Execute the following query:


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

SELECT /*+ INDEX(c) */ c.*


FROM customers c
WHERE cust_gender = 'M'
AND cust_credit_limit = 10000;
What do you observe?
a) The query is almost the same as in the previous step, but the predicate is removed.
The optimizer can still use the concatenated index, but the resulting cost is much e
higher because cust_credit_limit is not very selective. n s
ice
SQL> @query03
b l el
SQL> set echo on
fe ra
SQL>
a n s
SQL> set linesize 200 pagesize 1000
n - tr
SQL> set timing on
o
SQL> set autotrace traceonly
s an
SQL>
) h a deฺ
SQL>
2
SELECT /*+ INDEX(c) */ c.*
FROM customers c
r ฺ e s Gui
= 'M'i nt
3 WHERE cust_gender
p ฺ m d e
4
5
AND
/ d g Stu
cust_credit_limit = 10000

b i a@ this
i a (ja use
4101 rows selected.

A
Elapsed: to
b 00:00:00.08
ni o
ntoExecution Plan
e A ----------------------------------------------------------

J o s Plan hash value: 2871279522

--------------------------------------------------------------------
--------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
--------------------------------------
| 0 | SELECT STATEMENT | |
3469 | 609K| 3454 (1)| 00:00:42 |
| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
3469 | 609K| 3454 (1)| 00:00:42 |
|* 2 | INDEX RANGE SCAN | CUST_GENDER_LIMIT_CODE_IDX |
3469 | | 12 (0)| 00:00:01 |
--------------------------------------------------------------------
--------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 72


Practice 4-1: Using Different Access Paths (continued)
2 - access("CUST_GENDER"='M' AND "CUST_CREDIT_LIMIT"=10000)

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

0 db block gets
4391 consistent gets
0 physical reads
0 redo size
795172 bytes sent via SQL*Net to client
3423 bytes received via SQL*Net from client
275 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4101 rows processed
n s e
i ce
SQL>
b l el
SQL>
SQL> set autotrace off
fer a
SQL> set timing off
a n s
SQL>
o n -tr
18) Execute the following query:
s an
SELECT /*+ INDEX(c) */ c.*
) h a deฺ
FROM customers c
r ฺ e s Gui
mi dent
WHERE cust_gender = 'M'
AND cust_postal_code = 40804;
p ฺ
g Stu
What do you observe? d
a) You replacedja b ia@ this
( u s e is used and with
cust_credit_limit cust_postal_code, which has
b ia to
better selectivity. The index the resulting cost is better.
i A
o @query04
n
SQL>
toSQL> set echo on
e An SQL>
Jos
SQL> set timing on
SQL> set autotrace traceonly
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT /*+ INDEX(c) */ c.*
2 FROM customers c
3 WHERE cust_gender = 'M'
4 AND cust_postal_code = 40804
5 /

75 rows selected.

Elapsed: 00:00:00.02

Execution Plan
----------------------------------------------------------
Plan hash value: 2871279522

--------------------------------------------------------------------
--------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 73


Practice 4-1: Using Different Access Paths (continued)
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
--------------------------------------
| 0 | SELECT STATEMENT | |
45 | 8100 | 133 (1)| 00:00:02 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |


45 | 8100 | 133 (1)| 00:00:02 |
|* 2 | INDEX RANGE SCAN | CUST_GENDER_LIMIT_CODE_IDX |
45 | | 87 (0)| 00:00:02 |
--------------------------------------------------------------------
--------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

n s e
2 - access("CUST_GENDER"='M')
i ce
filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)
b l el
fe ra
Statistics
a n s
-
----------------------------------------------------------
n tr
o
an
1 recursive calls
0 db block gets
196 consistent gets
h s
a deฺ
101 physical reads
e )
s Gui
r ฺ
mi dent
0 redo size

p ฺ
16237 bytes sent via SQL*Net to client
g Stu
d
464 bytes received via SQL*Net from client

b i a@ this
6 SQL*Net roundtrips to/from client
0 sorts (memory)

i a (ja use
0 sorts (disk)

A b to
75 rows processed

o n io
SQL>
nt
e A SQL> set timing off

J o s SQL> set autotrace off


SQL>

19) Execute the following query:


SELECT /*+ INDEX(c) */ c.*
FROM customers c
WHERE cust_postal_code = 40804
AND cust_credit_limit = 10000;
What do you observe?
a) The leading part of the concatenated index is no longer part of the query.
However, the optimizer is still able to use the index by doing a fast full index
scan. Nevertheless, the resulting cost is not the best.
SQL> @query05
SQL> set echo on
SQL>
SQL> set timing on
SQL> set autotrace traceonly
SQL> set linesize 200 pagesize 1000
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 74


Practice 4-1: Using Different Access Paths (continued)
SQL> SELECT /*+ INDEX(c) */ c.*
2 FROM customers c
3 WHERE cust_postal_code = 40804
4 AND cust_credit_limit = 10000
5 /
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

15 rows selected.

Elapsed: 00:00:00.02

Execution Plan
----------------------------------------------------------
Plan hash value: 2438361736

--------------------------------------------------------------------
--------------------------------------
n s e
| Id | Operation | Name |
i ce
Rows | Bytes | Cost (%CPU)| Time |
b l el
--------------------------------------------------------------------
--------------------------------------
fer a
| 0 | SELECT STATEMENT |
a n s |
11 | 1980 | 185 (1)| 00:00:03 |
o n -tr
an
| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
11 | 1980 | 185 (1)| 00:00:03 |
|* 2 | INDEX FULL SCAN
h s
a deฺ
| CUST_GENDER_LIMIT_CODE_IDX |
11 | | 173 (1)| 00:00:03 |
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------

p ฺ
--------------------------------------
g Stuby operation id):
d
Predicate Information
b i a@ this
(identified
---------------------------------------------------

i a (ja use
b to
2 - access("CUST_CREDIT_LIMIT"=10000)
A
n i o filter(TO_NUMBER("CUST_POSTAL_CODE")=40804 AND

nto"CUST_CREDIT_LIMIT"=10000)

e A
J o s Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
189 consistent gets
56 physical reads
0 redo size
4617 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
15 rows processed

SQL>
SQL> set timing off
SQL> set autotrace off
SQL>
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 75


Practice 4-1: Using Different Access Paths (continued)
20) Case 4: Drop all the CUSTOMERS indexes except its primary key index. After this,
create three different bitmap indexes on the following columns of the CUSTOMERS
table:
cust_gender
cust_postal_code
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

cust_credit_limit
SQL> @drop_customers_indexes
SQL> set termout off
SQL>
SQL> @create_cust_gender_bindex
SQL> set echo on
SQL>
SQL> CREATE BITMAP INDEX cust_cust_gender_bidx ON
customers(cust_gender)
n s e
2 NOLOGGING COMPUTE STATISTICS;
i ce
b l el
Index created.
fe r a
SQL>
a n s
SQL> @create_cust_postal_code_bindex
o n -tr
an
SQL> set echo on
SQL>
h s
a deฺ
SQL> CREATE BITMAP INDEX cust_cust_postal_code_bidx ON
customers(cust_postal_code)
e )
s Gui
r ฺ
mi dent
2 NOLOGGING COMPUTE STATISTICS;
p ฺ
g Stu
Index created.
d
b i a@ this
(ja use
SQL>

SQL> setb a
SQL> @create_cust_credit_limit_bindex
i
echo onto
A
io CREATE BITMAP INDEX cust_cust_credit_limit_bidx ON
SQL>
t o n
SQL>
A n customers(cust_credit_limit)

o s e 2 NOLOGGING COMPUTE STATISTICS;


J Index created.

SQL>
SQL> @list_customers_indexes
SQL> set echo on
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT ui.table_name
2 , decode(ui.index_type
3 ,'NORMAL', ui.uniqueness
4 ,ui.index_type) AS index_type
5 , ui.index_name
6 FROM user_indexes ui
7 WHERE ui.table_name = 'CUSTOMERS'
8 ORDER BY ui.table_name
9 , ui.uniqueness desc;

TABLE_NAME INDEX_TYPE
INDEX_NAME

Oracle Database 11g: SQL Tuning Workshop A - 76


Practice 4-1: Using Different Access Paths (continued)
------------------------------ --------------------------- ---------
---------------------
CUSTOMERS UNIQUE
CUSTOMERS_PK
CUSTOMERS BITMAP
CUST_CUST_GENDER_BIDX
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CUSTOMERS BITMAP
CUST_CUST_CREDIT_LIMIT_BIDX
CUSTOMERS BITMAP
CUST_CUST_POSTAL_CODE_BIDX

SQL>

21) Execute the following query:


SELECT /*+ INDEX_COMBINE(c) */ c.*
FROM customers c
n s e
WHERE cust_gender = 'M'
i ce
AND
AND
cust_postal_code = 40804
cust_credit_limit = 10000; b l el
fe r a
What do you observe?
a n s
- tr
a) The optimizer uses only two bitmap indexes to solve this query. However, the
n
o
an
cost is not a good one. It is a little lesser than cost of the full table scan.
s
SQL> @query02
) h a deฺ
SQL> set echo on
r ฺ e s Gui
SQL>
p
SQL> set linesize 200 pagesize ฺ mi1000dent
SQL> set timing on d g Stu
i a@ this
SQL> set autotrace traceonly
b
SQL>
SQL> SELECT (
a ja INDEX_COMBINE(c)
u se
b i /*+
t o
*/ c.*
A cust_gender
2 FROM
3o WHERE
i
customers c
= 'M'
t o n 4 AND cust_postal_code = 40804
A n 5 AND cust_credit_limit = 10000
o s e 6 /
J
6 rows selected.

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 3047829365

--------------------------------------------------------------------
----------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
----------------------------------------
| 0 | SELECT STATEMENT | |
6 | 1080 | 402 (1)| 00:00:05 |
|* 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS |
6 | 1080 | 402 (1)| 00:00:05 |

Oracle Database 11g: SQL Tuning Workshop A - 77


Practice 4-1: Using Different Access Paths (continued)
| 2 | BITMAP CONVERSION TO ROWIDS| |
| | | |
| 3 | BITMAP AND | |
| | | |
|* 4 | BITMAP INDEX SINGLE VALUE| CUST_CUST_CREDIT_LIMIT_BIDX |
| | | |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 5 | BITMAP INDEX SINGLE VALUE| CUST_CUST_GENDER_BIDX |


| | | |
--------------------------------------------------------------------
----------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)
4 - access("CUST_CREDIT_LIMIT"=10000)
n s e
5 - access("CUST_GENDER"='M')
i ce
b l el
Statistics
fe r a
----------------------------------------------------------
a n s
1 recursive calls
n - tr
o
an
0 db block gets
813 consistent gets
5 physical reads
h s
a deฺ
0 redo size
e )
s Gui
r ฺ
mi dent
2570 bytes sent via SQL*Net to client

p ฺ
420 bytes received via SQL*Net from client
g Stu
d
2 SQL*Net roundtrips to/from client

b i a@ this
0 sorts (memory)
0 sorts (disk)

i a (ja use
6 rows processed

A b to
o n io
SQL>
SQL> set autotrace off
nt
e A SQL> set timing off

J o s SQL>
SQL>

22) Case 5: Drop all the CUSTOMERS indexes except its primary key index. After this,
create two bitmap indexes on the following columns of the CUSTOMERS table:
cust_year_of_birth
cust_credit_limit
SQL> @drop_customers_indexes
SQL> set termout off
SQL>
SQL> @create_cust_year_of_birth_bindex
SQL> set echo on
SQL>
SQL> CREATE BITMAP INDEX cust_cust_year_of_birth_bidx
2 ON customers(cust_year_of_birth)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 78


Practice 4-1: Using Different Access Paths (continued)
SQL> @create_cust_credit_limit_bindex
SQL> set echo on
SQL>
SQL> CREATE BITMAP INDEX cust_cust_credit_limit_bidx ON
customers(cust_credit_limit)
2 NOLOGGING COMPUTE STATISTICS;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Index created.

SQL>

23) Execute the following query:


SELECT /*+ INDEX_COMBINE(c) */ c.*
FROM customers c
WHERE c.cust_year_of_birth = 1953
OR c.cust_credit_limit = 10000;
n s e
What do you observe? li ce
b l e
a) The query uses an OR construct. The optimizer can use both bitmap indexes.
f e ra
However, this resulting cost is not the best.
t r a ns
SQL> @query06
o n -
SQL> set echo on
SQL>
s an
SQL> set linesize 200
) h a deฺ
SQL> set timing on
r ฺ e s Gui
SQL> set autotrace traceonly
SQL> p ฺ mi dent
d g Stu*/ c.*
SQL> SELECT /*+ INDEX_COMBINE(c)
2 FROM
i
customers
b a@c this
4 OR a ( ja use
3 WHERE c.cust_year_of_birth = 1953

5 / bi
c.cust_credit_limit = 10000
A to
o
ni rows selected.
nt o7158
eA
Jos
Elapsed: 00:00:00.12

Execution Plan
----------------------------------------------------------
Plan hash value: 1912490408

--------------------------------------------------------------------
-----------------------------------------
| Id | Operation | Name
| Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-----------------------------------------
| 0 | SELECT STATEMENT |
| 7585 | 1333K| 581 (1)| 00:00:07 |
| 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS
| 7585 | 1333K| 581 (1)| 00:00:07 |
| 2 | BITMAP CONVERSION TO ROWIDS|
| | | | |
| 3 | BITMAP OR |
| | | | |

Oracle Database 11g: SQL Tuning Workshop A - 79


Practice 4-1: Using Different Access Paths (continued)
|* 4 | BITMAP INDEX SINGLE VALUE| CUST_CUST_CREDIT_LIMIT_BIDX
| | | | |
|* 5 | BITMAP INDEX SINGLE VALUE| CUST_CUST_YEAR_OF_BIRTH_BIDX
| | | | |
--------------------------------------------------------------------
-----------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Predicate Information (identified by operation id):


---------------------------------------------------

4 - access("C"."CUST_CREDIT_LIMIT"=10000)
5 - access("C"."CUST_YEAR_OF_BIRTH"=1953)

Statistics
----------------------------------------------------------
n s e
1 recursive calls
i ce
0 db block gets
b l el
1683 consistent gets
3 physical reads
fe r a
0 redo size
a n s
1391886 bytes sent via SQL*Net to client
o n -tr
an
5667 bytes received via SQL*Net from client
479 SQL*Net roundtrips to/from client
0 sorts (memory)
h s
a deฺ
0 sorts (disk)
e )
s Gui
7158 rows processed r ฺ
i ent
p ฺ m d
SQL>
SQL> set timing off @ d g S t u
SQL> set autotraceb i aoff this
SQL>
i a (ja use
SQL>
A b to
o
ni 6: Drop all the CUSTOMERS indexes except its primary key index. After this,
24) Case
t o
e An create three different bitmap indexes on the following columns of the CUSTOMERS
Jos
table:
cust_year_of_birth
cust_credit_limit
cust_postal_code
SQL> @drop_customers_indexes
SQL> set termout off
SQL>
SQL> @create_cust_year_of_birth_bindex
SQL> set echo on
SQL>
SQL> CREATE BITMAP INDEX cust_cust_year_of_birth_bidx
2 ON customers(cust_year_of_birth)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>
SQL> @create_cust_credit_limit_bindex
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 80


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> CREATE BITMAP INDEX cust_cust_credit_limit_bidx ON
customers(cust_credit_limit)
2 NOLOGGING COMPUTE STATISTICS;

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

SQL>
SQL> @create_cust_postal_code_bindex
SQL> set echo on
SQL>
SQL> CREATE BITMAP INDEX cust_cust_postal_code_bidx ON
customers(cust_postal_code)
2 NOLOGGING COMPUTE STATISTICS;

Index created.
n s e
ice
SQL>
b l el
25) Execute the following query:
fe r a
SELECT c.*
a n s
FROM customers c
WHERE (c.cust_year_of_birth = '1970' And c.cust_postal_code o n -tr =
40804 ) a n
AND NOT cust_credit_limit = 15000;
h a s eฺ
) i d
What do you observe?
i r ฺes nt Gu
a) The query has a complex WHERE
g p ฺmclauseu e is well suited for using bitmap
dthat
d t
than the full tablei @cost.his S indexes and the resulting cost is better
indexes. The optimizer uses two bitmap
a
scan
( j a b et
i a u s
SQL> @query07
b
A echo ont
SQL> set o
i o
n set timing on
SQL>
ntoSQL>
e A SQL> set autotrace traceonly

J o s SQL>
SQL> SELECT c.*
2 FROM customers c
3 WHERE (c.cust_year_of_birth = '1970' And c.cust_postal_code =
40804 )
4 AND NOT cust_credit_limit = 15000
5 /

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
Plan hash value: 576122600

--------------------------------------------------------------------
------------------------------------------
| Id | Operation | Name
| Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 81


Practice 4-1: Using Different Access Paths (continued)
| 0 | SELECT STATEMENT |
| 1 | 180 | 133 (0)| 00:00:02 |
|* 1 | TABLE ACCESS BY INDEX ROWID | CUSTOMERS
| 1 | 180 | 133 (0)| 00:00:02 |
| 2 | BITMAP CONVERSION TO ROWIDS |
| | | | |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 3 | BITMAP MINUS |
| | | | |
| 4 | BITMAP MINUS |
| | | | |
|* 5 | BITMAP INDEX SINGLE VALUE| CUST_CUST_YEAR_OF_BIRTH_BIDX
| | | | |
|* 6 | BITMAP INDEX SINGLE VALUE| CUST_CUST_CREDIT_LIMIT_BIDX
| | | | |
|* 7 | BITMAP INDEX SINGLE VALUE | CUST_CUST_CREDIT_LIMIT_BIDX
| | | | |
n s e
--------------------------------------------------------------------
i ce
------------------------------------------
b l el
Predicate Information (identified by operation id):
fe r a
---------------------------------------------------
a n s
o n -tr
an
1 - filter(TO_NUMBER("C"."CUST_POSTAL_CODE")=40804)
5 - access("C"."CUST_YEAR_OF_BIRTH"=1970)
6 - access("CUST_CREDIT_LIMIT"=15000)
h s
a deฺ
7 - )
s Gui
access("CUST_CREDIT_LIMIT" IS NULL)
e
r ฺ
i ent
p ฺ m d
Statistics
d g S t u
b
1 recursivea@ calls
t h is
----------------------------------------------------------
i
a blocksegets
0 (jdb
b i
773a t o u gets
consistent

n i o A 30 physical reads

nto redo size

e A 2024 bytes sent via SQL*Net to client

J o s 420
2
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set timing off
SQL> set autotrace off
SQL>

26) Make sure the optimizer can no longer use the bitmap index you created on the
cust_year_of_birth column.
a) The best solution is to render it invisible.
SQL> show parameter optimizer_use_invisible_indexes

NAME TYPE VALUE


------------------------------------ ----------- -------------------
-----------

Oracle Database 11g: SQL Tuning Workshop A - 82


Practice 4-1: Using Different Access Paths (continued)
optimizer_use_invisible_indexes boolean FALSE
SQL> alter index cust_cust_year_of_birth_bidx invisible;

Index altered.

SQL> select index_name, visibility from user_indexes where


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

table_owner='SH' and table_name='CUSTOMERS';

INDEX_NAME VISIBILIT
------------------------------ ---------
CUST_CUST_CREDIT_LIMIT_BIDX VISIBLE
CUST_CUST_YEAR_OF_BIRTH_BIDX INVISIBLE
CUST_CUST_POSTAL_CODE_BIDX VISIBLE
CUSTOMERS_PK VISIBLE

4 rows selected.
n s e
i ce
SQL>
b l el
27) Execute the following query:
fe r a
SELECT c.*
a n s
FROM customers c
WHERE (c.cust_year_of_birth = '1970' And c.cust_postal_code o n -tr =
40804 ) a n
AND NOT cust_credit_limit = 15000;
h a s eฺ
) i d
What do you observe?
i r ฺes nt Gu
a) This is the same query as in the
g p ฺm
previous
u d e However, the optimizer can no
step.
d t
i a @ his S indexes.
longer find a good plan that uses bitmap
SQL> @query07 b
( j aon se t
ia to u
SQL> set echo
b
SQL>
i
SQL>o A timing on
set
n
toSQL> set autotrace traceonly
A n SQL>
o s e SQL> SELECT c.*
J 2 FROM customers c
3 WHERE (c.cust_year_of_birth = '1970' And c.cust_postal_code =
40804 )
4 AND NOT cust_credit_limit = 15000
5 /

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
Plan hash value: 2008213504

--------------------------------------------------------------------
-----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-----------
| 0 | SELECT STATEMENT | | 1 | 180 | 407 (1)|
00:00:05 |

Oracle Database 11g: SQL Tuning Workshop A - 83


Practice 4-1: Using Different Access Paths (continued)
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 1 | 180 | 407 (1)|
00:00:05 |
--------------------------------------------------------------------
-----------

Predicate Information (identified by operation id):


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

---------------------------------------------------

1 - filter(TO_NUMBER("C"."CUST_POSTAL_CODE")=40804 AND
"C"."CUST_YEAR_OF_BIRTH"=1970 AND
"CUST_CREDIT_LIMIT"<>15000)

Statistics
----------------------------------------------------------
393 recursive calls
n s e
0 db block gets
i ce
1590 consistent gets
b l el
0 physical reads
0 redo size
fer a
2024 bytes sent via SQL*Net to client
a n s
420 bytes received via SQL*Net from client
n -tr
o
an
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
h s
a deฺ
1 rows processed
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m d
SQL> set timing off
d g S t u
SQL> set autotrace off
SQL> b i a@ this
a
(jthe se query:
i a
28) Case 7: Execute
b u
following
to
o
SELECTA c.*

t o ni customers
FROM c

e An WHERE cust_id IN (88340,104590,44910);

Jos What do you observe?


a) The optimizer can use the CUSTOMERS primary key index to solve this query.
The cost is very low for the resulting plan.
SQL> @query08
SQL> set echo on
SQL>
SQL> set timing on
SQL> set linesize 200
SQL> set autotrace traceonly
SQL>
SQL> SELECT c.*
2 FROM customers c
3 WHERE cust_id IN (88340,104590,44910)
4 /

Elapsed: 00:00:00.07

Execution Plan
----------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 84


Practice 4-1: Using Different Access Paths (continued)
Plan hash value: 293792914

--------------------------------------------------------------------
-------------------------
| Id | Operation | Name | Rows | Bytes
| Cost (%CPU)| Time |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
-------------------------
| 0 | SELECT STATEMENT | | 3 | 540
| 7 (0)| 00:00:01 |
| 1 | INLIST ITERATOR | | |
| | |
| 2 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS | 3 | 540
| 7 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN | CUSTOMERS_PK | 3 |
| 4 (0)| 00:00:01 |
n s e
--------------------------------------------------------------------
i ce
-------------------------
b l el
Predicate Information (identified by operation id):
fe r a
---------------------------------------------------
a n s
o n -tr
an
3 - access("CUST_ID"=44910 OR "CUST_ID"=88340 OR
"CUST_ID"=104590)
h s
a deฺ
e )
s Gui
Statistics r ฺ
i ent
ฺ m
----------------------------------------------------------
p d
d
2 recursive calls g S t u
3 db block
b i a@gets
9 consistent t h
getsis
a
4 (jphysical sereads
b i
560a redo u
to sent via SQL*Net to client
size
i o A2022 bytes

nton 420 bytes received via SQL*Net from client

e A 2 SQL*Net roundtrips to/from client

J o s 0
0
sorts (memory)
sorts (disk)
1 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

29) Case 8: Drop all the indexes on the CUSTOMERS table except its primary key index.
After this, create a concatenated B*-tree index on the following columns of the
CUSTOMERS table, and in the order here:
cust_last_name
cust_first_name
SQL> @drop_customers_indexes
SQL> set termout off
SQL>
SQL> @create_last_first_name_index
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 85


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> CREATE INDEX cust_last_first_name_idx
2 ON customers(cust_last_name,cust_first_name)
3 NOLOGGING COMPUTE STATISTICS;

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

SQL>

30) Execute the following query:


SELECT c.cust_last_name
, c.cust_first_name
FROM customers c;
What do you observe?
a) The optimizer can use only the index without accessing the table itself. The n s e
resulting cost is very good. i ce
b l el
SQL> @query09
fe ra
SQL> set echo on
a n s
SQL>
SQL> set linesize 200
n - tr
o
an
SQL> set timing on
SQL>
SQL>
set autotrace traceonly
h s
a deฺ
)
s Gui
c.cust_first_name irฺe
SQL>SELECT c.cust_last_name
2
3
,
FROM customers c
p ฺ m dent
4 / d g Stu
b i a@ this
i a (ja use
55500 rows selected.

A
Elapsed: to
b 00:00:00.19
ni o
ntoExecution Plan

e A ----------------------------------------------------------

J o s Plan hash value: 445338993

--------------------------------------------------------------------
-----------------------------
| Id | Operation | Name | Rows |
Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-----------------------------
| 0 | SELECT STATEMENT | | 55500 |
812K| 55 (2)| 00:00:01 |
| 1 | INDEX FAST FULL SCAN| CUST_LAST_FIRST_NAME_IDX | 55500 |
812K| 55 (2)| 00:00:01 |
--------------------------------------------------------------------
-----------------------------

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
3886 consistent gets

Oracle Database 11g: SQL Tuning Workshop A - 86


Practice 4-1: Using Different Access Paths (continued)
193 physical reads
0 redo size
792248 bytes sent via SQL*Net to client
41109 bytes received via SQL*Net from client
3701 SQL*Net roundtrips to/from client
0 sorts (memory)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 sorts (disk)
55500 rows processed

SQL>
SQL>
SQL> set autotrace off
SQL> set timing off
SQL>
SQL>

31) Case 9: Drop all the indexes on the CUSTOMERS table except its primary key index. n s e
i ce
After this, create two B*-tree indexes on the following columns of the CUSTOMERS
b l el
table:
fe ra
cust_last_name
a n s
cust_first_name
n - tr
o
SQL> @drop_customers_indexes
s an
SQL> set termout off
) h a deฺ
s Gui
SQL>
SQL> @create_last_name_index
r ฺ e
SQL> set echo on
p ฺ mi dent
SQL>
d g Stu
SQL> CREATE INDEX cust_cust_last_name_idx
i a@ STATISTICS;
t h is
2 ON customers(cust_last_name)
b
3 NOLOGGING
i a (ja use
COMPUTE

Index A b
created. to
ni o
ntoSQL>
e A SQL> @create_first_name_index

J o s SQL> set echo on


SQL>
SQL> CREATE INDEX cust_cust_first_name_idx
2 ON customers(cust_first_name)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>

32) Execute the following query:


SELECT /*+ INDEX_JOIN(c cust_cust_first_name_idx
cust_cust_last_name_idx) */ c.cust_last_name
, c.cust_first_name
FROM customers c;
What do you observe?
a) Although the optimizer can use both the indexes, the resulting cost is not better
than the concatenated index case.

Oracle Database 11g: SQL Tuning Workshop A - 87


Practice 4-1: Using Different Access Paths (continued)
SQL> @query10
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> SELECT /*+ INDEX_JOIN(c cust_cust_first_name_idx
cust_cust_last_name_idx) */ c.cust_last_name
2 , c.cust_first_name
3 FROM customers c
4 /

55500 rows selected.

Elapsed: 00:00:00.27
n s e
i ce
Execution Plan
b l el
----------------------------------------------------------
fer a
Plan hash value: 3557918892
a n s
n -tr
--------------------------------------------------------------------
o
an
-------------------------------
| Id | Operation
Bytes | Cost (%CPU)| Time |
| Name
h s
a deฺ
| Rows |

)
s Gui
--------------------------------------------------------------------
e
r ฺ
mi dent
-------------------------------
| 0 | SELECT STATEMENT
p ฺ
g Stu
| | 55500 |
812K| 511
d
(1)| 00:00:07 |
| 1 | VIEW
b i a@ this | index$_join$_001 | 55500 |

(ja use
812K| 511 (1)| 00:00:07 |
|* 2 |
| b i a
HASH JOIN
| to |
| | |

|
io
3 | A INDEX FAST FULL SCAN| CUST_CUST_FIRST_NAME_IDX | 55500 |
t o n
812K| 175 (1)| 00:00:03 |
A n | 4 | INDEX FAST FULL SCAN| CUST_CUST_LAST_NAME_IDX | 55500 |

o s e 812K| 178 (0)| 00:00:03 |


J --------------------------------------------------------------------
-------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access(ROWID=ROWID)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
3985 consistent gets
279 physical reads
0 redo size
811440 bytes sent via SQL*Net to client
41109 bytes received via SQL*Net from client
3701 SQL*Net roundtrips to/from client
0 sorts (memory)

Oracle Database 11g: SQL Tuning Workshop A - 88


Practice 4-1: Using Different Access Paths (continued)
0 sorts (disk)
55500 rows processed

SQL>
SQL>
SQL> set autotrace off
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set timing off


SQL>
SQL>

33) CASE 10: Drop all the indexes on the CUSTOMERS table except its primary key
index. Then, create one bitmap index on the following column of the CUSTOMERS
table:
cust_credit_limit
SQL> @drop_customers_indexes
n s e
i ce
el
SQL> set termout off
SQL>
a b l
SQL> @create_cust_credit_limit_bindex
fer
SQL> set echo on
a n s
-tr
SQL>
SQL> CREATE BITMAP INDEX cust_cust_credit_limit_bidx ON
o n
customers(cust_credit_limit)
2 NOLOGGING COMPUTE STATISTICS; s an
) h a deฺ
Index created.
r ฺ e s Gui
SQL> p ฺ mi dent
d g Stu
34) Execute the followinga
i @ his
query:
SELECT count(*)
j a b e t
credit_limit
FROM (
ia to u = 10000;
customers s
A b
WHERE cust_credit_limit
n i
What
o do you observe?
n to
e A a) Although cust_credit_limit is not a selective column, the COUNT
Jos operation on its bitmap index is very efficient.
SQL> @query11
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> SELECT count(*) credit_limit
2 FROM customers
3 WHERE cust_credit_limit = 10000
4 /

Elapsed: 00:00:00.00

Execution Plan
----------------------------------------------------------
Plan hash value: 37937133

Oracle Database 11g: SQL Tuning Workshop A - 89


Practice 4-1: Using Different Access Paths (continued)
--------------------------------------------------------------------
---------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
---------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 0 | SELECT STATEMENT | |
1 | 4 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | |
1 | 4 | | |
| 2 | BITMAP CONVERSION COUNT | |
6938 | 27752 | 1 (0)| 00:00:01 |
|* 3 | BITMAP INDEX SINGLE VALUE| CUST_CUST_CREDIT_LIMIT_BIDX |
| | | |
--------------------------------------------------------------------
---------------------------------------
n s e
i ce
Predicate Information (identified by operation id):
b l el
---------------------------------------------------
fe r a
3 - access("CUST_CREDIT_LIMIT"=10000)
a n s
o n -tr
Statistics
s an
h a deฺ
----------------------------------------------------------
)
1 recursive calls
r ฺ e s Gui
3 consistent getsฺmi nt
0 db block gets

p
g Stu d e
d
2 physical reads
@ via
0 redo size
b
423 bytes i asent t h isSQL*Net to client
a received
420 (jbytes e via SQL*Net from client
sroundtrips
i a
b 0 sorts
2 u
to (memory) to/from client
SQL*Net
io A
t o n 0 sorts (disk)
A n 1 rows processed

o s e
J SQL>
SQL>
SQL> set autotrace off
SQL> set timing off
SQL>
SQL>

35) Case 11: Drop all the CUSTOMERS indexes except its primary key index. After this,
create one B*-tree index on the following column of the CUSTOMERS table:
cust_credit_limit
SQL> @drop_customers_indexes
SQL> set termout off
SQL>
SQL> @create_cust_credit_limit_index
SQL> set echo on
SQL>
SQL> CREATE INDEX cust_cust_credit_limit_idx
2 ON customers(cust_credit_limit)
3 NOLOGGING COMPUTE STATISTICS;

Oracle Database 11g: SQL Tuning Workshop A - 90


Practice 4-1: Using Different Access Paths (continued)
Index created.

SQL>

36) Execute the following query:


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

SELECT count(*) credit_limit


FROM customers
WHERE cust_credit_limit = 10000;
What do you observe?
a) The optimizer can still use the index; however, this is less efficient compared to
the corresponding bitmap index from the previous case.
SQL> @query11
SQL> set echo on
n s e
i ce
el
SQL>
SQL> set linesize 200
a b l
SQL> set timing on
fe r
SQL> set autotrace traceonly
a n s
-tr
SQL>
SQL> SELECT count(*) credit_limit
o n
an
2 FROM customers
3
4
WHERE cust_credit_limit = 10000
/ h s
a deฺ
e )
s Gui
r ฺ
Elapsed: 00:00:00.00
p ฺ mi dent
Execution Plan d g Stu
a@ this
Plan hash value:bi3421880850
----------------------------------------------------------

i a (ja use
A b to
--------------------------------------------------------------------
i o
----------------------------

nto|n Id | Operation | Name | Rows |

e A Bytes | Cost (%CPU)| Time |

J o s --------------------------------------------------------------------
----------------------------
| 0 | SELECT STATEMENT | | 1 |
4 | 14 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 |
4 | | |
|* 2 | INDEX RANGE SCAN| CUST_CUST_CREDIT_LIMIT_IDX | 6938 |
27752 | 14 (0)| 00:00:01 |
--------------------------------------------------------------------
----------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("CUST_CREDIT_LIMIT"=10000)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets

Oracle Database 11g: SQL Tuning Workshop A - 91


Practice 4-1: Using Different Access Paths (continued)
14 consistent gets
13 physical reads
0 redo size
423 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL>
SQL> set autotrace off
SQL> set timing off
SQL>
SQL>
n s e
i ce
37) Case 12: Drop all the indexes on the CUSTOMERS table except its primary key index.
After this, create one B*-tree index on the following column of the CUSTOMERS b l el
fe r a
table: s
cust_last_name tra n
o n -
SQL> @drop_customers_indexes
SQL> set termout off s an
SQL>
) h a deฺ
SQL> @create_last_name_index
r ฺ e s Gui
SQL> set echo on
SQL> p ฺ mi dent
d g Stu
SQL> CREATE INDEX cust_cust_last_name_idx
ia@ STATISTICS;
h is
2 ON customers(cust_last_name)
b
3 NOLOGGINGaCOMPUTE t
i a (j u se
A b
Index created. to
ni o
ntoSQL>
e A
J o s 38) Execute the following query:
SELECT cust_id, country_id
FROM customers
WHERE LOWER( cust_last_name) LIKE 'gentle';
What do you observe?
a) Although there is an index, it cannot be used because its column is modified by a
function.
SQL> @query12
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> SELECT cust_id, country_id
2 FROM customers
3 WHERE LOWER( cust_last_name) LIKE 'gentle'
4 /

Oracle Database 11g: SQL Tuning Workshop A - 92


Practice 4-1: Using Different Access Paths (continued)
80 rows selected.

Elapsed: 00:00:00.01

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

----------------------------------------------------------
Plan hash value: 2008213504

--------------------------------------------------------------------
-----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-----------
| 0 | SELECT STATEMENT | | 555 | 9990 | 406 (1)|
n s e
00:00:05 |
i ce
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 555 | 9990 | 406 (1)|
b l el
00:00:05 |
--------------------------------------------------------------------
fe r a
-----------
a n s
o n -tr
an
Predicate Information (identified by operation id):
---------------------------------------------------
h s
a deฺ
)
s Gui
1 - filter(LOWER("CUST_LAST_NAME")='gentle')
e
r ฺ
i ent
p ฺ m d
Statistics
d g S t u
b
1 recursivea@ calls
t h is
----------------------------------------------------------
i
a blocksegets
0 (jdb
b i
1464a t o u gets
consistent

n i o A 00 physical reads

nto redo size

e A 2077 bytes sent via SQL*Net to client

J o s 475
7
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
80 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

39) How can you enhance the performance of the previous query without modifying the
statement itself? Implement your solution.
a) You can create a function-based index.
SQL> @create_lower_cust_last_name_index
SQL> set echo on
SQL>
SQL> CREATE INDEX lower_cust_last_name_idx ON
2 customers(LOWER(cust_last_name))
3 /

Oracle Database 11g: SQL Tuning Workshop A - 93


Practice 4-1: Using Different Access Paths (continued)
Index created.

SQL>

40) Check if your solution executes faster than in the case of the query in step 38.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> @query12
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> SELECT cust_id, country_id
2 FROM customers
n s e
ce
3 WHERE LOWER( cust_last_name) LIKE 'gentle'
4 /
eli
a b l
80 rows selected.
fe r
a n s
Elapsed: 00:00:00.00
o n -tr
Execution Plan
s an
) h a deฺ
----------------------------------------------------------

s Gui
Plan hash value: 967065894
r ฺ e
p ฺ mi dent
--------------------------------------------------------------------

| Id | Operation d g Stu | Name


------------------------------------
|
@ s
Rows | Bytes | Cost
j a b ia
e thi
(%CPU)| Time |

i a ( u s
--------------------------------------------------------------------
------------------------------------
| 0A b t o
| SELECT STATEMENT | |
i o
to|n 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS
555 | 17760 | 41 (0)| 00:00:01 |

A n |

o s e 555 | 17760 |
|* 2 |
41 (0)| 00:00:01 |
INDEX RANGE SCAN | LOWER_CUST_LAST_NAME_IDX |
J 222 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------
------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access(LOWER("CUST_LAST_NAME")='gentle')

Statistics
----------------------------------------------------------
24 recursive calls
0 db block gets
23 consistent gets
1 physical reads
0 redo size
2077 bytes sent via SQL*Net to client
475 bytes received via SQL*Net from client
7 SQL*Net roundtrips to/from client

Oracle Database 11g: SQL Tuning Workshop A - 94


Practice 4-1: Using Different Access Paths (continued)
0 sorts (memory)
0 sorts (disk)
80 rows processed

SQL>
SQL> set autotrace off
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set timing off


SQL>
SQL>

41) Case 13: Execute the iot_setup.sql script to set up the environment for this
case.
SQL> @iot_setup
SQL> set echo on
SQL>
n s e
SQL> CREATE table promotions_iot
i ce
2 (promo_id number primary key
b l el
3
4
, promo_name VARCHAR2(40)
, promo_subcategory VARCHAR2 (30)
fe ra
5 , promo_category VARCHAR2 (30)
a n s
6
7
, promo_cost NUMBER
, promo_begin_date DATE o n -tr
8 , promo_end_date DATE)
s an
9 ORGANIZATION INDEX
) h a deฺ
10 /
r ฺ e s Gui
Table created.
p ฺ mi dent
d g Stu
SQL>
a@ this
SQL> INSERT INTO ipromotions_iot
b
2 SELECT (
a ja usepromo_name, promo_subcategory, promo_category,
promo_id,
i
promo_cost,
to
b promotions
promo_begin_date, promo_end_date
A
i4o /
3 FROM

t o n
A n
o s e 503 rows created.
J SQL>

42) Execute the following two queries:


SELECT *
FROM promotions
WHERE promo_id > 300;

SELECT /*+ INDEX(promotions) */ *


FROM promotions
WHERE promo_id > 300;
What do you observe?
a) The first lets the optimizer decide the plan, and the best it can find is to do a full
table scan. Forcing the use of the index is not a good idea, as it takes longer to
execute.
SQL> @query13
SQL> set echo on
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 95


Practice 4-1: Using Different Access Paths (continued)
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> SELECT *
2 FROM promotions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 WHERE promo_id > 300


4 /

235 rows selected.

Elapsed: 00:00:00.02

Execution Plan
----------------------------------------------------------
Plan hash value: 4106015420
n s e
i ce
--------------------------------------------------------------------
b l el
------------
| Id | Operation | Name | Rows | Bytes | Cost
fer a
(%CPU)| Time |
a n s
-tr
--------------------------------------------------------------------
n
o
an
------------
| 0 | SELECT STATEMENT | | 364 | 35308 | 17
(0)| 00:00:01 |
h s
a deฺ
|* 1 | TABLE ACCESS FULL| PROMOTIONS |
e )
s Gui
364 | 35308 | 17
r ฺ
mi dent
(0)| 00:00:01 |

p
g Stuฺ
--------------------------------------------------------------------
------------
d
b i
Predicate Informationa@(identified
t h is by operation id):
i a (ja use
---------------------------------------------------

A b to
i o
1 - filter("PROMO_ID">300)

nton
e A
J o s Statistics
----------------------------------------------------------
123 recursive calls
0 db block gets
88 consistent gets
3 physical reads
0 redo size
21829 bytes sent via SQL*Net to client
585 bytes received via SQL*Net from client
17 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
235 rows processed

SQL>
SQL> SELECT /*+ INDEX(promotions) */ *
2 FROM promotions
3 WHERE promo_id > 300
4 /

235 rows selected.

Oracle Database 11g: SQL Tuning Workshop A - 96


Practice 4-1: Using Different Access Paths (continued)
Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 4044283270
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
----------------------
| Id | Operation | Name | Rows | Bytes |
Cost (%CPU)| Time |
--------------------------------------------------------------------
----------------------
| 0 | SELECT STATEMENT | | 364 | 35308 |
353 (0)| 00:00:05 |
| 1 | TABLE ACCESS BY INDEX ROWID| PROMOTIONS | 364 | 35308 |
n s e
353 (0)| 00:00:05 |
i ce
|* 2 | INDEX RANGE SCAN | PROMO_PK | 364 | |
b l el
1 (0)| 00:00:01 |
--------------------------------------------------------------------
fe r a
----------------------
a n s
o n -tr
an
Predicate Information (identified by operation id):
---------------------------------------------------
h s
a deฺ
2 - access("PROMO_ID">300)
e )
s Gui
r ฺ
i ent
p ฺ m d
Statistics
d g S t u
b
1 recursivea@ calls
t h is
----------------------------------------------------------
i
a blocksegets
0 (jdb
b i
243a t o u gets
consistent

n i o A 10 physical reads

nto redo size

e A 27052 bytes sent via SQL*Net to client

J o s 585
17
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
235 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

43) Execute the following query:


SELECT *
FROM promotions_iot
WHERE promo_id > 300;
What do you observe?
a) The optimizer directly uses the index-organized structure, which is extremely
efficient in this case compared to the previous step.
SQL> @query14

Oracle Database 11g: SQL Tuning Workshop A - 97


Practice 4-1: Using Different Access Paths (continued)
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SELECT *
2 FROM promotions_iot
3 WHERE promo_id > 300
4 /

235 rows selected.

Elapsed: 00:00:00.00

Execution Plan
n s e
----------------------------------------------------------
i ce
Plan hash value: 1463021396
b l el
fer
-------------------------------------------------------------------- a
------------------
a n s
| Id | Operation | Name -tr
| Rows | Bytes | Cost
n
o
an
(%CPU)| Time |
--------------------------------------------------------------------
------------------
h s
a deฺ
| 0 | SELECT STATEMENT |
e )
s Gui | 235 | 23500 | 2
r ฺ
mi dent
(0)| 00:00:01 |

p ฺ
|* 1 | INDEX RANGE SCAN| SYS_IOT_TOP_72170 |
g Stu
235 | 23500 | 2
(0)| 00:00:01 |
d
b
------------------ i a@ this
--------------------------------------------------------------------

i a (ja use(identified by operation id):


b
Predicate
A to
Information
o
ni 1 - access("PROMO_ID">300)
---------------------------------------------------

n t o
e A
Jos Note
-----
- dynamic sampling used for this statement

Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
42 consistent gets
0 physical reads
116 redo size
19922 bytes sent via SQL*Net to client
585 bytes received via SQL*Net from client
17 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
235 rows processed

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 98


Practice 4-1: Using Different Access Paths (continued)
SQL> set autotrace off
SQL> set timing off
SQL>

44) Execute the iot_cleanup.sql script to clean up your environment.


SQL> @iot_cleanup
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set echo on


SQL>
SQL> drop table promotions_iot purge;

Table dropped.

SQL>
SQL>

45) Case 14: Execute shc_setup.sql to set up your environment for this lab. n s e
i ce
SQL> @shc_setup
b l el
SQL> set echo on
fer a
SQL>
a n s
SQL> set linesize 200
SQL>
n - tr
o
an
SQL> drop cluster bigemp_cluster including tables;
drop cluster bigemp_cluster including tables
* h s
a deฺ
ERROR at line 1:
e )
s Gui
r ฺ
ORA-00943: cluster does not exist
p ฺ mi dent
d g Stu
SQL>
b i a@ t h is
a
2 (deptno(jnumber,
SQL> CREATE CLUSTER
e number sort)
bigemp_cluster
ssal
b i a u
toHASH IS deptno SIZE 50
o A
3 HASHKEYS 10000

t o ni5 tablespace users;


4 single table

An
e
Jos
Cluster created.

SQL>
SQL> create table bigemp_fact (
2 empno number primary key, sal number sort, job varchar2(12) not
null,
3 deptno number not null, hiredate date not null)
4 CLUSTER bigemp_cluster (deptno, sal);

Table created.

SQL>
SQL>
SQL> begin
2 for i in 1..1400000 loop
3 insert into bigemp_fact values(i,i,'J1',10,sysdate);
4 end loop;
5 commit;
6 end;
7 /

Oracle Database 11g: SQL Tuning Workshop A - 99


Practice 4-1: Using Different Access Paths (continued)
PL/SQL procedure successfully completed.

SQL>
SQL> begin
2 for i in 1..1400000 loop
3 insert into bigemp_fact values(1400000+i,i,'J1',20,sysdate);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> exec dbms_stats.gather_schema_stats('SH');
n s e
ice
PL/SQL procedure successfully completed.
b l el
SQL>
fe r a
a n s
46) Execute the query15.sql script. What do you observe?
o n -tr
a) Because you may have a lot of memory on your system, n the script first reduces
a optimizer
the amount of memory available to your session.
h a sThe
e ฺ decides to use the
s) costG
cluster access path to retrieve the data. The ui d
is minimal.
r ฺ e
SQL> @query15
p ฺ mi dent
SQL> set echo on
d g Stu
SQL>
SQL> set linesizeia
@ his
a on se t
b 200
SQL> set timing( j
a to utraceonly
SQL> set iautotrace
SQL> A
b
o n io alter session set workarea_size_policy=manual;
SQL>
nt
e A Session altered.
J o s
Elapsed: 00:00:00.02
SQL> alter session set sort_area_size=50000;

Session altered.

Elapsed: 00:00:00.01
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.16
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:06.69
SQL>
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 100


Practice 4-1: Using Different Access Paths (continued)
SQL> select * from bigemp_fact where deptno=10;

1400000 rows selected.

Elapsed: 00:00:07.21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Execution Plan
----------------------------------------------------------
Plan hash value: 865757019

--------------------------------------------------------------------
-------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------
n s e
| 0 | SELECT STATEMENT | | 1400K| 32M| 1
ice
(0)| 00:00:01 |
b l el
|* 1 | TABLE ACCESS HASH| BIGEMP_FACT | 1400K|
(0)| 00:00:01 |
32M| 1
fe ra
--------------------------------------------------------------------
a n s
-------------
o n -tr
Predicate Information (identified by operation
s a nid):
h a deฺ
---------------------------------------------------
)
r ฺ e s Gui
mi dent
1 - access("DEPTNO"=10)

p ฺ
g Stu
d
Statistics
b i a@ this
----------------------------------------------------------
a
689 (jrecursive segets
calls
b i a
0 db
to u
block
i o A99451 consistent gets
to n 5999 physical reads
A n 116 redo size

o s e 33605911 bytes sent via SQL*Net to client


J 1027083
93335
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
10 sorts (memory)
0 sorts (disk)
1400000 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

47) Execute the query16.sql script. What do you observe?


a) Again, the script first ensures that the amount of memory available to your session
is reduced. Then the script executes the same query, but asks for ordering the
result based on the sorted sal column. The optimizer can still use the cluster
access path without sorting the data. The cost is still minimal.
SQL> @query16
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 101


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> set linesize 200
SQL> set timing on
SQL> set autotrace traceonly
SQL>
SQL> alter session set workarea_size_policy=manual;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Session altered.

Elapsed: 00:00:00.00
SQL> alter session set sort_area_size=50000;

Session altered.

Elapsed: 00:00:00.00
SQL>
n s e
SQL> alter system flush shared_pool;
i ce
b l el
System altered.
fe r a
Elapsed: 00:00:00.10
a n s
SQL> alter system flush buffer_cache;
o n -tr
System altered.
s an
) h a deฺ
Elapsed: 00:00:00.04
r ฺ e s Gui
miwherededeptno=10
nt
SQL>
SQL> select * from bigemp_fact
g p ฺ u
order by sal;
d S t
@ s
thi
1400000 rows selected.

a b ia
j e
ia ( to us
Elapsed: 00:00:07.41
b
i o A Plan
Execution
n
ntoPlan hash value: 865757019
----------------------------------------------------------

e A
J o s --------------------------------------------------------------------
-------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------
| 0 | SELECT STATEMENT | | 1400K| 32M| 1
(0)| 00:00:01 |
|* 1 | TABLE ACCESS HASH| BIGEMP_FACT | 1400K| 32M| 1
(0)| 00:00:01 |
--------------------------------------------------------------------
-------------

Predicate Information (identified by operation id):


---------------------------------------------------

1 - access("DEPTNO"=10)

Statistics

Oracle Database 11g: SQL Tuning Workshop A - 102


Practice 4-1: Using Different Access Paths (continued)
----------------------------------------------------------
1090 recursive calls
10 db block gets
99512 consistent gets
6012 physical reads
0 redo size
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

33605911 bytes sent via SQL*Net to client


1027083 bytes received via SQL*Net from client
93335 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
1400000 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
n s e
SQL>
ice
48) Execute the query17.sql script. What do you observe? b l el
fe ra
a n s
a) Again, the script first ensures that the amount of memory available to your session
n - tr
is reduced. Then the script executes the same query, but asks to order the result
o
based on the sorted sal column in the descending order. The optimizer can still
an
use the cluster access path without sorting the data. The cost is still minimal.
s
) h a deฺ
SQL> @query17
r ฺ e s Gui
mi dent
SQL> set echo on
SQL>
SQL> set linesize 200 dg p ฺ
SQL> set timing on @ S tu
b i atraceonly
t h is
(ja use
SQL> set autotrace
SQL>
i a
b sessionto set workarea_size_policy=manual;
A
SQL> alter
o
ni
toSession altered.
A n
o s e Elapsed: 00:00:00.00
J SQL> alter session set sort_area_size=50000;

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.09
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:00.12
SQL>
SQL> select * from bigemp_fact where deptno=10 order by sal desc;

1400000 rows selected.

Oracle Database 11g: SQL Tuning Workshop A - 103


Practice 4-1: Using Different Access Paths (continued)
Elapsed: 00:00:07.35

Execution Plan
----------------------------------------------------------
Plan hash value: 865757019
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
-------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------
| 0 | SELECT STATEMENT | | 1400K| 32M| 1
(0)| 00:00:01 |
|* 1 | TABLE ACCESS HASH| BIGEMP_FACT | 1400K| 32M| 1
n s e
(0)| 00:00:01 |
ice
--------------------------------------------------------------------
b l el
-------------
fe r a
Predicate Information (identified by operation id):
a n s
---------------------------------------------------
o n -tr
1 - access("DEPTNO"=10)
s an
) h a deฺ
r ฺ e s Gui
Statistics
ฺ m i ent
----------------------------------------------------------
d
1090 recursive calls g p t u d
10 db block
a@gets is S
a b i
99509 consistent t h
gets
6005 (jphysical
i a u sereads
A b bytes
0
to sent via SQL*Net to client
redo size
i o
33605911

nton 1027083 bytes received via SQL*Net from client

e A 93335 SQL*Net roundtrips to/from client

J o s 12
0
sorts (memory)
sorts (disk)
1400000 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

49) Execute the query18.sql script. What do you observe?


a) Again, the script first ensures that the amount of memory available to your session
is reduced. Then the script executes the same query but this time asks to order the
result based on the nonsorted empno column. The optimizer can still make use of
the cluster access path, but must sort the data making the cost of the query higher.
SQL> @query18
SQL> set echo on
SQL>
SQL> set linesize 200
SQL> set timing on

Oracle Database 11g: SQL Tuning Workshop A - 104


Practice 4-1: Using Different Access Paths (continued)
SQL> set autotrace traceonly
SQL>
SQL> alter session set workarea_size_policy=manual;

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

Elapsed: 00:00:00.00
SQL> alter session set sort_area_size=50000;

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL> alter system flush shared_pool;

System altered.
n s e
i ce
Elapsed: 00:00:00.10
b l el
SQL> alter system flush buffer_cache;
fer a
System altered.
a n s
o n -tr
an
Elapsed: 00:00:00.04
SQL>
h s
a deฺ
SQL> select * from bigemp_fact where deptno=10 order by empno;

e )
s Gui
1400000 rows selected. r ฺ
i ent
p ฺ m
Elapsed: 00:00:10.01
d g Stud
Execution Plan bia
@ his
j a e t
( s
----------------------------------------------------------
u
iavalue:to1775608660
b
Plan hash

n i oA
nto----------------------
--------------------------------------------------------------------

e A
J o s | Id | Operation
Cost (%CPU)| Time |
| Name | Rows | Bytes |TempSpc|

--------------------------------------------------------------------
----------------------
| 0 | SELECT STATEMENT | | 1400K| 32M| |
47728 (1)| 00:09:33 |
| 1 | SORT ORDER BY | | 1400K| 32M| 107M|
47728 (1)| 00:09:33 |
|* 2 | TABLE ACCESS HASH| BIGEMP_FACT | 1400K| 32M| |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
----------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("DEPTNO"=10)

Statistics
----------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 105


Practice 4-1: Using Different Access Paths (continued)
1139 recursive calls
12 db block gets
6178 consistent gets
12238 physical reads
0 redo size
33605911 bytes sent via SQL*Net to client
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1027083 bytes received via SQL*Net from client


93335 SQL*Net roundtrips to/from client
12 sorts (memory)
1 sorts (disk)
1400000 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>
n s e
ice
50) Execute the query19.sql script. What do you observe?
b l el
r a
a) Again, the script first ensures that the amount of memory available to your session
fe
n s
is reduced. Then the script executes the same query, but this time asks to order the
a
- tr
result based on the sal, deptno key. The optimizer can still make use of the
n
o
an
cluster access path, but must sort the data making the cost of the query higher.
s
SQL> @query19
) h a deฺ
SQL> set echo on
r ฺ e s Gui
mi dent
SQL>
SQL> set linesize 200
p ฺ
g Stu
SQL> set timing on d
b i a@ this
SQL> set autotrace traceonly

(ja usetseworkarea_size_policy=manual;
SQL>

i a
SQL> alter session
baltered.to
i o A
Session

nton
e A Elapsed: 00:00:00.00

J o s SQL> alter session set sort_area_size=50000;

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.10
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:00.09
SQL>
SQL> select * from bigemp_fact where deptno=10 order by sal,empno;

1400000 rows selected.

Oracle Database 11g: SQL Tuning Workshop A - 106


Practice 4-1: Using Different Access Paths (continued)
Elapsed: 00:00:09.25

Execution Plan
----------------------------------------------------------
Plan hash value: 1775608660
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
----------------------
| Id | Operation | Name | Rows | Bytes |TempSpc|
Cost (%CPU)| Time |
--------------------------------------------------------------------
----------------------
| 0 | SELECT STATEMENT | | 1400K| 32M| |
47728 (1)| 00:09:33 |
| 1 | SORT ORDER BY | | 1400K| 32M| 107M|
47728 (1)| 00:09:33 |
n s e
|* 2 | TABLE ACCESS HASH| BIGEMP_FACT | 1400K| 32M| |
i ce
1 (0)| 00:00:01 |
b l el
--------------------------------------------------------------------
----------------------
fer a
a n s
Predicate Information (identified by operation id):
o n -tr
an
---------------------------------------------------

2 - access("DEPTNO"=10)
h s
a deฺ
e )
s Gui
r ฺ
i ent
Statistics
p ฺ m d
d g t u
----------------------------------------------------------
S
12 db b i a@gets
1139 recursive
block
calls
t h is
a sereads
6178 (jconsistent gets
i a
b 0 redotosize
12238 u
physical
i o A
nton 33605911 bytes sent via SQL*Net to client

e A 1027083 bytes received via SQL*Net from client

J o s 93335
12
SQL*Net roundtrips to/from client
sorts (memory)
1 sorts (disk)
1400000 rows processed

SQL>
SQL> set autotrace off
SQL> set timing off
SQL>

51) Execute the shc_cleanup.sql script to clean up your environment.


SQL> @shc_cleanup
SQL> set echo on
SQL>
SQL> drop cluster bigemp_cluster including tables;

Cluster dropped.

SQL>
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 107


Practice 4-1: Using Different Access Paths (continued)
52) Case 15: Execute the nic_setup.sql script to set up your environment for this
case.
SQL> @nic_setup
SQL> set echo on
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> drop cluster emp_dept including tables;


drop cluster emp_dept including tables
*
ERROR at line 1:
ORA-00943: cluster does not exist

SQL>
SQL> drop table emp purge;
drop table emp purge
n s e
*
i ce
ERROR at line 1:
b l el
ORA-00942: table or view does not exist
fer a
a n s
SQL> drop table dept purge;
n - tr
o
an
drop table dept purge
*
ERROR at line 1:
h s
a deฺ
e
ORA-00942: table or view does not exist )
s Gui
r ฺ
p ฺ mi dent
SQL> d g Stu
SQL>
i @( his
aemp
SQL> CREATE TABLE
j b
a NUMBER(7)e t
2 ( u s
ia toVARCHAR2(15)
empno ,
3 b
4o A job
ename NOT NULL,

o n i VARCHAR2(9) ,

nt 6 hiredate DATE
5 mgr NUMBER(7) ,

e A ,

Jos
7 sal NUMBER(7) ,
8 comm NUMBER(7) ,
9 deptno NUMBER(3)
10 );

Table created.

SQL>
SQL> CREATE TABLE dept (
2 deptno NUMBER(3) ,
3 dname VARCHAR2(14),
4 loc VARCHAR2(14),
5 c VARCHAR2(500)
6 );

Table created.

SQL>
SQL> CREATE INDEX emp_index
2 ON emp(deptno)
3 TABLESPACE users

Oracle Database 11g: SQL Tuning Workshop A - 108


Practice 4-1: Using Different Access Paths (continued)
4 STORAGE (INITIAL 50K
5 NEXT 50K
6 MINEXTENTS 2
7 MAXEXTENTS 10
8 PCTINCREASE 33);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Index created.

SQL>
SQL> CREATE INDEX dept_index
2 ON dept(deptno)
3 TABLESPACE users
4 STORAGE (INITIAL 50K
5 NEXT 50K
6 MINEXTENTS 2
7 MAXEXTENTS 10
n s e
8 PCTINCREASE 33);
i ce
b l el
Index created.
fer a
SQL>
a n s
SQL>
n - tr
o
an
SQL> begin
2 for i in 1..999 loop
3 insert into dept values
h s
a deฺ
)
s Gui
(i,'D'||i,'L'||i,dbms_random.string('u',500));
e
r ฺ
mi dent
4 end loop;
5 commit;
p ฺ
g Stu
6 end;
d
7 /
b i a@ this
i a (ja successfully
PL/SQL procedure
u se completed.

SQL> A
b to
n i o
t oSQL>
n SQL> begin
e A
Jos
2 for i in 1..500000 loop
3 insert into emp values
(i,dbms_random.string('u',15),dbms_random.string('u',9),i,sysdate,i,
i,mod(i,999));
4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL> exec dbms_stats.gather_schema_stats('SH');

PL/SQL procedure successfully completed.

SQL>

53) Execute the nic_query.sql script. What do you observe?

Oracle Database 11g: SQL Tuning Workshop A - 109


Practice 4-1: Using Different Access Paths (continued)
a) The script first ensures that the amount of memory available to your session is
reduced. Then the script executes a join between the EMP and DEPT tables. The
optimizer is able to make use of the index to resolve the join.
SQL> @nic_query
SQL> set echo on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> set timing on
SQL> set autotrace traceonly
SQL> set linesize 200
SQL>
SQL> alter session set workarea_size_policy=manual;

Session altered.

Elapsed: 00:00:00.00
n s e
i ce
el
SQL> alter session set sort_area_size=50000;

a b l
Session altered.
fe r
a n s
-tr
Elapsed: 00:00:00.00
SQL> alter session set hash_area_size=5000;
o n
Session altered. s an
) h a deฺ
Elapsed: 00:00:00.00
r ฺ e s Gui
SQL>
SQL> p ฺ mi dent
d g where
SQL> select * from emp,dept
S tuemp.deptno=dept.deptno and
emp.deptno > 800; a@ is
b i t h
(ja use
99000 rowsaselected.
i
A b to
i o
ton
Elapsed: 00:00:02.88

A n Execution Plan
o s e ----------------------------------------------------------
J Plan hash value: 128236434

--------------------------------------------------------------------
-------------------------------
| Id | Operation | Name | Rows | Bytes
|TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------
-------------------------------
| 0 | SELECT STATEMENT | | 19780 | 10M|
| 3449 (1)| 00:00:42 |
|* 1 | HASH JOIN | | 19780 | 10M|
104K| 3449 (1)| 00:00:42 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 199 | 99K|
| 18 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | DEPT_INDEX | 199 | |
| 2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | EMP | 99198 | 5521K|
| 1207 (2)| 00:00:15 |
--------------------------------------------------------------------
-------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 110


Practice 4-1: Using Different Access Paths (continued)
Predicate Information (identified by operation id):
---------------------------------------------------

1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")
3 - access("DEPT"."DEPTNO">800)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4 - filter("EMP"."DEPTNO">800)

Statistics
----------------------------------------------------------
9 recursive calls
0 db block gets
4365 consistent gets
10146 physical reads
0 redo size
n s e
57968984 bytes sent via SQL*Net to client
i ce
73009 bytes received via SQL*Net from client
b l el
6601 SQL*Net roundtrips to/from client
0 sorts (memory)
fer a
0 sorts (disk)
a n s
99000 rows processed
o n -tr
SQL>
s an
SQL> set autotrace off
) h a deฺ
SQL> set timing off;
r ฺ e s Gui
mi ofdthe nt
SQL>

p ฺ e
g Stu previous query? Implement your
54) How would you enhance the performance
d
solution.
b i a@ this
i a (ja to store
a) Create a cluster
u sethe two tables.
A b
SQL> @ic_setup to
o
ni set echo on
SQL>
t o
An SQL> drop table emp purge;
SQL>
e
Jos Table dropped.

SQL> drop table dept purge;

Table dropped.

SQL>
SQL> drop cluster emp_dept including tables;
drop cluster emp_dept including tables
*
ERROR at line 1:
ORA-00943: cluster does not exist

SQL>
SQL> CREATE CLUSTER emp_dept (deptno NUMBER(3))
2 SIZE 600
3 TABLESPACE users
4 STORAGE (INITIAL 200K
5 NEXT 300K

Oracle Database 11g: SQL Tuning Workshop A - 111


Practice 4-1: Using Different Access Paths (continued)
6 MINEXTENTS 2
7 PCTINCREASE 33);

Cluster created.

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

SQL> CREATE TABLE emp (


2 empno NUMBER(7) ,
3 ename VARCHAR2(15) NOT NULL,
4 job VARCHAR2(9) ,
5 mgr NUMBER(7) ,
6 hiredate DATE ,
7 sal NUMBER(7) ,
8 comm NUMBER(7) ,
9 deptno NUMBER(3))
10 CLUSTER emp_dept (deptno);
n s e
i ce
Table created.
b l el
SQL>
fer a
SQL> CREATE TABLE dept (
a n s
2 deptno NUMBER(3) ,
n -tr
o
an
3 dname VARCHAR2(14),
4 loc VARCHAR2(14),
5 c VARCHAR2(500))
h s
a deฺ
6 CLUSTER emp_dept (deptno);
e )
s Gui
r ฺ
Table created.
p ฺ mi dent
d g Stu
SQL>
i @ his
aemp_dept_index
SQL> CREATE INDEX
j b
a emp_depte t
2 (
ON CLUSTER
ia to uusers s
3 bTABLESPACE
4 A STORAGE (INITIAL 50K
n i o
o
nt 6
5 NEXT 50K

e A MINEXTENTS 2

Jos
7 MAXEXTENTS 10
8 PCTINCREASE 33);

Index created.

SQL>
SQL> begin
2 for i in 1..999 loop
3 insert into dept values
(i,'D'||i,'L'||i,dbms_random.string('u',500));
4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> begin
2 for i in 1..500000 loop

Oracle Database 11g: SQL Tuning Workshop A - 112


Practice 4-1: Using Different Access Paths (continued)
3 insert into emp values
(i,dbms_random.string('u',15),dbms_random.string('u',9),i,sysdate,i,
i,mod(i,999));
4 end loop;
5 commit;
6 end;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7 /

PL/SQL procedure successfully completed.

SQL>
SQL> exec dbms_stats.gather_schema_stats('SH');

PL/SQL procedure successfully completed.

SQL>
n s e
i ce
55) Execute the following query to confirm the performance enhancement of the previous
query: b l el
select * fe r a
from emp,dept
a n s
where emp.deptno=dept.deptno and emp.deptno > 800;
o n -tr
What do you observe?
s an
h
a) The optimizer is able to use the cluster access
) i eฺ the query execute
apath thatdmakes
faster.
i r ฺes nt Gu
SQL> @ic_query
g p ฺm ude
SQL> set echo on d S t
@ s
SQL>
j a b ia
e thi
SQL> set timing
i a (
SQL> set autotrace
on
u s
traceonly
b
A linesizet 200
SQL> set o
i o
n select * from emp,dept where emp.deptno=dept.deptno and
SQL>
ntoSQL>
e A emp.deptno > 800;

J o s
99000 rows selected.

Elapsed: 00:00:01.20

Execution Plan
----------------------------------------------------------
Plan hash value: 593050162

--------------------------------------------------------------------
--------------------
| Id | Operation | Name | Rows | Bytes |
Cost (%CPU)| Time |
--------------------------------------------------------------------
--------------------
| 0 | SELECT STATEMENT | | 19780 | 10M|
11515 (1)| 00:02:19 |
| 1 | NESTED LOOPS | | 19780 | 10M|
11515 (1)| 00:02:19 |
| 2 | TABLE ACCESS CLUSTER| DEPT | 199 | 99K|
167 (0)| 00:00:03 |

Oracle Database 11g: SQL Tuning Workshop A - 113


Practice 4-1: Using Different Access Paths (continued)
|* 3 | INDEX RANGE SCAN | EMP_DEPT_INDEX | 1 | |
2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS CLUSTER| EMP | 99 | 5643 |
57 (0)| 00:00:01 |
--------------------------------------------------------------------
--------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Predicate Information (identified by operation id):


---------------------------------------------------

3 - access("DEPT"."DEPTNO">800)
4 - filter("EMP"."DEPTNO">800 AND "EMP"."DEPTNO"="DEPT"."DEPTNO")

Statistics
----------------------------------------------------------
n s e
1 recursive calls
i ce
0 db block gets
b l el
30131 consistent gets
0 physical reads
fer a
0 redo size
a n s
6259293 bytes sent via SQL*Net to client
o n -tr
an
73009 bytes received via SQL*Net from client
6601 SQL*Net roundtrips to/from client
0 sorts (memory)
h s
a deฺ
0 sorts (disk)
e )
s Gui
99000 rows processed r ฺ
i ent
p ฺ m d
SQL>
d g S t u
SQL> set timing b i
offa@ this
SQL> set autotrace off

SQL>
i a (ja use
A
56) Execute
b to
the ic_cleanup.sql script to clean up your environment for this case.
n i o
toSQL> @ic_cleanup
e An SQL> set echo on
Jos SQL>
SQL> drop cluster emp_dept including tables;

Cluster dropped.

SQL>
SQL> drop table emp purge;
drop table emp purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> drop table dept purge;


drop table dept purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 114


Practice 4-1: Using Different Access Paths (continued)
57) Case 16: Execute the iss_setup.sql script to set up your environment for this
lab.
SQL> @iss_setup
SQL> set echo on
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> create table t(c number, d number);

Table created.

SQL>
SQL> begin
2 for i in 1..10000 loop
3 insert into t values(1,i);
4 end loop;
5 end;
n s e
6 /
i ce
b l el
PL/SQL procedure successfully completed.
fer a
SQL>
a n s
SQL> create index it on t(c,d);
o n -tr
Index created.
s an
) h a deฺ
SQL>
r ฺ e s Gui
58) Execute the following query:
p ฺ mi dent
select count(*) from t where
d g d=1;S tu
What do you observe?
b i a@ this
a (jais notuusing
a) The optimizer
i se the index and does a full table scan.
A b to
o
ni set echo on
SQL> @query20
t oSQL>

e An SQL>
Jos
SQL> set linesize 200
SQL>
SQL> set timing on
SQL> set autotrace on
SQL>
SQL> select count(*) from t where d=1;

COUNT(*)
----------
1

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |

Oracle Database 11g: SQL Tuning Workshop A - 115


Practice 4-1: Using Different Access Paths (continued)
--------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | 1 | 13 | 7 (0)|
00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 2 | TABLE ACCESS FULL| T | 1 | 13 | 7 (0)|


00:00:01 |
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - filter("D"=1)

n s e
Note
i ce
-----
b l el
- dynamic sampling used for this statement
fe ra
a n s
Statistics
n - tr
o
an
----------------------------------------------------------
5 recursive calls
0 db block gets
h s
a deฺ
48 consistent gets
e )
s Gui
r ฺ
mi dent
0 physical reads
0 redo size
p ฺ
g Stu
d
418 bytes sent via SQL*Net to client

b i a@ this
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client

i a (ja use
0 sorts (memory)

A b to
0 sorts (disk)

o n io 1 rows processed

nt SQL>
e A
J o s SQL> set timing off
SQL> set autotrace off
SQL>

59) How would you improve the performance of a query, such as the one in the previous
step? Implement your solution.
a) Make sure you gather correctly the statistics for your table so that the index skip
scan can be used.
SQL> @iss_gather_stats
SQL> set echo on
SQL>
SQL> execute dbms_stats.gather_table_stats('SH','T',cascade=>TRUE);

PL/SQL procedure successfully completed.

SQL>

60) Execute the following query:


select count(*) from t where d=1;

Oracle Database 11g: SQL Tuning Workshop A - 116


Practice 4-1: Using Different Access Paths (continued)
What do you observe?
a) The optimizer now uses the index to perform an index skip scan.
SQL> @query20
SQL> set echo on
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set linesize 200


SQL>
SQL> set timing on
SQL> set autotrace on
SQL>
SQL> select count(*) from t where d=1;

COUNT(*)
----------
n s e
ce
1

eli
Elapsed: 00:00:00.00
a b l
fer
Execution Plan
a n s
Plan hash value: 2609927160
o n -tr
----------------------------------------------------------

s an
) h a deฺ
--------------------------------------------------------------------

ui | Cost (%CPU)| Time


-----
| Id | Operation | Name | e
r ฺ s | GBytes
Rows
|
p ฺ mi dent
g Stu
--------------------------------------------------------------------
d
a@ t|his | 1 | 4 | 2 (0)|
-----
|
b i
0 | SELECT STATEMENT
00:00:01 |
a (jaAGGREGATE
1 | iSORT u se |
|
| b t o | 1 |
A INDEX SKIP SCAN| IT | 1 | 4 | 2 (0)|
4 | |

|*io2 |
n
to00:00:01 |
A n
o s e --------------------------------------------------------------------
-----
J
Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("D"=1)
filter("D"=1)

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
23 consistent gets
0 physical reads
0 redo size
418 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)

Oracle Database 11g: SQL Tuning Workshop A - 117


Practice 4-1: Using Different Access Paths (continued)
0 sorts (disk)
1 rows processed

SQL>
SQL> set timing off
SQL> set autotrace off
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

61) Compare the result of executing the previous query with the result you obtain when
you execute the following query:
select /*+ INDEX_FFS(t it) */ count(*) from t where d=1;
What do you observe?
a) The optimizer uses a fast full index scan, but this is not better than the index skip
scan.
n s e
ice
el
SQL> @query21
SQL> set echo on
a b l
SQL>
fe r
SQL> set linesize 200
a n s
SQL>
SQL> set timing on
o n -tr
SQL> set autotrace on
s an
etฺ where d=1;
SQL>
) h a fromd
SQL> select /*+ INDEX_FFS(t it) */ count(*)
i
i r ฺes nt Gu
COUNT(*)
----------
g p ฺm ude
1 d S t
@ s
j a b ia
e thi
i a (
Elapsed: 00:00:00.00
u s
b
A Plan t
Execution o
i o
n hash value: 273610729
----------------------------------------------------------
ntoPlan
e A
J o s --------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 1 | 4 | 9 (0)|
00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 4 | |
|
|* 2 | INDEX FAST FULL SCAN| IT | 1 | 4 | 9 (0)|
00:00:01 |
--------------------------------------------------------------------
----------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - filter("D"=1)

Oracle Database 11g: SQL Tuning Workshop A - 118


Practice 4-1: Using Different Access Paths (continued)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
31 consistent gets
0 physical reads
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 redo size
418 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set timing off
n s e
SQL> set autotrace off
i ce
SQL>
b l el
r a
62) Execute the iss_cleanup.sql script to clean up your environment for this case.
fe
a n s
SQL> @iss_cleanup
SQL> set echo on
o n -tr
SQL>
s an
SQL> drop table t purge;
) h a deฺ
Table dropped.
r ฺ e s Gui
p ฺ mi dent
SQL>
d g Stu
a@session,
SQL>
b i t h is
j a
63) Exit from your(SQL*Plus
s e and execute the ap_cleanup.sh script to clean
u
bia tofor this lab.
up your environment
A
io exit;
o n
SQL>
t Disconnected from Oracle Database 11g Enterprise Edition Release
e An 11.1.0.6.0 - Production
Jos With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Access_Paths]$ ./ap_cleanup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 9 15:22:25 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> revoke dba from sh;

Revoke succeeded.

Oracle Database 11g: SQL Tuning Workshop A - 119


Practice 4-1: Using Different Access Paths (continued)
SQL>
SQL> @sh_main sh example temp oracle
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/
/home/oracle/ v3
SQL> Rem
SQL> Rem $Header: sh_main.sql 06-mar-2008.15:00:45 cbauwens Exp $
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> Rem

....

<<<<< CHANNELS DIMENSION >>>>>


Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
- default display hierarchy
n s e
Level Attributes - name, display name, description
i ce
Drop dimension attributes prior to re-creation
b l el
No attribute to drop
Create dimension attributes and add their level attributes
fer a
- Long Description created
a n s
- Short Description created
n -tr
o
an
Classify entity descriptor use
- Long Description
- Short Description
h s
a deฺ
-
e )
s Gui
r ฺ
mi dent
<<<<< FINAL PROCESSING >>>>>

p
g Sฺ
- Changes have been committed
d tu
b i a@ this
PL/SQL procedure successfully completed.

i a (ja use
b
Commit complete.
A to
ni o
ntogathering
e A statistics ...

J o s PL/SQL procedure successfully completed.

PL/SQL procedure successfully completed.

Disconnected from Oracle Database 11g Enterprise Edition Release


11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Access_Paths]$

Oracle Database 11g: SQL Tuning Workshop A - 120


Practice 4-2: Using the Result Cache
In this practice, you explore the various possibilities of caching query results in the
System Global Area (SGA). Perform the following steps to understand the use of Query
Result Cache.
1) Change to the $HOME/solutions/Query_Result_Cache directory and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

execute the result_cache_setup.sh script.


[oracle@edrsr33p1-orcl ~]$ cd $HOME/solutions/Query_Result_Cache
[oracle@edrsr33p1-orcl Query_Result_Cache]$ ./result_cache_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 25 14:46:14


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
i ce
b l el
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
fe ra
Production
a n s
-
With the Partitioning, Oracle Label Security, OLAP, Data Mining
n tr
and Real Application Testing options
n o
s a
SQL> SQL> SQL> SQL> drop user qrc cascade
) h a deฺ
*
r ฺ e s Gui
miexistdent
ERROR at line 1:
p ฺ
ORA-01918: user 'QRC' does not
g Stu
d
@ his
SQL> SQL> 2 bi3a t
( j a s e
ia to u
User created.
b
i o Asucceeded.
SQL> SQL>
n
nto
Grant

e A
Jos
SQL> SQL> Connected.
SQL> SQL>
PL/SQL procedure successfully completed.

SQL> SQL> drop table cachejfv purge


*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> SQL>
Table created.

SQL> SQL>
1 row created.

SQL>
1 row created.

SQL>
2 rows created.

Oracle Database 11g: SQL Tuning Workshop A - 121


Practice 4-2: Using the Result Cache (continued)
SQL>
4 rows created.

SQL>
8 rows created.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
16 rows created.

SQL>
32 rows created.

SQL>
64 rows created.

n s e
SQL>
i ce
128 rows created.
b l el
SQL>
fer a
256 rows created.
a n s
n -tr
o
an
SQL>
512 rows created.
h s
a deฺ
SQL>
e )
s Gui
1024 rows created. r ฺ
i ent
p ฺ m d
SQL>
2048 rows created. @ d g S t u
b i a t h is
SQL>
i a (ja use
b
4096 rows
A to
created.

n i o
ntoSQL>
e A 8192 rows created.

J o s SQL>
16384 rows created.

SQL>
32768 rows created.

SQL>
65536 rows created.

SQL>
131072 rows created.

SQL>
262144 rows created.

SQL>
524288 rows created.

SQL>
1048576 rows created.

Oracle Database 11g: SQL Tuning Workshop A - 122


Practice 4-2: Using the Result Cache (continued)
SQL> SQL>
1 row created.

SQL> SQL>
Commit complete.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SQL>
System altered.

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Query_Result_Cache]$

n s e
i ce
--------------------------------------------------------------
b l el
fer a
#!/bin/bash
a n s
o n -tr
an
cd /home/oracle/solutions/Query_Result_Cache

export ORACLE_SID=orcl
h s
a deฺ
e )
s Gui
r ฺ
i ent
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

p ฺ m d
export
d g S t u
b i a@ this
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

sqlplus b/ia
(ja u<<FIN!
se
A to
as sysdba
o
ni echo on
ntoset
e A
J o s drop user qrc cascade;

create user qrc identified by qrc


default tablespace users
temporary tablespace temp;

grant connect, resource, dba to qrc;

connect qrc/qrc

exec dbms_result_cache.flush;

drop table cachejfv purge;

create table cachejfv(c varchar2(500)) tablespace users;

insert into cachejfv


values('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;

Oracle Database 11g: SQL Tuning Workshop A - 123


Practice 4-2: Using the Result Cache (continued)
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

insert into cachejfv select * from cachejfv;


insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
insert into cachejfv select * from cachejfv;
n s e
insert into cachejfv select * from cachejfv;
i ce
insert into cachejfv select * from cachejfv;
b l el
insert into cachejfv select * from cachejfv;
fer a
insert into cachejfv values('b');
a n s
o n -tr
an
commit;

alter system flush buffer_cache;


h s
a deฺ
e )
s Gui
FIN! r ฺ
i ent
p ฺ m
2) Open a terminal window, invoke
d g SQL*Plus,
S t udand connect as the qrc user. From now
on, do not disconnect from
i a @this hsession.
i s Determine the current content of the query
j a b
cache using the following t
statement:
e
i a ( u s
select type,status,name,object_no,row_count,row_size_avg
from A b to
v$result_cache_objects order by 1;
i o
n do you observe?
n toWhat
e A a) Use the check_result_cache.sql script. Right now, the query cache
Jos should be empty.
[oracle@edrsr33p1-orcl Query_Result_Cache]$ sqlplus qrc/qrc

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 25 14:49:02


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> @check_result_cache
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

Oracle Database 11g: SQL Tuning Workshop A - 124


Practice 4-2: Using the Result Cache (continued)
no rows selected

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

--------------------------------------------------------------

set echo on

select type,status,name,object_no,row_count,row_size_avg from


v$result_cache_objects order by 1;

3) Set timing on and execute the query as follows. You can use the query1.sql
n s e
script. Note the time that it takes to execute.
i ce
select /*+ result_cache q_name(Q1) */ count(*) b l el
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv
fer a
c4,cachejfv c5,cachejfv c6, cachejfv c7
a n s
n -tr
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
o
an
c5.c='b' and c6.c='b' and c7.c='b';
SQL> set timing on
h s
a deฺ
SQL> @query1
e )
s Gui
r ฺ
COUNT(*)
p ฺ mi dent
----------
1 d g Stu
b i a@ this
SQL>
i a (ja use
Elapsed: 00:00:02.46

A b to
i o
ton
--------------------------------------------------------------

A n select /*+ result_cache q_name(Q1) */ count(*)


o s e from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
J c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';

4) Determine the execution plan of the previous query by using the


explain_query1.sql script. What do you observe?
a) Because of the result_cache hint, the result of the query is computed using the
result cache.
SQL> @explain_query1
SQL> set echo on
SQL>
SQL> explain plan for
2 select /*+ result_cache q_name(Q1) */ count(*)
3 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
4 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
c5.c='b' and c6.c='b' and c7.c='b';

Oracle Database 11g: SQL Tuning Workshop A - 125


Practice 4-2: Using the Result Cache (continued)
Explained.

Elapsed: 00:00:00.05
SQL>
SQL> set linesize 180
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set pagesize 200


SQL>
SQL> select plan_table_output from
table(dbms_xplan.display('plan_table',null,'ALL'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------
Plan hash value: 2531260445
n s e
i ce
--------------------------------------------------------------------
b l el
--------------------------------------
| Id | Operation | Name
fe
|r a
Rows | Bytes | Cost (%CPU)| Time |
a n s
- tr
--------------------------------------------------------------------
n
o
an
--------------------------------------
| 0 | SELECT STATEMENT | |
1 | 1764 | 7316P (1)|999:59:59 |
h s
a deฺ
| 1 | RESULT CACHE )
s Gui
| b7rh5vw33py4ug4n8xaav6525g |
e
r ฺ
mi dent
| | | |
| 2 | SORT AGGREGATE
p ฺ
g Stu
| |
1 | 1764 | |
d |
| 3 |
294P| b i a@ this
MERGE JOIN CARTESIAN
15E| 7316P (1)|999:59:59 |
| |

| 4 |
i a (ja use
MERGE JOIN CARTESIAN | |
b
941T| 1264P|
A to
23P (1)|999:59:59 |

o
|
n io
5 |
3007G| 3446T|
MERGE JOIN CARTESIAN |
74T (1)|999:59:59 |
|

nt
e A | 6 | MERGE JOIN CARTESIAN | |

J o s 9606M| 9018G|
| 7 |
238G (1)|999:59:59 |
MERGE JOIN CARTESIAN | |
30M| 21G| 761M (1)|999:59:59 |
| 8 | MERGE JOIN CARTESIAN| |
98011 | 47M| 2432K (1)| 08:06:26 |
|* 9 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7748 (1)| 00:01:33 |
| 10 | BUFFER SORT | |
313 | 78876 | 2424K (1)| 08:04:53 |
|* 11 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
| 12 | BUFFER SORT | |
313 | 78876 | 761M (1)|999:59:59 |
|* 13 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
| 14 | BUFFER SORT | |
313 | 78876 | 238G (1)|999:59:59 |
|* 15 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
| 16 | BUFFER SORT | |
313 | 78876 | 74T (1)|999:59:59 |

Oracle Database 11g: SQL Tuning Workshop A - 126


Practice 4-2: Using the Result Cache (continued)
|* 17 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
| 18 | BUFFER SORT | |
313 | 78876 | 23P (1)|999:59:59 |
|* 19 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 20 | BUFFER SORT | |
313 | 78876 | 7316P (1)|999:59:59 |
|* 21 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
--------------------------------------------------------------------
--------------------------------------

Query Block Name / Object Alias (identified by operation id):


-------------------------------------------------------------

n s e
1 - SEL$1
i ce
9 - SEL$1 / C7@SEL$1
b l el
11
13
-
-
SEL$1
SEL$1
/
/
C6@SEL$1
C5@SEL$1
fer a
15 - SEL$1 / C4@SEL$1
a n s
17 - SEL$1 / C3@SEL$1
n - tr
o
an
19 - SEL$1 / C2@SEL$1
21 - SEL$1 / C1@SEL$1
h s eฺ
a did):
Predicate Information (identified by )operation i
i r ฺes nt Gu
---------------------------------------------------

g p ฺm ude
d
9 - filter("C7"."C"='b')
S t
@ s
thi
11 - filter("C6"."C"='b')
b ia
13 - filter("C5"."C"='b')
a
j e
ia ( to us
15 - filter("C4"."C"='b')
b
17 - filter("C3"."C"='b')

n i o Afilter("C2"."C"='b')
19 -

nto 21 - filter("C1"."C"='b')

e A
J o s Column Projection Information (identified by operation id):
-----------------------------------------------------------

1 - COUNT(*)[22]
2 - (#keys=0) COUNT(*)[22]
3 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500],
"C2"."C"[VARCHAR2,500], "C1"."C"[VARCHAR2,500]
4 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500],
"C2"."C"[VARCHAR2,500]
5 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500]
6 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500]
7 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500]

Oracle Database 11g: SQL Tuning Workshop A - 127


Practice 4-2: Using the Result Cache (continued)
8 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500]
9 - "C7"."C"[VARCHAR2,500]
10 - (#keys=0) "C6"."C"[VARCHAR2,500]
11 - "C6"."C"[VARCHAR2,500]
12 - (#keys=0) "C5"."C"[VARCHAR2,500]
13 - "C5"."C"[VARCHAR2,500]
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

14 - (#keys=0) "C4"."C"[VARCHAR2,500]
15 - "C4"."C"[VARCHAR2,500]
16 - (#keys=0) "C3"."C"[VARCHAR2,500]
17 - "C3"."C"[VARCHAR2,500]
18 - (#keys=0) "C2"."C"[VARCHAR2,500]
19 - "C2"."C"[VARCHAR2,500]
20 - (#keys=0) "C1"."C"[VARCHAR2,500]
21 - "C1"."C"[VARCHAR2,500]

Result Cache Information (identified by operation id):


n s e
------------------------------------------------------
i ce
b l el
1 - column-count=1; dependencies=(QRC.CACHEJFV);
attributes=(single-row); parameters=(nls); name="select /*+
fer a
result_cache q_name(Q1) */ count(*)
a n s
o n -tr
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv

an
c5,cachejfv c6, cac"

h s
a deฺ
Note
e )
s Gui
r ฺ
mithisdstatement
nt
-----

- dynamic sampling used for
p
g Stu e
89 rows selected. @d
b i a t h is
i a (ja use
Elapsed: 00:00:00.03
SQL>
A b to
ni o
nto--------------------------------------------------------------
e A
J o s set echo on

explain plan for


select /*+ result_cache q_name(Q1) */ count(*)
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';

set linesize 180


set pagesize 200

select plan_table_output from


table(dbms_xplan.display('plan_table',null,'ALL'));

5) Determine the current content of the query cache by using the


check_result_cache.sql script. What do you observe?
a) You can now see the result of your query cached.

Oracle Database 11g: SQL Tuning Workshop A - 128


Practice 4-2: Using the Result Cache (continued)
SQL> @check_result_cache
SQL> set echo on
SQL>
SQL> set long 2000
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

v$result_cache_objects order by 1;

TYPE STATUS NAME


OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV
n s e
71474 0
i ce
0
b l el
fer a
Result
0 1
Published select /*+ result_cache q_name(Q1) */ count(*)
a n s
n -tr
from cachejfv c1,cachejfv c2,cachejfv
o
an
c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5
h s
a deฺ
e )
s Gui
r ฺ
mi dent
Elapsed: 00:00:00.00
SQL>
p ฺ
Elapsed: 00:00:00.00@
dg Stu
b i a t h is
(ja use
SQL>

b i a to
--------------------------------------------------------------
o A
ni echo on
toset
A n
o s e set long 2000
J select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

6) Flush the buffer cache of your instance and rerun the query executed in step 3. What
do you observe?
a) The execution time for the query is now almost instantaneous.
SQL> alter system flush buffer_cache;

System altered.

Elapsed: 00:00:00.05
SQL> @query1
SQL> select /*+ result_cache q_name(Q1) */ count(*)
2 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7

Oracle Database 11g: SQL Tuning Workshop A - 129


Practice 4-2: Using the Result Cache (continued)
3 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
c5.c='b' and c6.c='b' and c7.c='b';

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

Elapsed: 00:00:00.00
SQL>

--------------------------------------------------------------

select /*+ result_cache q_name(Q1) */ count(*)


from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
n s e
and c6.c='b' and c7.c='b';
i ce
b l el
7) Insert a new row into the CACHEJFV table using the following statement: fer a
insert into cachejfv values('c');
a n s
What do you observe?
o n -tr
n
a invalidated.
a) The corresponding result cache entry is automatically
h s
a deฺ
SQL> insert into cachejfv values('c');
e )
s Gui
r ฺ
1 row created.
p ฺ mi dent
Elapsed: 00:00:00.00 d
g Stu
SQL> commit;
b i a@ this
i a (ja use
b
Commit complete.
A to
ni o
ntoElapsed: 00:00:00.00
SQL> @check_result_cache
e A SQL> set echo on
J o s SQL>
SQL> set long 2000
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

TYPE STATUS NAME


OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV
71474 0
0

Result Invalid select /*+ result_cache q_name(Q1) */ count(*)


0 1

Oracle Database 11g: SQL Tuning Workshop A - 130


Practice 4-2: Using the Result Cache (continued)
from cachejfv c1,cachejfv c2,cachejfv
c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5

Elapsed: 00:00:00.00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

--------------------------------------------------------------

set echo on

set long 2000

select type,status,name,object_no,row_count,row_size_avg from


v$result_cache_objects order by 1;
n s e
ice
b l
8) Execute your query from step 3 and step 6 again and check the result cache. What do el
you observe? fe r a
a n s
entry has been added for the new result. o n -tr
a) Again, it takes some time to execute the query. The result cache shows that a new

s an
SQL> @query1
) h i d eฺ
a*/ count(*)
es t Gc3,cachejfv
u
SQL> select /*+ result_cache q_name(Q1)
i r ฺ
2 from cachejfv c1,cachejfv c2,cachejfv
c5,cachejfv c6, cachejfv c7 m n
c4,cachejfv

d g pฺ tuanddec3.c='b' and c4.c='b' and


3 where c1.c='b' and c2.c='b'

i a @ his S
c5.c='b' and c6.c='b' and c7.c='b';

COUNT(*) (ja
b et
b ia to us
----------

n i oA 1
ntoElapsed: 00:00:02.33
e A SQL>
J o s SQL> @check_result_cache
SQL> set echo on
SQL>
SQL> set long 2000
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

TYPE STATUS NAME


OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV
71474 0
0

Oracle Database 11g: SQL Tuning Workshop A - 131


Practice 4-2: Using the Result Cache (continued)
Result Invalid select /*+ result_cache q_name(Q1) */ count(*)
0 1
from cachejfv c1,cachejfv c2,cachejfv
c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Result Published select /*+ result_cache q_name(Q1) */ count(*)


0 1
from cachejfv c1,cachejfv c2,cachejfv
c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5

Elapsed: 00:00:00.00
SQL>

n s e
--------------------------------------------------------------
i ce
b l el
select /*+ result_cache q_name(Q1) */ count(*)
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
fe ra
c5,cachejfv c6, cachejfv c7
a n s
o n -tr
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'

an
and c6.c='b' and c7.c='b';

h s
a deฺ
e )
s Gui
set echo on r ฺ
i ent
p ฺ m
set long 2000
d g Stud
b i a@ this
select type,status,name,object_no,row_count,row_size_avg from
j a e
ia ( to us
v$result_cache_objects order by 1;
b
n io A a detailed result cache memory report.
9)oGenerate
t
e An SQL> set serveroutput on
Jos SQL> EXEC DBMS_RESULT_CACHE.MEMORY_REPORT(detailed=>true);
R e s u l t C a c h e M e m o r y R e p o r t
[Parameters]
Block Size = 1K bytes
Maximum Cache Size = 2080K bytes (2080 blocks)
Maximum Result Size = 104K bytes (104 blocks)
[Memory]
Total Memory = 116008 bytes [0.035% of the Shared Pool]
... Fixed Memory = 5132 bytes [0.002% of the Shared Pool]
....... Cache Mgr = 108 bytes
....... Memory Mgr = 124 bytes
....... Bloom Fltr = 2K bytes
....... State Objs = 2852 bytes
... Dynamic Memory = 110876 bytes [0.033% of the Shared Pool]
....... Overhead = 78108 bytes
........... Hash Table = 32K bytes (4K buckets)
........... Chunk Ptrs = 12K bytes (3K slots)
........... Chunk Maps = 12K bytes
........... Miscellaneous = 20764 bytes
....... Cache Memory = 32K bytes (32 blocks)

Oracle Database 11g: SQL Tuning Workshop A - 132


Practice 4-2: Using the Result Cache (continued)
........... Unused Memory = 29 blocks
........... Used Memory = 3 blocks
............... Dependencies = 1 blocks (1 count)
............... Results = 2 blocks
................... SQL = 1 blocks (1 count)
................... Invalid = 1 blocks (1 count)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.12
SQL>

10) Execute your query from step 3 and step 6 again. What do you observe?
a) The query again uses the result that was previously cached.
SQL> @query1
n s e
ice
el
SQL> select /*+ result_cache q_name(Q1) */ count(*)
2 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
a b l
c5,cachejfv c6, cachejfv c7
fe r
3 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
a n s
-tr
c5.c='b' and c6.c='b' and c7.c='b';

o n
an
COUNT(*)
----------
1 h s
a deฺ
e )
s Gui
r ฺ
Elapsed: 00:00:00.01
SQL> p ฺ mi dent
d g Stu
i a@ this
--------------------------------------------------------------
b
i a (ja use q_name(Q1) */ count(*)
select /*+ result_cache
A b
from cachejfv o
tcachejfv
c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
o
ni c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
c5,cachejfv c6, c7

ntowhere
e A and c6.c='b' and c7.c='b';

J o s
11) Ensure that you bypass the result cache before performing the next step.
SQL>
SQL> exec DBMS_RESULT_CACHE.BYPASS(bypass_mode=>true);

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
SQL>

12) Execute your query again. What do you observe?


a) The query again takes longer to execute because it no longer uses the result cache.
SQL> @query1
SQL> select /*+ result_cache q_name(Q1) */ count(*)
2 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
3 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
c5.c='b' and c6.c='b' and c7.c='b';

Oracle Database 11g: SQL Tuning Workshop A - 133


Practice 4-2: Using the Result Cache (continued)
COUNT(*)
----------
1

Elapsed: 00:00:02.34
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

--------------------------------------------------------------

select /*+ result_cache q_name(Q1) */ count(*)


from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';

n s e
i ce
13) Ensure that you no longer bypass the result cache and check that your query uses it
again. b l el
fe ra
SQL> exec DBMS_RESULT_CACHE.BYPASS(bypass_mode=>false);
a n s
PL/SQL procedure successfully completed. o n -tr
s an
Elapsed: 00:00:00.01
) h a deฺ
SQL> @query1
r ฺ e ui
s */Gcount(*)
2 from cachejfv c1,cachejfv
ฺ m ic2,cachejfv
SQL> select /*+ result_cache q_name(Q1)
e nt c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv gc7p d
tuand c3.c='b' and c4.c='b' and
3 where c1.c='b' @ anddc2.c='b'
S
b i
c5.c='b' and c6.c='b' is
a andthc7.c='b';
i a (ja use
b
COUNT(*)
A to
o
----------
i
nton 1

e A
J o s Elapsed: 00:00:00.00
SQL>
SQL>

--------------------------------------------------------------

select /*+ result_cache q_name(Q1) */ count(*)


from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';

14) Execute the following query by using the query2.sql script:


select count(*) from cachejfv c1,cachejfv c2,cachejfv
c3,cachejfv c4,cachejfv c5,cachejfv c6, cachejfv c7 where
c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';
What do you observe?

Oracle Database 11g: SQL Tuning Workshop A - 134


Practice 4-2: Using the Result Cache (continued)
a) Although the query is the same as the one used in step 3, it is not recognized as
cached because it does not contain the hint. So its execution time is long again.
SQL> @query2
SQL> select count(*)
2 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

c5,cachejfv c6, cachejfv c7


3 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
c5.c='b' and c6.c='b' and c7.c='b';

COUNT(*)
----------
1

Elapsed: 00:00:02.41
SQL>
n s e
ice
--------------------------------------------------------------
b l el
select count(*) fe ra
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
a n s
c5,cachejfv c6, cachejfv c7
o n -tr
an
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';
h s
a deฺ
e )
suse the G ui result without using hints?
15) How would you force the previous query r ฺ to cached
Use the force_query2.sqlp ฺ miand dthen
script e t that you successfully used the
nverify
cached result. Finally, undo d g change.
your S tu
b i a@ this
i a
SQL> set echo (jaon use
SQL> @force_query2

SQL>
A b to result_cache_mode
ni
SQL>o show parameter
t o
e An NAME TYPE VALUE

Jos
------------------------------------ ----------- -------------------
-----------
result_cache_mode string MANUAL
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

TYPE STATUS NAME


OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV
71474 0
0

Result Invalid select /*+ result_cache q_name(Q1) */ count(*)


0 1

Oracle Database 11g: SQL Tuning Workshop A - 135


Practice 4-2: Using the Result Cache (continued)
from cachejfv c1,cachejfv c2,cachejfv
c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5

Result Published select /*+ result_cache q_name(Q1) */ count(*)


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

from cachejfv c1,cachejfv c2,cachejfv


c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5

Elapsed: 00:00:00.00
SQL>
SQL> alter session set result_cache_mode=force;

Session altered.
n s e
i ce
Elapsed: 00:00:00.04
b l el
SQL>
SQL> explain plan for
fer a
2 select count(*)
a n s
-tr
3 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
n
o
an
c5,cachejfv c6, cachejfv c7
4 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
c5.c='b' and c6.c='b' and c7.c='b';
h s
a deฺ
e )
s Gui
Explained. r ฺ
i ent
p ฺ m d
Elapsed: 00:00:00.05
d g S t u
SQL>
i @ his
a180
( j a 200se t
SQL> set linesize
SQL> set pagesize
b
SQL> b ia to u
i o A plan_table_output from
SQL> select
n
ntotable(dbms_xplan.display('plan_table',null,'ALL'));

e A
J o s PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------
Plan hash value: 2531260445

--------------------------------------------------------------------
--------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
--------------------------------------
| 0 | SELECT STATEMENT | |
1 | 1764 | 7316P (1)|999:59:59 |
| 1 | RESULT CACHE | b7rh5vw33py4ug4n8xaav6525g |
| | | |
| 2 | SORT AGGREGATE | |
1 | 1764 | | |
| 3 | MERGE JOIN CARTESIAN | |
294P| 15E| 7316P (1)|999:59:59 |

Oracle Database 11g: SQL Tuning Workshop A - 136


Practice 4-2: Using the Result Cache (continued)
| 4 | MERGE JOIN CARTESIAN | |
941T| 1264P| 23P (1)|999:59:59 |
| 5 | MERGE JOIN CARTESIAN | |
3007G| 3446T| 74T (1)|999:59:59 |
| 6 | MERGE JOIN CARTESIAN | |
9606M| 9018G| 238G (1)|999:59:59 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 7 | MERGE JOIN CARTESIAN | |


30M| 21G| 761M (1)|999:59:59 |
| 8 | MERGE JOIN CARTESIAN| |
98011 | 47M| 2432K (1)| 08:06:26 |
|* 9 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7748 (1)| 00:01:33 |
| 10 | BUFFER SORT | |
313 | 78876 | 2424K (1)| 08:04:53 |
|* 11 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
n s e
| 12 | BUFFER SORT | |
i ce
313 | 78876 | 761M (1)|999:59:59 |
b l el
|* 13 | TABLE ACCESS FULL | CACHEJFV
313 | 78876 | 7746 (1)| 00:01:33 |
|
fer a
| 14 | BUFFER SORT |
a n s
|
313 | 78876 | 238G (1)|999:59:59 |
n -tr
o
an
|* 15 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 (1)| 00:01:33 |
| 16 | BUFFER SORT |
h s
a deฺ |
313 | 78876 | 74T (1)|999:59:59 |
e )
s Gui
r ฺ
mi dent
|* 17 | TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746 ฺ
(1)| 00:01:33 |
p
g Stu
| 18 | BUFFER SORT
d | |
313 | 78876 |
|* 19 | b a@ this
23P (1)|999:59:59 |
i
TABLE ACCESS FULL | CACHEJFV |
313 | 78876 | 7746
i a (ja use
(1)| 00:01:33 |
| 20 |
A b to
BUFFER SORT | |

o n io
313 | 78876 | 7316P (1)|999:59:59 |
|* 21 | TABLE ACCESS FULL | CACHEJFV |
nt
e A 313 | 78876 | 7746 (1)| 00:01:33 |

J o s --------------------------------------------------------------------
--------------------------------------

Query Block Name / Object Alias (identified by operation id):


-------------------------------------------------------------

1 - SEL$1
9 - SEL$1 / C7@SEL$1
11 - SEL$1 / C6@SEL$1
13 - SEL$1 / C5@SEL$1
15 - SEL$1 / C4@SEL$1
17 - SEL$1 / C3@SEL$1
19 - SEL$1 / C2@SEL$1
21 - SEL$1 / C1@SEL$1

Predicate Information (identified by operation id):


---------------------------------------------------

9 - filter("C7"."C"='b')
11 - filter("C6"."C"='b')
13 - filter("C5"."C"='b')

Oracle Database 11g: SQL Tuning Workshop A - 137


Practice 4-2: Using the Result Cache (continued)
15 - filter("C4"."C"='b')
17 - filter("C3"."C"='b')
19 - filter("C2"."C"='b')
21 - filter("C1"."C"='b')

Column Projection Information (identified by operation id):


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

-----------------------------------------------------------

1 - COUNT(*)[22]
2 - (#keys=0) COUNT(*)[22]
3 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500],
"C2"."C"[VARCHAR2,500], "C1"."C"[VARCHAR2,500]
4 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
n s e
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500],
i ce
"C2"."C"[VARCHAR2,500]
b l el
5 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500],
fer a
"C4"."C"[VARCHAR2,500], "C3"."C"[VARCHAR2,500]
a n s
-tr
6 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
n
o
an
"C5"."C"[VARCHAR2,500],
"C4"."C"[VARCHAR2,500]
h s
a deฺ
7 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500],
"C5"."C"[VARCHAR2,500]
e )
s Gui
r ฺ
mi dent
8 - (#keys=0) "C7"."C"[VARCHAR2,500], "C6"."C"[VARCHAR2,500]
9 - "C7"."C"[VARCHAR2,500]
p ฺ
g Stu
d
10 - (#keys=0) "C6"."C"[VARCHAR2,500]

b i a@ this
11 - "C6"."C"[VARCHAR2,500]
12 - (#keys=0) "C5"."C"[VARCHAR2,500]

i a (ja use
13 - "C5"."C"[VARCHAR2,500]
b to
14 - (#keys=0) "C4"."C"[VARCHAR2,500]
A
o n io
15 - "C4"."C"[VARCHAR2,500]
16 - (#keys=0) "C3"."C"[VARCHAR2,500]
nt
e A 17 - "C3"."C"[VARCHAR2,500]

J o s 18 - (#keys=0) "C2"."C"[VARCHAR2,500]
19 - "C2"."C"[VARCHAR2,500]
20 - (#keys=0) "C1"."C"[VARCHAR2,500]
21 - "C1"."C"[VARCHAR2,500]

Result Cache Information (identified by operation id):


------------------------------------------------------

1 - column-count=1; dependencies=(QRC.CACHEJFV);
attributes=(single-row); parameters=(nls); name="select count(*)
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2."

Note
-----
- dynamic sampling used for this statement

89 rows selected.

Oracle Database 11g: SQL Tuning Workshop A - 138


Practice 4-2: Using the Result Cache (continued)
Elapsed: 00:00:00.07
SQL>
SQL> select count(*)
2 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
3 where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

c5.c='b' and c6.c='b' and c7.c='b';

COUNT(*)
----------
1

Elapsed: 00:00:00.04
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;
n s e
i ce
TYPE STATUS NAME
b l el
OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
fer a
n s
--------------------------------------------------------------------
a
------------- ---------- ----------
o n -tr
an
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV
h s
a deฺ
71474 0
e )
s Gui
0 r ฺ
i ent
p ฺ m d
Result Invalid
d g
select
t u
/*+ result_cache
S
q_name(Q1) */ count(*)
0 1
b i a@fromthcachejfv
is c1,cachejfv c2,cachejfv
c3,cachejfv ( j a
c4,cachejfv s e c5,cachejfv c6, cac
b i a5
to u
i o A
n
toResult Published select /*+ result_cache q_name(Q1) */ count(*)
A n 0 1

o s e from cachejfv c1,cachejfv c2,cachejfv


J c3,cachejfv c4,cachejfv c5,cachejfv c6, cac
5

Elapsed: 00:00:00.00
SQL>
SQL> alter session set result_cache_mode=manual;

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL>

--------------------------------------------------------------

set echo on

show parameter result_cache_mode

Oracle Database 11g: SQL Tuning Workshop A - 139


Practice 4-2: Using the Result Cache (continued)
select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

alter session set result_cache_mode=force;

explain plan for


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

select count(*)
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'
and c6.c='b' and c7.c='b';

set linesize 180


set pagesize 200

select plan_table_output from


n s e
table(dbms_xplan.display('plan_table',null,'ALL'));
i ce
b l el
select count(*)
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
fe r a
c5,cachejfv c6, cachejfv c7
a n s
o n -tr
where c1.c='b' and c2.c='b' and c3.c='b' and c4.c='b' and c5.c='b'

an
and c6.c='b' and c7.c='b';

s
a deฺ
v$result_cache_objects order by 1; ) h
select type,status,name,object_no,row_count,row_size_avg from

r ฺ e s Gui
ฺ mi dent
alter session set result_cache_mode=manual;
p
d g Stu
i @ V$RESULT_CACHE_OBJECTS
aQuery h is
a b
16) Clear the result cache.
e t to verify the clear
operation. a (j u s
b i t o
i o A dbms_result_cache.flush;
SQL> exec
n
to
e An PL/SQL procedure successfully completed.
Jos Elapsed: 00:00:00.01
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

no rows selected

Elapsed: 00:00:00.00
SQL>

17) Create a PL/SQL function that uses the result cache by running the cre_func.sql
script.
SQL> @cre_func
SQL> create or replace function CACHEJFV_COUNT(v varchar2)
2 return number
3 result_cache relies_on (cachejfv)
4 is
5 cnt number;
6 begin
7 select count(*) into cnt

Oracle Database 11g: SQL Tuning Workshop A - 140


Practice 4-2: Using the Result Cache (continued)
8 from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
9 where c1.c=v and c2.c=v and c3.c=v and c4.c=v and c5.c=v and
c6.c=v and c7.c=v;
10 return cnt;
11 end;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

12 /

Function created.

Elapsed: 00:00:00.15
SQL>

--------------------------------------------------------------

create or replace function CACHEJFV_COUNT(v varchar2)


n s e
return number
i ce
result_cache relies_on (cachejfv)
b l el
is
cnt number;
fe r a
begin
a n s
select count(*) into cnt
o n -tr
an
from cachejfv c1,cachejfv c2,cachejfv c3,cachejfv c4,cachejfv
c5,cachejfv c6, cachejfv c7
h s
a deฺ
where c1.c=v and c2.c=v and c3.c=v and c4.c=v and c5.c=v and c6.c=v
and c7.c=v;
e )
s Gui
r ฺ
mi dent
return cnt;
end;
p ฺ
g Stu
/
d
b i a@ this
ain the result
18) Determine what(jis se cache by querying V$RESULT_CACHE_OBJECTS.
i a to u
b type,status,name,object_no,row_count,row_size_avg
i o A
SQL> select from
n
v$result_cache_objects order by 1;

Anto
e no rows selected
Jos Elapsed: 00:00:00.00
SQL>

19) Call the new function with ‘b’ as its argument. What do you observe?
a) It takes a long time to execute because the result is not cached yet. After
executing the function, the function’s result for the ‘b’ argument is cached.
SQL> select cachejfv_count('b') from dual;

CACHEJFV_COUNT('B')
-------------------
1

Elapsed: 00:00:01.56
SQL>
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

Oracle Database 11g: SQL Tuning Workshop A - 141


Practice 4-2: Using the Result Cache (continued)
TYPE STATUS NAME
OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------
ROW_SIZE_AVG
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

------------
Dependency Published QRC.CACHEJFV_COUNT
71475 0
0

Dependency Published QRC.CACHEJFV


71474 0
0

Result Published
n s e
"QRC"."CACHEJFV_COUNT"::8."CACHEJFV_COUNT"#8440831613f0f5d3 #1
i ce
0 1
b l el
4
fe ra
a n s
Elapsed: 00:00:00.00
o n -tr
an
SQL>
SQL>
h s
a What edoฺ you observe?
20) Call the new function with ‘b’ as its argument )
s Guagain. i d
a) This time the function executes m i
almostrฺeinstantaneously.
nt
p ฺ
g Sfrom d e
d
SQL> select cachejfv_count('b') tu dual;
b i a@ this
i a (ja use
CACHEJFV_COUNT('B')
-------------------
A b to1
ni o
t o
An SQL>
Elapsed: 00:00:00.00

e
Jos 21) Call the new function with ‘c’ as its argument again. What do you observe?
a) Again it takes a long time to execute the function because of the new value for the
argument. After execution, the second result is cached.
SQL> select cachejfv_count('c') from dual;

CACHEJFV_COUNT('C')
-------------------
1

Elapsed: 00:00:04.15
SQL> select type,status,name,object_no,row_count,row_size_avg from
v$result_cache_objects order by 1;

TYPE STATUS NAME


OBJECT_NO ROW_COUNT
---------- --------- -----------------------------------------------
--------------------------------------------------------------------
------------- ---------- ----------

Oracle Database 11g: SQL Tuning Workshop A - 142


Practice 4-2: Using the Result Cache (continued)
ROW_SIZE_AVG
------------
Dependency Published QRC.CACHEJFV_COUNT
71475 0
0
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Dependency Published QRC.CACHEJFV


71474 0
0

Result Published
"QRC"."CACHEJFV_COUNT"::8."CACHEJFV_COUNT"#8440831613f0f5d3 #1
0 1
4

Result Published
n s e
"QRC"."CACHEJFV_COUNT"::8."CACHEJFV_COUNT"#8440831613f0f5d3 #1
i ce
0 1
b l el
4
fer a
a n s
Elapsed: 00:00:00.01
n -tr
o
an
SQL>
SQL>
h
SQL> select cachejfv_count('c') from dual; s
a deฺ
e )
s Gui
CACHEJFV_COUNT('C') r ฺ
i ent
-------------------
p ฺ m
1
d g Stud
b i
Elapsed: 00:00:00.00 a@ this
SQL>
i a (ja use
A b to
i o
nton
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 143


Practices for Lesson 5
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 144


Practice 5-1: Extracting Execution Plans
In this practice, you use various methods to extract the execution plan used by the
optimizer to execute a query. Note that all scripts needed for this lab can be found in your
$HOME/solutions/Explain_Plan directory.
1) Connected as the oracle user from a terminal session, execute the ep_setup.sh
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

script. This script creates a new user called EP and a table called TEST used
throughout this lab.
[oracle@edrsr33p1-orcl Explain_Plan]$ ./ep_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 2 20:11:45 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
ice
Connected to:
b l el
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
fe ra
Production
a n s
-tr
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
o n
SQL>
s an
SQL> drop user ep cascade;
) h a deฺ
drop user ep cascade
r ฺ e s Gui
*
ERROR at line 1: p ฺ mi dent
d
ORA-01918: user 'EP' does g not S tu
exist

b i a@ this
SQL>
i a (ja use
A b usertoep identified by ep default tablespace users
SQL> create
n i o
temporary tablespace temp;
nt o
e A User created.

J o s SQL>
SQL> grant connect, resource, dba to ep;

Grant succeeded.

SQL>
SQL> connect ep/ep
Connected.
SQL>
SQL> drop table test purge;
drop table test purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table test(c number, d varchar2(500));

Table created.

Oracle Database 11g: SQL Tuning Workshop A - 145


Practice 5-1: Extracting Execution Plans (continued)
SQL>
SQL> begin
2 for i in 1..20000 loop
3 insert into test
values(1,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL>
n s e
SQL> create index test_c_indx on test(c);
i ce
b l el
Index created.
fer a
SQL>
a n s
SQL> exec dbms_stats.gather_schema_stats('EP');
n -tr
o
PL/SQL procedure successfully completed.
s an
) h a deฺ
SQL>
r ฺ e s Gui
ฺ m i ent
SQL> alter system flush shared_pool;

System altered.
d p
g Stud
SQL> b i a@ this
i a (ja flush
SQL> alter system
u se buffer_cache;
A b
altered. t
o
ni o
System

ntoSQL>
e A
J o s SQL> set echo off
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Explain_Plan]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Explain_Plan

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

Oracle Database 11g: SQL Tuning Workshop A - 146


Practice 5-1: Extracting Execution Plans (continued)
sqlplus / as sysdba @ep_setup.sql

--------------------------------------------------------------

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

drop user ep cascade;

create user ep identified by ep default tablespace users temporary


tablespace temp;

grant connect, resource, dba to ep;

connect ep/ep

drop table test purge;


n s e
i ce
create table test(c number, d varchar2(500));
b l el
begin
fe r a
for i in 1..20000 loop
a n s
insert into test
n - tr
o
an
values(1,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
h s
a deฺ
end loop;
e )
s Gui
r ฺ
mi dent
commit;
end;
p ฺ
g Stu
/
d
b i a@ ton
create index test_c_indx h istest(c);
i a (ja use
b to
exec dbms_stats.gather_schema_stats('EP');
A
o
ni system flush shared_pool;
n t oalter

e A
Jos
alter system flush buffer_cache;

set echo off


set term off

select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;

exit;

2) From the same terminal session (referred to as session 1 in the rest of this lab), be
ready to execute the ep_session_issue.sh script. Enter the command, but do
not execute it yet.
Session 1:
----------

[oracle@edrsr33p1-orcl Explain_Plan]$ ./ep_session_issue.sh

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 147


Practice 5-1: Extracting Execution Plans (continued)
#!/bin/bash

cd /home/oracle/solutions/Explain_Plan

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

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus ep/ep @ep_session_issue.sql

--------------------------------------------------------------

n s e
set echo off
i ce
set termout off
b l el
alter session set optimizer_mode=rule;
fe r a
a n s
set termout on
o n -tr
an
set echo on

set timing on
h s
a deฺ
e )
st2 where i
ut1.c=t2.c
select count(*) from test t1, test
i r ฺ n t G and t1.c=1;
m
exit;
d gpฺ Stude
b i a@ this
ja usession
3) From a second (terminal
a se (referred to as session 2 in the rest of this lab),
connect as i to user. After this, connect to a SQL*Plus session as the SYS
b the oracle
i
user.o A
From that SQL*Plus session, be ready to use SQL Monitoring to monitor the
t o n
e An execution plan used by session 1. You can execute the ep_monitoring.sql
script for that purpose. Enter the command, but do not execute it yet. Note: Ensure
Jos that you understand the coordination between both sessions by pre-reading steps 4
and 5 before you continue.
Session 2:
----------

[oracle@edrsr33p1-orcl Explain_Plan]$ sqlplus / as sysdba

SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 2 20:12:28 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> @ep_monitor

Oracle Database 11g: SQL Tuning Workshop A - 148


Practice 5-1: Extracting Execution Plans (continued)
--------------------------------------------------------------

set echo on
set long 10000000
set longchunksize 10000000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

set linesize 200


set pagesize 1000

exec dbms_lock.sleep(8);

select
dbms_sqltune.report_sql_monitor(sql_id=>'dkz7v96ym42c6',report_level
=>'ALL') from dual;

n s e
4) After you are ready in both the sessions, press [Enter] in session 1 to start the
ice
execution of the ep_session_issue.sh script. Note: Do not wait. Proceed with
b l el
the next step immediately.
fe ra
a n s
-tr
Session 1:
----------
o n
s an
[oracle@edrsr33p1-orcl Explain_Plan]$ ./ep_session_issue.sh
SQL*Plus: Release 11.1.0.6.0 - Production
h a on Wedd ฺ 2 20:12:47 2008
eApr
) i
Copyright (c) 1982, 2007, Oracle.i r ฺesAllnrights
t Gu reserved.
g p ฺm ude
d S t
@ s
thi Edition Release 11.1.0.6.0 -
ia Enterprise
Connected to:
Oracle Database b
Production ( j a 11g
e
b a to us Oracle Label Security, OLAP, Data Mining
iPartitioning,
A Application Testing options
With the
andoReal
i
to n
A n SQL>

o s e SQL> set timing on


J SQL>
SQL> select count(*) from test t1, test t2 where t1.c=t2.c and
t1.c=1;

5) In session 2, enter return to start the execution of the ep_monitor.sql script.


After the execution, enter “/” and go back to your SQL*Plus session as many times
as necessary until session 1 is done with its execution. What do you observe?
a) You can see that session 1 uses NESTED LOOPS on top of two INDEX RANGE
SCANS to execute the query. It takes approximately 47 seconds to execute session
1’s query. The time depends on your environment. The big advantage of SQL
Monitoring is that you can clearly see which steps in the execution plan take most
of the resources. In this case, you clearly see that you do only one scan of the
index, and that for each row returned, you execute another index scan to probe.
This is not really efficient. Also there is no costs information for this monitored
plan.

Oracle Database 11g: SQL Tuning Workshop A - 149


Practice 5-1: Extracting Execution Plans (continued)
Session 2:
----------

SQL> @ep_monitor
SQL> set long 10000000
SQL> set longchunksize 10000000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set linesize 200


SQL> set pagesize 1000
SQL>
SQL> exec dbms_lock.sleep(8);

PL/SQL procedure successfully completed.

SQL>
SQL> select
dbms_sqltune.report_sql_monitor(sql_id=>'dkz7v96ym42c6',report_level
n s e
=>'ALL') from dual;
i ce
b l el
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
f e ra
ns
:SESSID,REPORT_LEVEL=>'ALL')

- t r a
--------------------------------------------------------------------
n
--------------------------------------------------------------------
o
an
----------------------------------------------------------------
SQL Monitoring Report
h s
a deฺ
SQL Text
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------
p ฺ
--------------------------------------------------------------------
dg S tu t2 where t1.c=t2.c and t1.c=1
-------------------------------------------
select count(*) from@
b i a t h is
test t1, test

(ja use
--------------------------------------------------------------------

b a
--------------------------------------------------------------------
i to
-------------------------------------------
o A
t o ni Information
Global
An Status : EXECUTING
e
Jos
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:12:56

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 8.10 | 8.09 | 0.01 | 134K |
-----------------------------------------

SQL Plan Monitoring Details


====================================================================
=================================================================

Oracle Database 11g: SQL Tuning Workshop A - 150


Practice 5-1: Extracting Execution Plans (continued)
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Activity | Activity Detail |
| | | | (Estim) | |
Active(s) | Active | | (Actual) | (percent) | (sample #)
|
====================================================================
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

=================================================================
| 0 | SELECT STATEMENT | | | |
| | 1 | | | |
| 1 | SORT AGGREGATE | | | |
1 | +6 | 1 | 0 | 12.50 | Cpu (1) |
| -> 2 | NESTED LOOPS | | | |
3 | +8 | 1 | 79096K | | |
| -> 3 | INDEX RANGE SCAN | TEST_C_INDX | | |
3 | +8 | 1 | 3954 | | |
| -> 4 | INDEX RANGE SCAN | TEST_C_INDX | | |
n s e
9 | +2 | 3955 | 79096K | 87.50 | Cpu (7) |
i ce
====================================================================
b l el
=================================================================
fer a
a n s
SQL>
o n -tr
an
SQL> /

h s
a deฺ
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
e )
s Gui
r ฺ
i ent
--------------------------------------------------------------------
ฺ m
--------------------------------------------------------------------
p d
d g t u
----------------------------------------------------------------
S
SQL Monitoring Report
b i a@ this
SQL Text
i a (ja use
b to
--------------------------------------------------------------------
A
i o
--------------------------------------------------------------------
n
nto-------------------------------------------
e A select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1

J o s --------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : EXECUTING
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:12:58

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 10 | 10 | 0.01 | 167K |
-----------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 151


Practice 5-1: Extracting Execution Plans (continued)

SQL Plan Monitoring Details


====================================================================
=================================================================
| Id | Operation | Name | Rows | Cost |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Time | Start | Starts | Rows | Activity | Activity Detail |


| | | | (Estim) | |
Active(s) | Active | | (Actual) | (percent) | (sample #)
|
====================================================================
=================================================================
| 0 | SELECT STATEMENT | | | |
| | 1 | | | |
| 1 | SORT AGGREGATE | | | |
1 | +6 | 1 | 0 | 10.00 | Cpu (1) |
n s e
| -> 2 | NESTED LOOPS | | | |
i ce
5 | +8 | 1 | 96151K | | |
b l el
| -> 3 |
5 | +8 |
INDEX RANGE SCAN | TEST_C_INDX |
1 | 4807 | |
| |
fe
| r a
| -> 4 | INDEX RANGE SCAN | TEST_C_INDX | |
a n s |
11 | +2 | 4808 | 96151K | 90.00 | Cpu (9)
o n -tr |

an
====================================================================
=================================================================
h s
a deฺ
e )
s Gui
SQL> / r ฺ
i ent
p ฺ m d
d g t u
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
S
b i a@ this
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------

i a (ja use
--------------------------------------------------------------------
b to
----------------------------------------------------------------
A
o
ni Text
SQL Monitoring Report

ntoSQL
e A
J o s --------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : EXECUTING
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:13:00

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |

Oracle Database 11g: SQL Tuning Workshop A - 152


Practice 5-1: Extracting Execution Plans (continued)
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 12 | 12 | 0.01 | 201K |
-----------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL Plan Monitoring Details


====================================================================
=================================================================
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Activity | Activity Detail |
| | | | (Estim) | |
Active(s) | Active | | (Actual) | (percent) | (sample #)
|
====================================================================
=================================================================
n s e
| 0 | SELECT STATEMENT | | | |
i ce
| | 1 | | | |
b l el
|
1 |
1 | SORT AGGREGATE
+6 | 1 |
|
0 |
|
8.33 | Cpu (1)
| |
fe
| r a
| -> 2 | NESTED LOOPS | | |
a n s |
7 | +8 | 1 | 113M | |
o n -tr |

an
| -> 3 | INDEX RANGE SCAN | TEST_C_INDX | | |
7 | +8 | 1 | 5664 | | |
| -> 4 |
h s
a deฺ
INDEX RANGE SCAN | TEST_C_INDX | | |
13 | +2 | 5665 | 113M |
e )
s Gui
91.67 | Cpu (11) |
r ฺ
mi dent
====================================================================

p ฺ
=================================================================
g Stu
d
SQL> / b i a@ this
i a (ja use
b to
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
A
i o
:SESSID,REPORT_LEVEL=>'ALL')
n
nto--------------------------------------------------------------------
e A --------------------------------------------------------------------

J o s ----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : EXECUTING
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54

Oracle Database 11g: SQL Tuning Workshop A - 153


Practice 5-1: Extracting Execution Plans (continued)
Last Refresh Time : 04/02/2008 20:13:08

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 20 | 20 | 0.04 | 334K |
-----------------------------------------

SQL Plan Monitoring Details


====================================================================
=================================================================
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Activity | Activity Detail |
| | | | (Estim) | |
n s e
Active(s) | Active | | (Actual) | (percent) | (sample #)
i ce
|
b l el
====================================================================
fe
================================================================= r a
| 0 | SELECT STATEMENT | | |
a n|s
| | 1 | | |
n - tr |
o
an
| 1 | SORT AGGREGATE | | | |
1 | +6 | 1 | 0 | 5.00 | Cpu (1) |
| -> 2 | NESTED LOOPS |
h |s
a deฺ | |
15 | +8 | 1 | 182M |
e )
s Gui| |
r ฺ
mi dent
| -> 3 | INDEX RANGE SCAN | TEST_C_INDX | | |
15 | +8 | 1 | ฺ
9082 |
p
g Stu
| |
| -> 4 |
d
INDEX RANGE SCAN | TEST_C_INDX | | |
21 | +2 | 9083 |
b i a@ this
182M | 95.00 | Cpu (19) |
====================================================================

i a (ja use
=================================================================

Ab to
o n io
SQL> /
nt
e A
J o s DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : EXECUTING
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6

Oracle Database 11g: SQL Tuning Workshop A - 154


Practice 5-1: Extracting Execution Plans (continued)
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:13:17
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 28 | 28 | 0.05 | 468K |
-----------------------------------------

SQL Plan Monitoring Details


====================================================================
n s e
=================================================================
i ce
| Id | Operation | Name | Rows | Cost |
b l el
Time
| |
| Start | Starts | Rows
|
| Activity | Activity Detail |
| (Estim) | |
fer a
Active(s) | Active | | (Actual) | (percent) | n s
(sample #)
a
|
n - tr
o
an
====================================================================
=================================================================
| 0 | SELECT STATEMENT |
h |s
a deฺ | |
| | 1 | |
e )
s Gui
| |
r ฺ
mi dent
| 1 | SORT AGGREGATE | | | |
1 | +6 | 1 |
p ฺ
0 |
g Stu
3.45 | Cpu (1) |
| -> 2 | NESTED LOOPS
d | | | |
24 |
| -> 3 |
+8 |
b
1 |
i a@ this
250M | |
INDEX RANGE SCAN | TEST_C_INDX | | |
|

24 | +8 |
i a (ja use
1 | 12518 | | |
| -> 4 |
Ab to
INDEX RANGE SCAN | TEST_C_INDX | | |

o n io
30 | +2 | 12519 | 250M | 96.55 | Cpu (28) |
====================================================================
nt
e A =================================================================

J o s
SQL> /

DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information

Oracle Database 11g: SQL Tuning Workshop A - 155


Practice 5-1: Extracting Execution Plans (continued)
Status : EXECUTING
Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Execution Started : 04/02/2008 20:12:46


First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:13:29

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 40 | 40 | 0.06 | 669K |
-----------------------------------------
n s e
i ce
b l el
SQL Plan Monitoring Details
fer
==================================================================== a
n s
=================================================================
a
| Id | Operation | Name | Rows
n - tr
| Cost |
o
an
Time | Start | Starts | Rows | Activity | Activity Detail |
| | | | (Estim) | |
Active(s) | Active |
h s
a deฺ
| (Actual) | (percent) | (sample #)
|
e )
s Gui
r ฺ
mi dent
====================================================================

p ฺ
=================================================================
g Stu
|
d
0 | SELECT STATEMENT | | | |
|
| 1 |
| 1 |
b i a@ this
SORT AGGREGATE
|
|
|
| |
|
|
35 | +6 |
i a (ja use
1 | 0 | 4.88 | Cpu (2) |
| -> 2 |
Ab to
NESTED LOOPS | | | |

o n io
36 |
| -> 3 |
+8 | 1 | 353M | |
INDEX RANGE SCAN | TEST_C_INDX | | |
|

nt
e A 36 | +8 | 1 | 17663 | | |

J o s | -> 4 |
42 |
INDEX RANGE SCAN | TEST_C_INDX |
+2 | 17664 | 353M | 95.12 | Cpu (39)
| |
|
====================================================================
=================================================================

SQL> /

DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1

Oracle Database 11g: SQL Tuning Workshop A - 156


Practice 5-1: Extracting Execution Plans (continued)
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : DONE (ALL ROWS)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Instance ID : 1
Session ID : 138
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777222
Plan Hash Value : 1643938535
Execution Started : 04/02/2008 20:12:46
First Refresh Time : 04/02/2008 20:12:54
Last Refresh Time : 04/02/2008 20:13:34

-------------------------------------------------
n s e
| Elapsed | Cpu | Other | Fetch | Buffer |
ice
| Time(s) | Time(s) | Waits(s) | Calls | Gets |
b l el
-------------------------------------------------
| 46 | 46 | 0.06 | 1 | 760K |
fe r a
-------------------------------------------------
a n s
o n -tr
SQL Plan Monitoring Details
s an
h a deฺ
====================================================================
)
s G| uRows i
===============================================================
e
| rฺName
| iActivity t
| Id | Operation | Cost | Time
| Start | Starts | Rows ฺm n
p | tude | (Estim) |
| Activity Detail |
| |
Active(s) | Active |@ d g | S
|

| b i a t h is (Actual) | (percent) | (sample #)

i a (ja use
====================================================================
b to
===============================================================
A
1ni|
o
| 0 | SELECT STATEMENT | | | |

nto| 1 | SORT AGGREGATE |


+48 | 1 | 1 | | |

e A | | |

J o s 43 |
| 2 |
+6 | 1 |
NESTED LOOPS |
1 | 4.35 | Cpu (2)
| | |
|

41 | +8 | 1 | 400M | | |
| 3 | INDEX RANGE SCAN | TEST_C_INDX | | |
41 | +8 | 1 | 20000 | | |
| 4 | INDEX RANGE SCAN | TEST_C_INDX | | |
47 | +2 | 20000 | 400M | 95.65 | Cpu (44) |
====================================================================
===============================================================

SQL>

6) After approximately 47 seconds (depending on your environment), you should see the
following output in your session 1:

Session 1:
----------

Oracle Database 11g: SQL Tuning Workshop A - 157


Practice 5-1: Extracting Execution Plans (continued)

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

Elapsed: 00:00:47.21
SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

[oracle@edrsr33p1-orcl Explain_Plan]$
n s e
i ce
7) From session 1, connect as the EP user in the SQL*Plus session.
b l el
Session 1:
fe ra
----------
a n s
o
[oracle@edrsr33p1-orcl Explain_Plan]$ sqlplus ep/ep n -tr
n
SQL*Plus: Release 11.1.0.6.0 - Production s onaWed Apr 2 20:14:03 2008
) h a deฺ
Copyright (c) 1982, 2007, Oracle.ฺes
r G uireserved.
All rights

p ฺ mi dent
d g Stu
a@
Connected to:
b i
Oracle Database 11g
t h is Edition Release 11.1.0.6.0 -
Enterprise
Production
a (ja useOracle Label Security, OLAP, Data Mining
With the iPartitioning,
A
and Real to Testing options
b Application
ni o
ntoSQL>
e A
J o s 8) Use PLAN_TABLE to determine the execution plan of the query that was executed in
step 4. What do you observe?
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;
a) This time the execution plan uses a hash join on top of two index fast full scans.
Session 1:
----------

SQL> @ep_explain
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> explain plan for
2 select count(*) from test t1, test t2 where t1.c=t2.c and
t1.c=1;

Explained.

SQL>
SQL> select * from table(dbms_xplan.display);

Oracle Database 11g: SQL Tuning Workshop A - 158


Practice 5-1: Extracting Execution Plans (continued)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
Plan hash value: 3253233075
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
------------------
| 0 | SELECT STATEMENT | | 1 | 6 | 2131
(99)| 00:00:22 |
| 1 | SORT AGGREGATE | | 1 | 6 |
n s e
| |
i ce
|* 2 | HASH JOIN | | 400M| 2288M| 2131
b l el
(99)| 00:00:22 |
|* 3 | INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 | 13
fe r a
(0)| 00:00:01 |
a n s
|* 4 | -
INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 |
n tr
13
o
an
(0)| 00:00:01 |
--------------------------------------------------------------------
------------------
h s
a deฺ
e )
s Gui id):
r ฺ
mi dent
Predicate Information (identified by operation

p ฺ
---------------------------------------------------
g Stu
d
b i a@ this
2 - access("T1"."C"="T2"."C")
3 - filter("T1"."C"=1)

i a (ja use
4 - filter("T2"."C"=1)

A bselected.to
ni o
18 rows

ntoSQL>
e A
J o s --------------------------------------------------------------

set echo on

set linesize 200 pagesize 1000

explain plan for


select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;

select * from table(dbms_xplan.display);

9) Now, you want to monitor the previous execution plan to compare it with the one
generated in step 4. In addition, you want to make sure you use the correct plan this
time. So, in your session 1, start autotrace, and be ready to execute the following
query. Do not execute it yet as you need to start SQL Monitoring in your session 2:
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;

Oracle Database 11g: SQL Tuning Workshop A - 159


Practice 5-1: Extracting Execution Plans (continued)

Session 1:
----------

SQL> set autotrace on


SQL> @ep_execute
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

10) From your session 2, be ready to execute your SQL Monitoring command again. Do
not execute it yet though.

Session 2:
----------

SQL> @ep_monitor
n s e
i ce
--------------------------------------------------------------
b l el
fe r a
set echo on
a n s
-tr
set long 10000000
set longchunksize 10000000
o n
an
set linesize 200
set pagesize 1000
h s
a deฺ
exec dbms_lock.sleep(8);
e )
s Gui
r ฺ
select
p ฺ mi dent
dg Stu
dbms_sqltune.report_sql_monitor(sql_id=>'dkz7v96ym42c6',report_level
=>'ALL') from dual; @
b i a t h is
i a (ja use
execution tofoyour query from session 1 by pressing [Enter]. Note: Move to
11) Start the b
i
next o A
stepwithout waiting.
t o n
e An
Jos
Session 1:
----------

SQL> @ep_execute
SQL> set echo on
SQL>
SQL> set timing on
SQL>
SQL> select count(*) from test t1, test t2 where t1.c=t2.c and
t1.c=1;

--------------------------------------------------------------

set echo on

set timing on

select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;

Oracle Database 11g: SQL Tuning Workshop A - 160


Practice 5-1: Extracting Execution Plans (continued)
12) From your session 2, start monitoring your query by pressing the return key. After the
query is executed, enter “/” and go back to your SQL*Plus session as many times as
necessary until session 1 is done with its execution. What do you observe?
a) You can see that the optimizer uses a hash join on top of two index fast full scans.
Looking at the various reports, you can clearly see how the optimizer processes a
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

hash join by reading the driving index in memory first. This operation is quick.
Though you cannot see it run, it is already done the first time you can look at it.
Then the probe in performed on the index again. This operation takes more time.
Also note that cost information is provided in the execution plan.

Session 2:
----------
n s e
ice
el
SQL> @ep_monitor
SQL> set echo on
a b l
SQL> set long 10000000
fe r
SQL> set longchunksize 10000000
a n s
-tr
SQL> set linesize 200
SQL> set pagesize 1000
o n
an
SQL>
SQL>exec dbms_lock.sleep(8);
h s
a deฺ
e
PL/SQL procedure successfully completed.)
s Gui
r ฺ
SQL> p ฺ mi dent
SQL> select d g Stu
=>'ALL') from a b ia@ this
dbms_sqltune.report_sql_monitor(sql_id=>'dkz7v96ym42c6',report_level

(j se
dual;

b i a to u
A
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
o
ni
:SESSID,REPORT_LEVEL=>'ALL')
to--------------------------------------------------------------------
A n --------------------------------------------------------------------
o s e ----------------------------------------------------------------
J SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : EXECUTING
Instance ID : 1
Session ID : 124
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777223
Plan Hash Value : 3253233075
Execution Started : 04/02/2008 20:14:41
First Refresh Time : 04/02/2008 20:14:49

Oracle Database 11g: SQL Tuning Workshop A - 161


Practice 5-1: Extracting Execution Plans (continued)
Last Refresh Time : 04/02/2008 20:14:49

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 6.29 | 6.29 | 0.00 | 8 |


-----------------------------------------

SQL Plan Monitoring Details


====================================================================
====================================================================
==========
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Memory | Activity | Activity
n s e
Detail |
i ce
| | | | (Estim) | |
b l el
Active(s) | Active |
(sample #) |
| (Actual) | | (percent) |
fer a
n s
====================================================================
a
- tr
====================================================================
n
o
an
==========
| 0 | SELECT STATEMENT | | | 2131 |
| | 1 | | |
h s
a deฺ |
|
e )
s Gui
r ฺ
mi dent
| 1 | SORT AGGREGATE | | 1 | |
8 | +0 | 1 |
p ฺ
0 |
g Stu
| 25.00 | Cpu (2)
|
d
| -> 2 |
8 | +1 |
HASH JOIN
b i
1 | a@ this |
113M | 1138K |
| 400M | 2131 |
75.00 | Cpu (6)
|
i a (ja use
| -> 3 |
Ab to
INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |

o
1 |
|n io +8 | 1 | 20000 | | |

nt
e A | -> 4 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |

J o s 1 |
|
+8 | 1 | 5632 | | |

====================================================================
====================================================================
==========

SQL>
SQL> /

DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 162


Practice 5-1: Extracting Execution Plans (continued)
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

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

Status : EXECUTING
Instance ID : 1
Session ID : 124
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777223
Plan Hash Value : 3253233075
Execution Started : 04/02/2008 20:14:41
First Refresh Time : 04/02/2008 20:14:49
Last Refresh Time : 04/02/2008 20:14:51

n s e
-----------------------------------------
i ce
| Elapsed | Cpu | Other | Buffer |
b l el
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
fer a
| 7.86 | 7.86 | 0.00 | 10 |
a n s
-----------------------------------------
o n -tr
s an
SQL Plan Monitoring Details
) h a deฺ
r ฺ e s Gui
====================================================================

========== ฺ m i ent
====================================================================

| Id |
d g
Operation p t u|d Name | Rows | Cost |
Time
a@ this
| Start | Starts | S
Rows | Memory | Activity | Activity
Detail | b i
| |
a ja use
(Active | | (Estim) | |
Active(s) i
b#) | to
| | | (Actual) | | (percent) |
i o A
(sample
n
to====================================================================
A n ====================================================================

o s e ==========
J |
|
0 | SELECT STATEMENT
| 1 | |
|
|
|
|
| 2131 |

|
| 1 | SORT AGGREGATE | | 1 | |
8 | +0 | 1 | 0 | | 20.00 | Cpu (2)
|
| -> 2 | HASH JOIN | | 400M | 2131 |
10 | +1 | 1 | 133M | 1138K | 80.00 | Cpu (8)
|
| 3 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |
1 | +8 | 1 | 20000 | | |
|
| -> 4 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |
3 | +8 | 1 | 6656 | | |
|
====================================================================
====================================================================
==========

Oracle Database 11g: SQL Tuning Workshop A - 163


Practice 5-1: Extracting Execution Plans (continued)
SQL> /

DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
:SESSID,REPORT_LEVEL=>'ALL')
--------------------------------------------------------------------
--------------------------------------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

----------------------------------------------------------------
SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
n s e
-------------------------------------------
i ce
b l el
Global Information
Status : EXECUTING
fer a
Instance ID : 1
a n s
Session ID : 124
n -tr
o
an
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777223
Plan Hash Value : 3253233075
h s
a deฺ
Execution Started : )
s Gui
04/02/2008 20:14:41
e
r ฺ
mi dent
First Refresh Time : 04/02/2008 20:14:49
Last Refresh Time : ฺ
04/02/2008 20:14:55
p
g Stu
d
| Elapsed | b
Cpu a@ t
| Other is | Buffer |
-----------------------------------------
i h
| Time(s) | (
a ja u|seWaits(s) | Gets |
Time(s)
b i to12 | 0.00 | 15 |
-----------------------------------------
o A
ni
| 12 |

nto-----------------------------------------
e A
J o s SQL Plan Monitoring Details
====================================================================
====================================================================
==========
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Memory | Activity | Activity
Detail |
| | | | (Estim) | |
Active(s) | Active | | (Actual) | | (percent) |
(sample #) |
====================================================================
====================================================================
==========
| 0 | SELECT STATEMENT | | | 2131 |
| | 1 | | | |
|
| 1 | SORT AGGREGATE | | 1 | |
8 | +0 | 1 | 0 | | 14.29 | Cpu (2)
|

Oracle Database 11g: SQL Tuning Workshop A - 164


Practice 5-1: Extracting Execution Plans (continued)
| -> 2 | HASH JOIN | | 400M | 2131 |
14 | +1 | 1 | 184M | 1138K | 85.71 | Cpu (12)
|
| 3 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |
1 | +8 | 1 | 20000 | | |
|
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| -> 4 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |


7 | +8 | 1 | 9216 | | |
|
====================================================================
====================================================================
==========

SQL> /

n s e
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
i ce
:SESSID,REPORT_LEVEL=>'ALL')
b l el
fer a
--------------------------------------------------------------------
--------------------------------------------------------------------
n s
----------------------------------------------------------------
a
SQL Monitoring Report
o n -tr
SQL Text
s an
h a deฺ
--------------------------------------------------------------------
)
r ฺ e s Gui
--------------------------------------------------------------------

select count(*) from test t1,ฺ m i t2enwhere


-------------------------------------------
test t t1.c=t2.c and t1.c=1
d p
g Stu d
--------------------------------------------------------------------

b i a@ this
--------------------------------------------------------------------
-------------------------------------------

i a (ja use
b to : EXECUTING
Global Information
A
i o
Status

ntonInstance ID : 1

e A Session ID : 124

J o s SQL ID
SQL Execution ID
:
:
dkz7v96ym42c6
16777223
Plan Hash Value : 3253233075
Execution Started : 04/02/2008 20:14:41
First Refresh Time : 04/02/2008 20:14:49
Last Refresh Time : 04/02/2008 20:15:05

-----------------------------------------
| Elapsed | Cpu | Other | Buffer |
| Time(s) | Time(s) | Waits(s) | Gets |
-----------------------------------------
| 22 | 22 | 0.01 | 28 |
-----------------------------------------

SQL Plan Monitoring Details


====================================================================
====================================================================
==========

Oracle Database 11g: SQL Tuning Workshop A - 165


Practice 5-1: Extracting Execution Plans (continued)
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Memory | Activity | Activity
Detail |
| | | | (Estim) | |
Active(s) | Active | | (Actual) | | (percent) |
(sample #) |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

====================================================================
====================================================================
==========
| 0 | SELECT STATEMENT | | | 2131 |
| | 1 | | | |
|
| 1 | SORT AGGREGATE | | 1 | |
8 | +0 | 1 | 0 | | 8.33 | Cpu (2)
|
| -> 2 | HASH JOIN | | 400M | 2131 |
n s e
24 | +1 | 1 | 317M | 1138K | 91.67 | Cpu (22)
i ce
|
b l el
|
1 |
3 |
+8 |
INDEX FAST FULL SCAN | TEST_C_INDX |
1 | 20000 | |
20000 |
|
13 |
fer a
|
a n s
| -> 4 | INDEX FAST FULL SCAN | TEST_C_INDX |
o n -tr
20000 | 13 |

an
17 | +8 | 1 | 15872 | | |
|
h s
a deฺ
====================================================================
)
s Gui
====================================================================
e
r ฺ
mi dent
==========

p ฺ
g Stu
d
SQL> /
b i a@ this
i a (ja use
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'DKZ7V96YM42C6',SESSION_ID=>
b to
:SESSID,REPORT_LEVEL=>'ALL')
A
i o
--------------------------------------------------------------------
n
nto--------------------------------------------------------------------
e A ----------------------------------------------------------------

J o s SQL Monitoring Report

SQL Text
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------------------------------

Global Information
Status : DONE (ALL ROWS)
Instance ID : 1
Session ID : 124
SQL ID : dkz7v96ym42c6
SQL Execution ID : 16777223
Plan Hash Value : 3253233075
Execution Started : 04/02/2008 20:14:41
First Refresh Time : 04/02/2008 20:14:49
Last Refresh Time : 04/02/2008 20:15:11

Oracle Database 11g: SQL Tuning Workshop A - 166


Practice 5-1: Extracting Execution Plans (continued)
-------------------------------------------------
| Elapsed | Cpu | Other | Fetch | Buffer |
| Time(s) | Time(s) | Waits(s) | Calls | Gets |
-------------------------------------------------
| 28 | 28 | 0.01 | 1 | 37 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

-------------------------------------------------

SQL Plan Monitoring Details


====================================================================
====================================================================
========
| Id | Operation | Name | Rows | Cost |
Time | Start | Starts | Rows | Memory | Activity | Activity
Detail |
n s e
| | | | (Estim) | |
ice
Active(s) | Active | | (Actual) | (Max) | (percent) |
b l el
(sample #) |
====================================================================
fe ra
====================================================================
a n s
========
n - tr
o
an
| 0 | SELECT STATEMENT | | | 2131 |
1 | +30 | 1 | 1 | | |
|
h s
a deฺ
| 1 | SORT AGGREGATE |
e )
s Gui | 1 | |
r ฺ
mi dent
31 | +0 | 1 | 1 | | 10.00 | Cpu (3)
|
p ฺ
g Stu
| 2 | HASH JOIN
d | | 400M | 2131 |
30 |
|
+1 |
b
1 |
i a@ this
400M | 1138K | 90.00 | Cpu (27)

| 3 |
i a (ja use
INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |
1 |
Ab
+8 |
to
1 | 20000 | | |

o
|
n io
| 4 | INDEX FAST FULL SCAN | TEST_C_INDX | 20000 | 13 |
nt
e A 23 | +8 | 1 | 20000 | | |

J o s |
====================================================================
====================================================================
========

SQL>

13) When your query is executed, what do you observe in your session 1?
a) Session 1 also reports the same execution plan as the one you observed in session
2.

Session 1:
----------

COUNT(*)

Oracle Database 11g: SQL Tuning Workshop A - 167


Practice 5-1: Extracting Execution Plans (continued)
----------
400000000

Elapsed: 00:00:30.70

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

----------------------------------------------------------
Plan hash value: 3253233075

--------------------------------------------------------------------
------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
------------------
| 0 | SELECT STATEMENT | | 1 | 6 | 2131
n s e
(99)| 00:00:22 |
i ce
| 1 | SORT AGGREGATE | | 1 | 6 |
b l el
|
|* 2 |
|
HASH JOIN | |
fe
400M| 2288M| 2131 r a
(99)| 00:00:22 |
a n s
|* 3 | - tr
INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 |
n 13
o
an
(0)| 00:00:01 |
|* 4 | INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 | 13
(0)| 00:00:01 |
h s
a deฺ
)
s Gui
--------------------------------------------------------------------
e
------------------ r ฺ
i ent
p ฺ m d operation id):
d g
Predicate Information (identified
S t uby

b i a@ this
---------------------------------------------------

i a (ja use
2 - access("T1"."C"="T2"."C")
b to
3 - filter("T1"."C"=1)
A
i o
4 - filter("T2"."C"=1)

nton
e A
J o s Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
90 consistent gets
0 physical reads
0 redo size
418 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>

14) In session 1, disable autotrace.

Session 1:
----------

Oracle Database 11g: SQL Tuning Workshop A - 168


Practice 5-1: Extracting Execution Plans (continued)
SQL> set autotrace off
SQL>

15) From your session 1, how can you ensure that you gather all execution plan statistics
for the following query without changing any session parameters? Implement your
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

solution.
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1;

Session 1:
----------

SQL> @ep_execute_with_all
SQL> set echo on
SQL>
n s e
SQL> set timing on
ice
SQL>
select /*+ gather_plan_statistics */ count(*) from test t1, bl
el
SQL>
test t2 where t1.c=t2.c and t1.c=1; f e ra
t r a ns
COUNT(*)
o n -
an
----------
400000000
h s
a deฺ
Elapsed: 00:00:59.95
e )
s Gui
r ฺ
mi dent
SQL>
p
g Stuฺ
16) From your session 1, retrieve
d
@ hexecution
all plans corresponding to all the queries you
executed since the b i a t i s
( j abeginning
s eof this lab. What is your conclusion?
u
bia waytoto find out all the plans is to look at the content of the SGA using
a) The easiest
A
iothe dbms_xplan.display_cursor function. First, you must determine the
o n
t SQL_Ids used to represent your queries. You essentially have two queries, and
e An one that has two children. You should now understand what happened at step 4.
Jos The fact that there was no cost information is probably due to the use of the rule-
based optimizer instead of the cost-based one.

Session 1:
----------

SQL> @ep_retrieve_all_plans
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> col sql_text format a50
SQL>
SQL> select sql_id,plan_hash_value,sql_text from v$sql where
sql_text like '%from test t1, test t2%';

SQL_ID PLAN_HASH_VALUE SQL_TEXT


------------- --------------- --------------------------------------
------------

Oracle Database 11g: SQL Tuning Workshop A - 169


Practice 5-1: Extracting Execution Plans (continued)
dkz7v96ym42c6 3253233075 select count(*) from test t1, test t2
where t1.c=t
2.c and t1.c=1

dkz7v96ym42c6 1643938535 select count(*) from test t1, test t2


where t1.c=t
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2.c and t1.c=1

8w580dd6ncgqw 3253233075 select /*+ gather_plan_statistics */


count(*) from
test t1, test t2 where t1.c=t2.c and
t1.c=1

0w0va2d7hhtxa 3253233075 explain plan for select count(*) from


test t1, tes
t t2 where t1.c=t2.c and t1.c=1
n s e
i ce
dd09kf5dnp1gt 903671040 select sql_id,plan_hash_value,sql_text
b l el
from v$sql
fer
where sql_text like '%from test t1, a
test t2%'
a n s
o n -tr
an
32fqwuk16uf23 3253233075 EXPLAIN PLAN SET
STATEMENT_ID='PLUS2140495' FOR se
h s
a deฺ
lect count(*) from test t1, test t2
where t1.c=t2.
e )
s Gui
r ฺ
mi dent
c and t1.c=1

p ฺ
g Stu
d
6 rows selected.
b i a@ this
i a (ja use
Elapsed: 00:00:00.02
SQL>
A b * from to
ni o
SQL> select

ntotable(dbms_xplan.display_cursor('dkz7v96ym42c6',null,'TYPICAL'));
e A
J o s PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID dkz7v96ym42c6, child number 0
-------------------------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1

Plan hash value: 3253233075

--------------------------------------------------------------------
------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
------------------
| 0 | SELECT STATEMENT | | | | 2131
(100)| |
| 1 | SORT AGGREGATE | | 1 | 6 |
| |

Oracle Database 11g: SQL Tuning Workshop A - 170


Practice 5-1: Extracting Execution Plans (continued)
|* 2 | HASH JOIN | | 400M| 2288M| 2131
(99)| 00:00:22 |
|* 3 | INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 | 13
(0)| 00:00:01 |
|* 4 | INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 | 13
(0)| 00:00:01 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("T1"."C"="T2"."C")
3 - filter("T1"."C"=1)
4 - filter("T2"."C"=1)

n s e
SQL_ID dkz7v96ym42c6, child number 1
i ce
-------------------------------------
b l el
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1
fer a
Plan hash value: 1643938535
a n s
n -tr
o
an
------------------------------------------
| Id | Operation | Name |
------------------------------------------
h s
a deฺ
| 0 | SELECT STATEMENT |
e )
s Gui
|
r ฺ
mi dent
| 1 | SORT AGGREGATE | |
| 2 | NESTED LOOPS |
p ฺ
g Stu
|
|* 3 |
d
INDEX RANGE SCAN| TEST_C_INDX |
|* 4 |
b i a@ this
INDEX RANGE SCAN| TEST_C_INDX |
------------------------------------------

i a (ja use(identified by operation id):


b
Predicate
A to
Information
o
ni 3 - access("T1"."C"=1)
---------------------------------------------------

n t o
e A
Jos
4 - access("T1"."C"="T2"."C")

Note
-----
- rule based optimizer used (consider using cbo)

49 rows selected.

Elapsed: 00:00:00.04
SQL>
SQL> select * from
table(dbms_xplan.display_cursor('8w580dd6ncgqw',null,'ADVANCED
ALLSTATS LAST'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 8w580dd6ncgqw, child number 0
-------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 171


Practice 5-1: Extracting Execution Plans (continued)
select /*+ gather_plan_statistics */ count(*) from test t1, test t2
where t1.c=t2.c and t1.c=1

Plan hash value: 3253233075

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

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Starts | E-Rows |E-
Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem
| 1Mem | Used-Mem |
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------
| 1 | SORT AGGREGATE | | 1 | 1 |
6 | | | 1 |00:00:59.94 | 90 | |
n s e
| |
i ce
|* 2 | HASH JOIN | | 1 | 400M|
b l el
2288M| 2131 (99)| 00:00:22 |
1155K| 1155K| 1115K (0)|
400M|00:00:00.01 | 90 |
fer a
|* 3 | INDEX FAST FULL SCAN| TEST_C_INDX | n s
1 | 20000 |
a
60000 | 13 (0)| 00:00:01 | 20000 |00:00:00.02 |
o n -tr 45 |

an
| | |
|* 4 | INDEX FAST FULL SCAN| TEST_C_INDX | 1 | 20000 |
60000 | 13
h s
a deฺ
(0)| 00:00:01 | 20000 |00:00:00.16 | 45 |
| | |
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------

p ฺ
--------------------------------------------------------------------
g Stu
-------------------
d
@ Alias
Query Block Namebi/aObjectth is (identified by operation id):
i a (ja use
-------------------------------------------------------------

A b to
i o
1 - SEL$1

nton 34 -- SEL$1 / T1@SEL$1

e A SEL$1 / T2@SEL$1

J o s Outline Data
-------------

/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.1.0.6')
DB_VERSION('11.1.0.6')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
INDEX_FFS(@"SEL$1" "T1"@"SEL$1" ("TEST"."C"))
INDEX_FFS(@"SEL$1" "T2"@"SEL$1" ("TEST"."C"))
LEADING(@"SEL$1" "T1"@"SEL$1" "T2"@"SEL$1")
USE_HASH(@"SEL$1" "T2"@"SEL$1")
END_OUTLINE_DATA
*/

Predicate Information (identified by operation id):


---------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 172


Practice 5-1: Extracting Execution Plans (continued)
2 - access("T1"."C"="T2"."C")
3 - filter("T1"."C"=1)
4 - filter("T2"."C"=1)

Column Projection Information (identified by operation id):


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

1 - (#keys=0) COUNT(*)[22]
2 - (#keys=1)
3 - "T1"."C"[NUMBER,22]
4 - "T2"."C"[NUMBER,22]

55 rows selected.

Elapsed: 00:00:00.12
n s e
SQL>
i ce
b l el
--------------------------------------------------------------
fe r a
set echo on
a n s
o n -tr
an
set linesize 200 pagesize 1000

col sql_text format a50


h s
a deฺ
e )
s from i where sql_text
uv$sql
r ฺ G
mi dent
select sql_id,plan_hash_value,sql_text

p ฺ
like '%from test t1, test t2%';
g Stu
d
select * from
b i a@ this
table(dbms_xplan.display_cursor('dkz7v96ym42c6',null,'TYPICAL'));

i a (ja use
b
select *
A
from
to
ni o
table(dbms_xplan.display_cursor('8w580dd6ncgqw',null,'ADVANCED

ntoALLSTATS LAST'));

e A
J o s 17) From session 1, try to retrieve your execution plans from the Automatic Workload
Repository. What happens and why?
a) You can use the previously found SQL_Ids to search through the
DBA_HIST_SQLTEXT view. You should see that right now, none of your
queries were stored in the AWR.

Session 1:
----------

SQL> @ep_retrieve_awr
SQL> set echo on
SQL>
SQL> set linesize 200
SQL>
SQL> SELECT SQL_ID, SQL_TEXT FROM dba_hist_sqltext
2 WHERE SQL_ID in ('dkz7v96ym42c6','8w580dd6ncgqw');

no rows selected

Oracle Database 11g: SQL Tuning Workshop A - 173


Practice 5-1: Extracting Execution Plans (continued)
SQL>

Elapsed: 00:00:00.01
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

set echo on

set linesize 200

SELECT SQL_ID, SQL_TEXT FROM dba_hist_sqltext


WHERE SQL_ID in ('dkz7v96ym42c6','8w580dd6ncgqw');

n s e
18) How can you ensure that you retrieve your queries from the Automatic Workload
i ce
Repository? Implement your solution.
b l el
a) You must flush the SGA information to the AWR. You can use fer a
DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT for this purpose. a n s
o n -tr
Session 1: a n
----------
h s
a deฺ
e )
s Gui
r ฺ
mi dent
SQL> @ep_save_awr
SQL> set echo on
p ฺ
g Stu
SQL>
d
b i a@ this
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT('ALL');

(ja successfully
PL/SQL procedure
i a u se completed.

A to
b 00:00:02.27
ni o
Elapsed:

ntoSQL>
e A
J o s --------------------------------------------------------------

set echo on

EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT('ALL');

19) Verify that your solution works.

Session 1:
----------

SQL> @ep_show_awr
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT PLAN_TABLE_OUTPUT
2 FROM
3 TABLE (DBMS_XPLAN.DISPLAY_AWR('dkz7v96ym42c6'));

Oracle Database 11g: SQL Tuning Workshop A - 174


Practice 5-1: Extracting Execution Plans (continued)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID dkz7v96ym42c6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------
select count(*) from test t1, test t2 where t1.c=t2.c and t1.c=1

Plan hash value: 1643938535

------------------------------------------
| Id | Operation | Name |
------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | SORT AGGREGATE | |
n s e
| 2 | NESTED LOOPS | |
i ce
| 3 | INDEX RANGE SCAN| TEST_C_INDX |
b l el
| 4 | INDEX RANGE SCAN| TEST_C_INDX |
------------------------------------------
fer a
a n s
Note
o n -tr
an
-----
- rule based optimizer used (consider using cbo)
h s
a deฺ
SQL_ID dkz7v96ym42c6
e )
s Gui
-------------------- r ฺ
i t2enwheret t1.c=t2.c and t1.c=1

select count(*) from test t1,
p mtest d
d g Stu
b i a@ this
Plan hash value: 3253233075

i a (ja use
--------------------------------------------------------------------
b to
------------------
| Id A
n i o | Operation | Name | Rows | Bytes | Cost

nto--------------------------------------------------------------------
(%CPU)| Time |

e A
J o s ------------------
| 0 | SELECT STATEMENT | | | | 543
(100)| |
| 1 | SORT AGGREGATE | | 1 | 6 |
| |
| 2 | HASH JOIN | | 100M| 572M| 543
(98)| 00:00:06 |
| 3 | INDEX FAST FULL SCAN| TEST_C_INDX | 10000 | 30000 | 8
(0)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN| TEST_C_INDX | 10000 | 30000 | 8
(0)| 00:00:01 |
--------------------------------------------------------------------
------------------

36 rows selected.

Elapsed: 00:00:00.04
SQL>
SQL> SELECT PLAN_TABLE_OUTPUT
2 FROM

Oracle Database 11g: SQL Tuning Workshop A - 175


Practice 5-1: Extracting Execution Plans (continued)
3 TABLE
(DBMS_XPLAN.DISPLAY_AWR('8w580dd6ncgqw',null,null,'TYPICAL ALLSTATS
LAST'));

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

--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 8w580dd6ncgqw
--------------------
select /*+ gather_plan_statistics */ count(*) from test t1, test t2
where t1.c=t2.c and t1.c=1

Plan hash value: 3253233075

--------------------------------------------------------------------
n s e
-------------------
i ce
| Id | Operation | Name | E-Rows |E-Bytes| Cost
b l el
(%CPU)| E-Time |
fer
-------------------------------------------------------------------- a
-------------------
a n s
| 0 | SELECT STATEMENT | |
n -
| tr |
o
an
2131 (100)| |
| 1 | SORT AGGREGATE | | 1 | 6 |
| |
h s
a deฺ
| 2 | HASH JOIN |
e )
s Gui | 400M| 2288M|
r ฺ
mi dent
2131 (99)| 00:00:22 |
| 3 | ฺ
INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 |
p
g Stu
13 (0)| 00:00:01 |
d
|
13
4 |
b
(0)| 00:00:01 |i a@ this
INDEX FAST FULL SCAN| TEST_C_INDX | 20000 | 60000 |

i a (ja use
--------------------------------------------------------------------
b to
-------------------
A
o n io
Note
nt
e A -----

J o s - Warning: basic plan statistics not available. These are only


collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or
system level

23 rows selected.

Elapsed: 00:00:00.02
SQL>

--------------------------------------------------------------

set echo on

set linesize 200 pagesize 1000

SELECT PLAN_TABLE_OUTPUT
FROM
TABLE (DBMS_XPLAN.DISPLAY_AWR('dkz7v96ym42c6'));

Oracle Database 11g: SQL Tuning Workshop A - 176


Practice 5-1: Extracting Execution Plans (continued)
SELECT PLAN_TABLE_OUTPUT
FROM
TABLE (DBMS_XPLAN.DISPLAY_AWR('8w580dd6ncgqw',null,null,'TYPICAL
ALLSTATS LAST'));
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

20) Exit from both SQL*Plus sessions, and clean up your environment by executing the
ep_cleanup.sh script from one of your terminal sessions.

Session 1:
----------

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release
n s e
11.1.0.6.0 - Production
i ce
With the Partitioning, Oracle Label Security, OLAP, Data Mining
b l el
and Real Application Testing options
[oracle@edrsr33p1-orcl Explain_Plan]$ ./ep_cleanup.sh fe r a
a n s
n -tr
SQL*Plus: Release 11.1.0.6.0 - Production on Wed Apr 2 20:19:37 2008
o
Copyright (c) 1982, 2007, Oracle. All rights
n
a reserved.
h s
a deฺ
e )
s Gui
r ฺ
miEdition t
Connected to:
p
Oracle Database 11g Enterprise ฺ d e nRelease 11.1.0.6.0 -
Production g
dOracle S t u
@ s
thi options
ia Testing
With the Partitioning, Label Security, OLAP, Data Mining
a b
and Real Application
j e
SQL> b ia ( to us
i
SQL> A user ep cascade;
o drop
to n
A n User dropped.

o s e
J SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Explain_Plan]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Explain_Plan

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

Oracle Database 11g: SQL Tuning Workshop A - 177


Practice 5-1: Extracting Execution Plans (continued)
export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @ep_cleanup.sql


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

--------------------------------------------------------------

set echo on

drop user ep cascade;

exit;

Do not forget to exit from your session 2:


n s e
i ce
Session 2: b l el
----------
fer a
a n s
SQL> exit
n - tr
o
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
s an
) h a deฺ
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
r ฺ
[oracle@edrsr33p1-orcl Explain_Plan]$e s Gui
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 178


Practices for Lesson 6
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 179


Practice 6-1: Star Schema Tuning
In this practice, you optimize a query to use star transformation and access the benefits of
using this optimizer technique.
1) From a terminal session, connected as the oracle user, execute the
setup_star_schema_lab.sh script located in your
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

/home/oracle/solutions/Star_Schema_Tuning directory.

[oracle@edrsr33p1-orcl Star_Schema_Tuning]$
./setup_star_schema_lab.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Mar 21 00:17:20


2008
n s e
Copyright (c) 1982, 2007, Oracle. All rights reserved.
ice
b l el
fe ra
Connected to:
a n s
-tr
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
o n
an
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
h s
a deฺ
SQL> SQL> SQL> SQL>
e )
s Gui
r ฺ
mi dent
Grant succeeded.
p ฺ
g Stu
SQL> SQL> d
User altered.
b i a@ this
i a (ja usefrom Oracle Database 11g Enterprise Edition
SQL> SQL> Disconnected
to - Production
Release b11.1.0.6.0
A
n o
i Real Application Testing
With the Partitioning, Oracle Label Security, OLAP, Data Mining

nt oand options

e A [oracle@edrsr33p1-orcl Star_Schema_Tuning]$

J o s --------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Star_Schema_Tuning

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

sqlplus / as sysdba <<FIN!

set echo on

grant dba to sh;

Oracle Database 11g: SQL Tuning Workshop A - 180


Practice 6-1: Star Schema Tuning (continued)
alter user sh identified by sh account unlock;

FIN!

2) From the same terminal window, start a SQL*Plus session connected as the SH user
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

and do not disconnect from it until this lab finishes. Before executing the following
SQL statement, ensure that you flush both the shared pool and the buffer cache to
avoid caching issues as much as possible. After this, analyze the execution of the
following query (You can use the query.sql script.):

SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,


SUM(s.amount_sold) sales_amount
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id AND
n s e
ce
s.cust_id = c.cust_id AND
s.channel_id = ch.channel_id AND
el i
c.cust_state_province = 'CA' AND
a b l
ch.channel_desc in ('Internet','Catalog') AND
fe r
a n s
t.calendar_quarter_desc IN ('1999-01','1999-02','2000-03','2000-04')

-tr
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

o n
What are your conclusions?
s an
a) As you can see in the output of the executionhplan,
) i eฺ seems to use a large
a thisdquery
number of bytes to access the SALESฺe
r s Basically,
table. G u the optimizer performs a
i t
enbest way to handle it.
ฺmnot ubedthe
full scan of this table. This might
d g p t
@ s S
[oracle@edrsr33p1-orcl
j a b e thi
ia Star_Schema_Tuning]$ sqlplus sh/sh

SQL*Plus:ia
( u s
2008 A b t o
Release 11.1.0.6.0 - Production on Fri Mar 21 00:31:10

o n io
nt Copyright (c) 1982, 2007, Oracle. All rights reserved.
e A
J o s
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> @first_run
SQL>
SQL> alter system flush shared_pool;

System altered.

SQL> alter system flush buffer_cache;

System altered.

SQL>
SQL> set pagesize 200
SQL> set linesize 250

Oracle Database 11g: SQL Tuning Workshop A - 181


Practice 6-1: Star Schema Tuning (continued)
SQL> set timing on
SQL> set autotrace on
SQL>
SQL> SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
2 SUM(s.amount_sold) sales_amount
3 FROM sales s, times t, customers c, channels ch
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4 WHERE s.time_id = t.time_id


5 AND s.cust_id = c.cust_id
6 AND s.channel_id = ch.channel_id
7 AND c.cust_state_province = 'CA'
8 AND ch.channel_desc in ('Internet','Catalog')
9 AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-03','2000-04')
10 GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

CHANNEL_CLASS CUST_CITY CALENDA


SALES_AMOUNT
-------------------- ------------------------------ ------- --------
n s e
----
i ce
Indirect
987.3
Quartzhill 1999-01
b l el
Indirect Arbuckle 1999-02
fe r a

a n s
Montara 1999-02
n tr
-1999-02 1618.01
Indirect Quartzhill o
412.83
s an
) h a deฺ
41 rows selected.
r ฺ e s Gui
Elapsed: 00:00:00.68
p ฺ mi dent
d g Stu
a@ this
Execution Plan

i
----------------------------------------------------------

b
(ja use
Plan hash value: 1647000731

b i a
-------------------------------------------------------------------------------------------------------------
| Id | Operation
to | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |

|
i A
-------------------------------------------------------------------------------------------------------------

o0 | SELECT STATEMENT | | 1144 | 96096 | 935 (4)| 00:00:12 | | |

ton
| 1 | HASH GROUP BY | | 1144 | 96096 | 935 (4)| 00:00:12 | | |
|* 2 | HASH JOIN | | 6231 | 511K| 934 (3)| 00:00:12 | | |

A n |* 3 |
|* 4 |
TABLE ACCESS FULL
HASH JOIN
| CHANNELS |
|
2 |
| 12462 |
42 |
766K|
3
930
(0)| 00:00:01 |
(3)| 00:00:12 |
|
|
|
|

o s e | 5 | PART JOIN FILTER CREATE | :BF0000 | 365 | 5840 | 18 (0)| 00:00:01 | | |

J
|* 6 | TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01 | | |
|* 7 | HASH JOIN | | 49822 | 2286K| 911 (3)| 00:00:11 | | |
|* 8 | TABLE ACCESS FULL | CUSTOMERS | 383 | 9958 | 406 (1)| 00:00:05 | | |
| 9 | PARTITION RANGE JOIN-FILTER| | 918K| 18M| 498 (4)| 00:00:06 |:BF0000|:BF0000|
| 10 | TABLE ACCESS FULL | SALES | 918K| 18M| 498 (4)| 00:00:06 |:BF0000|:BF0000|
-------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 -
access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
3 -
filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')
4 -
access("S"."TIME_ID"="T"."TIME_ID")
6 -
filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
7 - access("S"."CUST_ID"="C"."CUST_ID")
8 - filter("C"."CUST_STATE_PROVINCE"='CA')

Statistics
----------------------------------------------------------
16783 recursive calls
0 db block gets
5491 consistent gets
2021 physical reads
0 redo size
1888 bytes sent via SQL*Net to client
442 bytes received via SQL*Net from client

Oracle Database 11g: SQL Tuning Workshop A - 182


Practice 6-1: Star Schema Tuning (continued)
4 SQL*Net roundtrips to/from client
141 sorts (memory)
0 sorts (disk)
41 rows processed

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

SQL>

--------------------------------------------------------------

set echo on

alter system flush shared_pool;


alter system flush buffer_cache;

set pagesize 200


n s e
set linesize 250
i ce
set timing on
b l el
set autotrace on
fe r a
SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
a n s
SUM(s.amount_sold) sales_amount
n - tr
o
an
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id
AND s.cust_id = c.cust_id
h s
a deฺ
AND s.channel_id = ch.channel_id
e )
s Gui
r ฺ
mi dent
AND c.cust_state_province = 'CA'
AND ฺ
ch.channel_desc in ('Internet','Catalog')
p
g Stu
AND
d
t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
b i a@ this
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

i a (ja use
Ab to
3) Without modifying the SH schema, how can you improve the execution plan for the
o n io
query mentioned in step 2? Verify your solution and explain why it is probably a
nt
e A better solution.
J o s a) Enable star transformation in your session. In this step, you do not want to use a
temporary table for the star transformation. Looking at the previous execution
plan, the optimizer estimates the data that is to be manipulated in megabytes.
Using the star transformation as follows, the estimation is now expressed in
kilobytes. That is why this new execution plan is probably a much better
alternative. However, note that this time the CUSTOMERS table is accessed using
full scan twice. If the table is larger, the impact is significant.
SQL> @second_run
SQL> set echo on
SQL>
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.10
SQL> alter system flush buffer_cache;

Oracle Database 11g: SQL Tuning Workshop A - 183


Practice 6-1: Star Schema Tuning (continued)
System altered.

Elapsed: 00:00:00.20
SQL>
SQL> set pagesize 200
SQL> set linesize 250
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set timing on


SQL> set autotrace on
SQL>
SQL>
SQL> ALTER SESSION SET star_transformation_enabled=TEMP_DISABLE;

Session altered.

Elapsed: 00:00:00.00
SQL>
n s e
SQL>
i ce
SQL> SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
b l el
2 SUM(s.amount_sold) sales_amount
3 FROM sales s, times t, customers c, channels ch
fer a
4 WHERE s.time_id = t.time_id
a n s
5 AND s.cust_id = c.cust_id
o n -tr
an
6 AND s.channel_id = ch.channel_id
7 AND c.cust_state_province = 'CA'
8 AND
h s
a deฺ
ch.channel_desc in ('Internet','Catalog')
9 AND )
s Gui
t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
e
r ฺ
mi dent
03','2000-04')

p ฺ
10 GROUP BY ch.channel_class, c.cust_city,
g Stu
d
t.calendar_quarter_desc;
@ his
CHANNEL_CLASS bia CUST_CITY
t CALENDA
SALES_AMOUNT(ja e
b ia to us------------------------------ ------- --------
--------------------
---- A
o n io
Indirect San Francisco 2000-04
nt 13227.99
e A
J o s Indirect
1319
Montara 2000-04

Indirect Cloverdale 2000-04


7.27

Indirect Pala 2000-03
Indirect Montara 1999-02
1618.01
Indirect Quartzhill 1999-02
412.83
Indirect San Francisco 1999-01
3058.27
Indirect Pala 1999-01
3263.93

41 rows selected.

Elapsed: 00:00:00.33

Execution Plan
----------------------------------------------------------
Plan hash value: 2525768690

Oracle Database 11g: SQL Tuning Workshop A - 184


Practice 6-1: Star Schema Tuning (continued)
------------------------------------------------------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 3 | 252 | 983 (1)| 00:00:12 |
| |
| 1 | HASH GROUP BY | | 3 | 252 | 983 (1)| 00:00:12 |
| |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 2 | HASH JOIN | | 3 | 252 | 982 (1)| 00:00:12 |


| |
|* 3 | HASH JOIN | | 7 | 441 | 978 (1)| 00:00:12 |
| |
|* 4 | HASH JOIN | | 27 | 1269 | 960 (1)| 00:00:12 |
| |
|* 5 | TABLE ACCESS FULL | CUSTOMERS | 383 | 9958 | 406 (1)| 00:00:05 |
| |
| 6 | PARTITION RANGE SUBQUERY | | 507 | 10647 | 553 (1)| 00:00:07
|KEY(SQ)|KEY(SQ)|
| 7 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES | 507 | 10647 | 553 (1)| 00:00:07
|KEY(SQ)|KEY(SQ)|
| 8 | BITMAP CONVERSION TO ROWIDS | | | | | |
| |
| 9 | BITMAP AND | | | | | |
|
| 10 |
|
BITMAP MERGE | | | | | |
n s e
| |
ice
el
| 11 | BITMAP KEY ITERATION | | | | | |
|
| 12 |
|
BUFFER SORT | | | | |
a b|l
|
|* 13 |
|
TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3
fe r
(0)| 00:00:01 |
| |

a n s
tr
|* 14 | BITMAP INDEX RANGE SCAN | SALES_CHANNEL_BIX | | | |
|KEY(SQ)|KEY(SQ)|
| 15 | BITMAP MERGE |
o n - | | | | |

an
| |
| 16 | BITMAP KEY ITERATION | | | | | |
|
| 17 |
|
BUFFER SORT
h
| s
a deฺ | | | | |
|
|* 18 |
|
TABLE ACCESS FULL
e )
s Gui
| TIMES | 365 | 5840 | 18 (0)| 00:00:01 |
| |
r ฺ
mi dent
|* 19 | BITMAP INDEX RANGE SCAN | SALES_TIME_BIX | | | |
|KEY(SQ)|KEY(SQ)|
| 20 | BITMAP MERGE
p ฺ
g Stu
| | | | | |
| |
d
a@ this
| 21 | BITMAP KEY ITERATION | | | | | |
|
| 22 |
|

b i
BUFFER SORT | | | | | |

(ja use
| |

a
|* 23 | TABLE ACCESS FULL | CUSTOMERS | 383 | 9958 | 406 (1)| 00:00:05 |
| |
b i to
|* 24 |

io A
|KEY(SQ)|KEY(SQ)|
BITMAP INDEX RANGE SCAN | SALES_CUST_BIX | | | |

t o|n
|* 25 |
|
TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01 |

A n |* 26 |
| |
TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3 (0)| 00:00:01 |

o s e ------------------------------------------------------------------------------------------------------------------
----------

J Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
3 - access("S"."TIME_ID"="T"."TIME_ID")
4 - access("S"."CUST_ID"="C"."CUST_ID")
5 - filter("C"."CUST_STATE_PROVINCE"='CA')
13 - filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')
14 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
18 - filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
19 - access("S"."TIME_ID"="T"."TIME_ID")
23 - filter("C"."CUST_STATE_PROVINCE"='CA')
24 - access("S"."CUST_ID"="C"."CUST_ID")
25 - filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
26 - filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')

Note
-----
- star transformation used for this statement

Statistics
----------------------------------------------------------
17362 recursive calls
0 db block gets
36930 consistent gets

Oracle Database 11g: SQL Tuning Workshop A - 185


Practice 6-1: Star Schema Tuning (continued)
2130 physical reads
0 redo size
1896 bytes sent via SQL*Net to client
442 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
147 sorts (memory)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 sorts (disk)
41 rows processed

SQL>

--------------------------------------------------------------

set echo on

n s e
alter system flush shared_pool;
ice
alter system flush buffer_cache;
b l el
set pagesize 200
fe ra
set linesize 250
a n s
set timing on
o n -tr
an
set autotrace on

h s
a deฺ
)
s Gui
ALTER SESSION SET star_transformation_enabled=TEMP_DISABLE;
e
r ฺ
i ent
p ฺ m
SELECT ch.channel_class, gc.cust_city, d t.calendar_quarter_desc,
d S t u
SUM(s.amount_sold)
b
FROM sales s, timesi a@ t h is c, channels ch
sales_amount
t, customers

i a (ja == t.time_id
WHERE s.time_id
u se
AND
AND A
b
s.cust_id
to = ch.channel_id
c.cust_id

n o
i c.cust_state_province = 'CA'
s.channel_id

ntoAND
e A AND ch.channel_desc in ('Internet','Catalog')

J o s AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-


03','2000-04')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

4) How would you enhance the previous optimization without changing the SH schema?
a) Let the optimizer decide if it is better to use a temporary table. You can try to set
the STAR_TRANSFORMATION_ENABLED parameter to TRUE.

SQL> @third_run
SQL> set echo on
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.10
SQL> alter system flush buffer_cache;

Oracle Database 11g: SQL Tuning Workshop A - 186


Practice 6-1: Star Schema Tuning (continued)
System altered.

Elapsed: 00:00:00.21
SQL>
SQL> set pagesize 200
SQL> set linesize 250
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set timing on


SQL> set autotrace on
SQL>
SQL> ALTER SESSION SET star_transformation_enabled=TRUE;

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL> SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
n s e
2 SUM(s.amount_sold) sales_amount
ice
3 FROM sales s, times t, customers c, channels ch
b l el
4 WHERE s.time_id = t.time_id
5 AND s.cust_id = c.cust_id
fe r a
6 AND s.channel_id = ch.channel_id
a n s
7 AND c.cust_state_province = 'CA'
o n -tr
an
8 AND ch.channel_desc in ('Internet','Catalog')
9 AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
h s
a deฺ
10 GROUP BY ch.channel_class, c.cust_city,
e )
s Gui
t.calendar_quarter_desc; r ฺ
i ent
p ฺ m d
CHANNEL_CLASS
d g
CUST_CITY
S t u CALENDA
SALES_AMOUNT
b
--------------------i a@------------------------------
t h is ------- --------
j a e
ia ( to usSan Francisco
----
Indirect
A b 2000-04

o io
13227.99
n
Indirect Montara 2000-04
nt 1319
e A
J o s …
Indirect San Francisco 1999-01
3058.27
Indirect Pala 1999-01
3263.93

41 rows selected.

Elapsed: 00:00:00.30

Execution Plan
----------------------------------------------------------
Plan hash value: 163104418

------------------------------------------------------------------------------------------------------------------
------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
| Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------
------------------
| 0 | SELECT STATEMENT | | 3 | 219 | 985 (1)| 00:00:12
| | |
| 1 | TEMP TABLE TRANSFORMATION | | | | |
| | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9D660F_8947E | | | |
| | |
|* 3 | TABLE ACCESS FULL | CUSTOMERS | 383 | 9958 | 406 (1)| 00:00:05
| | |

Oracle Database 11g: SQL Tuning Workshop A - 187


Practice 6-1: Star Schema Tuning (continued)
| 4 | HASH GROUP BY | | 3 | 219 | 579 (2)| 00:00:07
| | |
|* 5 | HASH JOIN | | 3 | 219 | 578 (1)| 00:00:07
| | |
|* 6 | HASH JOIN | | 7 | 364 | 574 (1)| 00:00:07
| | |
|* 7 | HASH JOIN | | 28 | 1008 | 556 (1)| 00:00:07
| | |
| 8 | TABLE ACCESS FULL | SYS_TEMP_0FD9D660F_8947E | 383 | 5745 | 2 (0)| 00:00:01
| | |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 9 | PARTITION RANGE SUBQUERY | | 507 | 10647 | 553 (1)| 00:00:07


|KEY(SQ)|KEY(SQ)|
| 10 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES | 507 | 10647 | 553 (1)| 00:00:07
|KEY(SQ)|KEY(SQ)|
| 11 | BITMAP CONVERSION TO ROWIDS | | | | |
| | |
| 12 | BITMAP AND | | | | |
| | |
| 13 | BITMAP MERGE | | | | |
| | |
| 14 | BITMAP KEY ITERATION | | | | |
| | |
| 15 | BUFFER SORT | | | | |
| | |
|* 16 | TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3 (0)| 00:00:01
|
|* 17 |
| |
BITMAP INDEX RANGE SCAN | SALES_CHANNEL_BIX | | | |
n s e
|KEY(SQ)|KEY(SQ)|
ice
el
| 18 | BITMAP MERGE | | | | |
|
| 19 |
| |
BITMAP KEY ITERATION | | | |
a
|
b l
|
| 20 |
| |
BUFFER SORT | | | |
fe r |
| | |

a n s
tr
|* 21 | TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01
|
|* 22 |
| |
BITMAP INDEX RANGE SCAN
n -
| SALES_TIME_BIX
o
| | | |

an
|KEY(SQ)|KEY(SQ)|
| 23 | BITMAP MERGE | | | | |
|
| 24 |
| |
BITMAP KEY ITERATION
h
| s
a deฺ | | | |
|
| 25 |
| |
BUFFER SORT
e )
s Gui
| | | | |
| | |
r ฺ
mi dent
| 26 | TABLE ACCESS FULL | SYS_TEMP_0FD9D660F_8947E | 1 | 13 | 2 (0)| 00:00:01
|
|* 27 |
| |

p ฺ
g Stu
BITMAP INDEX RANGE SCAN | SALES_CUST_BIX | | | |
|KEY(SQ)|KEY(SQ)|
d
a@ this
|* 28 | TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01
|
|* 29 |
| |

b i
TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3 (0)| 00:00:01

(ja use
| | |

a
------------------------------------------------------------------------------------------------------------------

b i
------------------

to
io A
Predicate Information (identified by operation id):

t o n
---------------------------------------------------

n 3 - filter("C"."CUST_STATE_PROVINCE"='CA')

eA
5 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")

Jos
6 - access("S"."TIME_ID"="T"."TIME_ID")
7 - access("S"."CUST_ID"="C0")
16 - filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')
17 - access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
21 - filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
22 - access("S"."TIME_ID"="T"."TIME_ID")
27 - access("S"."CUST_ID"="C0")
28 - filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
29 - filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')

Note
-----
- star transformation used for this statement

Statistics
----------------------------------------------------------
17911 recursive calls
19 db block gets
35577 consistent gets
2157 physical reads
1616 redo size
1896 bytes sent via SQL*Net to client
442 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client

Oracle Database 11g: SQL Tuning Workshop A - 188


Practice 6-1: Star Schema Tuning (continued)
155 sorts (memory)
0 sorts (disk)
41 rows processed

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

--------------------------------------------------------------

set echo on

alter system flush shared_pool;


alter system flush buffer_cache;

set pagesize 200


n s e
set linesize 250
i ce
set timing on
b l el
set autotrace on
fe r a
ALTER SESSION SET star_transformation_enabled=TRUE;
a n s
n - tr
o
an
SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
h s
a deฺ
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id
e )
s Gui
r ฺ
mi dent
AND s.cust_id = c.cust_id
AND ฺ
s.channel_id = ch.channel_id
p
g Stu
AND
d
c.cust_state_province = 'CA'
AND
AND b i a@ this
ch.channel_desc in ('Internet','Catalog')
t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
i a (ja use
b to
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;
A
o n io
t
n5) How do you eliminate one table access on the CUSTOMERS table from the previous
e A
J o s execution plan for the same SELECT statement seen in step 3?
a) Create a bitmap join index between the SALES and CUSTOMERS tables.
6) Try to apply your finding. What happens and why?
a) Because the CUSTOMERS_PK primary key constraint is not enforced, it is not
possible to create a bitmap join index between the SALES and CUSTOMERS
tables.

SQL> @create_bji.sql
SQL>
SQL> CREATE BITMAP INDEX sales_c_state_bjix ON
sales(customers.cust_state_province) FROM sales, customers WHERE
sales.cust_id=customers.cust_id
2 LOCAL NOLOGGING COMPUTE STATISTICS;
CREATE BITMAP INDEX sales_c_state_bjix ON
sales(customers.cust_state_province) FROM sales, customers WHERE
sales.cust_id=customers.cust_id

Oracle Database 11g: SQL Tuning Workshop A - 189


Practice 6-1: Star Schema Tuning (continued)
*
ERROR at line 1:
ORA-25954: missing primary key or unique constraint on dimension
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

--------------------------------------------------------------

set echo on

CREATE BITMAP INDEX sales_c_state_bjix ON


sales(customers.cust_state_province) FROM sales, customers WHERE
sales.cust_id=customers.cust_id
LOCAL NOLOGGING COMPUTE STATISTICS;
n s e
i ce
7) Fix the issue you found and apply your solution from step 5 again. b l el
fer a
a) You need to ENABLE VALIDATE the CUSTOMERS_PK constraint before you
a n s
can create the bitmap join index.
o n -tr
s an
SQL> @recreate_bji
) h a deฺ
SQL>
SQL> alter table customers enableฺe
i
s Gucustomers_pk;
r
mi dent
constraint

Table altered. p ฺ
g Stu
d
SQL>
b i a@ this
SQL> CREATE (
a ja uINDEX
BITMAP se sales_c_state_bjix ON
b i to
sales(customers.cust_state_province) FROM sales, customers WHERE
o A
sales.cust_id=customers.cust_id
i
nton 2 LOCAL NOLOGGING COMPUTE STATISTICS;
e A Index created.
J o s
SQL>
SQL>

--------------------------------------------------------------

set echo on

alter table customers enable constraint customers_pk;

CREATE BITMAP INDEX sales_c_state_bjix ON


sales(customers.cust_state_province) FROM sales, customers WHERE
sales.cust_id=customers.cust_id
LOCAL NOLOGGING COMPUTE STATISTICS;

8) Verify that you solved the problem from step 5.

SQL> @fourth_run

Oracle Database 11g: SQL Tuning Workshop A - 190


Practice 6-1: Star Schema Tuning (continued)
SQL> set echo on
SQL>
SQL> ALTER SESSION SET star_transformation_enabled=TRUE;

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

SQL>
SQL> alter system flush shared_pool;

System altered.

SQL> alter system flush buffer_cache;

System altered.

SQL>
n s e
SQL> set pagesize 200
ice
SQL> set linesize 250
b l el
SQL> set timing on
SQL> set autotrace on
fe r a
SQL>
a n s
-
SQL> SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
n tr
o
an
2 SUM(s.amount_sold) sales_amount
3 FROM sales s, times t, customers c, channels ch
4 WHERE s.time_id = t.time_id
h s
a deฺ
5 AND s.cust_id = c.cust_id
e )
s Gui
r ฺ
mi dent
6 AND s.channel_id = ch.channel_id
7 AND ฺ
c.cust_state_province = 'CA'
p
g Stu
8 AND
d
ch.channel_desc in ('Internet','Catalog')
9 AND
03','2000-04') b i a@ this
t.calendar_quarter_desc IN ('1999-01','1999-02','2000-

i a (ja use
10 GROUP BY ch.channel_class, c.cust_city,
b to
t.calendar_quarter_desc;
A
o n io
CHANNEL_CLASS CUST_CITY CALENDA
t
e An SALES_AMOUNT

Jos
-------------------- ------------------------------ ------- --------
----
Indirect Quartzhill 1999-01
987.3
Indirect Arbuckle 1999-02
241.2
Indirect Pala 2000-03

Indirect Montara 1999-02
1618.01
Indirect Quartzhill 1999-02
412.83

41 rows selected.

Elapsed: 00:00:00.23

Execution Plan
----------------------------------------------------------
Plan hash value: 632695221

------------------------------------------------------------------------------------------------------------------
-----------

Oracle Database 11g: SQL Tuning Workshop A - 191


Practice 6-1: Star Schema Tuning (continued)
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------
-----------
| 0 | SELECT STATEMENT | | 498 | 41832 | 557 (2)| 00:00:07 |
| |
| 1 | HASH GROUP BY | | 498 | 41832 | 557 (2)| 00:00:07 |
| |
|* 2 | HASH JOIN | | 498 | 41832 | 556 (2)| 00:00:07 |
| |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 3 | TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3 (0)| 00:00:01 |


| |
|* 4 | HASH JOIN | | 996 | 62748 | 552 (1)| 00:00:07 |
| |
|* 5 | TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01 |
| |
|* 6 | HASH JOIN | | 3984 | 182K| 533 (1)| 00:00:07 |
| |
|* 7 | TABLE ACCESS FULL | CUSTOMERS | 383 | 9958 | 406 (1)| 00:00:05 |
| |
| 8 | PARTITION RANGE SUBQUERY | | 73467 | 1506K| 126 (1)| 00:00:02
|KEY(SQ)|KEY(SQ)|
| 9 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES | 73467 | 1506K| 126 (1)| 00:00:02
|KEY(SQ)|KEY(SQ)|
| 10 | BITMAP CONVERSION TO ROWIDS | | | | | |
|
| 11 |
|
BITMAP AND | | | | | |
n s e
| |
ice
el
| 12 | BITMAP MERGE | | | | | |
|
| 13 |
|
BITMAP KEY ITERATION | | | | |
a b l
|
|
| 14 |
|
BUFFER SORT | | | | |
fe r |
| |

a n s
tr
|* 15 | TABLE ACCESS FULL | CHANNELS | 2 | 42 | 3 (0)| 00:00:01 |
|
|* 16 |
|
BITMAP INDEX RANGE SCAN
n -
| SALES_CHANNEL_BIX |
o
| | |

an
|KEY(SQ)|KEY(SQ)|
| 17 | BITMAP MERGE | | | | | |
|
| 18 |
|
BITMAP KEY ITERATION
h
| s
a deฺ | | | | |
|
| 19 |
|
BUFFER SORT
e )
s Gui
| | | | | |
| |
r ฺ
mi dent
|* 20 | TABLE ACCESS FULL | TIMES | 365 | 5840 | 18 (0)| 00:00:01 |
|
|* 21 |
|

p ฺ
g Stu
BITMAP INDEX RANGE SCAN | SALES_TIME_BIX | | | |
|KEY(SQ)|KEY(SQ)|
d
a@ this
|* 22 | BITMAP INDEX SINGLE VALUE | SALES_C_STATE_BJIX | | | |
|KEY(SQ)|KEY(SQ)|

b i
------------------------------------------------------------------------------------------------------------------

(ja use
-----------

b i a to
Predicate Information (identified by operation id):

A
---------------------------------------------------

io
t o n2
3
-
-
access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')

A n 4
5
-
-
access("S"."TIME_ID"="T"."TIME_ID")
filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR

o s e 6 -
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
access("S"."CUST_ID"="C"."CUST_ID")

J 7
15
16
-
-
-
filter("C"."CUST_STATE_PROVINCE"='CA')
filter("CH"."CHANNEL_DESC"='Catalog' OR "CH"."CHANNEL_DESC"='Internet')
access("S"."CHANNEL_ID"="CH"."CHANNEL_ID")
20 - filter("T"."CALENDAR_QUARTER_DESC"='1999-01' OR "T"."CALENDAR_QUARTER_DESC"='1999-02' OR
"T"."CALENDAR_QUARTER_DESC"='2000-03' OR "T"."CALENDAR_QUARTER_DESC"='2000-04')
21 - access("S"."TIME_ID"="T"."TIME_ID")
22 - access("S"."SYS_NC00008$"='CA')

Note
-----
- star transformation used for this statement

Statistics
----------------------------------------------------------
18151 recursive calls
0 db block gets
8759 consistent gets
2002 physical reads
0 redo size
1888 bytes sent via SQL*Net to client
442 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
154 sorts (memory)
0 sorts (disk)

Oracle Database 11g: SQL Tuning Workshop A - 192


Practice 6-1: Star Schema Tuning (continued)
41 rows processed

SQL>

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

set echo on

ALTER SESSION SET star_transformation_enabled=TRUE;

alter system flush shared_pool;


alter system flush buffer_cache;

set pagesize 200


set linesize 250
set timing on
n s e
set autotrace on
i ce
b l el
SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
fer a
FROM sales s, times t, customers c, channels ch
a n s
WHERE s.time_id = t.time_id
o n -tr
an
AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id
AND c.cust_state_province = 'CA'
h s
a deฺ
AND )
s Gui
ch.channel_desc in ('Internet','Catalog')
e
r ฺ
mi dent
AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
p ฺ
g Stu
d
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

b i a@ this
9) Determine how(the
a ja system
u e dynamically determine which SALES partitions to
scould
i to query.
access forbthe previous
o A
t o ni View the OTHER column of PLAN_TABLE for the PARTITION RANGE
a)
An operation.
e
Jos SQL>
SQL>
@dynamic_partition_pruning

SQL> set autotrace off


SQL> set timing off
SQL>
SQL> set long 4000
SQL>
SQL> alter session set star_transformation_enabled=true;

Session altered.

SQL>
SQL> explain plan for
2 SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
3 SUM(s.amount_sold) sales_amount
4 FROM sales s, times t, customers c, channels ch
5 WHERE s.time_id = t.time_id
6 AND s.cust_id = c.cust_id
7 AND s.channel_id = ch.channel_id
8 AND c.cust_state_province = 'CA'

Oracle Database 11g: SQL Tuning Workshop A - 193


Practice 6-1: Star Schema Tuning (continued)
9 AND ch.channel_desc in ('Internet','Catalog')
10 AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
11 GROUP BY ch.channel_class, c.cust_city,
t.calendar_quarter_desc;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Explained.

SQL>
SQL> SELECT other
2 FROM plan_table
3 WHERE operation='PARTITION RANGE';

OTHER
--------------------------------------------------------------------
------------
n s e
SELECT distinct TBL$OR$IDX$PART$NUM("SALES", 0, d#, p#,
i ce
PAP_ALIAS_0."PAP_COL_ALI
b l el
AS_0") FROM (SELECT /*+ SEMIJOIN_DRIVER */ "T"."TIME_ID"
"PAP_COL_ALIAS_0" FROM
fer a
n
"TIMES" "T" WHERE "T"."CALENDAR_QUARTER_DESC"='1999-01' OR
a s
"T"."CALENDAR_QUARTER
o n -tr
an
_DESC"='1999-02' OR "T"."CALENDAR_QUARTER_DESC"='2000-03' OR
"T"."CALENDAR_QUART
h
ER_DESC"='2000-04') PAP_ALIAS_0 ORDER BY 1 s
a deฺ
e )
s Gui
r ฺ
SQL>
p ฺ mi dent
d g Stu
b i a@ this
--------------------------------------------------------------

i a (ja use
b
set echo
A
on
to
n o
i autotrace off
ntoset
e A set timing off

J o s set long 4000

alter session set star_transformation_enabled=true;

explain plan for


SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id
AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id
AND c.cust_state_province = 'CA'
AND ch.channel_desc in ('Internet','Catalog')
AND t.calendar_quarter_desc IN ('1999-01','1999-02','2000-
03','2000-04')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc;

SELECT other
FROM plan_table
WHERE operation='PARTITION RANGE';

Oracle Database 11g: SQL Tuning Workshop A - 194


Practice 6-1: Star Schema Tuning (continued)
10) Clean up your environment by removing the index you created and returning the
constraint to its original state.

SQL> @cleanup_star_schema_lab
SQL> set echo on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> set timing off
SQL> set autotrace off
SQL>
SQL> drop index sales_c_state_bjix;

Index dropped.

SQL>
SQL> alter table customers enable novalidate constraint
n s e
customers_pk;
i ce
b l el
Table altered.
fe r a
SQL>
a n s
SQL> connect / as sysdba
o n -tr
an
Connected.
SQL>
SQL> revoke dba from sh;
h s
a deฺ
e )
s Gui
r ฺ
mi dent
Revoke succeeded.
p
g Stuฺ
SQL> d
@ Database
SQL> exit
b i aOracle t h is 11g Enterprise Edition Release
a
11.1.0.6.0 -(jProduction
Disconnected from
se
i a to
b Partitioning, u
A
With the
andoReal
Oracle Label Security, OLAP, Data Mining

ni Application Testing options


to[oracle@edrsr33p1-orcl Star_Schema_Tuning]$
A n
o s e --------------------------------------------------------------
J set echo on

set timing off


set autotrace off

drop index sales_c_state_bjix;

alter table customers enable novalidate constraint customers_pk;

connect / as sysdba

revoke dba from sh;

exit;

Oracle Database 11g: SQL Tuning Workshop A - 195


Practices for Lesson 7
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 196


Practice 7-1: Using System Statistics
In this practice, you manipulate system statistics and show that they are important for the
optimizer to select the correct execution plans.
1. Connected as the oracle user in a terminal session, execute the
sysstats_setup.sh script located in your
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

$HOME/solutions/System_Stats directory. This script creates a user


called JFV and some tables used throughout this lab. The script also makes sure
that object statistics are correctly gathered.
[oracle@edrsr33p1-orcl ~]$ cd $HOME/solutions/System_Stats
[oracle@edrsr33p1-orcl System_Stats]$
[oracle@edrsr33p1-orcl System_Stats]$ ./sysstats_setup.sh

n s e
ce
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 19:11:42
2008
el i
a b l
Copyright (c) 1982, 2007, Oracle. All rights reserved.
fe r
a n s
n - tr
Connected to:
o
an
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
s
Production
) h a deฺ
r ฺ
and Real Application Testing options s Gui
With the Partitioning, Oracle Label Security, OLAP, Data Mining
e
p ฺ mi dent
SQL>
d g Stu
a@ this
SQL> connect / as sysdba;
Connected.
b i
SQL>
(ja cascade;
user jfv u
SQL> dropia se
A b to
o
i dropped.
ton
User

A n
o s e SQL>
J SQL> create user jfv identified by jfv default tablespace users
temporary tablespace temp;

User created.

SQL>
SQL> grant connect, resource, dba to jfv;

Grant succeeded.

SQL>
SQL>
SQL> connect jfv/jfv
Connected.
SQL>
SQL> drop table t purge;
drop table t purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

Oracle Database 11g: SQL Tuning Workshop A - 197


Practice 7-1: Using System Statistics (continued)

SQL>
SQL> drop table z purge;
drop table z purge
*
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table t(c number);

Table created.

SQL>
n s e
SQL> insert into t values (1);
i ce
b l el
1 row created.
fer a
SQL>
a n s
SQL> commit;
n - tr
o
Commit complete.
s an
) h a deฺ
SQL>
e
SQL> insert into t select * fromrฺt;
s Gui
ฺ m i ent
1 row created.
d p
g Stud
SQL> b i a@ this
SQL> /
i a (ja use
A b
created. t
o
o
ni /
2 rows

ntoSQL>
e A
J o s 4 rows created.

SQL> /

8 rows created.

SQL> /

16 rows created.

SQL> /

32 rows created.

SQL> /

64 rows created.

SQL> /

Oracle Database 11g: SQL Tuning Workshop A - 198


Practice 7-1: Using System Statistics (continued)
128 rows created.

SQL> /

256 rows created.


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

SQL> /

512 rows created.

SQL> /

1024 rows created.

SQL>
SQL> commit;
n s e
i ce
Commit complete.
b l el
SQL>
fer a
SQL> insert into t select * from t;
a n s
n -tr
o
an
2048 rows created.

SQL>
h s
a deฺ
SQL> /
e )
s Gui
r ฺ
4096 rows created.
p ฺ mi dent
d g Stu
SQL> /
b i a@ this
i a (ja use
8192 rows created.

A b to
i o
SQL> /
n
t o
n 16384 rows created.
e A
Jos SQL> /

32768 rows created.

SQL> /

65536 rows created.

SQL> /

131072 rows created.

SQL> /

262144 rows created.

SQL>
SQL> commit;

Commit complete.

Oracle Database 11g: SQL Tuning Workshop A - 199


Practice 7-1: Using System Statistics (continued)
SQL>
SQL> create table z(d number);

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

SQL>
SQL> begin
2 for i in 1..100 loop
3 insert into z values (i);
4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.


n s e
i ce
SQL>
b l el
SQL> create unique index iz on z(d);
fer a
Index created.
a n s
o n -tr
an
SQL>
SQL> execute dbms_stats.gather_table_stats('JFV','T',cascade=>true);
h s
a deฺ
PL/SQL procedure successfully completed.
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m
d g Stud
SQL> execute dbms_stats.gather_table_stats('JFV','Z',cascade=>true);

a@ this completed.
PL/SQL procedurebisuccessfully

i a (ja use
SQL>
SQL> A
b to
n i o
ntoSQL>
e A SQL> connect / as sysdba;

J o s Connected.
SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> alter system flush buffer_cache;

System altered.

SQL>
SQL> execute dbms_stats.delete_system_stats;

PL/SQL procedure successfully completed.

SQL>
SQL> execute DBMS_STATS.SET_SYSTEM_STATS (pname => 'cpuspeednw',
pvalue => 0);

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 200


Practice 7-1: Using System Statistics (continued)
SQL>
SQL> select sname,pname,pval1 from aux_stats$;

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

------------------------------ ------------------------------ ------


----
SYSSTATS_INFO STATUS
SYSSTATS_INFO DSTART
SYSSTATS_INFO DSTOP
SYSSTATS_INFO FLAGS
1
SYSSTATS_MAIN CPUSPEEDNW
0
SYSSTATS_MAIN IOSEEKTIM
n s e
10
i ce
SYSSTATS_MAIN IOTFRSPEED
b l el
4096
SYSSTATS_MAIN SREADTIM
fer a
SYSSTATS_MAIN MREADTIM
a n s
SYSSTATS_MAIN CPUSPEED
o n -tr
an
SYSSTATS_MAIN MBRC

SNAME PNAME
h s
a deฺ
PVAL1
e )
s Gui
i r ฺ n t
------------------------------ ------------------------------ ------
---- m
pฺ MAXTHR de
SYSSTATS_MAIN
d g t u
SYSSTATS_MAIN
i a @ his SSLAVETHR
( j ab se t
13 rows selected.
b ia to u
SQL> A
o n io exit;
SQL>
nt Disconnected from Oracle Database 11g Enterprise Edition Release
e A
J o s 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl System_Stats]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/System_Stats

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @sysstats_setup.sql

Oracle Database 11g: SQL Tuning Workshop A - 201


Practice 7-1: Using System Statistics (continued)
[oracle@edrsr33p1-orcl System_Stats]$

--------------------------------------------------------------

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

connect / as sysdba;

drop user jfv cascade;

create user jfv identified by jfv default tablespace users temporary


tablespace temp;

grant connect, resource, dba to jfv;

n s e
connect jfv/jfv
i ce
b l el
drop table t purge;
fer a
drop table z purge;
a n s
n -tr
o
an
create table t(c number);

insert into t values (1);


h s
a deฺ
e )
s Gui
commit; r ฺ
i ent
p ฺ m
d g t;Stud
insert into t select * from

/ b i a@ this
/
i a (ja use
/
A b to
/ni
/
o
t
n /o
e A
Jos
/
/
/
/

commit;

insert into t select * from t;

/
/
/
/
/
/
/

commit;

create table z(d number);

Oracle Database 11g: SQL Tuning Workshop A - 202


Practice 7-1: Using System Statistics (continued)
begin
for i in 1..100 loop
insert into z values (i);
end loop;
commit;
end;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

create unique index iz on z(d);

execute dbms_stats.gather_table_stats('JFV','T',cascade=>true);

execute dbms_stats.gather_table_stats('JFV','Z',cascade=>true);

n s e
connect / as sysdba;
i ce
b l el
alter system flush shared_pool;
fe r a
alter system flush buffer_cache;
a n s
o n -tr
execute dbms_stats.delete_system_stats;
n
a 'cpuspeednw',
execute DBMS_STATS.SET_SYSTEM_STATS (pname
h a s=>
e ฺ pvalue
=> 0);
s) Gui d
r ฺ
i ente
select sname,pname,pval1 from
p ฺ maux_stats$;
d
d g S t u
exit;
b i a@ this
ja use
2. Frombyouria (terminal
t o session, connect as the JFV user in the SQL*Plus session.
A
ioAfter this, execute the following statement and determine how long it takes to
t o n execute:
A n
o s e select /* Without system stats */ count(*)
from t,z
J where t.c=z.d;
[oracle@edrsr33p1-orcl System_Stats]$ sqlplus jfv/jfv

SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 19:11:57


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> @select_without_sysstats
SQL>
SQL> set timing on
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 203


Practice 7-1: Using System Statistics (continued)
SQL> select /* Without system stats */ count(*)
2 from t,z
3 where t.c=z.d;

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

524288

Elapsed: 00:00:00.24
SQL>
SQL> set timing off
SQL>
SQL>

--------------------------------------------------------------
set echo on
n s e
ice
set timing on
b l el
select /* Without system stats */ count(*)
fe r a
from t,z
a n s
where t.c=z.d;
o n -tr
set timing off
s an
) h a deฺ
3. Determine the execution plan used to r ฺ e
execute ui statement. In addition,
s theGprevious
determine the optimizer’s cost,ฺm
i
CPU cost,eand ntI/O cost for the previous execution.
What do you observe? dg
p tu d
@use CPU
anot S
is costing. This is because system statistics were
b
a) The optimizer doesi t h
(jathe first
deleted during
i a u e of this lab. The plan chosen by the optimizer might
sstep
notA to
bebthe best one.
o
ni @show_latest_exec_plan
t oSQL>
n SQL> set echo on
e A
Jos
SQL>
SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
------------
SQL_ID 6avdu58tamzju, child number 0
-------------------------------------
select /* Without system stats */ count(*) from t,z where t.c=z.d

Plan hash value: 3698032250

------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 134 |
| 1 | SORT AGGREGATE | | 1 | 6 | |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
------------

Oracle Database 11g: SQL Tuning Workshop A - 204


Practice 7-1: Using System Statistics (continued)
| 2 | NESTED LOOPS | | 524K| 3072K| 134 |
| 3 | TABLE ACCESS FULL| T | 524K| 1536K| 134 |
|* 4 | INDEX UNIQUE SCAN| IZ | 1 | 3 | |
------------------------------------------------------------

Predicate Information (identified by operation id):


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

---------------------------------------------------

4 - access("T"."C"="Z"."D")

Note

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
------------
-----
n s e
- cpu costing is off (consider enabling it)
i ce
b l el
25 rows selected.
fer a
a n s
SQL>
n - tr
o
an
SQL> col operations format a20
SQL> col object_name format a11
SQL> col options format a15
h s
a deฺ
SQL> col cost_cpu_io format a30
e )
s Gui
r ฺ
mi dent
SQL>
SQL>
p ฺ
g Stu
d
SQL> select operation operations, object_name, options,
2
b i a@ this
cost||' -- '||cpu_cost||' -- '||io_cost cost_cpu_io
3 from (select * from v$sql_plan where address in (select address
4
i a (ja use from v$sql
5
Ab to where sql_text

o 6io
like '%system stats%' and
n sql_text
nt
e A not like '%connect%'));

J o s OPERATIONS OBJECT_NAME OPTIONS COST_CPU_IO


-------------------- ----------- --------------- -------------------
-----------
SELECT STATEMENT 134 -- --
SORT AGGREGATE -- --
NESTED LOOPS 134 -- -- 134
TABLE ACCESS T FULL 134 -- -- 134
INDEX IZ UNIQUE SCAN -- --

SQL>

--------------------------------------------------------------

set echo on

select * from table(dbms_xplan.display_cursor);

col operations format a20


col object_name format a11
col options format a15

Oracle Database 11g: SQL Tuning Workshop A - 205


Practice 7-1: Using System Statistics (continued)
col cost_cpu_io format a30

select operation operations, object_name, options,


cost||' -- '||cpu_cost||' -- '||io_cost cost_cpu_io
from (select * from v$sql_plan where address in (select address
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

from v$sql
where sql_text like
'%system stats%' and
sql_text not
like '%connect%'));

4. How can you ensure that the optimizer finds a better plan during future executions
of the same statement? Implement your solution.
b) Gather system statistics again. Because you do not have yet a real workload, you n s e
ice
can gather system statistics in NOWORKLOAD mode.
b l el
SQL> connect / as sysdba;
fe r a
Connected.
a n s
SQL> @gather_system_stats
o n -tr
an
SQL> set echo on
SQL>
h s
a deฺ
SQL> execute DBMS_STATS.GATHER_SYSTEM_STATS(gathering_mode =>
'NOWORKLOAD');
e )
s Gui
r ฺ
i ent

PL/SQL procedure successfully
p mcompleted.
d
d g S t u
SQL>
b i a@ this from aux_stats$;
SQL> select sname,pname,pval1

SNAME bia
(ja use
PVAL1 A to PNAME

o n io
------------------------------ ------------------------------ ------
nt
e A ----

J o s SYSSTATS_INFO
SYSSTATS_INFO
STATUS
DSTART
SYSSTATS_INFO DSTOP
SYSSTATS_INFO FLAGS
1
SYSSTATS_MAIN CPUSPEEDNW
1893.021
SYSSTATS_MAIN IOSEEKTIM
8.043
SYSSTATS_MAIN IOTFRSPEED
4096
SYSSTATS_MAIN SREADTIM
SYSSTATS_MAIN MREADTIM
SYSSTATS_MAIN CPUSPEED
SYSSTATS_MAIN MBRC

SNAME PNAME
PVAL1
------------------------------ ------------------------------ ------
----
SYSSTATS_MAIN MAXTHR

Oracle Database 11g: SQL Tuning Workshop A - 206


Practice 7-1: Using System Statistics (continued)
SYSSTATS_MAIN SLAVETHR

13 rows selected.

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

--------------------------------------------------------------

set echo on

execute DBMS_STATS.GATHER_SYSTEM_STATS(gathering_mode =>


'NOWORKLOAD');

select sname,pname,pval1 from aux_stats$;

n s e
5. Before verifying your solution, you should flush the System Global Area (SGA)
ice
pools, such as the shared pool and the buffer cache. This is done to prevent
b l el
interferences from the previous steps.
fe ra
SQL> @flush_sga
a n s
SQL>
n - tr
o
an
SQL> set echo on
SQL>
SQL> alter system flush shared_pool;
h s
a deฺ
e )
s Gui
r ฺ
mi dent
System altered.
p ฺ
g Stu
SQL>
d
b i a@ this
SQL> alter system flush buffer_cache;

a (ja use
System altered.
i
A b to
ni o
SQL>

ntoSQL>
e A
J o s --------------------------------------------------------------

set echo on

alter system flush shared_pool;

alter system flush buffer_cache;

6. Connected as the JFV user again, check your solution against the following
query:
select /* With system stats */ count(*)
from t,z
where t.c=z.d;
What do you observe?
c) The optimizer can make a better decision because it was able to use meaningful
system statistics. You can see that the execution time is now half the value it was
previously, and the execution plan now includes CPU costing information.

Oracle Database 11g: SQL Tuning Workshop A - 207


Practice 7-1: Using System Statistics (continued)
SQL> connect jfv/jfv
Connected.
SQL> @select_with_sysstats
SQL> set timing on
SQL>
SQL> select /* With system stats */ count(*)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2 from t,z
3 where t.c=z.d;

COUNT(*)
----------
524288

Elapsed: 00:00:00.11
SQL>
SQL> set timing off
n s e
SQL>
i ce
SQL> @show_latest_exec_plan
b l el
SQL> set echo on
fer a
SQL>
SQL> select * from table(dbms_xplan.display_cursor);
a n s
o n -tr
an
PLAN_TABLE_OUTPUT

------------
h s
--------------------------------------------------------------------
a deฺ
SQL_ID 2x55txn3742by, child number 0
e )
s Gui
r ฺ
mi dent
-------------------------------------
p ฺ
select /* With system stats */ count(*) from t,z where t.c=z.d
g Stu
d
b i a@ this
Plan hash value: 2407521827

a (ja use
--------------------------------------------------------------------
i
--------
A b to
n i
| Id o | Operation | Name | Rows | Bytes | Cost (%CPU)|

ntoTime |

e A --------------------------------------------------------------------

J o s --------
| 0 | SELECT STATEMENT | | | | 272 (100)|
|
| 1 | SORT AGGREGATE | | 1 | 6 | |
|

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
------------
|* 2 | HASH JOIN | | 524K| 3072K| 272 (3)|
00:00:03 |
| 3 | INDEX FULL SCAN | IZ | 100 | 300 | 1 (0)|
00:00:01 |
| 4 | TABLE ACCESS FULL| T | 524K| 1536K| 267 (2)|
00:00:03 |
--------------------------------------------------------------------
--------

Predicate Information (identified by operation id):


---------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 208


Practice 7-1: Using System Statistics (continued)
2 - access("T"."C"="Z"."D")

21 rows selected.

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

SQL> col operations format a20


SQL> col object_name format a11
SQL> col options format a15
SQL> col cost_cpu_io format a30
SQL>
SQL>
SQL> select operation operations, object_name, options,
2 cost||' -- '||cpu_cost||' -- '||io_cost cost_cpu_io
3 from (select * from v$sql_plan where address in (select address
4 from v$sql
n s e
5 where sql_text
i ce
like '%system stats%' and
b l el
6
not like '%connect%'));
sql_text
fer a
a n s
OPERATIONS OBJECT_NAME OPTIONS -tr
COST_CPU_IO
o n
a n 272 -- --
-------------------- ----------- --------------- -------------------
-----------
SELECT STATEMENT
h s
a deฺ-- --
SORT )
s Gui 272 -- 146844065 --
AGGREGATE
e
HASH JOIN r ฺ
i ent
264
IZ g p ฺ m d SCAN
INDEX
d S t u
FULL 1 -- 27121 -- 1
TABLE ACCESS
263 b i a@ this
T FULL 267 -- 84867339 --

i a (ja use
SQL>
Ab to
o n io
--------------------------------------------------------------
nt
e A
J o s set timing on

select /* With system stats */ count(*)


from t,z
where t.c=z.d;

set timing off

--------------------------------------------------------------

set echo on

select * from table(dbms_xplan.display_cursor);

col operations format a20


col object_name format a11
col options format a15
col cost_cpu_io format a30

select operation operations, object_name, options,

Oracle Database 11g: SQL Tuning Workshop A - 209


Practice 7-1: Using System Statistics (continued)
cost||' -- '||cpu_cost||' -- '||io_cost cost_cpu_io
from (select * from v$sql_plan where address in (select address
from v$sql
where sql_text like
'%system stats%' and
sql_text not
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

like '%connect%'));

7. Exit from your SQL*Plus session and clean up your environment for this lab by
executing the systats_cleanup.sh script.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
n s e
With the Partitioning, Oracle Label Security, OLAP, Data Mining
i ce
and Real Application Testing options
b l el
[oracle@edrsr33p1-orcl System_Stats]$ ./sysstats_cleanup.sh
fer a
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 19:13:54
a n s
2008
o n -tr
Copyright (c) 1982, 2007, Oracle. All rights
n
a reserved.
h s
a deฺ
e )
s Gui
r ฺ
miEdition t
Connected to:
p
Oracle Database 11g Enterprise ฺ d e nRelease 11.1.0.6.0 -
Production g
dOracle S t u
@ s
thi options
ia Testing
With the Partitioning, Label Security, OLAP, Data Mining
a b
and Real Application
j e
SQL> b ia ( to us
i
SQL> A user jfv cascade;
o drop
to n
A n User dropped.

o s e
J SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl System_Stats]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/System_Stats

export ORACLE_SID=orcl

Oracle Database 11g: SQL Tuning Workshop A - 210


Practice 7-1: Using System Statistics (continued)
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

sqlplus / as sysdba @sysstats_cleanup.sql

--------------------------------------------------------------

set echo on

drop user jfv cascade;

alter system flush shared_pool;


n s e
i ce
exit;
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
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 211


Practice 7-2: Automatic Statistics Gathering
In this practice, you manipulate object statistics to see their incidences on the execution
plans of your SQL statements. Note: All scripts needed for this lab can be found in your
$HOME/solutions/Automatic_Gather_Stats directory.
1) From a terminal window connected as the oracle user, set up your environment for
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

this lab by executing the ags_setup.sh script. This script creates a user called
AGS that you use throughout this lab. The script also creates a table called EMP and
an index.
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$ ./ags_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Mon Apr 7 15:55:28 2008

n s e
ce
Copyright (c) 1982, 2007, Oracle. All rights reserved.

el i
a b l
Connected to:
fe r
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
a n s
Production
n - tr
o
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
s an
) h a deฺ
SQL>
SQL> drop user ags cascade;
r ฺ e s Gui
drop user ags cascade
p ฺ mi dent
*
d g Stu
a@doesthinot
ERROR at line 1:
b i
ORA-01918: user 'AGS' s exist
i a (ja use
SQL> Ab to
o
i create user ags identified by ags;
ton
SQL>

A n
o s e User created.
J SQL>
SQL> grant dba to ags;

Grant succeeded.

SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> --
SQL> -- Turn off AUTOTASK
SQL> --
SQL>
SQL> alter system set "_enable_automatic_maintenance"=0;

System altered.

Oracle Database 11g: SQL Tuning Workshop A - 212


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>
SQL> connect ags/ags
Connected.
SQL>
SQL> drop table emp purge;
drop table emp purge
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table emp
2 (
3 empno number,
4 ename varchar2(20),
n s e
5 phone varchar2(20),
i ce
6 deptno number
b l el
7 );
fer a
Table created.
a n s
n - tr
o
an
SQL>
SQL>
SQL> insert into emp
h s
a deฺ
2 with tdata as
e )
s Gui
r ฺ
mi dent
3 (select rownum empno
4 ฺ
from all_objects
p
g Stu
5
d
where rownum <= 1000)
6
7
select rownum,
b i a@ this
dbms_random.string ('u', 20),
8
i a (ja use
dbms_random.string ('u', 20),
9
Ab case
to
o
10
n
10)io when rownum/100000 <= 0.001 then mod(rownum,

nt
e A 11 else 10

J o s 12
13
end
from tdata a, tdata b
14 where rownum <= 100000;

100000 rows created.

SQL>
SQL> commit;

Commit complete.

SQL>
SQL> create index emp_i1 on emp(deptno);

Index created.

SQL>
SQL> exec dbms_stats.delete_schema_stats('AGS');

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 213


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Automatic_Gather_Stats

export ORACLE_SID=orcl

n s e
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
i ce
b l el
export
fer
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/locaa
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin
a n s
o n -tr
an
sqlplus / as sysdba @ags_setup.sql

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

e )
s Gui
set echo on r ฺ
i ent
p ฺ m
drop user ags cascade; g
d S tud
create user ags b i a@ thibys ags;
identified
a
(jags; se
b
grant dba i a
to
to u
o A
ni system flush shared_pool;
toalter
A n
o s e --
J -- Turn off AUTOTASK
--

alter system set "_enable_automatic_maintenance"=0;

connect ags/ags

drop table emp purge;

create table emp


(
empno number,
ename varchar2(20),
phone varchar2(20),
deptno number
);

insert into emp


with tdata as

Oracle Database 11g: SQL Tuning Workshop A - 214


Practice 7-2: Automatic Statistics Gathering (continued)
(select rownum empno
from all_objects
where rownum <= 1000)
select rownum,
dbms_random.string ('u', 20),
dbms_random.string ('u', 20),
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

case
when rownum/100000 <= 0.001 then mod(rownum, 10)
else 10
end
from tdata a, tdata b
where rownum <= 100000;

commit;

create index emp_i1 on emp(deptno);


n s e
i ce
exec dbms_stats.delete_schema_stats('AGS');
b l el
exit;
fe r a
a n s
2) Connect as the AGS user under a SQL*Plus session. o n -tr
s a nsqlplus ags/ags
) h a deฺ
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$

e s GonuMon i Apr 7 15:55:47 2008


r ฺ
SQL*Plus: Release 11.1.0.6.0 - Production

p ฺ mi dAll e t
nrights
Copyright (c) 1982, 2007,
d g Stu
Oracle. reserved.

b i a@ this
Connected to:
i a (ja 11guEnterprise
se
b
Oracle Database
A to Edition Release 11.1.0.6.0 -

o n io the Partitioning, Oracle Label Security, OLAP, Data Mining


Production
With
nt and Real Application Testing options
e A
J o s SQL>

3) Determine the distribution of the deptno values found in the EMP table. What do
you observe?
a) You can see that there are 11 different department values, each repeating 0.01%
of the time except value 10 that repeats 99.9% of the time.
SQL> @show_deptno_distribution
SQL>
SQL> select deptno, count(*) cnt_per_deptno, (count(*)*100)/nr
deptno_percent
2 from emp, (select max(empno) nr
3 from emp)
4 group by deptno, nr
5 order by deptno;

DEPTNO CNT_PER_DEPTNO DEPTNO_PERCENT


---------- -------------- --------------
0 10 .01

Oracle Database 11g: SQL Tuning Workshop A - 215


Practice 7-2: Automatic Statistics Gathering (continued)
1 10 .01
2 10 .01
3 10 .01
4 10 .01
5 10 .01
6 10 .01
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7 10 .01
8 10 .01
9 10 .01
10 99900 99.9

11 rows selected.

SQL>

--------------------------------------------------------------
n s e
i ce
set echo on
b l el
select deptno, count(*) cnt_per_deptno, (count(*)*100)/nr
fe r a
deptno_percent
a n s
from emp, (select max(empno) nr
o n -tr
an
from emp)
group by deptno, nr
order by deptno;
h s
a deฺ
e )
s Gui
r ฺ
ฺ mi doneany
4) Determine if there are histograms available
p nt column of the EMP table. What do
you observe? d g Stu
i @histograms
ano h is defined on any column of the EMP table.
a) Currently, therebare t
i a (ja use
b echo onto
SQL> @check_emp_histogram
A
o
SQL> set
ni select column_name, histogram, num_buckets
t oSQL>

e An 2 from user_tab_columns
SQL>

Jos 3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE
ENAME NONE
PHONE NONE
DEPTNO NONE

SQL>

--------------------------------------------------------------

set echo on

select column_name, histogram, num_buckets


from user_tab_columns
where table_name='EMP';

Oracle Database 11g: SQL Tuning Workshop A - 216


Practice 7-2: Automatic Statistics Gathering (continued)
5) Determine if you currently have statistics gathered on your EMP table. What do you
observe?
a) Currently, there are no statistics gathered on your EMP table.
SQL> @check_table_last_analyzed
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set echo on


SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
2 FROM user_tables
3 WHERE table_name = 'EMP';

LAST_ANAL SAMPLE_SIZE MON NUM_ROWS BLOCKS TABLE_NAME


n s e
--------- ----------- --- ---------- ---------- --------------------
i ce
----------
b l el
YES EMP
fer a
SQL>
a n s
o n -tr
n
--------------------------------------------------------------
a
set echo on
h s
a deฺ
)
s Gui
set linesize 200 pagesize 1000 rฺe
ฺ m i ent
SELECT
d g p t u d
@ his S
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
FROM user_tablesbia t
( j a = 'EMP';
s e
ia to u
WHERE table_name
b
n io A if you currently have statistics gathered on the index created on top of the
6)oDetermine
t
e An EMP table. What do you observe?
Jos a) Currently, EMP_I1 has no statistics gathered.
SQL> @check_index_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT index_name name,num_rows,
2 last_analyzed,distinct_keys,leaf_blocks,
3 avg_leaf_blocks_per_key,join_index
4 FROM user_indexes
5 WHERE table_name = 'EMP';

NAME NUM_ROWS LAST_ANAL DISTINCT_KEYS


LEAF_BLOCKS AVG_LEAF_BLOCKS_PER_KEY JOI
------------------------------ ---------- --------- ------------- --
--------- ----------------------- ---
EMP_I1
NO

Oracle Database 11g: SQL Tuning Workshop A - 217


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>

--------------------------------------------------------------

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

set linesize 200 pagesize 1000

SELECT index_name name,num_rows,


last_analyzed,distinct_keys,leaf_blocks,
avg_leaf_blocks_per_key,join_index
FROM user_indexes
WHERE table_name = 'EMP';

7) Execute the following two statements and determine their execution plans:
n s e
select count(*), max(empno) from emp where deptno = 9;
ice
select count(*), max(empno) from emp where deptno = 10;
b l el
What do you observe and why?
f a
er
a) You see that for literal 9, the optimizer decided to use the indexra
s
n for literal
whereas
10, the optimizer decided to do TABLE ACCESS FULL. - t
n optimizer
The
n o
a dynamic sampling was
determined the correct plans in both cases. This is because
sobjects.
used, as there were no statistics gathered on h
) a
both
i d eฺ
SQL> @check_exec_plans
i r ฺ es t Gu
SQL> set echo on
g p ฺm uden
SQL>
d S t
SQL> set linesize 200 @ pagesize
s 1000
SQL>
j a b ia
e thi
ia ( to us
SQL> set autotrace on
SQL>
b
n i o A count(*), max(empno) from emp where deptno = 9;
SQL> select

nto COUNT(*) MAX(EMPNO)


e A
J o s ---------- ----------
10 99

Execution Plan
----------------------------------------------------------
Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT | | 1 | 26 |
2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 |
| |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 10 | 260 |
2 (0)| 00:00:01 |

Oracle Database 11g: SQL Tuning Workshop A - 218


Practice 7-2: Automatic Statistics Gathering (continued)
|* 3 | INDEX RANGE SCAN | EMP_I1 | 10 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
-------------------

Predicate Information (identified by operation id):


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

---------------------------------------------------

3 - access("DEPTNO"=9)

Note
-----
- dynamic sampling used for this statement

Statistics
n s e
----------------------------------------------------------
i ce
9 recursive calls
b l el
0 db block gets
64 consistent gets
fer a
1 physical reads
a n s
0 redo size
o n -tr
an
481 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
h
2 SQL*Net roundtrips to/from client s
a deฺ
0 sorts (memory)
e )
s Gui
r ฺ
mi dent
0 sorts (disk)
1 rows processed
p
g Stuฺ
d
SQL>
b i a@max(empno)
SQL> select count(*), t h is from emp where deptno = 10;
i a (ja use
COUNT(*)
A to
b ----------
MAX(EMPNO)
o
oni 99900 100000
----------

nt
eA
Jos Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | 1 | 26 | 265 (1)|
00:00:03 |
| 1 | SORT AGGREGATE | | 1 | 26 | |
|
|* 2 | TABLE ACCESS FULL| EMP | 86262 | 2190K| 265 (1)|
00:00:03 |
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):

Oracle Database 11g: SQL Tuning Workshop A - 219


Practice 7-2: Automatic Statistics Gathering (continued)
---------------------------------------------------

2 - filter("DEPTNO"=10)

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

- dynamic sampling used for this statement

Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
871 consistent gets
5 physical reads
0 redo size
n s e
482 bytes sent via SQL*Net to client
i ce
420 bytes received via SQL*Net from client
b l el
2 SQL*Net roundtrips to/from client
0 sorts (memory)
fer a
0 sorts (disk)
a n s
1 rows processed
o n -tr
SQL>
s an
SQL> set autotrace off
) h a deฺ
SQL>
r ฺ e s Gui
ฺ mi dent
--------------------------------------------------------------
p
d g Stu
set echo on
b i a@ this
a pagesize
set linesize(j200 se 1000
b i a o u
A ont
o
ni count(*), max(empno) from emp where deptno = 9;
set autotrace

ntoselect
e A
J o s select count(*), max(empno) from emp where deptno = 10;

set autotrace off

8) Confirm your assumption from the previous step.


SQL> show parameter optimizer_dynamic_sampling

NAME TYPE VALUE


------------------------------------ ----------- -------------------
-----------
optimizer_dynamic_sampling integer 2
SQL>

9) Make sure you disable the mechanism found in the previous step.
SQL> alter session set optimizer_dynamic_sampling=0;

Session altered.
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 220


Practice 7-2: Automatic Statistics Gathering (continued)
10) Perform step 7 again. What do you observe and why?
a) Because dynamic sampling is not used, the optimizer cannot use any statistics. It
uses default statistics that are not a good choice for the second statement.
SQL> @check_exec_plans
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> set echo on


SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> set autotrace on
SQL>
SQL> select count(*), max(empno) from emp where deptno = 9;

COUNT(*) MAX(EMPNO)
---------- ----------
n s e
10 99
i ce
b l el
Execution Plan
fe r a
----------------------------------------------------------
a n s
Plan hash value: 3184478295
o n -tr
an
--------------------------------------------------------------------
s
-------------------
|h
a | eRows
Name id
ฺ | Bytes | Cost
| Id | Operation )
es t Gu
(%CPU)| Time |
i r ฺ
g p ฺm uden
--------------------------------------------------------------------
-------------------
d S t |
| 0 | SELECT STATEMENT
(0)| 00:00:01ia
@ i s | 1 | 26 |
5
( j b e th
|
aAGGREGATE
| 1 | SORT
i a u s | | 1 | 26 |
|
2A
|
o
b TABLE tACCESS
|
o |
5ni (0)| 00:00:01 |
BY INDEX ROWID| EMP | 714 | 18564 |

nto|* 3 | INDEX RANGE SCAN | EMP_I1 | 286 | |


e A 1 (0)| 00:00:01 |
J o s --------------------------------------------------------------------
-------------------

Predicate Information (identified by operation id):


---------------------------------------------------

3 - access("DEPTNO"=9)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
481 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)

Oracle Database 11g: SQL Tuning Workshop A - 221


Practice 7-2: Automatic Statistics Gathering (continued)
0 sorts (disk)
1 rows processed

SQL>
SQL> select count(*), max(empno) from emp where deptno = 10;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

Execution Plan
----------------------------------------------------------
Plan hash value: 3184478295

--------------------------------------------------------------------
n s e
-------------------
i ce
| Id | Operation | Name | Rows | Bytes | Cost
b l el
(%CPU)| Time |
fer
-------------------------------------------------------------------- a
-------------------
a n s
| 0 | SELECT STATEMENT | | -
1 |
n tr 26 |
o
an
5 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 |
| |
h s
a deฺ
| 2 | )
s Gui
TABLE ACCESS BY INDEX ROWID| EMP
e
| 714 | 18564 |
r ฺ
mi dent
5 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN
p ฺ
g Stu
| EMP_I1 | 286 | |
1 (0)| 00:00:01 |
d
b
-------------------i a@ this
--------------------------------------------------------------------

i a (ja use(identified by operation id):


b
Predicate
A to
Information
o
ni 3 - access("DEPTNO"=10)
---------------------------------------------------

n t o
e A
Jos Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
954 consistent gets
190 physical reads
0 redo size
482 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set autotrace off
SQL>

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 222


Practice 7-2: Automatic Statistics Gathering (continued)
set echo on

set linesize 200 pagesize 1000

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

select count(*), max(empno) from emp where deptno = 9;

select count(*), max(empno) from emp where deptno = 10;

set autotrace off

11) Reset dynamic sampling as it was at the beginning of this lab.


n s e
ce
SQL> alter session set optimizer_dynamic_sampling=2;

eli
Session altered.
a b l
fer
SQL>
a n s
n -tr
12) Set the following wrong statistic values for your EMP table:
o
- Set its number of rows to 10.
s an
- Set its number of blocks to 5.
) h a deฺ
r ฺ e s Gui
mi dent
SQL> @set_wrong_stats
SQL> set echo on
SQL> p ฺ
g Stu
d
a@ this
SQL> exec
b i
dbms_stats.set_table_stats('AGS','EMP',null,null,null,10,5);

i a (ja successfully
u se
b
PL/SQL procedure
A to completed.

o n io
SQL>
nt
e A --------------------------------------------------------------
J o s
set echo on

exec dbms_stats.set_table_stats('AGS','EMP',null,null,null,10,5);

13) Check that you modified correctly the statistics of the EMP table.
SQL> @check_table_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
2 FROM user_tables
3 WHERE table_name = 'EMP';

LAST_ANAL SAMPLE_SIZE MON NUM_ROWS BLOCKS TABLE_NAME


--------- ----------- --- ---------- ---------- --------------------
----------

Oracle Database 11g: SQL Tuning Workshop A - 223


Practice 7-2: Automatic Statistics Gathering (continued)
07-APR-08 2000 YES 10 5 EMP

SQL>

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

set echo on

set linesize 200 pagesize 1000

SELECT
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
FROM user_tables
WHERE table_name = 'EMP';

n s e
14) Perform step 7 again. What do you observe and why?
ice
l el
a) Because there are statistics defined on the EMP table, the optimizer uses them, and
b
fe
not dynamic sampling. However, because the statistics are incorrect, the ra
n s
generated plans are also incorrect, at least for the second statement.
a
o n -tr
an
SQL> @check_exec_plans
SQL> set echo on
s
a deฺ
set linesize 200 pagesize 1000 ) h
SQL>
SQL>
r ฺ e s Gui
mi dent
SQL>
SQL> set autotrace on
p ฺ
g Stufrom emp where deptno = 9;
SQL>
d
SQL>
b i a@ this
select count(*), max(empno)

i a (ja use
COUNT(*) MAX(EMPNO)
b10
----------
A to 99
----------
i o
nton
e A
J o s Execution Plan
----------------------------------------------------------
Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT | | 1 | 26 |
2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 |
| |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 26 |
2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | EMP_I1 | 1 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
-------------------

Oracle Database 11g: SQL Tuning Workshop A - 224


Practice 7-2: Automatic Statistics Gathering (continued)
Predicate Information (identified by operation id):
---------------------------------------------------

3 - access("DEPTNO"=9)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
481 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
n s e
0 sorts (memory)
i ce
0 sorts (disk)
b l el
1 rows processed
fer a
SQL>
a n s
o n -tr
SQL> select count(*), max(empno) from emp where deptno = 10;

COUNT(*) MAX(EMPNO)
s an
---------- ----------
) h a deฺ
99900 100000
r ฺ e s Gui
ฺ m i ent
Execution Plan
d g p t u d
a@ this S
----------------------------------------------------------
Plan hash value:bi3184478295

i a (ja use
b to
--------------------------------------------------------------------
A
o
niId | Operation
-------------------

nto|(%CPU)| | Name | Rows | Bytes | Cost

e A Time |

J o s --------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT | | 1 | 26 |
2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 |
| |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 26 |
2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | EMP_I1 | 1 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
-------------------

Predicate Information (identified by operation id):


---------------------------------------------------

3 - access("DEPTNO"=10)

Statistics
----------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 225


Practice 7-2: Automatic Statistics Gathering (continued)
0 recursive calls
0 db block gets
805 consistent gets
0 physical reads
0 redo size
482 bytes sent via SQL*Net to client
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

420 bytes received via SQL*Net from client


2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set autotrace off
SQL>

n s e
--------------------------------------------------------------
i ce
b l el
set echo on
fer a
set linesize 200 pagesize 1000
a n s
o n -tr
a n = 9;
set autotrace on

select count(*), max(empno) from emp where


h s
a deฺ
deptno

select count(*), max(empno) fromrฺempe ) ui = 10;


s whereGdeptno
ฺ m i ent
set autotrace off
d p
g Stud
b i a@ this
ja ugather
15) Make sure you(manually
a se statistics on your EMP table and its corresponding
index. b i to
o A
t o ni @gather_stats_manually
SQL>

e An SQL> set echo on


Jos
SQL>
SQL> exec dbms_stats.gather_table_stats('AGS', 'EMP', cascade =>
true);

PL/SQL procedure successfully completed.

SQL>

--------------------------------------------------------------

set echo on

exec dbms_stats.gather_table_stats('AGS', 'EMP', cascade => true);

16) Make sure statistics were gathered on your objects.


SQL> @check_table_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000

Oracle Database 11g: SQL Tuning Workshop A - 226


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>
SQL> SELECT
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
2 FROM user_tables
3 WHERE table_name = 'EMP';
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

LAST_ANAL SAMPLE_SIZE MON NUM_ROWS BLOCKS TABLE_NAME


--------- ----------- --- ---------- ---------- --------------------
----------
07-APR-08 100000 YES 100000 874 EMP

SQL>
SQL> @check_index_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
n s e
SQL>
ice
SQL> SELECT index_name name,num_rows,
b l el
2
3
last_analyzed,distinct_keys,leaf_blocks,
avg_leaf_blocks_per_key,join_index
fe r a
4 FROM user_indexes
a n s
5 WHERE table_name = 'EMP';
o n -tr
NAME a n DISTINCT_KEYS
NUM_ROWS LAST_ANAL
LEAF_BLOCKS AVG_LEAF_BLOCKS_PER_KEY JOI as
) h i d eฺ ------------- --
------------------------------ ----------
r ฺ s Gu
e---
---------
--------- -----------------------
EMP_I1 ฺ m i e nt 07-APR-08
100000 11
196
d g p
17 NO
t u d
@ s S
SQL>
j a b ia
e thi
SQL> setb a ( onto us
SQL> @check_emp_histogram
iecho
SQL> A
o n io select column_name, histogram, num_buckets
SQL>
nt 2 from user_tab_columns
e A
J o s 3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE 1
ENAME NONE 1
PHONE NONE 1
DEPTNO FREQUENCY 7

SQL>

17) Perform step 7 again. What do you observe and why?


a) Because statistics were correctly gathered on both objects, the optimizer is able to
use correct execution plans for both statements.
SQL> @check_exec_plans
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000

Oracle Database 11g: SQL Tuning Workshop A - 227


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>
SQL> set autotrace on
SQL>
SQL> select count(*), max(empno) from emp where deptno = 9;

COUNT(*) MAX(EMPNO)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

---------- ----------
10 99

Execution Plan
----------------------------------------------------------
Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
n s e
| Id | Operation | Name | Rows | Bytes | Cost
i ce
(%CPU)| Time |
b l el
-------------------
fer a
--------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 |
a n s
8 |
2 (0)| 00:00:01 |
n -tr
o
an
| 1 | SORT AGGREGATE | | 1 | 8 |
| |
| 2 |
h
TABLE ACCESS BY INDEX ROWID| EMP s
a deฺ
| 18 | 144 |
2 (0)| 00:00:01 |
e )
s Gui
r ฺ
mi dent
|* 3 | INDEX RANGE SCAN | EMP_I1 | 18 | |
1 (0)| 00:00:01 |
p ฺ
g Stu
d
--------------------------------------------------------------------
-------------------
b i a@ this
i a (ja use(identified by operation id):
Predicate Information
b to
---------------------------------------------------
A
i o
nton 3 - access("DEPTNO"=9)
e A
J o s Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
481 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> select count(*), max(empno) from emp where deptno = 10;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

Oracle Database 11g: SQL Tuning Workshop A - 228


Practice 7-2: Automatic Statistics Gathering (continued)

Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | 1 | 8 | 265 (1)|
00:00:03 |
| 1 | SORT AGGREGATE | | 1 | 8 | |
|
n s e
|* 2 | TABLE ACCESS FULL| EMP | 99864 | 780K| 265 (1)|
i ce
00:00:03 |
b l el
--------------------------------------------------------------------
-------
fer a
a n s
Predicate Information (identified by operation id):
o n -tr
an
---------------------------------------------------

2 - filter("DEPTNO"=10)
h s
a deฺ
e )
s Gui
r ฺ
i ent
Statistics
p ฺ m d
d g t u
----------------------------------------------------------
S
0 db b i a@gets
0 recursive
block
calls
t h is
a sereads
805 (jconsistent gets
i a
b 0 redotosize
0 u
physical
i o A
nton 482 bytes sent via SQL*Net to client

e A 420 bytes received via SQL*Net from client

J o s 2
0
SQL*Net roundtrips to/from client
sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set autotrace off
SQL>

18) Ensure you delete all statistics previously generated on your objects.
SQL> @delete_stats
SQL> set echo on
SQL>
SQL> exec dbms_stats.delete_schema_stats('AGS');

PL/SQL procedure successfully completed.

SQL>

--------------------------------------------------------------
set echo on

Oracle Database 11g: SQL Tuning Workshop A - 229


Practice 7-2: Automatic Statistics Gathering (continued)
exec dbms_stats.delete_schema_stats('AGS');

19) Verify that you no longer have statistics gathered on both objects.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> @check_table_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
2 FROM user_tables
3 WHERE table_name = 'EMP';
n s e
i ce
LAST_ANAL SAMPLE_SIZE MON NUM_ROWS BLOCKS TABLE_NAME
--------- ----------- --- ---------- ---------- -------------------- b l el
----------
fer a
YES EMP
a n s
n - tr
SQL> o
SQL> @check_index_last_analyzed
s an
SQL> set echo on
) h a deฺ
SQL>
SQL>
r
set linesize 200 pagesize 1000ฺ e s Gui
SQL>
p ฺ mi dent
SQL> g Stu
SELECT index_name name,num_rows,
d
a@ this
2 last_analyzed,distinct_keys,leaf_blocks,
3
b i
avg_leaf_blocks_per_key,join_index
4 (ja use= 'EMP';
FROM user_indexes
i a
5
b
WHERE
A to
table_name

n i o
n t oNAME
LEAF_BLOCKS
NUM_ROWS LAST_ANAL DISTINCT_KEYS
AVG_LEAF_BLOCKS_PER_KEY JOI
e A ------------------------------ ---------- --------- ------------- --
Jos --------- ----------------------- ---
EMP_I1
NO

SQL>
SQL> @check_emp_histogram
SQL> set echo on
SQL>
SQL> select column_name, histogram, num_buckets
2 from user_tab_columns
3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE
ENAME NONE
PHONE NONE
DEPTNO NONE

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 230


Practice 7-2: Automatic Statistics Gathering (continued)
20) How would you determine the list of automated tasks that exist on your database?
a) You can use Enterprise Manager Database Control by navigating to the
Automated Maintenance Tasks page (Home > Server> Automated Maintenance
Tasks). On the Automated Maintenance Tasks page, you can see the three
automated tasks implemented by default on your database.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

b) Another possibility is to use the DBA_AUTOTASK_TASK table as shown by the


following statement:
SQL> select task_name from dba_autotask_task;

TASK_NAME
----------------------------------------------------------------
gather_stats_prog
auto_space_advisor_prog
n s e
i ce
el
AUTO_SQL_TUNING_PROG

a b l
SQL>
fe r
21) Exit from your SQL*Plus session. a n s
o n -tr
an
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
h s
a deฺ
)
s Gui
With the Partitioning, Oracle Label Security, OLAP, Data Mining
e
r ฺ
mi dent
and Real Application Testing options

p ฺ
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$
g ofSthe
22) You now want to observe d
the effects tu automatic statistics-gathering feature of
b i
your database. However, a@you dot h is want to wait until the database automatically
not
opens the next
a ja usewindow. So from your terminal session, execute the
(maintenance
i
b script. toThis script forces the execution of the automatic statistics-
A
run_ags.sh
o
t o ni
gathering task.

e An [oracle@edrsr33p1-orcl Automatic_Gather_Stats]$ ./run_ags.sh


Jos SQL*Plus: Release 11.1.0.6.0 - Production on Mon Apr 7 16:00:03 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> exec dbms_workload_repository.create_snapshot;

PL/SQL procedure successfully completed.

SQL>
SQL> variable window varchar2(20);
SQL>
SQL> begin

Oracle Database 11g: SQL Tuning Workshop A - 231


Practice 7-2: Automatic Statistics Gathering (continued)
2 select upper(to_char(sysdate,'fmday'))||'_WINDOW' into :window
from dual;
3 end;
4 /

PL/SQL procedure successfully completed.


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

SQL>
SQL> print window;

WINDOW
--------------------------------
MONDAY_WINDOW

SQL>
SQL> --
n s e
SQL> -- Open the corresponding maintenance window, but with other
i ce
clients disabled
b l el
SQL> --
SQL>
fer a
SQL> alter system set "_enable_automatic_maintenance"=1
a n s
2 /
n -tr
o
System altered.
s an
) h a deฺ
SQL>
r ฺ e s - Gui
mi :window);
nt
SQL> exec dbms_auto_task_admin.disable(
> ฺ
'auto space advisor', null,
p
g S d e
d tu
b i a@ this
PL/SQL procedure successfully completed.

SQL>
i a (ja use
A o
b tuning tadvisor',
SQL> exec dbms_auto_task_admin.disable( -

o
>
n io'sql null, :window);

nt PL/SQL procedure successfully completed.


e A
J o s SQL>
SQL>
SQL> exec dbms_scheduler.open_window(:window, null, true);

PL/SQL procedure successfully completed.

SQL>
SQL> --
SQL> -- Close the maintenance window when auto optimizer stats
collection is done
SQL> --
SQL>
SQL>
SQL> exec dbms_lock.sleep(120);

PL/SQL procedure successfully completed.

SQL>
SQL> exec dbms_scheduler.close_window(:window);

Oracle Database 11g: SQL Tuning Workshop A - 232


Practice 7-2: Automatic Statistics Gathering (continued)
PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> alter system set "_enable_automatic_maintenance"=0
2 /
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

System altered.

SQL>
SQL> --
SQL> -- Re-enable the other guys so they look like they are enabled
in EM.
SQL> -- Still they will be disabled because we have set the
underscore.
SQL> --
n s e
SQL>
i ce
SQL> exec dbms_auto_task_admin.enable( -
b l el
> 'auto space advisor', null, :window);
fer a
PL/SQL procedure successfully completed.
a n s
o n -tr
an
SQL>
SQL> exec dbms_auto_task_admin.enable( -
> 'sql tuning advisor', null, :window);
h s
a deฺ
e )
s Gui
r ฺ
i ent
PL/SQL procedure successfully completed.

p ฺ m d
SQL>
d g S t u
SQL> exit;
i @ Database
aOracle h is 11g Enterprise Edition Release
Disconnected from
a b t
11.1.0.6.0 -(jProduction
i a u seOracle Label Security, OLAP, Data Mining
With the
A to Testing options
b Application
Partitioning,

ni o
and Real

nto[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$

e A
J o s --------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Automatic_Gather_Stats

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @run_ags.sql

--------------------------------------------------------------

set echo on

exec dbms_workload_repository.create_snapshot;

Oracle Database 11g: SQL Tuning Workshop A - 233


Practice 7-2: Automatic Statistics Gathering (continued)
variable window varchar2(20);

begin
select upper(to_char(sysdate,'fmday'))||'_WINDOW' into :window from
dual;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

end;
/

print window;

--
-- Open the corresponding maintenance window, but with other clients
disabled
--

n s e
alter system set "_enable_automatic_maintenance"=1
i ce
/
b l el
exec dbms_auto_task_admin.disable( -
fer a
'auto space advisor', null, :window);
a n s
o n -tr
an
exec dbms_auto_task_admin.disable( -
'sql tuning advisor', null, :window);
h s
a deฺ
e )
s null, ui true);
r ฺ
i ent
exec dbms_scheduler.open_window(:window, G
p ฺ m d
--
d g S t u
is done b i a@ twindow
-- Close the maintenance
h is when auto optimizer stats collection
--
i a (ja use
A b to
o
ni dbms_lock.sleep(120);
ntoexec
e A
J o s exec dbms_scheduler.close_window(:window);

alter system set "_enable_automatic_maintenance"=0


/

--
-- Re-enable the other guys so they look like they are enabled in
EM.
-- Still they will be disabled because we have set the underscore.
--

exec dbms_auto_task_admin.enable( -
'auto space advisor', null, :window);

exec dbms_auto_task_admin.enable( -
'sql tuning advisor', null, :window);

exit;

Oracle Database 11g: SQL Tuning Workshop A - 234


Practice 7-2: Automatic Statistics Gathering (continued)
23) Connect again as the AGS user from a SQL*Plus session.
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$ sqlplus ags/ags

SQL*Plus: Release 11.1.0.6.0 - Production on Mon Apr 7 16:02:44 2008


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

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
n s e
i ce
24) View the statistics of your objects. What do you observe and why?
b l el
f e ra
a) The statistics were automatically gathered by the database during the maintenance
window. You can also see this directly from the Automated Maintenance
t r a ns Tasks
o n -
page in Enterprise Manager. The important thing is that the database
n
automatically gathered the right statistics and histograms. Depending on your
environment, you may see different sample sizes.s a
) h a deฺ
SQL> @check_table_last_analyzed
r ฺ e s Gui
mi1000dent
SQL>
SQL> set linesize 200 pagesize
p
g Stuฺ
SQL> d
SQL> SELECT
b i a@ this
(ja use
last_analyzed,sample_size,monitoring,num_rows,blocks,table_name
i a
2 FROM user_tables
to = 'EMP';
b table_name
A
3 WHERE
o
t ni
oLAST_ANAL SAMPLE_SIZE MON NUM_ROWS BLOCKS TABLE_NAME
n
eA
--------- ----------- --- ---------- ---------- --------------------

Jos
----------
07-APR-08 100000 YES 100000 874 EMP

SQL>
SQL> @check_index_last_analyzed
SQL> set echo on
SQL>
SQL> set linesize 200 pagesize 1000
SQL>
SQL> SELECT index_name name,num_rows,
2 last_analyzed,distinct_keys,leaf_blocks,
3 avg_leaf_blocks_per_key,join_index
4 FROM user_indexes
5 WHERE table_name = 'EMP';

NAME NUM_ROWS LAST_ANAL DISTINCT_KEYS


LEAF_BLOCKS AVG_LEAF_BLOCKS_PER_KEY JOI
------------------------------ ---------- --------- ------------- --
--------- ----------------------- ---
EMP_I1 100000 07-APR-08 11
196 17 NO

Oracle Database 11g: SQL Tuning Workshop A - 235


Practice 7-2: Automatic Statistics Gathering (continued)
SQL>
SQL> @check_emp_histogram
SQL> set echo on
SQL>
SQL> select column_name, histogram, num_buckets
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2 from user_tab_columns
3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE 1
ENAME NONE 1
PHONE NONE 1
DEPTNO FREQUENCY 5

n s e
SQL>
ice
25) Perform step 7 again. What do you observe and why? b l el
fe r a
a n s
a) The optimizer can make the right decisions for both statements. This is because of

o n -tr
the statistics that were automatically gathered by the database previously.
SQL> @check_exec_plans
s an
SQL> set echo on
) h a deฺ
s Gui
SQL>
SQL> set linesize 200 pagesize 1000
r ฺ e
SQL>
p ฺ mi dent
SQL> set autotrace on
SQL> d g Stu
i
SQL> select count(*),
b a@max(empno)
t h is from emp where deptno = 9;
COUNT(*)i a (ja use
MAX(EMPNO)
A
---------- to
b ----------
i o
ton
10 99

A n
o s e Execution Plan
J ----------------------------------------------------------
Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT | | 1 | 8 |
2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 |
| |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 9 | 72 |
2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | EMP_I1 | 9 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
-------------------

Oracle Database 11g: SQL Tuning Workshop A - 236


Practice 7-2: Automatic Statistics Gathering (continued)
Predicate Information (identified by operation id):
---------------------------------------------------

3 - access("DEPTNO"=9)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
481 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
n s e
0 sorts (memory)
i ce
0 sorts (disk)
b l el
1 rows processed
fer a
SQL>
a n s
o n -tr
SQL> select count(*), max(empno) from emp where deptno = 10;

COUNT(*) MAX(EMPNO)
s an
---------- ----------
) h a deฺ
99900 100000
r ฺ e s Gui
ฺ m i ent
Execution Plan
d g p t u d
a@ this S
----------------------------------------------------------
Plan hash value:bi2083865914

i a (ja use
b to
--------------------------------------------------------------------
A
o
niId | Operation
-------

nto|Time | Name | Rows | Bytes | Cost (%CPU)|

e A |

J o s --------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | 1 | 8 | 265 (1)|
00:00:03 |
| 1 | SORT AGGREGATE | | 1 | 8 | |
|
|* 2 | TABLE ACCESS FULL| EMP | 99863 | 780K| 265 (1)|
00:00:03 |
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - filter("DEPTNO"=10)

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets

Oracle Database 11g: SQL Tuning Workshop A - 237


Practice 7-2: Automatic Statistics Gathering (continued)
805 consistent gets
0 physical reads
0 redo size
482 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 sorts (memory)
0 sorts (disk)
1 rows processed

SQL>
SQL> set autotrace off
SQL>

26) Exit from your SQL*Plus session.


n s e
ce
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
eli
11.1.0.6.0 - Production
a b l
With the Partitioning, Oracle Label Security, OLAP, Data Mining
fer
and Real Application Testing options
a n s
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$
o n -tr
27) From your terminal window, clean up your environment by
a nexecuting the
ags_cleanup.sh script.
h a s eฺ
e s ) u id ./ags_cleanup.sh

ir ent G
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$

ฺ m
SQL*Plus: Release 11.1.0.6.0
d gp SProduction
-
tud
on Mon Apr 7 16:03:37 2008

Copyright (c) 1982,


b i a@ h is
2007, Oracle.
t All rights reserved.

i a (ja use
A
Connectedb to: to
n i o
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
t o
An With the Partitioning, Oracle Label Security, OLAP, Data Mining
Production
e
Jos and Real Application Testing options

SQL>
SQL> drop user ags cascade;

User dropped.

SQL>
SQL> alter system set "_enable_automatic_maintenance"=1;

System altered.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Automatic_Gather_Stats]$

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 238


Practice 7-2: Automatic Statistics Gathering (continued)
#!/bin/bash

cd /home/oracle/solutions/Automatic_Gather_Stats

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

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

sqlplus / as sysdba @ags_cleanup.sql

--------------------------------------------------------------
n s e
i ce
set echo on
b l el
drop user ags cascade;
fer a
a n s
alter system set "_enable_automatic_maintenance"=1;
n - tr
o
exit;
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
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 239


Practices for Lesson 8
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 240


Practice 8-1: Understanding Adaptive Cusrsor Sharing
In this practice, you experiment with bind variable peeking and adaptive cursor sharing.
1) Connected to a terminal session as the oracle user, execute the acs_setup.sh
script to set up the environment used for this lab. You can locate this script in your
$HOME/solutions/Adaptive_Cursor_Sharing directory.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[oracle@edrsr33p1-orcl ~]$ cd solutions/Adaptive_Cursor_Sharing


[oracle@edrsr33p1-orcl Adaptive_Cursor_Sharing]$ ./acs_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Mar 28 16:54:36


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

n s e
i ce
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - b l el
Production
fe ra
With the Partitioning, Oracle Label Security, OLAP, Data Mining
a n s
and Real Application Testing options
o n -tr
SQL>
s an
SQL> drop user acs cascade;
) h a deฺ
drop user acs cascade
r ฺ e s Gui
miexistdent
*
ERROR at line 1:
ORA-01918: user 'ACS' does p ฺ
g Stu
not
d
b i a@ this
SQL>
a ja use
(user
i toacs identified
b tablespace
SQL> create by acs default tablespace users

io A
temporary temp;
o n
t User created.
A n
o s e
J SQL>
SQL> grant dba, connect to acs;

Grant succeeded.

SQL>
SQL> connect acs/acs
Connected.
SQL>
SQL> drop table emp purge;
drop table emp purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table emp
2 (
3 empno number,

Oracle Database 11g: SQL Tuning Workshop A - 241


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
4 ename varchar2(20),
5 phone varchar2(20),
6 deptno number
7 );
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Table created.

SQL>
SQL>
SQL> insert into emp
2 with tdata as
3 (select rownum empno
4 from all_objects
5
6
where rownum <= 1000)
select rownum,
n s e
i ce
el
7 dbms_random.string ('u', 20),
8 dbms_random.string ('u', 20),
a b l
9 case
fer
10
s
when rownum/100000 <= 0.001 then mod(rownum,
a n
-tr
10)
11 else 10
o n
12
13
end
from tdata a, tdata b
s an
14 where rownum <= 100000;
) h a deฺ
r ฺ e s Gui
100000 rows created.
p ฺ mi dent
SQL> d g Stu
SQL> create index a
i @ on
emp_i1
h isemp(deptno);
b t
i a (ja use
Index created.
A b to
o
ni exec dbms_stats.gather_table_stats(null, 'EMP', METHOD_OPT =>
SQL>
ntoSQL>
e A 'FOR COLUMNS DEPTNO SIZE 10', CASCADE => TRUE);

J o s PL/SQL procedure successfully completed.

SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Adaptive_Cursor_Sharing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Adaptive_Cursor_Sharing

Oracle Database 11g: SQL Tuning Workshop A - 242


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @acs_setup.sql

--------------------------------------------------------------

set echo on

n s e
ce
drop user acs cascade;

eli
create user acs identified by acs default tablespace users temporary
a b l
tablespace temp;
fer
a n s
grant dba, connect to acs;
n -tr
o
connect acs/acs
s an
drop table emp purge;
) h a deฺ
r ฺ e s Gui
create table emp
( ฺ m i ent
empno number,
d g p t u d
ename
a@ this
varchar2(20), S
phone b i
varchar2(20),

i a (ja use
deptno number
);
A b to
o
ni into emp
ntoinsert
e A with tdata as

J o s (select rownum empno


from all_objects
where rownum <= 1000)
select rownum,
dbms_random.string ('u', 20),
dbms_random.string ('u', 20),
case
when rownum/100000 <= 0.001 then mod(rownum, 10)
else 10
end
from tdata a, tdata b
where rownum <= 100000;

create index emp_i1 on emp(deptno);

exec dbms_stats.gather_table_stats(null, 'EMP', METHOD_OPT => 'FOR


COLUMNS DEPTNO SIZE 10', CASCADE => TRUE);

alter system flush shared_pool;


exit;

Oracle Database 11g: SQL Tuning Workshop A - 243


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
2) In your terminal session, connect to the SQL*Plus session as the ACS user. Ensure
that you stay connected to the same SQL*Plus session until the end of this lab. After
you get connected, identify the columns of the EMP table that have histograms.
a) Only the DEPTNO column has a 10 buckets histogram.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[oracle@edrsr33p1-orcl Adaptive_Cursor_Sharing]$ sqlplus acs/acs

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Mar 28 16:54:49


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

n s e
ce
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
eli
Production
a b l
With the Partitioning, Oracle Label Security, OLAP, Data Mining
fe r
and Real Application Testing options
a n s
SQL> @check_emp_histogram
o n -tr
SQL>
s an
SQL>
a deฺ
select column_name, histogram, num_buckets
) h
2
3
from user_tab_columns
where table_name='EMP';
r ฺ e s Gui
p ฺ miHISTOGRAM
d e nt
COLUMN_NAME
d g S---------------
tu NUM_BUCKETS

i a @ his NONE
------------------------------ -----------

ab se t
EMPNO
ENAME
a ( j u
NONE
PHONE
b i t o NONE
DEPTNO
i o A HEIGHT BALANCED 10
n
toSQL>
A n
o s e
J --------------------------------------------------------------

set echo on

select column_name, histogram, num_buckets


from user_tab_columns
where table_name='EMP';

3) Determine the distribution of all the distinct values found in the DEPTNO column of
the EMP table. What do you find?
a) Values distribution is uniform for all of them (0.01%) except for value 10
(99.9%). This is typical of what is called data skew.
SQL> @show_deptno_distribution
SQL> set echo on
SQL>
SQL> select deptno, count(*) cnt_per_deptno, (count(*)*100)/nr
deptno_percent

Oracle Database 11g: SQL Tuning Workshop A - 244


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
2 from emp, (select max(empno) nr
3 from emp)
4 group by deptno, nr
5 order by deptno;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

DEPTNO CNT_PER_DEPTNO DEPTNO_PERCENT


---------- -------------- --------------
0 10 .01
1 10 .01
2 10 .01
3 10 .01
4 10 .01
5 10 .01
6
7
10
10
.01
.01
n s e
i ce
el
8 10 .01
9 10 .01
a b l
10 99900 99.9
fe r
a n s
-tr
11 rows selected.

o n
SQL>
s an
h a deฺ
--------------------------------------------------------------
)
r ฺ e s Gui
set echo on
p ฺ mi dent
g Stu
select deptno, count(*)dcnt_per_deptno, (count(*)*100)/nr
@ s
thi nr
iamax(empno)
deptno_percent
j
from emp, (selecta b
( emp)us e
b afrom
ideptno, tonr
i o A
group by

ton
order by deptno;

A n
o s e 4) Before you study the adaptive cursor sharing feature, disable its functionality by
J setting the OPTIMIZER_FEATURES_ENABLE session parameter back to
10.2.0.1. After this is done, ensure that you execute the following command in
your SQL*Plus session: set lines 200 pages 10000. This is used in the lab
to print execution plans correctly.
SQL> alter session set optimizer_features_enable="10.2.0.1";

Session altered.

SQL> set lines 200 pages 10000

5) Determine the execution plan for the following statement:


select /*ACS_L9*/ count(*), max(empno)
from emp
where deptno = 9;
What do you notice and why?
a) The optimizer uses an index range scan because value 9 is very selective.

Oracle Database 11g: SQL Tuning Workshop A - 245


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
SQL> @select_deptno_literal_9
SQL> set echo on
SQL>
SQL> select /*ACS_L9*/ count(*), max(empno)
2 from emp
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 where deptno = 9;

COUNT(*) MAX(EMPNO)
---------- ----------
10 99

SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
SQL> n s e
i ce
el
SQL> select * from table(dbms_xplan.display_cursor);

a b l
PLAN_TABLE_OUTPUT
fer
n s
--------------------------------------------------------------------
a
o n -tr
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 64ngy4j55d1z5, child number 0
s an
-------------------------------------
) h a deฺ
s Gui
select /*ACS_L9*/ count(*), max(empno) from emp where deptno = 9

r ฺ e
Plan hash value: 3184478295
p ฺ mi dent
d g Stu
--------------------------------------------------------------------
-------------------
b i a@ this
( j
| Id | Operation a | se | Name | Rows | Bytes | Cost

bia to
(%CPU)| Time u
A
--------------------------------------------------------------------
io
t o n
-------------------

A n | 0 | SELECT STATEMENT | | | |

o s e 2 (100)|
|
|
1 | SORT AGGREGATE | | 1 | 16 |
J | |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 12 | 192 |
2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | EMP_I1 | 12 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
-------------------

Predicate Information (identified by operation id):


---------------------------------------------------

3 - access("DEPTNO"=9)

20 rows selected.

SQL>

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 246


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
set echo on

select /*ACS_L9*/ count(*), max(empno)


from emp
where deptno = 9;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

6) Determine the execution plan for the following statement:


select /*ACS_L10*/ count(*), max(empno)
from emp
where deptno = 10;
What do you notice and why?
a) The optimizer uses a full table scan because value 10 is not a selective value. n s e
i ce
SQL> @select_deptno_literal_10
b l el
SQL> set echo on
fe r a
SQL>
SQL> select /*ACS_L10*/ count(*), max(empno)
a n s
2 from emp
n - tr
o
an
3 where deptno = 10;

COUNT(*) MAX(EMPNO)
h s
a deฺ
---------- ----------
e )
s Gui
r ฺ
mi dent
99900 100000
p ฺ
g Stu
SQL> d
@ his
SQL> set echo onbia
SQL> @show_latest_exec_plan
j a e t
SQL> (
ia * from s
utable(dbms_xplan.display_cursor);
b t o
oA
SQL> select

n i
ntoPLAN_TABLE_OUTPUT

e A --------------------------------------------------------------------

J o s --------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 3232j5gkp2u5h, child number 0
-------------------------------------
select /*ACS_L10*/ count(*), max(empno) from emp where deptno = 10

Plan hash value: 2083865914

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | | | 240 (100)|
|
| 1 | SORT AGGREGATE | | 1 | 16 | |
|
|* 2 | TABLE ACCESS FULL| EMP | 95000 | 1484K| 240 (1)|
00:00:03 |

Oracle Database 11g: SQL Tuning Workshop A - 247


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):


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

2 - filter("DEPTNO"=10)

19 rows selected.

SQL>

--------------------------------------------------------------
n s e
i ce
el
set echo on

a b l
select /*ACS_L10*/ count(*), max(empno)
fe r
from emp
a n s
-tr
where deptno = 10;

o n
n set it to value 9,
7) Define a bind variable called DEPTNO in your SQL*Plus s a session,
and execute the following query, and determine ) itsaexecution
h i d eฺplan:
select /*ACS_1*/ count(*), rmax(empno)
i ฺes nt Gu
from emp
g p ฺm ude
where deptno = :deptno; d S t
What do you notice b
@
iawhy? thi
and
s
( j a e
suses
a) Becauseiathe optimizer u bind peeking the first time you execute a statement
to and because
bbind variable,
i A
oexecution plan with index access isforused.
with a this first execution, value 9 is used, the
to n
e An SQL> variable deptno number;
Jos SQL> exec :deptno := 9

PL/SQL procedure successfully completed.

SQL> @select_deptno_bind
SQL> set echo on
SQL>
SQL> select /*ACS_1*/ count(*), max(empno)
2 from emp
3 where deptno = :deptno;

COUNT(*) MAX(EMPNO)
---------- ----------
10 99

SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
SQL>
SQL> select * from table(dbms_xplan.display_cursor);

Oracle Database 11g: SQL Tuning Workshop A - 248


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL_ID 272gr4hapc9w1, child number 0


-------------------------------------
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
:deptno

Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
n s e
i ce
el
(%CPU)| Time |
--------------------------------------------------------------------
a b l
-------------------
fer
| 0 | SELECT STATEMENT | | |
a n s
|
2 (100)|
|
|
1 | SORT AGGREGATE | | n -
1 |tr 16 |
o
|
| 2 |
|
TABLE ACCESS BY INDEX ROWID| EMP
s|an 12 | 192 |
2 (0)| 00:00:01 |
) h a deฺ
|* 3 | INDEX RANGE SCAN
r ฺ e s Gui
| EMP_I1 | 12 | |
1 (0)| 00:00:01 |
p ฺ mi dent
--------------------------------------------------------------------
------------------- d g Stu
b i a@(identified
t h is by operation id):
i a (ja use
Predicate Information
---------------------------------------------------
A b to
i o
ton
3 - access("DEPTNO"=:DEPTNO)

A n
o s e 20 rows selected.
J
SQL>

--------------------------------------------------------------

set echo on

select /*ACS_1*/ count(*), max(empno)


from emp
where deptno = :deptno;

--------------------------------------------------------------

set echo on

select * from table(dbms_xplan.display_cursor);

Oracle Database 11g: SQL Tuning Workshop A - 249


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
8) Determine the execution statistics in terms of child cursors, executions, and buffer
gets for the previously executed statement. What do you observe?
a) In V$SQL, only one child cursor exists, and it has been executed only once (first
time ever in this case). Also, the number of buffer gets is small due to the efficient
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

access path that was used.


SQL> @show_latest_exec_stats
SQL> set echo on
SQL>
SQL> select child_number, executions, buffer_gets
2 from v$sql
3 where sql_text like 'select /*ACS_1%';

n s e
CHILD_NUMBER EXECUTIONS BUFFER_GETS
ice
------------ ---------- -----------
0 1 3 b l el
fe r a
SQL>
a n s
--------------------------------------------------------------o n -tr
s an
set echo on
) h a deฺ
e
select child_number, executions, ฺbuffer_gets
r s Gui
from v$sql
p ฺ mi dent
where sql_text like 'select
d g Stu /*ACS_1%';

b i a@ this
a 8 again,
9) Perform steps 7(jand s ebut this time using 10 as the bind value for DEPTNO.
What do b i a
you observe u
toand why?
o A
t o ni The execution plan is identical. The index path is used although value 10 is not
a)

e An selective. This is because bind peeking only operates the first time you execute

Jos
your statement. Looking at V$SQL, you can clearly see that there is still only one
child cursor associated with your statement. However, this time, the number of
buffer gets was raised significantly due to the full table scan.
SQL> exec :deptno := 10

PL/SQL procedure successfully completed.

SQL> @select_deptno_bind
SQL> set echo on
SQL>
SQL> select /*ACS_1*/ count(*), max(empno)
2 from emp
3 where deptno = :deptno;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

SQL>
SQL> @show_latest_exec_plan

Oracle Database 11g: SQL Tuning Workshop A - 250


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
SQL> set echo on
SQL>
SQL> select * from table(dbms_xplan.display_cursor);

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

--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 272gr4hapc9w1, child number 0
-------------------------------------
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
:deptno

Plan hash value: 3184478295


n s e
i ce
el
--------------------------------------------------------------------
-------------------
a b l
| Id | Operation | Name | Rows | Bytes | Cost
fe r
(%CPU)| Time |
a n s
------------------- n - tr
--------------------------------------------------------------------

o
| 0 | SELECT STATEMENT
2 (100)| |
| |
s an | |

| 1 | SORT AGGREGATE |
) h a deฺ
| 1 | 16 |
| |
r ฺ e s Gui
|
2
2 |
(0)| 00:00:01 | p ฺ mi dent
TABLE ACCESS BY INDEX ROWID| EMP | 12 | 192 |

|* 3 | d
INDEX RANGE SCANg Stu | EMP_I1 | 12 | |
1 (0)| 00:00:01 |
b i a@ this
i a (ja use
--------------------------------------------------------------------
-------------------
A b to
o n io
Predicate Information (identified by operation id):
nt ---------------------------------------------------

e A
J o s 3 - access("DEPTNO"=:DEPTNO)

20 rows selected.

SQL>
SQL> @show_latest_exec_stats
SQL> set echo on
SQL>
SQL> select child_number, executions, buffer_gets
2 from v$sql
3 where sql_text like 'select /*ACS_1%';

CHILD_NUMBER EXECUTIONS BUFFER_GETS


------------ ---------- -----------
0 2 957

SQL>

10) Before the next step, flush your shared pool to make sure you wipe out all cursor’s
information.

Oracle Database 11g: SQL Tuning Workshop A - 251


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
SQL> alter system flush shared_pool;

System altered.

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

11) Perform step 9 again. What do you observe and why?


a) The execution plan is a full table scan because you used value 10 as your first
bind value. There is only one child cursor that is created so far to handle your
statement.
SQL> exec :deptno := 10

PL/SQL procedure successfully completed.


n s e
i ce
SQL> @select_deptno_bind
b l el
SQL> set echo on
fe r a
SQL>
a n s
SQL>
2
select /*ACS_1*/ count(*), max(empno)
from emp n - tr
o
an
3 where deptno = :deptno;

COUNT(*) MAX(EMPNO) h s
a deฺ
---------- ----------
e )
s Gui
r ฺ
99900 100000
p ฺ mi dent
SQL> d g Stu
onbi
a@ this
SQL> @show_latest_exec_plan
SQL> set echo a
SQL>
i a (j u se
A b * from
SQL> select to table(dbms_xplan.display_cursor);
ni o
ntoPLAN_TABLE_OUTPUT
e A --------------------------------------------------------------------

J o s --------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 272gr4hapc9w1, child number 0
-------------------------------------
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
:deptno

Plan hash value: 2083865914

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | | | 240 (100)|
|
| 1 | SORT AGGREGATE | | 1 | 16 | |
|
|* 2 | TABLE ACCESS FULL| EMP | 95000 | 1484K| 240 (1)|
00:00:03 |

Oracle Database 11g: SQL Tuning Workshop A - 252


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):


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

2 - filter("DEPTNO"=:DEPTNO)

19 rows selected.

SQL>
SQL> @show_latest_exec_stats
SQL>
SQL>
set echo on
n s e
ice
el
SQL> select child_number, executions, buffer_gets
2 from v$sql
a b l
3 where sql_text like 'select /*ACS_1%';
fe r
a n s
-tr
CHILD_NUMBER EXECUTIONS BUFFER_GETS
------------ ---------- -----------
o n
0 1 872
s an
SQL>
) h a deฺ
esyour bind
12) Perform step 9 again, but this time usei9rฺas G ui What do you observe and
why? p ฺ m dent value.

d g Stu
a) Although value 9 is
i a @ his a full table scan is still used. This is because
very selective,
the second time
j b
a you execute
e t your statement, bind peeking is not done. So you
(
iato use tthe
continue s
u child cursor.
same
b
A :deptno := 9 o
n i
SQL>o exec
to
e An PL/SQL procedure successfully completed.
Jos SQL> @select_deptno_bind
SQL> set echo on
SQL>
SQL> select /*ACS_1*/ count(*), max(empno)
2 from emp
3 where deptno = :deptno;

COUNT(*) MAX(EMPNO)
---------- ----------
10 99

SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
SQL>
SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT

Oracle Database 11g: SQL Tuning Workshop A - 253


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 272gr4hapc9w1, child number 0
-------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select /*ACS_1*/ count(*), max(empno) from emp where deptno =


:deptno

Plan hash value: 2083865914

--------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
n s e
ice
el
-------
| 0 | SELECT STATEMENT | | | | 240 (100)|
a b l
|
fe r
| 1 | SORT AGGREGATE | | 1 | 16 | |
a n s
|
|* 2 | TABLE ACCESS FULL| EMP | 95000 | 1484K| n -240 (1)| tr
o
00:00:03 |
s an
--------------------------------------------------------------------
-------
) h a deฺ
r ฺ e s Gui
ฺ m i eoperation
Predicate Information (identified by
n t id):

gp Stud
---------------------------------------------------
d
i a@ this
2 - filter("DEPTNO"=:DEPTNO)
b
i a (ja use
to
19 rows bselected.
A
ni o
ntoSQL>
e A SQL> @show_latest_exec_stats

J o s SQL>
SQL>
set echo on

SQL> select child_number, executions, buffer_gets


2 from v$sql
3 where sql_text like 'select /*ACS_1%';

CHILD_NUMBER EXECUTIONS BUFFER_GETS


------------ ---------- -----------
0 2 1693

SQL>

13) Before the next step, reset your session to use adaptive cursor sharing, and ensure that
you flush your shared pool again.
SQL> alter session set optimizer_features_enable="11.1.0.6";

Session altered.

SQL> alter system flush shared_pool;

Oracle Database 11g: SQL Tuning Workshop A - 254


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
System altered.

SQL>

14) Perform step 12 again. What do you observe, and why?


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

a) Because this is the first time you execute the statement, bind peeking is used, and
because value 9 is very selective, the index path is used. Only one child cursor is
used to handle this statement.
SQL> exec :deptno := 9

PL/SQL procedure successfully completed.

SQL> @select_deptno_bind
n s e
SQL> set echo on
ice
SQL>
b l el
SQL> select /*ACS_1*/ count(*), max(empno)
fe ra
2
3
from emp
where deptno = :deptno;
a n s
o n -tr
an
COUNT(*) MAX(EMPNO)
---------- ----------
10 99
h s
a deฺ
e )
s Gui
SQL> r ฺ
i ent
SQL> @show_latest_exec_plan ฺm
SQL> set echo on d p
g Stud
SQL>
i @ his
atable(dbms_xplan.display_cursor);
a
SQL> select * from
j b e t
(
ia to u s
b
oA
PLAN_TABLE_OUTPUT
i
--------------------------------------------------------------------
n
nto--------------------------------------------------------------------

e A ----------------------------------------------------------------

J o s SQL_ID 272gr4hapc9w1, child number 0


-------------------------------------
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
:deptno

Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT | | | |
2 (100)| |
| 1 | SORT AGGREGATE | | 1 | 16 |
| |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 16 |
2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | EMP_I1 | 1 | |
1 (0)| 00:00:01 |

Oracle Database 11g: SQL Tuning Workshop A - 255


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
--------------------------------------------------------------------
-------------------

Predicate Information (identified by operation id):


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

3 - access("DEPTNO"=:DEPTNO)

20 rows selected.

SQL>
SQL> @show_latest_exec_stats
SQL>
SQL>
set echo on
n s e
i ce
el
SQL> select child_number, executions, buffer_gets
2 from v$sql
a b l
3 where sql_text like 'select /*ACS_1%';
fe r
a n s
-tr
CHILD_NUMBER EXECUTIONS BUFFER_GETS
------------ ---------- -----------
o n
0 1 54
s an
SQL>
) h a deฺ
r ฺ e s 10 G ui bind value. What do you
observe and why? p ฺ mi dent
15) Perform step 14 again, but this time using value as your

a) Although value 10ais@


dg Sthe tusame index path as in the previous step is
b i t h s
not selective,
icurrently
used. Only one
( j a child cursor
s e is needed to represent your statement.
u
bia:deptnoto:= 10
A
SQL> exec
io procedure successfully completed.
t o n
PL/SQL

e An
Jos
SQL> @select_deptno_bind
SQL> set echo on
SQL>
SQL> select /*ACS_1*/ count(*), max(empno)
2 from emp
3 where deptno = :deptno;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
SQL>
SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 256


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
SQL_ID 272gr4hapc9w1, child number 0
-------------------------------------
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
:deptno
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Plan hash value: 3184478295

--------------------------------------------------------------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time |
--------------------------------------------------------------------
-------------------
| 0 | SELECT STATEMENT
2 (100)| |
| | | |
n s e
ice
el
| 1 | SORT AGGREGATE | | 1 | 16 |
| |
a b l
| 2 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 16 |
fe r
2 (0)| 00:00:01 |
a n s
|* 3 |
1
INDEX RANGE SCAN
(0)| 00:00:01 |
| EMP_I1 |
n
1 |
-
|
tr
o
-------------------
s an
--------------------------------------------------------------------

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

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

d g Stu
3 - access("DEPTNO"=:DEPTNO)

b i a@ this
i a (ja use
20 rows selected.
A b to
o
ni @show_latest_exec_stats
SQL>
ntoSQL>
e A SQL> set echo on

J o s SQL>
SQL> select child_number, executions, buffer_gets
2 from v$sql
3 where sql_text like 'select /*ACS_1%';

CHILD_NUMBER EXECUTIONS BUFFER_GETS


------------ ---------- -----------
0 2 1008

SQL>

16) Perform step 15 again. What do you observe and why?


a) Because you now use adaptive cursor sharing, the system realizes that you benefit
from another child cursor for handling your statement. This time, a full table
access path is used to better handle your statement.
SQL> exec :deptno := 10

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 257


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
SQL> @select_deptno_bind
SQL> set echo on
SQL>
SQL> select /*ACS_1*/ count(*), max(empno)
2 from emp
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 where deptno = :deptno;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
SQL>
n s e
i ce
el
SQL> select * from table(dbms_xplan.display_cursor);

a b l
PLAN_TABLE_OUTPUT
fer
a n s
--------------------------------------------------------------------

-tr
--------------------------------------------------------------------
n
----------------------------------------------------------------
o
SQL_ID 272gr4hapc9w1, child number 1
-------------------------------------
s an
h a deฺ
select /*ACS_1*/ count(*), max(empno) from emp where deptno =
)
:deptno
r ฺ e s Gui
p
Plan hash value: 2083865914 ฺ mi dent
d g Stu
i a@ this
--------------------------------------------------------------------
b
-------
i a (ja use | Name | Rows | Bytes | Cost (%CPU)|
| Id | Operation
Time
A b| to
ni o
--------------------------------------------------------------------
nto-------
e A | 0 | SELECT STATEMENT | | | | 240 (100)|

J o s |
| 1 | SORT AGGREGATE | | 1 | 16 | |
|
|* 2 | TABLE ACCESS FULL| EMP | 95000 | 1484K| 240 (1)|
00:00:03 |
--------------------------------------------------------------------
-------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - filter("DEPTNO"=:DEPTNO)

19 rows selected.

SQL>
SQL> @show_latest_exec_stats
SQL> set echo on
SQL>
SQL> select child_number, executions, buffer_gets

Oracle Database 11g: SQL Tuning Workshop A - 258


Practice 8-1: Understanding Adaptive Cusrsor Sharing
(continued)
2 from v$sql
3 where sql_text like 'select /*ACS_1%';

CHILD_NUMBER EXECUTIONS BUFFER_GETS


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

0 2 1008
1 1 821

SQL>

17) Exit your SQL*Plus session, and execute the acs_cleanup.sh script to clean up
your environment.
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
n s e
i ce
el
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
a b l
and Real Application Testing options
fe r
[oracle@edrsr33p1-orcl Adaptive_Cursor_Sharing]$ ./acs_cleanup.sh
a n s
n -tr
SQL*Plus: Release 11.1.0.6.0 - Production on Fri Mar 28 16:59:18
o
an
2008

Copyright (c) 1982, 2007, Oracle. All h


s eฺ
a dreserved.
rights
) i
i r ฺes nt Gu
Connected to:
g p ฺm ude
d
Oracle Database 11g Enterprise
S t
Edition Release 11.1.0.6.0 -
@ s
thi Label Security, OLAP, Data Mining
ia Oracle
Production
j a b
With the Partitioning,
e
ia ( to usTesting options
and Real Application
b
i
SQL>o A
n
toSQL> drop user acs cascade;
A n
o s e User dropped.
J
SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Adaptive_Cursor_Sharing]$

Oracle Database 11g: SQL Tuning Workshop A - 259


Practice 8-2: Understanding CURSOR_SHARING
In this practice, you investigate the use of the CURSOR_SHARING initialization
parameter.
1) You can find all the necessary scripts for this lab in your
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

$HOME/solutions/Cursor_Sharing directory. First, you must set up the


environment for this lab by executing the cs_setup.sh script from a terminal
session connected as the oracle user. This script creates a new user called CS and
the EMP table used throughout this lab.
[oracle@edrsr33p1-orcl ~]$ cd solutions/Cursor_Sharing
[oracle@edrsr33p1-orcl Cursor_Sharing]$
[oracle@edrsr33p1-orcl Cursor_Sharing]$ ./cs_setup.sh

n s e
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 14:10:59
i ce
2008
b l el
Copyright (c) 1982, 2007, Oracle. All rights reserved.
fe ra
a n s
Connected to: o n -tr
an
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
s
Production
) h a deฺ
r ฺ e s Gui
With the Partitioning, Oracle Label Security, OLAP, Data Mining

mi dent
and Real Application Testing options

SQL> p ฺ
g Stu
d
a@ this
SQL> drop user cs cascade;
b i
User dropped.
i a (ja use
SQL> A
b to
o
ni createtablespace
n t oSQL>
temporary
user cs identified by cs default tablespace users
temp;
e A
Jos User created.

SQL>
SQL> grant dba, connect to cs;

Grant succeeded.

SQL>
SQL> connect cs/cs
Connected.
SQL>
SQL> drop table emp purge;
drop table emp purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table emp

Oracle Database 11g: SQL Tuning Workshop A - 260


Practice 8-2: Understanding CURSOR_SHARING (continued)
2 (
3 empno number,
4 ename varchar2(20),
5 phone varchar2(20),
6 deptno number
7 );
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Table created.

SQL>
SQL>
SQL> insert into emp
2 with tdata as
3 (select rownum empno
4 from all_objects
5 where rownum <= 1000)
n s e
6 select rownum,
i ce
7 dbms_random.string ('u', 20),
b l el
8
9
dbms_random.string ('u', 20),
case
fer a
10 n s
when rownum/100000 <= 0.001 then mod(rownum,
a
10)
o n -tr
an
11 else 10
12 end
13 from tdata a, tdata b
h s
a deฺ
14 where rownum <= 100000;
e )
s Gui
r ฺ
100000 rows created.
p ฺ mi dent
d g Stu
SQL>
i @ on
aemp_i1 h isemp(deptno);
SQL> create indexb t
i a (ja use
b
Index created.
A to
n i o
ntoSQL>
e A SQL> execute dbms_stats.gather_table_stats(null, 'EMP', cascade =>

J o s true);

PL/SQL procedure successfully completed.

SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> connect / as sysdba
Connected.
SQL>
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>
SQL> startup;
ORACLE instance started.

Oracle Database 11g: SQL Tuning Workshop A - 261


Practice 8-2: Understanding CURSOR_SHARING (continued)
Total System Global Area 845348864 bytes
Fixed Size 1303188 bytes
Variable Size 541068652 bytes
Database Buffers 297795584 bytes
Redo Buffers 5181440 bytes
Database mounted.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Database opened.
SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Cursor_Sharing]$

--------------------------------------------------------------
n s e
i ce
#!/bin/bash
b l el
cd /home/oracle/solutions/Cursor_Sharing
fer a
a n s
export ORACLE_SID=orcl
o n -tr
an
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
s
) h a deฺ
export
r ฺ e s Gui
mi dent
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
p
sqlplus / as sysdba @ dg Stu
b i a t h is
@cs_setup.sql

i a (ja use
--------------------------------------------------------------

A b on to
o
ni user cs cascade;
set echo

ntodrop
e A
J o s create user cs identified by cs default tablespace users temporary
tablespace temp;

grant dba, connect to cs;

connect cs/cs

drop table emp purge;

create table emp


(
empno number,
ename varchar2(20),
phone varchar2(20),
deptno number
);

insert into emp


with tdata as

Oracle Database 11g: SQL Tuning Workshop A - 262


Practice 8-2: Understanding CURSOR_SHARING (continued)
(select rownum empno
from all_objects
where rownum <= 1000)
select rownum,
dbms_random.string ('u', 20),
dbms_random.string ('u', 20),
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

case
when rownum/100000 <= 0.001 then mod(rownum, 10)
else 10
end
from tdata a, tdata b
where rownum <= 100000;

create index emp_i1 on emp(deptno);

execute dbms_stats.gather_table_stats(null, 'EMP', cascade => true);


n s e
ice
alter system flush shared_pool;
b l el
connect / as sysdba
fe r a
a n s
shutdown immediate;
o n -tr
startup;
s an
) h a deฺ
exit;
r ฺ e s Gui
ฺ m i ent
2) From the same terminal session,
d p
g connect t uasdthe CS user in the SQL*Plus session, and
stay connected to that a @ until
session i S
sthe end of this lab. For formatting reasons, after
you have connected b i t h
a in thesSQL*Plus session, execute the following command:
( j u e
b ia to
set linesize 200 pagesize 1000

n i oA
[oracle@edrsr33p1-orcl Cursor_Sharing]$ sqlplus cs/cs
t o
e An SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 14:11:38
Jos
2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> set linesize 200 pagesize 1000


SQL>

3) Check the existence of histograms on the columns of the EMP table, and then
determine the data distribution in the DEPTNO column of the EMP table. What do you
observe?

Oracle Database 11g: SQL Tuning Workshop A - 263


Practice 8-2: Understanding CURSOR_SHARING (continued)
a) Currently, there are no histograms created on the columns of the EMP table. Also,
it is clear that you have data skew in the DEPTNO column. Value 10 repeats most
of the time (99.9%) whereas all other values only repeat 0.01%.
SQL> @check_emp_histogram
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> select column_name, histogram, num_buckets


2 from user_tab_columns
3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE 1
ENAME NONE 1
PHONE
DEPTNO
NONE
NONE
1
1 n s e
ice
SQL>
b l el
SQL> @show_deptno_distribution
fe ra
SQL> set echo on
a n s
SQL>
n - tr
o
SQL> select deptno, count(*) cnt_per_deptno, (count(*)*100)/nr
deptno_percent
s an
2 from emp, (select max(empno) nr
) h a deฺ
s Gui
3 from emp)
4 group by deptno, nr
r ฺ e
5 order by deptno;
p ฺ mi dent
d
DEPTNO CNT_PER_DEPTNOg DEPTNO_PERCENT
S tu
i a@ 10th--------------
---------- --------------
b is
1a (
0 ja use10 .01

b i2 t o .01

i o A 3 10 .01

t o n 10 .01

A n 5
4 10
10
.01
.01
o s e 6 10 .01
J 7 10 .01
8 10 .01
9 10 .01
10 99900 99.9

11 rows selected.

SQL>

--------------------------------------------------------------

set echo on

select column_name, histogram, num_buckets


from user_tab_columns
where table_name='EMP';

--------------------------------------------------------------

set echo on

Oracle Database 11g: SQL Tuning Workshop A - 264


Practice 8-2: Understanding CURSOR_SHARING (continued)
select deptno, count(*) cnt_per_deptno, (count(*)*100)/nr
deptno_percent
from emp, (select max(empno) nr
from emp)
group by deptno, nr
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

order by deptno;

4) Before you continue, ensure that you flush your shared pool.
SQL> alter system flush shared_pool;

System altered.

SQL>
n s e
ice
5) How would you force your SQL*Plus session to automatically replace statement b l el
ra
literals with bind variables to make sure the same cursor is used independently of the
fe
literal values?
a n s
SQL> alter session set cursor_sharing = force; o n -tr
s an
Session altered.
) h a deฺ
r ฺ e s Gui
mi dent
SQL>

p ฺ
g execute
6) From the same SQL*Plus d
session, S tuthe following two queries, and then
determine how many b i a@ arethgenerated
cursors is to handle these two statements, and what
(
execution plans j a used.seWhat do you observe
were and why?
b ia count(*),
t o u
i o
select A
select /*CS*/
/*CS*/ count(*),
max(empno)
max(empno)
from
from
emp where deptno = 9;
emp where deptno = 10;
t o n
e An a) Because of the previous step, literal values are replaced with bind variables. The
Jos
FORCE option forces the system to share only one child cursor in this case and
use the exact same execution plan (index range scan).
SQL> @select_deptno_literal_9
SQL> set echo on
SQL>
SQL> select /*CS*/ count(*), max(empno) from emp where deptno = 9;

COUNT(*) MAX(EMPNO)
---------- ----------
10 99

SQL>
SQL> @select_deptno_literal_10
SQL> set echo on
SQL>
SQL> select /*CS*/ count(*), max(empno) from emp where deptno = 10;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

Oracle Database 11g: SQL Tuning Workshop A - 265


Practice 8-2: Understanding CURSOR_SHARING (continued)
SQL>
SQL> @show_latest_cursors
SQL> set echo on
SQL>
SQL> col sql_text format a70
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> select sql_text,hash_value
2 from v$sql
3 where sql_text like '%select /*CS%';

SQL_TEXT
HASH_VALUE
--------------------------------------------------------------------
-- ----------
select /*CS*/ count(*), max(empno) from emp where deptno =
n s e
:"SYS_B_0" 3434097775
i ce
b l el
SQL> @show_latest_exec_plans
SQL> set echo on
fer a
SQL>
a n s
SQL> col object_name format a5
n -tr
o
an
SQL> col operation format a16
SQL> col options format a15
SQL>
h s
a deฺ
)
s Gui
SQL> select address,hash_value,child_number,
e
r ฺ
mi dent
operation,options,object_name
2 from v$sql_plan
p ฺ
g Stu
d
3 where (address,hash_value) in
4
5 from v$sql b i a@ this
(select address,hash_value

6 (ja use
where sql_text like '%select /*CS%');
i a
A b to
o n io
ADDRESS
OBJEC
HASH_VALUE CHILD_NUMBER OPERATION OPTIONS

nt
e A -------- ---------- ------------ ---------------- --------------- --

J o s ---
4C9C53D4 3434097775 0 SELECT STATEMENT
4C9C53D4 3434097775 0 SORT AGGREGATE
4C9C53D4 3434097775 0 TABLE ACCESS BY INDEX ROWID
EMP
4C9C53D4 3434097775 0 INDEX RANGE SCAN
EMP_I
1

SQL>

--------------------------------------------------------------

set echo on

select /*CS*/ count(*), max(empno) from emp where deptno = 9;

--------------------------------------------------------------

set echo on

Oracle Database 11g: SQL Tuning Workshop A - 266


Practice 8-2: Understanding CURSOR_SHARING (continued)
select /*CS*/ count(*), max(empno) from emp where deptno = 10;

--------------------------------------------------------------

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

col sql_text format a70

select sql_text,hash_value
from v$sql
where sql_text like '%select /*CS%';

--------------------------------------------------------------

set echo on
n s e
i ce
col object_name format a5
b l el
col operation format a16
col options format a15
fe r a
a n s
select address,hash_value,child_number,
o n -tr
an
operation,options,object_name
from v$sql_plan
where (address,hash_value) in
h s
a deฺ
(select address,hash_value
e )
s Gui
r ฺ
mi dent
from v$sql

p
g Stuฺ
where sql_text like '%select /*CS%');
d
b i
7) Ensure that you create aa @ t h ishistogram on the DEPTNO column of the EMP
10 buckets
table.
i a (ja use
A to
b dbms_stats.gather_table_stats(null,
i o
SQL> exec
n
'EMP', METHOD_OPT =>

nto
'FOR COLUMNS DEPTNO SIZE 10', CASCADE => TRUE);

e A
Jos
PL/SQL procedure successfully completed.

SQL> @check_emp_histogram
SQL> set echo on
SQL>
SQL> select column_name, histogram, num_buckets
2 from user_tab_columns
3 where table_name='EMP';

COLUMN_NAME HISTOGRAM NUM_BUCKETS


------------------------------ --------------- -----------
EMPNO NONE 1
ENAME NONE 1
PHONE NONE 1
DEPTNO HEIGHT BALANCED 10

SQL>

--------------------------------------------------------------

set echo on

Oracle Database 11g: SQL Tuning Workshop A - 267


Practice 8-2: Understanding CURSOR_SHARING (continued)
select column_name, histogram, num_buckets
from user_tab_columns
where table_name='EMP';
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

8) Before you continue, ensure that you flush your shared pool.
SQL> alter system flush shared_pool;

System altered.

SQL>

9) Perform step 6 again. What do you observe and why? e


n s
a) Although you captured histogram for the DEPTNO column that shows data skew, i ce
l
the system continues to share only one child cursor to handle both statements.
b el
This behavior is due to the FORCE option for the CURSOR_SHARING fe r a
initialization parameter. a n s
o n -tr
SQL> @select_deptno_literal_9 a n
SQL> set echo on
h s
a deฺ
SQL>
e )
s from i
uemp
i r
SQL> select /*CS*/ count(*), max(empno)ฺ n t G where deptno = 9;
m
COUNT(*) MAX(EMPNO)
d gpฺ Stude
---------- ----------
i @ his
a99
10
j a b e t
(
ia to u s
SQL>
A b
i
SQL>
n o @select_deptno_literal_10
ntoSQL> set echo on

e A SQL>

J o s SQL> select /*CS*/ count(*), max(empno) from emp where deptno = 10;

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

SQL>
SQL> @show_latest_cursors
SQL> set echo on
SQL>
SQL> col sql_text format a70
SQL>
SQL> select sql_text,hash_value
2 from v$sql
3 where sql_text like '%select /*CS%';

SQL_TEXT
HASH_VALUE
--------------------------------------------------------------------
-- ----------

Oracle Database 11g: SQL Tuning Workshop A - 268


Practice 8-2: Understanding CURSOR_SHARING (continued)
select /*CS*/ count(*), max(empno) from emp where deptno =
:"SYS_B_0" 3434097775

SQL>

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

set echo on

select /*CS*/ count(*), max(empno) from emp where deptno = 9;

--------------------------------------------------------------

set echo on

select /*CS*/ count(*), max(empno) from emp where deptno = 10;


n s e
i ce
--------------------------------------------------------------
b l el
set echo on
fer a
a n s
col sql_text format a70
o n -tr
select sql_text,hash_value
s an
where sql_text like '%select /*CS%'; ) h
from v$sql a deฺ
r ฺ e s Gui
ฺ m i ent
10) Before you continue, ensure that
d p
g you S tudyour shared pool.
flush
SQL> alter systemia @ shared_pool;
h is
b flush
t
i a (ja use
b
System altered.
A to
ni
SQL>o
t o
e An11) How would you ensure that you now use more than one child cursor to handle both
Jos statements? Implement your solution, and check it.
a) By setting CURSOR_SHARING to SIMILAR for your session, the system is able
to see that you benefit from using two different child cursors to handle both
statements because they lend themselves to very different execution plans.

SQL> alter session set cursor_sharing = similar;

Session altered.

SQL> @select_deptno_literal_9
SQL> set echo on
SQL>
SQL> select /*CS*/ count(*), max(empno) from emp where deptno = 9;

COUNT(*) MAX(EMPNO)
---------- ----------
10 99

Oracle Database 11g: SQL Tuning Workshop A - 269


Practice 8-2: Understanding CURSOR_SHARING (continued)
SQL>
SQL> @select_deptno_literal_10
SQL> set echo on
SQL>
SQL> select /*CS*/ count(*), max(empno) from emp where deptno = 10;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

COUNT(*) MAX(EMPNO)
---------- ----------
99900 100000

SQL>
SQL> @show_latest_cursors
SQL> set echo on
SQL>
SQL> col sql_text format a70
SQL>
n s e
SQL> select sql_text,hash_value
i ce
2 from v$sql
b l el
3 where sql_text like '%select /*CS%';
fer a
SQL_TEXT
a n s
HASH_VALUE
n -tr
o
an
--------------------------------------------------------------------
-- ----------
h s
a deฺ
select /*CS*/ count(*), max(empno) from emp where deptno =
:"SYS_B_0" 3434097775
e )
s Gui
r ฺ
mi dent
select /*CS*/ count(*), max(empno) from emp where deptno =
:"SYS_B_0" 3434097775
p ฺ
g Stu
d
@ his
SQL> @show_latest_exec_plans
SQL> set echo onbia t
SQL> ( j a s e
SQL> colb a to u format a5
iobject_name
i A operation format a16
o col
SQL> col
n
toSQL> options format a15
SQL>
A n
o s e SQL> select address,hash_value,child_number,
J operation,options,object_name
2 from v$sql_plan
3 where (address,hash_value) in
4 (select address,hash_value
5 from v$sql
6 where sql_text like '%select /*CS%');

ADDRESS HASH_VALUE CHILD_NUMBER OPERATION OPTIONS


OBJEC
-------- ---------- ------------ ---------------- --------------- --
---
4C9C53D4 3434097775 1 SELECT STATEMENT
4C9C53D4 3434097775 1 SORT AGGREGATE
4C9C53D4 3434097775 1 TABLE ACCESS FULL
EMP
4C9C53D4 3434097775 0 SELECT STATEMENT
4C9C53D4 3434097775 0 SORT AGGREGATE
4C9C53D4 3434097775 0 TABLE ACCESS BY INDEX ROWID
EMP

Oracle Database 11g: SQL Tuning Workshop A - 270


Practice 8-2: Understanding CURSOR_SHARING (continued)
4C9C53D4 3434097775 0 INDEX RANGE SCAN
EMP_I
1

7 rows selected.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

--------------------------------------------------------------

set echo on

select /*CS*/ count(*), max(empno) from emp where deptno = 9;

--------------------------------------------------------------
n s e
ice
set echo on
b l el
select /*CS*/ count(*), max(empno) from emp where deptno = 10;
fe r a
a n s
o n -tr
--------------------------------------------------------------

set echo on
s an
) h a deฺ
col sql_text format a70
r ฺ e s Gui
select sql_text,hash_value ฺmi e nt
from v$sql p
dg /*CS%'; tu d
where sql_text like @
a '%select
is S
b i t h
i a (ja use
--------------------------------------------------------------

A b on to
o
ni object_name format a5
set echo

ntocol
e A col operation format a16

J o s col options format a15


select address,hash_value,child_number,
operation,options,object_name
from v$sql_plan
where (address,hash_value) in
(select address,hash_value
from v$sql
where sql_text like '%select /*CS%');

12) Exit your SQL*Plus session and execute the cs_cleanup.sh script to clean up
your environment for this lab.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Cursor_Sharing]$
[oracle@edrsr33p1-orcl Cursor_Sharing]$ ./cs_cleanup.sh

Oracle Database 11g: SQL Tuning Workshop A - 271


Practice 8-2: Understanding CURSOR_SHARING (continued)
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Mar 31 14:15:49
2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


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

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> drop user cs cascade;

User dropped.
n s e
i ce
SQL>
b l el
SQL> alter system flush shared_pool;
fer a
System altered.
a n s
o n -tr
an
SQL>
SQL> exit;
h s
a deฺ
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
e )
s Gui
r ฺ
mi dent
With the Partitioning, Oracle Label Security, OLAP, Data Mining

p ฺ
and Real Application Testing options
g Stu
d
[oracle@edrsr33p1-orcl Cursor_Sharing]$

b i a@ this
--------------------------------------------------------------

i a (ja use
b
#!/bin/bash
A to
o
cdi /home/oracle/solutions/Cursor_Sharing
nton
e A export ORACLE_SID=orcl

J o s
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
sqlplus / as sysdba @cs_cleanup.sql
--------------------------------------------------------------
set echo on
drop user cs cascade;
alter system flush shared_pool;
exit;

Oracle Database 11g: SQL Tuning Workshop A - 272


Practices for Lesson 9
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 273


Practice 9-1: Using Hints
In this practice, you study five different hint cases. They are all independent from each
other. Note: You can find all the necessary scripts used for this lab in your
$HOME/solutions/Hints directory. You should be using a terminal session
connected as the oracle user.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1) From your terminal session, execute the iot_setup.sh script. This script creates
an index-organized table.
[oracle@edrsr33p1-orcl Hints]$ ./iot_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 15:17:24 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
i ce
Connected to:
b l el
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
fe ra
Production
a n s
-tr
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
o n
SQL>
s an
SQL> drop user iot cascade;
) h a deฺ
drop user iot cascade
r ฺ e s Gui
*
ERROR at line 1: p ฺ mi dent
d
ORA-01918: user 'IOT' doesg notSexist
tu
b i a@ this
SQL>
i a (ja use
A b usertoiot identified by iot default tablespace users
SQL> create
n i o
temporary tablespace temp;
nt o
e A User created.

J o s SQL>
SQL> grant connect, resource, dba to iot;

Grant succeeded.

SQL>
SQL> connect iot/iot
Connected.
SQL>
SQL> drop table iottab purge;
drop table iottab purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> CREATE TABLE IOTTAB (
2 OBJECT_ID NUMBER(14, 0) NOT NULL ENABLE
3 , OBJECT_ID_ATT NUMBER(14, 0) NOT NULL ENABLE

Oracle Database 11g: SQL Tuning Workshop A - 274


Practice 9-1: Using Hints (continued)
4 , OBJECT_ID_CAT NUMBER(14, 0) NOT NULL ENABLE
5 , BEGIN DATE NOT NULL ENABLE
6 , END DATE NOT NULL ENABLE
7 , STATUS NUMBER
8 , COMM VARCHAR2(32) NOT NULL ENABLE
9 , CONSTRAINT IOTTAB_PK
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

10 PRIMARY KEY (OBJECT_ID


11 , OBJECT_ID_ATT
12 , OBJECT_ID_CAT
13 , BEGIN
14 , END) ENABLE )
15 ORGANIZATION INDEX PCTTHRESHOLD 50 ;

Table created.

SQL>
n s e
SQL> CREATE INDEX OBJECT_ID_ATT_INDX ON IOTTAB (OBJECT_ID_ATT);
i ce
b l el
Index created.
fer a
SQL>
a n s
SQL> -- load data
n -tr
o
an
SQL>
SQL> begin
2 for i in 400001..500000 loop
h s
a deฺ
3 )
s Gui
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
e
r ฺ
mi dent
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
p ฺ
g Stu
4 end loop;
d
5 commit;
6 end; b i a@ this
7 /
i a (ja use
A b to
o n io
PL/SQL procedure successfully completed.

nt SQL>
e A
J o s SQL>
SQL> begin
2 for i in 100001..200000 loop
3 insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
4 end loop;
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> begin
2 for i in 300001..400000 loop
3 insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
4 end loop;

Oracle Database 11g: SQL Tuning Workshop A - 275


Practice 9-1: Using Hints (continued)
5 commit;
6 end;
7 /

PL/SQL procedure successfully completed.


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

SQL>
SQL>
SQL> begin
2 for i in 500001..600000 loop
3 insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
4 end loop;
5 commit;
6 end;
n s e
7 /
i ce
b l el
PL/SQL procedure successfully completed.
fer a
SQL>
a n s
SQL>
n -tr
o
an
SQL> begin
2 for i in 1..100000 loop
3
h s
a deฺ
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
)
s Gui
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
e
r ฺ
mi dent
|i);
4 end loop;
p ฺ
g Stu
5 commit;
d
6 end;
7 / b i a@ this
i a (ja successfully
u se
b
PL/SQL procedure
A to completed.

o n io
SQL>
Ant SQL>
e
Jos
SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> alter system flush buffer_cache;

System altered.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$

--------------------------------------------------------------

#!/bin/bash

Oracle Database 11g: SQL Tuning Workshop A - 276


Practice 9-1: Using Hints (continued)
cd /home/oracle/solutions/Hints

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @iot_setup.sql

--------------------------------------------------------------

set echo on
n s e
i ce
drop user iot cascade;
b l el
fer
create user iot identified by iot default tablespace users temporary a
tablespace temp;
a n s
n -tr
o
an
grant connect, resource, dba to iot;

connect iot/iot
h s
a deฺ
e )
s Gui
drop table iottab purge; r ฺ
i ent
p ฺ m d
CREATE TABLE IOTTAB (
d g S t u
OBJECT_ID
, OBJECT_ID_ATTbia
@ his0)
NUMBER(14, NOT NULL ENABLE
t
NUMBER(14, 0) NOT NULL ENABLE

i a (ja NUMBER(14,
, OBJECT_ID_CAT
u seNOT NULL0) NOT NULL ENABLE
, BEGIN
A b toDATE NOT NULL ENABLE
DATE

n o
,i STATUS
, END ENABLE

nto , COMM NUMBER

e A VARCHAR2(32) NOT NULL ENABLE

J o s , CONSTRAINT IOTTAB_PK
PRIMARY KEY (OBJECT_ID
, OBJECT_ID_ATT
, OBJECT_ID_CAT
, BEGIN
, END) ENABLE )
ORGANIZATION INDEX PCTTHRESHOLD 50 ;

CREATE INDEX OBJECT_ID_ATT_INDX ON IOTTAB (OBJECT_ID_ATT);

-- load data

begin
for i in 400001..500000 loop
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
end loop;
commit;
end;
/

Oracle Database 11g: SQL Tuning Workshop A - 277


Practice 9-1: Using Hints (continued)

begin
for i in 100001..200000 loop
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|i);
end loop;
commit;
end;
/

begin
for i in 300001..400000 loop
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
n s e
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
i ce
|i);
b l el
end loop;
commit;
fer a
end;
a n s
/
o n -tr
s an
begin
) h a deฺ
for i in 500001..600000 loop
r ฺ e s Gui
ฺ m i ent
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
d g p t u d
end loop;
a@ this S
commit; b i
end;
i a (ja use
/
A b to
ni o
ntobegin
e A
J o s for i in 1..100000 loop
insert into iottab values(i,mod(i,428),mod(i,20),sysdate-
mod(i,100),sysdate+mod(i,100),mod(i,3),'aaaaaaaaaaaaaaaaaaaaaaaaaa'|
|i);
end loop;
commit;
end;
/

alter system flush shared_pool;

alter system flush buffer_cache;

exit;

Oracle Database 11g: SQL Tuning Workshop A - 278


Practice 9-1: Using Hints (continued)
2) From your terminal session, connect as the IOT user in the SQL*Plus session. From
your SQL*Plus session, execute the set_session.sql script. This script sets a
number of parameters for the duration of this case. Ensure that you do not exit from
your SQL*Plus session until asked to do so.
[oracle@edrsr33p1-orcl Hints]$ sqlplus iot/iot
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 15:18:22 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
n s e
ce
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
el i
a b l
SQL> @set_session
fe r
a n s
Session altered.
o n -tr
SQL>
s an
) h a deฺ
r ฺ e s Gui
--------------------------------------------------------------

ฺ mi dent
ALTER SESSION SET OPTIMIZER_INDEX_CACHING=2;
p
d g Stu
a@ th1000
set timing on
set linesize 200 ipagesize
b is
i a (ja use
A
3) From your tosession, execute the following query and note down the time it
b SQL*Plus
n o
i to execute:
takes
t o
e An SELECT comm.

Jos
FROM iottab
WHERE object_id = 1
AND object_id_cat = 0
AND object_id_att = 426 ;
SQL> @select_iot
SQL>
SQL> SELECT comm
2 FROM iottab
3 WHERE object_id = 1
4 AND object_id_cat = 0
5 AND object_id_att = 426 ;

no rows selected

Elapsed: 00:00:00.07
SQL>

--------------------------------------------------------------

set echo on

Oracle Database 11g: SQL Tuning Workshop A - 279


Practice 9-1: Using Hints (continued)
SELECT comm
FROM iottab
WHERE object_id = 1
AND object_id_cat = 0
AND object_id_att = 426 ;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4) Use the DBMS_XPLAN package to display the execution plan associated with the
statement you executed in the previous step. What do you observe?
a) It is strange to see that the optimizer chooses a plan that accesses the secondary
index while the query references all columns of the primary key.
SQL> @show_latest_exec_plan
SQL> set echo on
n s e
SQL>
i ce
SQL> select * from
b l el
table(dbms_xplan.display_cursor(null,null,'TYPICAL'));
fe r a
PLAN_TABLE_OUTPUT
a n s
o n -tr
--------------------------------------------------------------------

an
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 6u4tsfpprgvzn, child number 0
h s
a deฺ
-------------------------------------
e )
s Gui
r ฺ
mi dent
SELECT comm FROM iottab WHERE object_id = 1 AND object_id_cat
= 0
p ฺ
g Stu
d
AND object_id_att = 426

a@ this
Plan hash value:bi2544181447

i a (ja use
b to
--------------------------------------------------------------------
A
i o
--------------------

nto|n Id | Operation | Name | Rows | Bytes |

e A Cost (%CPU)| Time |

J o s --------------------------------------------------------------------
--------------------
| 0 | SELECT STATEMENT | | | |
1 (100)| |
|* 1 | INDEX UNIQUE SCAN| IOTTAB_PK | 1 | 57 |
1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN| OBJECT_ID_ATT_INDX | 50 | |
1 (0)| 00:00:01 |
--------------------------------------------------------------------
--------------------

Predicate Information (identified by operation id):


---------------------------------------------------

1 - access("OBJECT_ID_ATT"=426)
filter(("OBJECT_ID"=1 AND "OBJECT_ID_CAT"=0))
2 - access("OBJECT_ID_ATT"=426)

Note
-----
- dynamic sampling used for this statement

Oracle Database 11g: SQL Tuning Workshop A - 280


Practice 9-1: Using Hints (continued)

26 rows selected.

Elapsed: 00:00:00.13
SQL>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

set echo on

select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL'));

5) Before trying to fix this issue, make sure you flush the important content of your SGA
by executing the flush_sga.sql script.
n s e
i ce
SQL> @flush_sga
b l el
SQL> set echo on
SQL>
fe r a
SQL> alter system flush shared_pool;
a n s
o n -tr
an
System altered.

Elapsed: 00:00:00.11
h s
a deฺ
SQL>
e )
s Gui
r ฺ
i ent
SQL> alter system flush buffer_cache;
p ฺ m
System altered.
d g Stud
b i
Elapsed: 00:00:00.43 a@ this
SQL>
i a (ja use
A b to
i o
--------------------------------------------------------------

nton
e A set echo on

J o s alter system flush shared_pool;

alter system flush buffer_cache;

6) Using hints only, how would you fix the issue raised at step 4? Implement your
solution and check if it works.
a) The idea is to use a hint to prevent the optimizer from using the secondary index.
You will not use the NO_INDEX hint.
SQL> @select_iot_hint
SQL> set echo on
SQL>
SQL> SELECT /*+ NO_INDEX(t OBJECT_ID_ATT_INDX) */ comm
2 FROM iottab t
3 WHERE object_id = 1
4 AND object_id_cat = 0
5 AND object_id_att = 426 ;

Oracle Database 11g: SQL Tuning Workshop A - 281


Practice 9-1: Using Hints (continued)
no rows selected

Elapsed: 00:00:00.03
SQL>
SQL> @show_latest_exec_plan
SQL> set echo on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 4zcxy7z1cg8da, child number 0
-------------------------------------
n s e
SELECT /*+ NO_INDEX(t OBJECT_ID_ATT_INDX) */ comm FROM iottab t
i ce
WHERE object_id = 1 AND object_id_cat = 0 AND object_id_att =
b l el
426
fer a
Plan hash value: 181430399
a n s
n - tr
o
an
--------------------------------------------------------------------
----------
| Id | Operation | Name
h s
a deฺ
| Rows | Bytes | Cost (%CPU)|
Time |
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------
----------
p ฺ
g Stu
|
d
0 | SELECT STATEMENT | | | | 1 (100)|
|
b i a@ this
|* 1 | INDEX RANGE SCAN| IOTTAB_PK | 1 | 57 | 1 (0)|
00:00:01 |
i a (ja use
b to
--------------------------------------------------------------------

n i oA
----------

ntoPredicate Information (identified by operation id):


e A
J o s ---------------------------------------------------

1 - access("OBJECT_ID"=1 AND "OBJECT_ID_ATT"=426 AND


"OBJECT_ID_CAT"=0)

Note
-----
- dynamic sampling used for this statement

24 rows selected.

Elapsed: 00:00:00.13
SQL>

--------------------------------------------------------------

set echo on

SELECT /*+ NO_INDEX(t OBJECT_ID_ATT_INDX) */ comm


FROM iottab t

Oracle Database 11g: SQL Tuning Workshop A - 282


Practice 9-1: Using Hints (continued)
WHERE object_id = 1
AND object_id_cat = 0
AND object_id_att = 426 ;

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

set echo on

select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL'));

7) Exit from your SQL*Plus session, and clean up your environment by executing the
iot_cleanup.sh script.
SQL> exit;
n s e
Disconnected from Oracle Database 11g Enterprise Edition Release
i ce
11.1.0.6.0 - Production
b l el
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options fe r a
[oracle@edrsr33p1-orcl Hints]$
a n s
[oracle@edrsr33p1-orcl Hints]$ ./iot_cleanup.sh
o n -tr
n
SQL*Plus: Release 11.1.0.6.0 - Production onaTue Apr 1 15:21:09 2008
s eฺ
a dreserved.
hrights
Copyright (c) 1982, 2007, Oracle. s ) i
ฺe nt Gu
All
i r
g p ฺm ude
Connected to:
Oracle Database 11g @
d S t
b ia hi s
Enterprise
t
Edition Release 11.1.0.6.0 -
Production
j a e
and Realb a ( to usTesting
With the Partitioning,
iApplication Oracle Label Security, OLAP, Data Mining
options
io A
t o n
SQL>
A n SQL> drop user iot cascade;

o s e
J User dropped.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Hints

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

Oracle Database 11g: SQL Tuning Workshop A - 283


Practice 9-1: Using Hints (continued)
export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @iot_cleanup.sql


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

--------------------------------------------------------------

set echo on

drop user iot cascade;

exit;

8) You study a second case of hint utilization to specify hints in lower query blocks.
n s e
From your terminal session connected as the oracle user, execute the
i ce
hr_hint_setup.sh script.
b l el
fe r a
[oracle@edrsr33p1-orcl Hints]$ ./hr_hint_setup.sh
a n s
n -tr
SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 16:08:51 2008
o
n
a reserved.
Copyright (c) 1982, 2007, Oracle. All rights
h s
a deฺ
e )
s Gui
r ฺ
Connected to:
Oracle Database 11g Enterprise
p ฺ miEdition
d e t
nRelease 11.1.0.6.0 -
Production d g Stu
b i
With the Partitioning,
a@Testing isoptions
Oracle
t h Label Security, OLAP, Data Mining

i a (ja use
and Real Application

SQL>
A b o
thr
n i
SQL> o alter user identified by hr account unlock;

nt o
e A User altered.

J o s SQL>
SQL> grant dba to hr
2
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Hints

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

Oracle Database 11g: SQL Tuning Workshop A - 284


Practice 9-1: Using Hints (continued)
export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @hr_hint_setup.sql


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

--------------------------------------------------------------

set echo on

alter user hr identified by hr account unlock;

grant dba to hr

exit;

n s e
i ce
9) From your terminal session, connect as the HR user in the SQL*Plus session. After
b l el
fe r a
you are connected, execute the create_hr_view1.sql script that creates a view

a n s
called V1 on top of the EMPLOYEES table. After this is done, execute the

o n -tr
create_hr_view2.sql script that creates a view V2 on top of V1.
[oracle@edrsr33p1-orcl Hints]$ sqlplus hr/hr
s an
h a on Tued ฺ 1 16:09:02 2008
eApr
SQL*Plus: Release 11.1.0.6.0 - Production ) i
i r ฺesAllnrights
t Gu reserved.
g p ฺm ude
Copyright (c) 1982, 2007, Oracle.

d S t
@ s
Connected to:
j a b ia
e thi Edition Release 11.1.0.6.0 -
a ( to us
Oracle Database 11g Enterprise
b
Productioni
i o A Partitioning,
With the Oracle Label Security, OLAP, Data Mining

ton
and Real Application Testing options

A n
o s e SQL> @create_hr_view1
J SQL>
SQL> CREATE OR REPLACE VIEW v1 AS
2 SELECT *
3 FROM employees
4 WHERE employee_id < 150;

View created.

SQL>
SQL> @create_hr_view2
SQL> set echo on
SQL>
SQL> CREATE OR REPLACE VIEW v2 AS
2 SELECT v1.employee_id employee_id, departments.department_id
department_id
3 FROM v1, departments
4 WHERE v1.department_id = departments.department_id;

View created.

Oracle Database 11g: SQL Tuning Workshop A - 285


Practice 9-1: Using Hints (continued)
SQL>

--------------------------------------------------------------

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

CREATE OR REPLACE VIEW v1 AS


SELECT *
FROM employees
WHERE employee_id < 150;

--------------------------------------------------------------

set echo on

CREATE OR REPLACE VIEW v2 AS


n s e
SELECT v1.employee_id employee_id, departments.department_id
i ce
department_id
b l el
FROM v1, departments
WHERE v1.department_id = departments.department_id;
fer a
a n s
n - tr
o
10) Determine the execution plan used to process the following query:
SELECT * FROM v2 WHERE department_id = 30; a n
h a s eฺ
SQL> set linesize 200 pagesize 1000
e s ) u id
SQL> @show_exec_plan_view2

ir ent G
SQL> set echo on
ฺ m
SQL>
SQL> explain plan for d
gp Stud
2 SELECT *
b i a@ this
3 FROM
a ja use = 30;
(department_id
v2
b
4 WHERE i to
i o A
ton
Explained.

A n
o s e SQL>
J SQL> select * from table(DBMS_XPLAN.DISPLAY(null,null,'ALL'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
Plan hash value: 389887213

--------------------------------------------------------------------
------------------------------
| Id | Operation | Name | Rows |
Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
------------------------------
| 0 | SELECT STATEMENT | | 3 |
33 | 1 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 3 |
33 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 |
4 | 0 (0)| 00:00:01 |

Oracle Database 11g: SQL Tuning Workshop A - 286


Practice 9-1: Using Hints (continued)
|* 3 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 3 |
21 | 1 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX | 6 |
| 0 (0)| 00:00:01 |
--------------------------------------------------------------------
------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Query Block Name / Object Alias (identified by operation id):


-------------------------------------------------------------

1 - SEL$5C160134
2 - SEL$5C160134 / DEPARTMENTS@SEL$2
3 - SEL$5C160134 / EMPLOYEES@SEL$3
4 - SEL$5C160134 / EMPLOYEES@SEL$3

Predicate Information (identified by operation id):


n s e
---------------------------------------------------
i ce
b l el
2 - access("DEPARTMENTS"."DEPARTMENT_ID"=30)
3 - filter("EMPLOYEE_ID"<150)
fer a
4 - access("DEPARTMENT_ID"=30)
a n s
o n -tr
an
Column Projection Information (identified by operation id):
-----------------------------------------------------------
h s
a deฺ
)
s Gui
1 - (#keys=0) "DEPARTMENTS"."DEPARTMENT_ID"[NUMBER,22],
e
"EMPLOYEE_ID"[NUMBER,22] r ฺ
i ent
ฺ m
2 - "DEPARTMENTS"."DEPARTMENT_ID"[NUMBER,22]
p d
d g
3 - "EMPLOYEE_ID"[NUMBER,22]
S t u
b i a@ this
4 - "EMPLOYEES".ROWID[ROWID,10]

i a (ja use
34 rows selected.

SQL> A
b to
ni o
nto--------------------------------------------------------------
e A
J o s set echo on

explain plan for


SELECT *
FROM v2
WHERE department_id = 30;

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

11) How do you force the query from step 10 to do a full table scan of the
departments table and a range scan of the emp_emp_id_pk index?
a) You have to use extended hint syntax to be able to specify hints that apply to
tables and indexes that appear in views. The NO_MERGE hint is used to make sure
that the view is not merged into the surrounding query blocks.
SQL> @show_exec_plan_view2_hints
SQL> set echo on
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 287


Practice 9-1: Using Hints (continued)
SQL> explain plan for
2 SELECT /*+ NO_MERGE(v2) INDEX(v2.v1.employees emp_emp_id_pk)
FULL(v2.departments) */ *
3 FROM v2
4 WHERE department_id = 30;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Explained.

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
Plan hash value: 1511767168
n s e
i ce
--------------------------------------------------------------------
b l el
---------------------------
| Id | Operation | Name | Rows |
fer a
Bytes | Cost (%CPU)| Time |
a n s
-tr
--------------------------------------------------------------------
n
o
an
---------------------------
| 0 | SELECT STATEMENT | | 3 |
78 | 5 (0)| 00:00:01 |
h s
a deฺ
| 1 | VIEW | V2
e )
s Gui | 3 |
r ฺ
mi dent
78 | 5 (0)| 00:00:01 |
| 2 | NESTED LOOPS
p ฺ
g Stu
| | |
| | |
d
|
57 |
3 |
5
NESTED LOOPS
b i a@ this
(0)| 00:00:01 |
| | 3 |

|* 4 |
i a (ja use
TABLE ACCESS FULL | DEPARTMENTS | 1 |
8 | 3
A b to
(0)| 00:00:01 |

o|n io
|* 5 |
1
INDEX RANGE SCAN
(0)| 00:00:01 |
| EMP_EMP_ID_PK | 50 |

nt
e A |* 6 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 3 |

J o s 33 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------
---------------------------

Query Block Name / Object Alias (identified by operation id):


-------------------------------------------------------------

1 - SEL$335DD26A / V2@SEL$1
2 - SEL$335DD26A
4 - SEL$335DD26A / DEPARTMENTS@SEL$2
5 - SEL$335DD26A / EMPLOYEES@SEL$3
6 - SEL$335DD26A / EMPLOYEES@SEL$3

Predicate Information (identified by operation id):


---------------------------------------------------

4 - filter("DEPARTMENTS"."DEPARTMENT_ID"=30)
5 - access("EMPLOYEE_ID"<150)
6 - filter("DEPARTMENT_ID"=30)

Column Projection Information (identified by operation id):

Oracle Database 11g: SQL Tuning Workshop A - 288


Practice 9-1: Using Hints (continued)
-----------------------------------------------------------

1 - "V2"."EMPLOYEE_ID"[NUMBER,22],
"V2"."DEPARTMENT_ID"[NUMBER,22]
2 - (#keys=0) "DEPARTMENTS"."DEPARTMENT_ID"[NUMBER,22],
"EMPLOYEE_ID"[NUMBER,22]
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 - (#keys=0) "DEPARTMENTS"."DEPARTMENT_ID"[NUMBER,22],
"EMPLOYEES".ROWID[ROWID,10], "EMPLOYEE_ID"[NUMBER,22]
4 - "DEPARTMENTS"."DEPARTMENT_ID"[NUMBER,22]
5 - "EMPLOYEES".ROWID[ROWID,10], "EMPLOYEE_ID"[NUMBER,22]

39 rows selected.

SQL>

--------------------------------------------------------------
n s e
i ce
set echo on
b l el
explain plan for
fer a
SELECT /*+ NO_MERGE(v2) INDEX(v2.v1.employees emp_emp_id_pk)
a n s
FULL(v2.departments) */ *
o n -tr
an
FROM v2
WHERE department_id = 30;
h s
a deฺ
)
s Gui
select * from table(DBMS_XPLAN.DISPLAY(null,null,'ALL'));
e
r ฺ
i ent
p ฺ m
g andSclean
12) Exit from your SQL*Plus session
d t udup your environment by executing the
hr_hint_cleanup.sh
b i a@script.
t h is
SQL> exit; (ja se Database 11g Enterprise Edition Release
i a to
b - Production
Disconnected from u
Oracle
o A
ni the Partitioning, Oracle Label Security, OLAP, Data Mining
11.1.0.6.0
t oWith

e An and Real Application Testing options

Jos
[oracle@edrsr33p1-orcl Hints]$ ./hr_hint_cleanup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 16:10:09 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> revoke dba from hr;
revoke dba from hr
*
ERROR at line 1:
ORA-01951: ROLE 'DBA' not granted to 'HR'

Oracle Database 11g: SQL Tuning Workshop A - 289


Practice 9-1: Using Hints (continued)
SQL>
SQL> drop view hr.v1;

View dropped.

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

SQL> drop view hr.v2;

View dropped.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
[oracle@edrsr33p1-orcl Hints]$
i ce
b l el
--------------------------------------------------------------
fe ra
#!/bin/bash
a n s
o n -tr
an
cd /home/oracle/solutions/Hints

export ORACLE_SID=orcl
h s
a deฺ
e )
s Gui
r ฺ
i ent
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

p ฺ m d
export
d g S t u
b i a@ this
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus b/ia
(ja u@hr_hint_cleanup.sql
se
A to
as sysdba

ni o
nto--------------------------------------------------------------
e A
J o s set echo on

revoke dba from hr;

drop view hr.v1;

drop view hr.v2;

exit;

13) In this third case, you investigate how to influence optimizer’s joins. From your
terminal session, execute the sh_hint_setup.sh script.
[oracle@edrsr33p1-orcl Hints]$ ./sh_hint_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 19:38:18 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop A - 290


Practice 9-1: Using Hints (continued)
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> alter user sh identified by sh acccount unlock;
alter user sh identified by sh acccount unlock
*
ERROR at line 1:
ORA-00922: missing or invalid option

SQL>
SQL> grant dba to sh;
n s e
i ce
Grant succeeded.
b l el
SQL>
fer a
SQL> connect sh/sh
a n s
Connected.
o n -tr
an
SQL>
SQL> exec dbms_stats.delete_schema_stats('SH');
h s
a deฺ
PL/SQL procedure successfully completed.
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m d
SQL> exec
d g S t u
b i a@ this
dbms_stats.set_table_stats('SH','SALES',null,null,null,10,5);

i a (ja successfully
PL/SQL procedure
u se completed.

SQL> A
b to
n o
i exit;
ntoSQL>
e A Disconnected from Oracle Database 11g Enterprise Edition Release

J o s 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$
[oracle@edrsr33p1-orcl Hints]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Hints

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @sh_hint_setup.sql

Oracle Database 11g: SQL Tuning Workshop A - 291


Practice 9-1: Using Hints (continued)
--------------------------------------------------------------

set echo on

alter user sh identified by sh acccount unlock;


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

grant dba to sh;

connect sh/sh

exec dbms_stats.delete_schema_stats('SH');

exec dbms_stats.set_table_stats('SH','SALES',null,null,null,10,5);

exit;
n s e
i ce
14) From your terminal session, connect as the SH user in the SQL*Plus session. After b l el
you are connected, ensure that you flush your SGA content using the f e ra
flush_sga.sql script, and set some important session parameters a s the
nusing
n t r
- from your
set_sh_session.sql script. Ensure that you do not disconnect
o
established SQL*Plus session.
s an
h a
sh/sh ide

)
rฺes nt GonuTue Apr 1 19:38:35 2008
[oracle@edrsr33p1-orcl Hints]$ sqlplus

SQL*Plus: Release 11.1.0.6.0 m - iProduction


d g pฺ tude
i a @ his S All rights reserved.
Copyright (c) 1982, 2007, Oracle.

( j ab se t
Connectedia
b to:
t o u
i o
Oracle A Database 11g Enterprise Edition Release 11.1.0.6.0 -
n the Partitioning, Oracle Label Security, OLAP, Data Mining
toWith
Production
n
A and Real Application Testing options
e
Jos SQL> @flush_sga
SQL>
SQL> alter system flush shared_pool;

System altered.

SQL>
SQL> alter system flush buffer_cache;

System altered.

SQL>
SQL> @set_sh_session
SQL>
SQL> set linesize 200
SQL>
SQL> set pagesize 200
SQL>
SQL> set timing on

Oracle Database 11g: SQL Tuning Workshop A - 292


Practice 9-1: Using Hints (continued)
SQL>
SQL> alter session set optimizer_dynamic_sampling=0;

Session altered.

Elapsed: 00:00:00.00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>

--------------------------------------------------------------

set echo on

alter system flush shared_pool;

alter system flush buffer_cache;

n s e
--------------------------------------------------------------
i ce
b l el
set linesize 200
fe r a
set pagesize 200
a n s
o n -tr
an
set timing on

alter session set optimizer_dynamic_sampling=0;


h s
a deฺ
e )
s Gui
r ฺ
15) From your SQL*Plus session, execute
p ฺ mithe following
d e nt query and determine its
execution plan:
select count(*) from@
dg S tu
b i a sales s,
t h is customers c where s.cust_id=c.cust_id;

a
SQL> set iecho
(jaon use
SQL> @exec_and_show_sh_exec_plan

SQL> A
b to
o
ni select count(*) from sales s, customers c where
t oSQL>

e An s.cust_id=c.cust_id;

Jos COUNT(*)
----------
918843

Elapsed: 00:00:03.59
SQL>
SQL> select * from
table(dbms_xplan.display_cursor(null,null,'TYPICAL'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------
--------------------------------------------------------------------
----------------------------------------------------------------
SQL_ID 88r79fy8nphrc, child number 0
-------------------------------------
select count(*) from sales s, customers c where s.cust_id=c.cust_id

Plan hash value: 2841872969

Oracle Database 11g: SQL Tuning Workshop A - 293


Practice 9-1: Using Hints (continued)
--------------------------------------------------------------------
----------------------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------
----------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 0 | SELECT STATEMENT | | | | 3
(100)| | | |
| 1 | SORT AGGREGATE | | 1 | 26 |
| | | |
| 2 | NESTED LOOPS | | 121K| 3081K| 3
(0)| 00:00:01 | | |
| 3 | PARTITION RANGE ALL| | 10 | 130 | 3
(0)| 00:00:01 | 1 | 28 |
| 4 | TABLE ACCESS FULL | SALES | 10 | 130 | 3
(0)| 00:00:01 | 1 | 28 |
n s e
|* 5 | INDEX UNIQUE SCAN | CUSTOMERS_PK | 12138 | 154K| 0
i ce
(0)| | | |
b l el
--------------------------------------------------------------------
----------------------------------
fe r a
a n s
Predicate Information (identified by operation id):
o n -tr
an
---------------------------------------------------

5 - access("S"."CUST_ID"="C"."CUST_ID")
h s
a deฺ
e )
s Gui
r ฺ
22 rows selected.
p ฺ mi dent
Elapsed: 00:00:00.15@d
g Stu
SQL> b i a t h is
i a (ja use
b to
--------------------------------------------------------------
A
o
ni echo on
ntoset
e A
J o s select count(*) from sales s, customers c where s.cust_id=c.cust_id;

select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL'));

16) Before you continue, ensure that you flush the content of your SGA to avoid caching
issues later. Use the flush_sga.sql script.
SQL> @flush_sga
SQL> set echo on
SQL>
SQL> alter system flush shared_pool;

System altered.

Elapsed: 00:00:00.12
SQL>
SQL> alter system flush buffer_cache;

System altered.

Oracle Database 11g: SQL Tuning Workshop A - 294


Practice 9-1: Using Hints (continued)
Elapsed: 00:00:00.18
SQL>

--------------------------------------------------------------

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

alter system flush shared_pool;

alter system flush buffer_cache;

17) Using hints only, how would you enhance the performance of the same query you
executed in step 15? Make sure you verify your implementation.
a) In step 15, the optimizer chose a NESTED LOOPS join. You can try to force a
n s e
hash join instead, using the LEADING and USE_HASH hints.
i ce
b l el
SQL> @exec_and_show_sh_hint_exec_plan
SQL> set echo on fe r a
SQL>
a n s
o n -tr
SQL> select /*+ LEADING(c s) USE_HASH(s) */ count(*) from sales s,
customers c where s.cust_id=c.cust_id;
a n
COUNT(*)
h s
a deฺ
----------
e )
s Gui
r ฺ
mi dent
918843
p ฺ
g Stu
Elapsed: 00:00:00.40
d
SQL>
b i a@ this
(ja use
SQL> select * from
table(dbms_xplan.display_cursor(null,null,'TYPICAL'));
b i a to
o A
ni
PLAN_TABLE_OUTPUT

nto--------------------------------------------------------------------
e A --------------------------------------------------------------------

J o s ----------------------------------------------------------------
SQL_ID 9k8wcn7ckb7c1, child number 0
-------------------------------------
select /*+ LEADING(c s) USE_HASH(s) */ count(*) from sales s,
customers
c where s.cust_id=c.cust_id

Plan hash value: 3568173901

--------------------------------------------------------------------
-------------------------------------------
| Id | Operation | Name | Rows | Bytes
|TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------
-------------------------------------------
| 0 | SELECT STATEMENT | | | |
| 182 (100)| | | |
| 1 | SORT AGGREGATE | | 1 | 26 |
| | | | |
|* 2 | HASH JOIN | | 121K| 3081K|
2968K| 182 (2)| 00:00:02 | | |

Oracle Database 11g: SQL Tuning Workshop A - 295


Practice 9-1: Using Hints (continued)
| 3 | INDEX FAST FULL SCAN| CUSTOMERS_PK | 121K| 1540K|
| 9 (0)| 00:00:01 | | |
| 4 | PARTITION RANGE ALL | | 10 | 130 |
| 3 (0)| 00:00:01 | 1 | 28 |
| 5 | TABLE ACCESS FULL | SALES | 10 | 130 |
| 3 (0)| 00:00:01 | 1 | 28 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------------
-------------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

2 - access("S"."CUST_ID"="C"."CUST_ID")

23 rows selected.
n s e
i ce
Elapsed: 00:00:00.13
b l el
SQL>
fer a
--------------------------------------------------------------
a n s
o n -tr
a nfrom sales s,
set echo on

h
select /*+ LEADING(c s) USE_HASH(s) */ count(*) s
a deฺ
customers c where s.cust_id=c.cust_id;
e )
s Gui
r ฺ
ฺ mi dent
select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL'));
p
d g Stu
i a@session
18) Exit from your SQL*Plus
b t h s clean up your environment by executing the
iand
a (ja usescript.
sh_hint_cleanup.sh
i
A b to
ni o
SQL> exit

n t oDisconnected from Oracle Database 11g Enterprise Edition Release

e A 11.1.0.6.0 - Production

Jos
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$
[oracle@edrsr33p1-orcl Hints]$ ./sh_hint_cleanup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 19:43:04 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> Connected.
SQL> SQL>
User altered.

SQL> SQL> revoke dba from sh

Oracle Database 11g: SQL Tuning Workshop A - 296


Practice 9-1: Using Hints (continued)
*
ERROR at line 1:
ORA-01951: ROLE 'DBA' not granted to 'SH'

SQL> SQL> SQL> Rem


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

SQL> Rem $Header: sh_main.sql 06-mar-2008.15:00:45 cbauwens Exp $


SQL> Rem
SQL> Rem sh_main.sql
SQL> Rem
...
SQL> Rem
SQL>
SQL> SET ECHO OFF

specify password for SH as parameter 1:


n s e
i ce
specify default tablespace for SH as parameter 2:
b l el
specify temporary tablespace for SH as parameter 3:
fer a
a n s
specify password for SYS as parameter 4:
o n -tr
n
specify directory path for the data files asaparameter 5:
a s e ฺ
h as idparameter
writeable directory path for the log )files
ฺ e s u 6:

7:i
specify version as parameterฺm
r
e n tG
d gp Stud
Session altered.bia
@ his
j a e t
(
ia to u s
b
i oA
User dropped.
n
ntoold 1: CREATE USER sh IDENTIFIED BY &pass
e A
J o s new 1: CREATE USER sh IDENTIFIED BY sh

User created.

old 1: ALTER USER sh DEFAULT TABLESPACE &tbs


new 1: ALTER USER sh DEFAULT TABLESPACE example
old 2: QUOTA UNLIMITED ON &tbs
new 2: QUOTA UNLIMITED ON example

User altered.

old 1: ALTER USER sh TEMPORARY TABLESPACE &ttbs


new 1: ALTER USER sh TEMPORARY TABLESPACE temp

User altered.

Grant succeeded.

...

Oracle Database 11g: SQL Tuning Workshop A - 297


Practice 9-1: Using Hints (continued)

Grant succeeded.

PL/SQL procedure successfully completed.


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

Connected.

Grant succeeded.

old 1: CREATE OR REPLACE DIRECTORY data_file_dir AS '&data_dir'


new 1: CREATE OR REPLACE DIRECTORY data_file_dir AS
'/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/'

Directory created.
n s e
i ce
old 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '&log_dir'
b l el
new 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '/home/oracle/'
fer a
Directory created.
a n s
n -tr
o
Grant succeeded.
s an
) h a deฺ
r ฺ e s Gui
mi dent
Grant succeeded.

p ฺ
g Stu
d
Grant succeeded.
b i a@ this
Connected. (ja se
i a
baltered.to u
i o A
Session

nton
e A
J o s Session altered.

Table created.

...

Comment created.

Creating OLAP metadata ...


<<<<< CREATE CWMLite Metadata for the Sales History Schema >>>>>
-

-
<<<<< FINAL PROCESSING >>>>>
- Changes have been committed

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 298


Practice 9-1: Using Hints (continued)

Commit complete.

gathering statistics ...


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

PL/SQL procedure successfully completed.

PL/SQL procedure successfully completed.

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
[oracle@edrsr33p1-orcl Hints]$
i ce
b l
19) With this fourth case, you study the influence of the INDEX and AND_EQUAL hints.el
fer a
From your terminal session, execute the sh_hint_index_setup.sh script to set
up the environment for this lab. a n s
o n -tr
an
[oracle@edrsr33p1-orcl Hints]$ ./sh_hint_index_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production


h s
a on Tue ฺ 1 20:16:22 2008
eApr
) i d
Copyright (c) 1982, 2007, Oracle.i r ฺesAllnrights
t Gu reserved.
g p ฺm ude
d S t
@ s
thi Edition Release 11.1.0.6.0 -
ia Enterprise
Connected to:
Oracle Database b
Production ( j a 11g
e
b a to us Oracle Label Security, OLAP, Data Mining
iPartitioning,
A Application Testing options
With the
andoReal
i
to n
A n SQL>

o s e SQL> alter user sh identified by sh acccount unlock;


J alter user sh identified by sh acccount unlock
*
ERROR at line 1:
ORA-00922: missing or invalid option

SQL>
SQL> grant dba to sh;

Grant succeeded.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Hints]$

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 299


Practice 9-1: Using Hints (continued)
#!/bin/bash

cd /home/oracle/solutions/Hints

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

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @sh_hint_index_setup.sql

--------------------------------------------------------------
n s e
ice
set echo on
b l el
alter user sh identified by sh acccount unlock;
fe ra
a n s
grant dba to sh;
o n -tr
exit;
s an
) h a deฺ
20) From your terminal session, connect to rtheฺ e s Gsession
SQL*Plus ui as the SH user. After
ฺ mi scripts
you are connected, execute the following
p d e ntot further set up your environment
for this lab. Ensure that you d g connected
stay S tu to your session throughout this case.
b i a@ Hints]$
t h is sqlplus sh/sh
(ja use
[oracle@edrsr33p1-orcl

SQL*Plus: b i a to11.1.0.6.0 - Production on Tue Apr 1 20:16:28 2008


Release
io A
t o n
Copyright (c) 1982, 2007, Oracle. All rights reserved.
A n
e
Jos Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> @drop_index_customers
SQL>
SQL> @dait
SQL>
SQL> drop index CUSTOMERS_YOB_BIX;

Index dropped.

SQL> drop index CUSTOMERS_MARITAL_BIX;

Index dropped.

SQL> drop index CUSTOMERS_GENDER_BIX;

Oracle Database 11g: SQL Tuning Workshop A - 300


Practice 9-1: Using Hints (continued)
Index dropped.

SQL>
SQL>
SQL> @create_cust_indexes
SQL> set echo on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL>
SQL> CREATE INDEX CUST_CUST_GENDER_idx
2 ON CUSTOMERS(CUST_GENDER)
3 NOLOGGING COMPUTE STATISTICS;

Index created.

SQL>
SQL> CREATE INDEX CUST_CUST_POSTAL_CODE_idx
2 ON CUSTOMERS(CUST_POSTAL_CODE)
n s e
3 NOLOGGING COMPUTE STATISTICS;
i ce
b l el
Index created.
fer a
SQL>
a n s
SQL> CREATE INDEX CUST_CUST_CREDIT_LIMIT_idx
o n -tr
an
2 ON CUSTOMERS(CUST_CREDIT_LIMIT)
3 NOLOGGING COMPUTE STATISTICS;
h s
a deฺ
Index created.
e )
s Gui
r ฺ
SQL>
p ฺ mi dent
d g Stu
b i a@ this
--------------------------------------------------------------

REM drop (
a ja indexes
all
u seindexes
on CUSTOMERS table
REM b i
does not
to
touch associated with constraints
REM A==================================================
o n io
nt set termout off
e A
J o s store
save
set sqlplus_settings replace
buffer.sql replace
set heading off verify off autotrace off feedback off

spool dait.sql

SELECT 'drop index '||i.index_name||';'


FROM user_indexes i
WHERE i.table_name = 'CUSTOMERS'
AND NOT EXISTS
(SELECT 'x'
FROM user_constraints c
WHERE c.index_name = i.index_name
AND c.table_name = i.table_name
AND c.status = 'ENABLED');

spool off

get buffer.sql nolist


@sqlplus_settings
set termout on

Oracle Database 11g: SQL Tuning Workshop A - 301


Practice 9-1: Using Hints (continued)
set echo on

@dait

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

set echo on

CREATE INDEX CUST_CUST_GENDER_idx


ON CUSTOMERS(CUST_GENDER)
NOLOGGING COMPUTE STATISTICS;

CREATE INDEX CUST_CUST_POSTAL_CODE_idx


ON CUSTOMERS(CUST_POSTAL_CODE)
NOLOGGING COMPUTE STATISTICS;
n s e
i ce
CREATE INDEX CUST_CUST_CREDIT_LIMIT_idx
b l el
ON CUSTOMERS(CUST_CREDIT_LIMIT)
NOLOGGING COMPUTE STATISTICS;
fer a
a n s
n - tr
o
21) Determine the execution plan for the following query:
SELECT
s an
c.*
) h a deฺ
FROM customers c
r ฺ e s Gui
WHERE cust_gender
p ฺ mi dent
= 'M'
AND g Stu
cust_postal_code = 40804
d
AND
a@ this
cust_credit_limit = 10000;
b i
a (ja 200
SQL> set autotrace
SQL> set ilinesize
sepagesize 1000
traceonly
u
A b to
o
SQL> @index
ni set echo on
n t oSQL>

e A SQL>

Jos
SQL> SELECT
2 c.*
3 FROM customers c
4 WHERE cust_gender = 'M'
5 AND cust_postal_code = 40804
6 AND cust_credit_limit = 10000
7 /

6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 2008213504

--------------------------------------------------------------------
-----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------
-----------

Oracle Database 11g: SQL Tuning Workshop A - 302


Practice 9-1: Using Hints (continued)
| 0 | SELECT STATEMENT | | 6 | 1080 | 448 (1)|
00:00:05 |
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 6 | 1080 | 448 (1)|
00:00:05 |
--------------------------------------------------------------------
-----------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Predicate Information (identified by operation id):


---------------------------------------------------

1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804 AND
"CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')

Statistics
----------------------------------------------------------
n s e
1 recursive calls
i ce
0 db block gets
b l el
1460 consistent gets
0 physical reads
fe ra
0 redo size
a n s
2570 bytes sent via SQL*Net to client
o n -tr
an
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
h s
a deฺ
0 sorts (disk)
e )
s Gui
6 rows processed r ฺ
i ent
p ฺ m
SQL>
d g Stud
b i a@ this
--------------------------------------------------------------

i a (ja use
set echo
A b on
to
n i o
ntoSELECT
e A c.*

J o s FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
AND cust_credit_limit = 10000
/

22) Try to get a better execution plan using the INDEX hint for the same query you
investigated in step 21. Which index is best suited?
a) The CUST_CUST_GENDER_IDX index is the best one for this query. Note that
using the INDEX hint without specifying any index leads the optimizer to use the
CUST_CUST_GENDER_IDX index.
SQL> @index_hint
SQL> set echo on
SQL>
SQL> SELECT /*+ INDEX (c &indexname) */
2 c.*
3 FROM customers c
4 WHERE cust_gender = 'M'

Oracle Database 11g: SQL Tuning Workshop A - 303


Practice 9-1: Using Hints (continued)
5 AND cust_postal_code = 40804
6 AND cust_credit_limit = 10000
7 /
Enter value for indexname: CUST_CUST_CREDIT_LIMIT_IDX
old 1: SELECT /*+ INDEX (c &indexname) */
new 1: SELECT /*+ INDEX (c CUST_CUST_CREDIT_LIMIT_IDX) */
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1407552528

--------------------------------------------------------------------
--------------------------------------
n s e
| Id | Operation | Name |
i ce
Rows | Bytes | Cost (%CPU)| Time |
b l el
--------------------------------------------------------------------
--------------------------------------
fer a
| 0 | SELECT STATEMENT |
a n s |
6 | 1080 | 1074 (1)| 00:00:11 |
o n -tr
an
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
6 | 1080 | 1074 (1)| 00:00:11 |
|* 2 | INDEX RANGE SCAN
h s
a deฺ
| CUST_CUST_CREDIT_LIMIT_IDX |
6938 | | 14 (0)| 00:00:01 |
e )
s Gui
r ฺ
mi dent
--------------------------------------------------------------------

p ฺ
--------------------------------------
g Stuby operation id):
d
Predicate Information
b i a@ this
(identified
---------------------------------------------------

i a (ja use
b to
1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804
A
AND

o n io2 - access("CUST_CREDIT_LIMIT"=10000)
"CUST_GENDER"='M')

nt
e A
J o s Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
1053 consistent gets
13 physical reads
0 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

SQL>
SQL> @index_hint
SQL> set echo on
SQL>
SQL> SELECT /*+ INDEX (c &indexname) */
2 c.*

Oracle Database 11g: SQL Tuning Workshop A - 304


Practice 9-1: Using Hints (continued)
3 FROM customers c
4 WHERE cust_gender = 'M'
5 AND cust_postal_code = 40804
6 AND cust_credit_limit = 10000
7 /
Enter value for indexname: CUST_CUST_GENDER_IDX
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

old 1: SELECT /*+ INDEX (c &indexname) */


new 1: SELECT /*+ INDEX (c CUST_CUST_GENDER_IDX) */

6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 3629874189

n s e
--------------------------------------------------------------------
i ce
--------------------------------
b l el
| Id | Operation
Bytes | Cost (%CPU)| Time |
| Name | Rows |
fer a
n s
--------------------------------------------------------------------
a
--------------------------------
o n -tr
an
| 0 | SELECT STATEMENT | | 6 |
1080 | 1164 (1)| 00:00:12 |
h s
a deฺ
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS | 6 |
1080 | 1164 (1)| 00:00:12 |
e )
s Gui
r ฺ
mi dent
|* 2 | INDEX RANGE SCAN | CUST_CUST_GENDER_IDX | 27750 |
| 51 (0)| 00:00:01 |
p ฺ
g Stu
d
--------------------------------------------------------------------

b i a@ this
--------------------------------

i a (ja use(identified by operation id):


Predicate Information
b to
---------------------------------------------------
A
o
ni 1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804 AND
nto"CUST_CREDIT_LIMIT"=10000)
e A
J o s 2 - access("CUST_GENDER"='M')

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
1399 consistent gets
68 physical reads
0 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

SQL>
SQL> @index_hint
SQL> set echo on
SQL>

Oracle Database 11g: SQL Tuning Workshop A - 305


Practice 9-1: Using Hints (continued)
SQL> SELECT /*+ INDEX (c &indexname) */
2 c.*
3 FROM customers c
4 WHERE cust_gender = 'M'
5 AND cust_postal_code = 40804
6 AND cust_credit_limit = 10000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7 /
Enter value for indexname: CUST_CUST_POSTAL_CODE_IDX
old 1: SELECT /*+ INDEX (c &indexname) */
new 1: SELECT /*+ INDEX (c CUST_CUST_POSTAL_CODE_IDX) */

6 rows selected.

Execution Plan
----------------------------------------------------------
n s e
Plan hash value: 1928091631
i ce
b l el
--------------------------------------------------------------------
-------------------------------------
fer a
| Id | Operation | Name
a n s |
Rows | Bytes | Cost (%CPU)| Time |
n - tr
o
an
--------------------------------------------------------------------
-------------------------------------
| 0 | SELECT STATEMENT |
h s
a deฺ |
6 | 1080 | 218 (1)| 00:00:03 |
e )
s Gui
r ฺ
mi dent
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
6 | 1080 | 218 ฺ
(1)| 00:00:03 |
p
g Stu
|* 2 |
d
INDEX FULL SCAN | CUST_CUST_POSTAL_CODE_IDX |
89 | | 134
b i a@ this
(1)| 00:00:02 |
--------------------------------------------------------------------

i a (ja use
-------------------------------------

Ab to
o n io
Predicate Information (identified by operation id):
---------------------------------------------------
nt
e A
J o s 1 - filter("CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')
2 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
251 consistent gets
132 physical reads
0 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

SQL>
SQL> @index_hint
SQL> set echo on

Oracle Database 11g: SQL Tuning Workshop A - 306


Practice 9-1: Using Hints (continued)
SQL>
SQL> SELECT /*+ INDEX (c &indexname) */
2 c.*
3 FROM customers c
4 WHERE cust_gender = 'M'
5 AND cust_postal_code = 40804
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

6 AND cust_credit_limit = 10000


7 /
Enter value for indexname:
old 1: SELECT /*+ INDEX (c &indexname) */
new 1: SELECT /*+ INDEX (c ) */

6 rows selected.

Execution Plan
n s e
----------------------------------------------------------
i ce
Plan hash value: 1928091631
b l el
fer
-------------------------------------------------------------------- a
-------------------------------------
a n s
| Id | Operation | Name
n - tr |
o
an
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-------------------------------------
h s
a deฺ
| 0 | SELECT STATEMENT |
e )
s Gui |
r ฺ
mi dent
6 | 1080 | 218 (1)| 00:00:03 |

p ฺ
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS
g Stu
|
6 | 1080 | 218
d
(1)| 00:00:03 |
|* 2 |
89 | | 134b a@ this
INDEX FULL SCAN
i(1)| 00:00:02 |
| CUST_CUST_POSTAL_CODE_IDX |

i a (ja use
--------------------------------------------------------------------
b to
-------------------------------------
A
o n io
Predicate Information (identified by operation id):
nt
e A ---------------------------------------------------

J o s 1 - filter("CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')


2 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
251 consistent gets
0 physical reads
0 redo size
2570 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 307


Practice 9-1: Using Hints (continued)
--------------------------------------------------------------

set echo on

SELECT /*+ INDEX (c &indexname) */


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

FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
AND cust_credit_limit = 10000
/

23) Investigate the same query, but this time using the AND_EQUAL hint. Can you find a
better execution plan with this hint?
n s e
a) Using the AND_EQUAL hint allows the optimizer to explicitly choose an
i ce
execution plan that uses an access path that merges the scans on several single-
b l el
column indexes. However, no combination is better than the
fe r a
CUST_CUST_GENDER_IDX index.
a n s
n - tr
SQL> @and_equal_hint
o
SQL> set echo on
s an
SQL>
) h a deฺ
s Gui
SQL> SELECT /*+ AND_EQUAL (c &index_name1, &index_name2) */
2 c.*
r ฺ e
3 FROM customers c
p ฺ mi dent
4 WHERE cust_gender
5 AND d g Stu
= 'M'
cust_postal_code = 40804
6 AND i a@ this
cust_credit_limit = 10000
b
7 /
i a (ja use
Enter value for index_name1: CUST_CUST_CREDIT_LIMIT_IDX
Ab to
Enter value for index_name2: CUST_CUST_GENDER_IDX

o n
oldio 1: SELECT /*+ AND_EQUAL (c &index_name1, &index_name2) */
nt new 1: SELECT /*+ AND_EQUAL (c CUST_CUST_CREDIT_LIMIT_IDX,
e A CUST_CUST_GENDER_IDX) */

J o s
6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1121089724

--------------------------------------------------------------------
--------------------------------------
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
--------------------------------------
| 0 | SELECT STATEMENT | |
6 | 1080 | 1416 (1)| 00:00:15 |
|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |
6 | 1080 | 1416 (1)| 00:00:15 |
| 2 | AND-EQUAL | |
| | | |

Oracle Database 11g: SQL Tuning Workshop A - 308


Practice 9-1: Using Hints (continued)
|* 3 | INDEX RANGE SCAN | CUST_CUST_CREDIT_LIMIT_IDX |
6938 | | 14 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | CUST_CUST_GENDER_IDX |
27750 | | 51 (0)| 00:00:01 |
--------------------------------------------------------------------
--------------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Predicate Information (identified by operation id):


---------------------------------------------------

1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804 AND
"CUST_CREDIT_LIMIT"=10000 AND
"CUST_GENDER"='M')
3 - access("CUST_CREDIT_LIMIT"=10000)
4 - access("CUST_GENDER"='M')

n s e
i ce
Statistics
b l el
----------------------------------------------------------
1 recursive calls
fer a
0 db block gets
a n s
9160 consistent gets
o n -tr
an
0 physical reads
0 redo size
h
2570 bytes sent via SQL*Net to clients
a deฺ
)
s Gui
420 bytes received via SQL*Net from client
e
r ฺ
mi dent
2 SQL*Net roundtrips to/from client
0 sorts (memory)
p ฺ
g Stu
d
0 sorts (disk)

b i a@ this
6 rows processed

SQL>
i a (ja use
b echo onto
SQL> @and_equal_hint
A
i o
SQL> set
n
t oSQL>
n SQL> SELECT /*+ AND_EQUAL (c &index_name1, &index_name2) */
e A
Jos
2 c.*
3 FROM customers c
4 WHERE cust_gender = 'M'
5 AND cust_postal_code = 40804
6 AND cust_credit_limit = 10000
7 /
Enter value for index_name1: CUST_CUST_CREDIT_LIMIT_IDX
Enter value for index_name2: CUST_CUST_POSTAL_CODE_IDX
old 1: SELECT /*+ AND_EQUAL (c &index_name1, &index_name2) */
new 1: SELECT /*+ AND_EQUAL (c CUST_CUST_CREDIT_LIMIT_IDX,
CUST_CUST_POSTAL_CODE_IDX) */

6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1928091631

--------------------------------------------------------------------
-------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 309


Practice 9-1: Using Hints (continued)
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------
-------------------------------------
| 0 | SELECT STATEMENT | |
6 | 1080 | 218 (1)| 00:00:03 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS |


6 | 1080 | 218 (1)| 00:00:03 |
|* 2 | INDEX FULL SCAN | CUST_CUST_POSTAL_CODE_IDX |
89 | | 134 (1)| 00:00:02 |
--------------------------------------------------------------------
-------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

n s e
1 - filter("CUST_CREDIT_LIMIT"=10000 AND "CUST_GENDER"='M')
i ce
2 - filter(TO_NUMBER("CUST_POSTAL_CODE")=40804)
b l el
fer a
Statistics
a n s
-
----------------------------------------------------------
n tr
o
an
1 recursive calls
0 db block gets
251 consistent gets
h s
a deฺ
0 physical reads
e )
s Gui
r ฺ
mi dent
0 redo size

p ฺ
2570 bytes sent via SQL*Net to client
g Stu
d
420 bytes received via SQL*Net from client

b i a@ this
2 SQL*Net roundtrips to/from client
0 sorts (memory)

i a (ja use
0 sorts (disk)

Ab to
6 rows processed

o n io
SQL>
nt
e A
J o s --------------------------------------------------------------

set echo on

SELECT /*+ AND_EQUAL (c &index_name1, &index_name2) */


c.*
FROM customers c
WHERE cust_gender = 'M'
AND cust_postal_code = 40804
AND cust_credit_limit = 10000
/

24) Exit from your SQL*Plus session and clean up your environment by executing the
sh_hint_cleanup.sh script.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

Oracle Database 11g: SQL Tuning Workshop A - 310


Practice 9-1: Using Hints (continued)
[oracle@edrsr33p1-orcl Hints]$
[oracle@edrsr33p1-orcl Hints]$ ./sh_hint_cleanup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Apr 1 19:43:04 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


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

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> Connected.
SQL> SQL>
n s e
User altered.
i ce
b l el
SQL> SQL> revoke dba from sh
*
fer a
ERROR at line 1:
a n s
ORA-01951: ROLE 'DBA' not granted to 'SH'
o n -tr
s an
SQL> SQL> SQL> Rem
) h a deฺ cbauwens Exp $
r ฺ e s Gui
SQL> Rem $Header: sh_main.sql 06-mar-2008.15:00:45
SQL> Rem
SQL> Rem sh_main.sql ฺ m i ent
SQL> Rem
d g p t u d
...
a@ this S
SQL> Rem b i
SQL>
i a (jaOFF use
SQL> SET
A b ECHO
to
ni o
ntospecify password for SH as parameter 1:

e A
J o s specify default tablespace for SH as parameter 2:

specify temporary tablespace for SH as parameter 3:

specify password for SYS as parameter 4:

specify directory path for the data files as parameter 5:

writeable directory path for the log files as parameter 6:

specify version as parameter 7:

Session altered.

User dropped.

old 1: CREATE USER sh IDENTIFIED BY &pass


new 1: CREATE USER sh IDENTIFIED BY sh

Oracle Database 11g: SQL Tuning Workshop A - 311


Practice 9-1: Using Hints (continued)
User created.

old 1: ALTER USER sh DEFAULT TABLESPACE &tbs


new 1: ALTER USER sh DEFAULT TABLESPACE example
old 2: QUOTA UNLIMITED ON &tbs
new 2: QUOTA UNLIMITED ON example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

User altered.

old 1: ALTER USER sh TEMPORARY TABLESPACE &ttbs


new 1: ALTER USER sh TEMPORARY TABLESPACE temp

User altered.

Grant succeeded.
n s e
i ce
b l el
...
fer a
a n s
Grant succeeded.
o n -tr
PL/SQL procedure successfully completed.as
an
) h i d eฺ
Connected.
i r ฺes nt Gu
g p ฺm ude
Grant succeeded.
d S t
old 1: CREATE b OR
@ s
iaREPLACEthiDIRECTORY data_file_dir AS '&data_dir'
new (
1: CREATEj a OR se DIRECTORY data_file_dir AS
REPLACE
b i a to u
'/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/'
o A
ni
toDirectory created.
A n
o s e old 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '&log_dir'
J new 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '/home/oracle/'

Directory created.

Grant succeeded.

Grant succeeded.

Grant succeeded.

Connected.

Session altered.

Session altered.

Oracle Database 11g: SQL Tuning Workshop A - 312


Practice 9-1: Using Hints (continued)
Table created.

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

Comment created.

Creating OLAP metadata ...


<<<<< CREATE CWMLite Metadata for the Sales History Schema >>>>>

<<<<< FINAL PROCESSING >>>>>
- Changes have been committed

n s e
PL/SQL procedure successfully completed.
i ce
b l el
Commit complete.
fer a
a n s
o n -tr
an
gathering statistics ...

PL/SQL procedure successfully completed.as


) h i d eฺ
i r ฺ es t Gu
PL/SQL procedure successfully
g p ฺmcompleted.
u d en
dfrom Oracle
S t Database 11g Enterprise Edition
@ s
thi Label Security, OLAP, Data Mining
SQL> SQL> Disconnected
Release 11.1.0.6.0
a b ia- Production
j e
b
and Real a ( to usTesting options
With the Partitioning,
iApplication
Oracle

i oA
[oracle@edrsr33p1-orcl
n
Hints]$

nto--------------------------------------------------------------
e A
J o s #!/bin/bash

cd /home/oracle/solutions/SQL_Access_Advisor/sh

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

cp * $ORACLE_HOME/demo/schema/sales_history

sqlplus / as sysdba <<FIN!

SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 8000

Oracle Database 11g: SQL Tuning Workshop A - 313


Practice 9-1: Using Hints (continued)
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
SET LONG 1000

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

alter user sh identified by sh account unlock;

revoke dba from sh;

@sh_main sh example temp oracle


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/
/home/oracle/ v3

exit;
n s e
i ce
FIN!
b l el
ra
25) In the fifth case, you retrieve the first rows of your query as fast as possible. Connect
fe
n s
to the SQL*Plus session as the SYS user and ensure that you set the following
a
SQL*Plus environment variables:
o n -tr
timing on
s an
autotrace on
) h a deฺ
pagesize 200
r ฺ e s Gui
linesize 200
ฺ m i ent
d p
[oracle@edrsr33p1-orclgHints]$ d
tusqlplus / as sysdba
a@ S
is - Production on Tue Apr 29
b i t h
(ja use
SQL*Plus: Release 11.1.0.6.0
i
21:02:19 2008
b a to
i o A
t o n
Copyright (c) 1982, 2007, Oracle. All rights reserved.

A n
o s e
J Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data
Mining
and Real Application Testing options

SQL> set timing on autotrace on pagesize 200 linesize 200


SQL>

26) From the same SQL*Plus session, execute the following query. Based on the fact that
you want to retrieve the first 10 rows as fast as possible, what do you observe?
SELECT employee_id, department_name
FROM hr.employees e, hr.departments d
WHERE e.department_id = d.department_id;
a) It takes a bit of time before the result starts to be printed on the screen. This is
because a merge join is used. It needs to sort all data before producing rows.

Oracle Database 11g: SQL Tuning Workshop A - 314


Practice 9-1: Using Hints (continued)
SQL> SELECT employee_id, department_name
FROM hr.employees e, hr.departments d
WHERE e.department_id = d.department_id;
2 3
EMPLOYEE_ID DEPARTMENT_NAME
----------- ------------------------------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

200 Administration
201 Marketing
202 Marketing
114 Purchasing
115 Purchasing
116 Purchasing
117 Purchasing

113 Finance n s e
i ce
el
205 Accounting
206 Accounting
a b l
fer
106 rows selected.
a n s
o n -tr
an
Elapsed: 00:00:00.09

Execution Plan h s
a deฺ
e )
s Gui
Plan hash value: 1343509718 i r ฺ
----------------------------------------------------------

p ฺ m dent
d g Stu
a@ this
--------------------------------------------------------------
b i
------------------------------
a
a (j (%CPU)|
| Id | Operation
Bytes |biCost u seTime | Name | Rows |
A to |

ni o
--------------------------------------------------------------
nto------------------------------
e A | 0 | SELECT STATEMENT | | 106 |

J o s 2438 | 6 (17)| 00:00:01 |


| 1 | MERGE JOIN | | 106 |
2438 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 |
432 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | DEPT_ID_PK | 27 |
| 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 107 |
749 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMPLOYEES | 107 |
749 | 3 (0)| 00:00:01 |
--------------------------------------------------------------
------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

Oracle Database 11g: SQL Tuning Workshop A - 315


Practice 9-1: Using Hints (continued)

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

19 consistent gets
0 physical reads
0 redo size
2435 bytes sent via SQL*Net to client
497 bytes received via SQL*Net from client
9 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
106 rows processed n s e
i ce
SQL>
b l el
fe r a
a n s
27) Using a hint, how can you ensure that the previous query starts fetching rows faster?
Test your solution.
o n -tr
a) Using the FIRST_ROWS(10) hint, a nested loop is chosen
a n by the optimizer. It is
faster to retrieve the first rows.
h a s eฺ
SQL> SELECT /*+ FIRST_ROWS(10) e*/ s u id department_name
)employee_id,

ir entd G
FROM hr.employees e, hr.departments
=p ฺ m
WHERE e.department_id g
d tud
d.department_id;
S
2 3
b i a@ this
EMPLOYEE_ID DEPARTMENT_NAME
a se
-----------(j------------------------------
i a to u
b200 Administration
i o A 201 Marketing
nton 202 Marketing
e A 114 Purchasing
J o s 115 Purchasing
116 Purchasing

112 Finance
113 Finance
205 Accounting
206 Accounting

106 rows selected.

Elapsed: 00:00:00.02

Execution Plan
----------------------------------------------------------
Plan hash value: 1021246405

--------------------------------------------------------------
------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 316


Practice 9-1: Using Hints (continued)
| Id | Operation | Name |
Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------
------------------------------------
| 0 | SELECT STATEMENT | |
10 | 230 | 3 (0)| 00:00:01 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

| 1 | NESTED LOOPS | |
| | | |
| 2 | NESTED LOOPS | |
10 | 230 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL | DEPARTMENTS |
25 | 400 | 2 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX |
8 | | 0 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | n s e
i ce
el
3 | 21 | 1 (0)| 00:00:01 |
--------------------------------------------------------------
a b l
------------------------------------
fer
a n s
Predicate Information (identified by operation id):
o n -tr
an
---------------------------------------------------

h s
a deฺ
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
e )
s Gui
r ฺ
p ฺ mi dent
Statistics
d g Stu
----------------------------------------------------------
b i a@ th
0 recursive is
calls
0(ja
db block se gets
i a to
b 0 physical
40 u
consistent gets
io A reads
t o n 0 redo size
A n 2435 bytes sent via SQL*Net to client
o s e 497 bytes received via SQL*Net from client
J 9 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
106 rows processed

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 317


Practices for Lesson 10
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 318


Practice 10-1: Tracing Applications
In this practice, you define a service and use it to generate traces. You then interpret
generated trace files. You can find all needed script files for this lab in your
$HOME/solutions/Application_Tracing directory.
1) Initialize your environment be executing the at_setup.sh script from a terminal
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

session connected as the oracle user.


[oracle@edrsr33p1-orcl Application_Tracing]$ ./at_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:25:55 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

n s e
Connected to:
i ce
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
b l el
Production
fe r a
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
a n s
o n -tr
an
SQL>
SQL> drop user trace cascade;
drop user trace cascade
h s
a deฺ
*
e )
s Gui
r ฺ
mi dent
ERROR at line 1:
p ฺ
ORA-01918: user 'TRACE' does not exist
g Stu
d
b i a@ this
SQL>
SQL> create (
a ja trace
user
u s eidentified by trace default tablespace users
i to temp;
b tablespace
temporary
o A
ni created.
toUser
A n
o s e SQL>
J SQL> grant connect, resource, dba to trace;

Grant succeeded.

SQL>
SQL>
SQL> drop tablespace tracetbs including contents and datafiles;
drop tablespace tracetbs including contents and datafiles
*
ERROR at line 1:
ORA-00959: tablespace 'TRACETBS' does not exist

SQL>
SQL> drop tablespace tracetbs3 including contents and datafiles;
drop tablespace tracetbs3 including contents and datafiles
*
ERROR at line 1:
ORA-00959: tablespace 'TRACETBS3' does not exist

Oracle Database 11g: SQL Tuning Workshop A - 319


Practice 10-1: Tracing Applications (continued)
SQL>
SQL> create tablespace tracetbs
2 datafile '/u01/app/oracle/oradata/orcl/tracetbs.dbf' size 100m
3 extent management local uniform size 40k;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Tablespace created.

SQL>
SQL> create tablespace tracetbs3
2 datafile '/u01/app/oracle/oradata/orcl/tracetbs3.dbf' size 100m
3 extent management local uniform size 10m;

Tablespace created.

SQL>
n s e
SQL>
i ce
SQL> connect trace/trace
b l el
Connected.
SQL>
fer a
SQL> drop table sales purge;
a n s
drop table sales purge
n -tr
o
an
*
ERROR at line 1:
ORA-00942: table or view does not exist
h s
a deฺ
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m
d g select
SQL> create table sales as
S tu*dfrom sh.sales;
Table created. bia
@ his
j a e t
(
ia to u s
SQL>
SQL> A
b
o n io drop table sales2 purge;
SQL>
Ant drop table sales2 purge
e
Jos
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> create table sales2 tablespace tracetbs as select * from
sh.sales where 1=2;

Table created.

SQL>
SQL> drop table sales3 purge;
drop table sales3 purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 320


Practice 10-1: Tracing Applications (continued)
SQL> create table sales3 tablespace tracetbs3 as select * from
sh.sales where 1=2;

Table created.

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

SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash
n s e
i ce
cd /home/oracle/solutions/Application_Tracing
b l el
export ORACLE_SID=orcl
fer a
a n s
o n -tr
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
s an
h a deฺ
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
)
r ฺ e s Gui
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

ฺ mi dent
sqlplus / as sysdba @at_setup.sql
p
d g Stu
b i a@ this
--------------------------------------------------------------

set echo on (ja se


i a o u
b trace tcascade;
o A
ni user trace identified by trace default tablespace users
drop user

ntocreate
e A
J o s temporary tablespace temp;

grant connect, resource, dba to trace;

drop tablespace tracetbs including contents and datafiles;

drop tablespace tracetbs3 including contents and datafiles;

create tablespace tracetbs


datafile '/u01/app/oracle/oradata/orcl/tracetbs.dbf' size 100m
extent management local uniform size 40k;

create tablespace tracetbs3


datafile '/u01/app/oracle/oradata/orcl/tracetbs3.dbf' size 100m
extent management local uniform size 10m;

connect trace/trace

drop table sales purge;

Oracle Database 11g: SQL Tuning Workshop A - 321


Practice 10-1: Tracing Applications (continued)
create table sales as select * from sh.sales;

drop table sales2 purge;


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

create table sales2 tablespace tracetbs as select * from sh.sales


where 1=2;

drop table sales3 purge;

create table sales3 tablespace tracetbs3 as select * from sh.sales


where 1=2;

exit;

n s e
ice
is available from the tnsnames.ora file you use to connect to your database. b l el
2) Before you can use a service in your applications, you have to make sure this service

fe r a
a n s
Modify this file to make sure it references a service called TRACESERV. You can use
the add_traceserv_tns.sh script to help you in this task.
TRACESERV = o n -tr
(DESCRIPTION =
s an
) h a deฺ
(ADDRESS = (PROTOCOL = TCP)(HOST = node)(PORT = 1521))
(CONNECT_DATA =
r ฺ e s Gui
mi dent
(SERVER = DEDICATED)
(SERVICE_NAME = TRACESERV)
p ฺ
g Stu
)
d
)
b i a@ this
( j a
[oracle@edrsr33p1-orcl
s eApplication_Tracing]$ ./add_traceserv_tns.sh
bia to
[oracle@edrsr33p1-orcl u Application_Tracing]$

io A
t o n
--------------------------------------------------------------

A n
o s e #!/bin/ksh
J y=`hostname`

DBNAME=orcl

sed 's/NODE/'$y'/'
/home/oracle/solutions/Application_Tracing/wrong_tnstraceserv.ora >
/home/oracle/solutions/Application_Tracing/tnstraceserv.ora

cp /u01/app/oracle/product/11.1.0/db_1/network/admin/tnsnames.ora
/u01/app/oracle/product/11.1.0/db_1/network/admin/tnsnames.ora.bak1
cat /home/oracle/solutions/Application_Tracing/tnstraceserv.ora >>
/u01/app/oracle/product/11.1.0/db_1/network/admin/tnsnames.ora

--------------------------------------------------------------

TRACESERV =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = NODE)(PORT = 1521))
(CONNECT_DATA =

Oracle Database 11g: SQL Tuning Workshop A - 322


Practice 10-1: Tracing Applications (continued)
(SERVER = DEDICATED)
(SERVICE_NAME = TRACESERV)
)
)

3) You now need to declare the TRACESERV service in your database. So connect to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

your database instance as the SYS user using a SQL*Plus session.


[oracle@edrsr33p1-orcl Application_Tracing]$ sqlplus / as sysdba

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:26:15 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

n s e
Connected to:
i ce
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
b l el
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining fe r a
and Real Application Testing options
a n s
o n -tr
SQL>
a n
s eฺ with the same
a TRACESERV
4) In your SQL*Plus session, create a new service called
h
) i d
network name.
i r ฺes nt Gu
SQL> @add_traceserv_db
g p ฺm ude
SQL>
d S t
@ s
thi
SQL> select name from dba_services;
a b ia
j e
ia ( to us
NAME
b
----------------------------------------------------------------
i oA
SYS$BACKGROUND
n
toSYS$USERS

e An seeddataXDB
Jos
seeddata
orclXDB
orcl

6 rows selected.

SQL>
SQL> exec DBMS_SERVICE.CREATE_SERVICE('TRACESERV','TRACESERV');

PL/SQL procedure successfully completed.

SQL>
SQL> select name from dba_services;

NAME
----------------------------------------------------------------
SYS$BACKGROUND
SYS$USERS
seeddataXDB
seeddata
orclXDB

Oracle Database 11g: SQL Tuning Workshop A - 323


Practice 10-1: Tracing Applications (continued)
orcl
TRACESERV

7 rows selected.

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

SQL>

--------------------------------------------------------------

set echo on

select name from dba_services;

exec DBMS_SERVICE.CREATE_SERVICE('TRACESERV','TRACESERV');

n s e
select name from dba_services;
i ce
b l el
5) From the same SQL*Plus session, start the TRACESERV service. fer a
a n s
SQL> @start_traceserv o n -tr
SQL> set echo on
s an
SQL>
) h a deฺ
SQL> show parameter service_names
r ฺ e s Gui
NAME
p ฺ mi deTYPE nt VALUE
d g
------------------------------------
S t u ----------- -------------------

a@ this
-----------
service_names
a b i string
SQL> ( j u s e
SQL> execia o
A b t
DBMS_SERVICE.START_SERVICE('TRACESERV');

i o
nton
PL/SQL procedure successfully completed.

e A
J o s SQL>
SQL> show parameter service_names

NAME TYPE VALUE


------------------------------------ ----------- -------------------
-----------
service_names string TRACESERV
SQL>
SQL>

--------------------------------------------------------------

set echo on

show parameter service_names

exec DBMS_SERVICE.START_SERVICE('TRACESERV');

show parameter service_names

Oracle Database 11g: SQL Tuning Workshop A - 324


Practice 10-1: Tracing Applications (continued)
6) Exit from your SQL*Plus session.
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

and Real Application Testing options


[oracle@edrsr33p1-orcl Application_Tracing]$

7) At this point, open a browser window and connect to Enterprise Manager Database
Control as the SYS user. Ensure that you navigate to the Top Services page.
a) Log in to Enterprise Manager as the SYS user.
b) On the Database Home page, click the Performance tab.
c) On the Performance page, click the Top Consumers link in the Additional n s e
Monitoring links section of the page. li ce
d) On the Top Consumers page, click the Top Services tab. This takes you tora b
the
l e
f e
Top Services page.
t r a ns
8) You now execute seven workload scripts that are traced. All workload
o n - scripts run
under the TRACESERV service. Your goal is to analyze thengenerated trace files to
interpret what happens in the seven cases. From your s a
terminalฺsession, execute the
) a degeneration of statistics
htrigger ithe
run_tracep0.sh script. This script is used s to
ฺeviewed u
G within Enterprise Manager.
for your TRACESERV service so it canirbe n tfrom
ฺ m e
p of theturun_tracep0.sh
As soon as you start the execution d script, move to the
d g S
next step of this lab.
b i a@ this
i a (ja useApplication_Tracing]$ ./run_tracep0.sh
[oracle@edrsr33p1-orcl

A
SQL*Plus: b Releaseto11.1.0.6.0 - Production on Fri Apr 4 20:28:40 2008
ni o
t o
e An Copyright (c) 1982, 2007, Oracle. All rights reserved.

Jos Connected to:


Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceP0';

Session altered.

SQL>
SQL> set termout off
SQL>
SQL> exit;

Oracle Database 11g: SQL Tuning Workshop A - 325


Practice 10-1: Tracing Applications (continued)
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

n s e
export
i ce
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
b l el
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
fer a
sqlplus / as sysdba @run_tracep0.sql
a n s
n -tr
o
an
--------------------------------------------------------------

set echo on
h s
a deฺ
e )
s Gui
connect trace/trace@TRACESERV
r ฺ
p ฺ mi dent
alter session set tracefile_identifier='mytraceP0';
d g Stu
set termout off
b i a@ this
(ja from
select count(*)
i a u e
sdba_objects;
b
select count(*)
A o
count(*) tfrom dba_objects;
from dba_objects;
o
i count(*) from dba_objects;
select
n
ntoselect
e A select count(*) from dba_objects;

J o s select
select
count(*)
count(*)
from
from
dba_objects;
dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;
select count(*) from dba_objects;

exec dbms_lock.sleep(60);

set termout on

exit;

Oracle Database 11g: SQL Tuning Workshop A - 326


Practice 10-1: Tracing Applications (continued)
9) Go back to the Top Consumers page in your Enterprise Manager session. Wait until
you see the TRACESERV service in the Active Services table, and enable tracing for
that service.
a) When you see TRACESERV in the Active Services table on the Top Services
page, select it, and click the Enable SQL Trace button.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

b) On the Enable SQL Trace page, make sure Waits is set to TRUE, and Binds is set
to FALSE. Click OK.
c) Back to the Top Services page, you should see a confirmation message near the
top of the Top Consumers page.
10) When tracing for TRACESERV is enabled, execute the run_tracep1.sh script
from your terminal session. Observe your screen.
n s e
ice
el
[oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep1.sh
b l
SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:29:48
f e ra2008
Copyright (c) 1982, 2007, Oracle. All rights reserved.
t r a ns
o n -
s an
eฺ
a d11.1.0.6.0
Connected to:
Oracle Database 11g Enterprise Edition h
s ) Release
u i -
Production
m i rฺe Security,
n t G OLAP, Data Mining

With the Partitioning, Oracle Label
gp options e
and Real Application Testing
d S tud
SQL>
b i a@ this
i a ( a use
SQL> connect jtrace/trace@TRACESERV

to
Connected.
SQL> Ab
o
ni alter session set workarea_size_policy=manual;
SQL>
n t o
e A Session altered.
Jos SQL>
SQL> alter session set sort_area_size=50000;

Session altered.

SQL>
SQL> alter session set hash_area_size=5000;

Session altered.

SQL>
SQL>
SQL> alter session set tracefile_identifier='mytraceP1';

Session altered.

SQL>
SQL>
SQL> set timing on

Oracle Database 11g: SQL Tuning Workshop A - 327


Practice 10-1: Tracing Applications (continued)
SQL>
SQL> select /*+ ORDERED USE_HASH(s2) */ count(*) from sales s1,
sales s2 where s1.cust_id=s2.cust_id;

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

172878975

Elapsed: 00:01:19.25
SQL>
SQL>
SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceS1';
n s e
i ce
Session altered.
b l el
Elapsed: 00:00:00.00
fer a
SQL>
a n s
SQL> set timing on
o n -tr
an
SQL>
SQL> select /*+ ORDERED USE_HASH(s2) S1 */ count(*) from sales s1,
sales s2 where s1.cust_id=s2.cust_id;
h s
a deฺ
e )
s Gui
COUNT(*) r ฺ
i ent
----------
p ฺ m
172878975
d g Stud
b i
Elapsed: 00:00:40.19a@ this
SQL>
i a (ja use
b fromtoOracle Database 11g Enterprise Edition Release
SQL> exit;
A
i o
Disconnected
n
nto11.1.0.6.0 - Production

e A With the Partitioning, Oracle Label Security, OLAP, Data Mining

J o s and Real Application Testing options


[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @run_tracep1.sql

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 328


Practice 10-1: Tracing Applications (continued)
[oracle@edrsr33p1-orcl Application_Tracing]$ cat run_tracep1.sql
set echo on

connect trace/trace@TRACESERV

alter session set workarea_size_policy=manual;


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

alter session set sort_area_size=50000;

alter session set hash_area_size=5000;

alter session set tracefile_identifier='mytraceP1';

set timing on
n s e
i ce
select /*+ ORDERED USE_HASH(s2) */ count(*) from sales s1, sales s2
b l el
where s1.cust_id=s2.cust_id;
fer a
a n s
o n -tr
an
connect trace/trace@TRACESERV

h s
a deฺ
alter session set tracefile_identifier='mytraceS1';

e )
s Gui
set timing on r ฺ
i ent
p ฺ m d*/ count(*) from sales s1, sales
d g
select /*+ ORDERED USE_HASH(s2)
S t
S1u
b i a@ this
s2 where s1.cust_id=s2.cust_id;

exit;
i a (ja use
A b to
11) o
ni the run_tracep2.sh script from your terminal session. Observe your
Execute
t o
e An screen.
Jos [oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep2.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:31:57 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceP2';

Session altered.

Oracle Database 11g: SQL Tuning Workshop A - 329


Practice 10-1: Tracing Applications (continued)
SQL>
SQL> set timing on
SQL>
SQL> declare
2 c number := dbms_sql.open_cursor;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 oname varchar2(50);
4 ignore integer;
5 begin
6 for i in 1 .. 5000 loop
7 dbms_sql.parse(c,'select object_name from dba_objects where
object_id = '||i , dbms_sql.native); -- use literal
8 dbms_sql.define_column(c, 1, oname, 50);
9 ignore := dbms_sql.execute(c);
10 if dbms_sql.fetch_rows(c)>0 then
11 dbms_sql.column_value(c, 1, oname);
n s e
12 end if;
i ce
13 end loop;
b l el
14 dbms_sql.close_cursor(c);
15 end;
fer a
16 /
a n s
o n -tr
an
PL/SQL procedure successfully completed.

Elapsed: 00:00:49.36
h s
a deฺ
SQL>
e )
s Gui
SQL> r ฺ
i ent
SQL>
p ฺ m d
d g
SQL> connect trace/trace@TRACESERV
S t u
Connected.
SQL> b i a@ this
i a (ja usetsetracefile_identifier='mytraceS2';
SQL> alter session

A baltered.to
ni o
Session

ntoElapsed:
e A 00:00:00.00

J o s SQL>
SQL> declare
2 c number := dbms_sql.open_cursor;
3 oname varchar2(50);
4 ignore integer;
5 begin
6 dbms_sql.parse(c,'select object_name from dba_objects where
object_id = :y' , dbms_sql.native); -- use bind var
7 for i in 1 .. 5000 loop
8 dbms_sql.bind_variable(c,':y',i);
9 dbms_sql.define_column(c, 1, oname, 50);
10 ignore := dbms_sql.execute(c);
11 if dbms_sql.fetch_rows(c)>0 then
12 dbms_sql.column_value(c, 1, oname);
13 end if;
14 end loop;
15 dbms_sql.close_cursor(c);
16 end;
17 /

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 330


Practice 10-1: Tracing Applications (continued)
Elapsed: 00:00:00.86
SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

With the Partitioning, Oracle Label Security, OLAP, Data Mining


and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

n s e
export ORACLE_SID=orcl
i ce
b l el
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
fer a
export
a n s
o n -tr
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

an
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @run_tracep2.sql


h s
a deฺ
e )
s Gui
r ฺ
i ent
--------------------------------------------------------------

p ฺ m
set echo on
d g Stud
b i a@ this
connect trace/trace@TRACESERV

i a (jaset tracefile_identifier='mytraceP2';
u se
b
alter session
A to
o
ni timing on
ntoset
e A
J o s declare
c number := dbms_sql.open_cursor;
oname varchar2(50);
ignore integer;
begin
for i in 1 .. 5000 loop
dbms_sql.parse(c,'select object_name from dba_objects where
object_id = '||i , dbms_sql.native); -- use literal
dbms_sql.define_column(c, 1, oname, 50);
ignore := dbms_sql.execute(c);
if dbms_sql.fetch_rows(c)>0 then
dbms_sql.column_value(c, 1, oname);
end if;
end loop;
dbms_sql.close_cursor(c);
end;
/

connect trace/trace@TRACESERV

Oracle Database 11g: SQL Tuning Workshop A - 331


Practice 10-1: Tracing Applications (continued)
alter session set tracefile_identifier='mytraceS2';

declare
c number := dbms_sql.open_cursor;
oname varchar2(50);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ignore integer;
begin
dbms_sql.parse(c,'select object_name from dba_objects where
object_id = :y' , dbms_sql.native); -- use bind var
for i in 1 .. 5000 loop
dbms_sql.bind_variable(c,':y',i);
dbms_sql.define_column(c, 1, oname, 50);
ignore := dbms_sql.execute(c);
if dbms_sql.fetch_rows(c)>0 then
dbms_sql.column_value(c, 1, oname);
n s e
end if;
i ce
end loop;
b l el
dbms_sql.close_cursor(c);
end;
fe r a
/
a n s
o n -tr
an
exit;

h s ฺ Observe your
aterminaldesession.
12) Execute the run_tracep3.sh script from )
your i
screen. i r ฺes nt Gu
g p ฺm ude
d t
[oracle@edrsr33p1-orcl Application_Tracing]$
S
./run_tracep3.sh

i @ his- Production on Fri Apr 4 20:32:53 2008


a11.1.0.6.0
SQL*Plus: Release
j a b e t
(
ia(c) 1982, s
u2007, Oracle. All rights reserved.
b
Copyright
t o
n i oA
to
e An Connected to:
Jos
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceP3';

Session altered.

SQL>
SQL> update sales set amount_sold=20000 where prod_id=13 and
cust_id=987;

2 rows updated.

SQL>
SQL> commit;

Oracle Database 11g: SQL Tuning Workshop A - 332


Practice 10-1: Tracing Applications (continued)
Commit complete.

SQL>
SQL>
SQL> connect trace/trace
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Connected.
SQL>
SQL> create index sales_prod_cust_indx on sales(prod_id,cust_id);

Index created.

SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
n s e
SQL> alter session set tracefile_identifier='mytraceS3';
i ce
b l el
Session altered.
fer a
SQL>
a n s
-tr
SQL> update sales set amount_sold=30000 where prod_id=13 and
n
o
an
cust_id=987;

2 rows updated.
h s
a deฺ
e )
s Gui
r ฺ
mi dent
SQL>
SQL> commit;
p ฺ
g Stu
d
Commit complete.
b i a@ this
SQL> a se
(jtrace/trace
b i
SQL> connecta to u
i o A
Connected.
n
toSQL>
A n SQL> drop index sales_prod_cust_indx;

o s e
J Index dropped.

SQL>
SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

Oracle Database 11g: SQL Tuning Workshop A - 333


Practice 10-1: Tracing Applications (continued)
export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @run_tracep3.sql


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

--------------------------------------------------------------

set echo on

connect trace/trace@TRACESERV

alter session set tracefile_identifier='mytraceP3';

update sales set amount_sold=20000 where prod_id=13 and cust_id=987;


n s e
i ce
commit;
b l el
fer a
connect trace/trace
a n s
o n -tr
an
create index sales_prod_cust_indx on sales(prod_id,cust_id);

connect trace/trace@TRACESERV
h s
a deฺ
e )
s Gui
r ฺ
i ent
alter session set tracefile_identifier='mytraceS3';

p ฺ m
d g Studwhere prod_id=13 and cust_id=987;
update sales set amount_sold=30000

commit; b i a@ this
ja e
ia ( to us
connect btrace/trace
i A
o index
to n
drop sales_prod_cust_indx;
A n
o s e
J exit;

13) Execute the run_tracep4.sh script from your terminal session. Observe your
screen.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep4.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:33:08 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 334


Practice 10-1: Tracing Applications (continued)
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceP4';

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

SQL>
SQL> set timing on
SQL>
SQL> DECLARE
2 TYPE SalesCurTyp IS REF CURSOR;
3 v_sales_cursor SalesCurTyp;
4 sales_record sh.sales%ROWTYPE;
5 v_stmt_str VARCHAR2(200);
6 BEGIN
n s e
7 -- Dynamic SQL statement with placeholder:
i ce
8 v_stmt_str := 'select * from sh.sales where amount_sold>0';
b l el
9
10 -- Open cursor and specify bind argument in USING clause:
fer a
11 OPEN v_sales_cursor FOR v_stmt_str;
a n s
12
n -tr
o
an
13 -- Fetch rows from result set one at a time:
14 LOOP
15
h s
a deฺ
FETCH v_sales_cursor INTO sales_record;
16 )
s Gui
EXIT WHEN v_sales_cursor%NOTFOUND;
e
17 END LOOP; r ฺ
i ent
18
-- Close cursor: g p ฺ m d
19
d S t u
20
21 END; b i a@ this
CLOSE v_sales_cursor;

22 /
i a (ja use
A b o
proceduret successfully completed.
i o
PL/SQL
n
t o
n Elapsed: 00:00:26.84
e A
Jos
SQL>
SQL>
SQL> connect trace/trace@TRACESERV
Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceS4';

Session altered.

Elapsed: 00:00:00.00
SQL>
SQL> set timing on
SQL>
SQL> DECLARE
2 TYPE SalesCurTyp IS REF CURSOR;
3 TYPE SalesList IS TABLE OF sh.sales%ROWTYPE;
4 v_sales_cursor SalesCurTyp;
5 sales_List SalesList;
6 v_stmt_str VARCHAR2(200);
7 BEGIN
8 -- Dynamic SQL statement with placeholder:

Oracle Database 11g: SQL Tuning Workshop A - 335


Practice 10-1: Tracing Applications (continued)
9 v_stmt_str := 'select /* S4 */ * from sh.sales where
amount_sold>0';
10
11 -- Open cursor:
12 OPEN v_sales_cursor FOR v_stmt_str;
13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

14 -- Fetch rows from result set one at a time:


15 LOOP
16 FETCH v_sales_cursor BULK COLLECT INTO Sales_List LIMIT
10000;
17 EXIT WHEN v_sales_cursor%NOTFOUND;
18 END LOOP;
19
20 -- Close cursor:
21 CLOSE v_sales_cursor;
22 END;
n s e
23 /
i ce
b l el
PL/SQL procedure successfully completed.
fer a
Elapsed: 00:00:02.09
a n s
SQL>
o n -tr
an
SQL>
SQL> exit;
h s
a deฺ
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
e )
s Gui
r ฺ
mi dent
With the Partitioning, Oracle Label Security, OLAP, Data Mining

p ฺ
and Real Application Testing options
g Stu
d
[oracle@edrsr33p1-orcl Application_Tracing]$

b i a@ this
--------------------------------------------------------------

i a (ja use
b
#!/bin/bash
A to
o
ni /home/oracle/solutions/Application_Tracing
ntocd
e A
J o s export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus / as sysdba @run_tracep4.sql

--------------------------------------------------------------
set echo on

connect trace/trace@TRACESERV

alter session set tracefile_identifier='mytraceP4';

set timing on

DECLARE
TYPE SalesCurTyp IS REF CURSOR;

Oracle Database 11g: SQL Tuning Workshop A - 336


Practice 10-1: Tracing Applications (continued)
v_sales_cursor SalesCurTyp;
sales_record sh.sales%ROWTYPE;
v_stmt_str VARCHAR2(200);
BEGIN
-- Dynamic SQL statement with placeholder:
v_stmt_str := 'select * from sh.sales where amount_sold>0';
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

-- Open cursor and specify bind argument in USING clause:


OPEN v_sales_cursor FOR v_stmt_str;

-- Fetch rows from result set one at a time:


LOOP
FETCH v_sales_cursor INTO sales_record;
EXIT WHEN v_sales_cursor%NOTFOUND;
END LOOP;

n s e
-- Close cursor:
i ce
CLOSE v_sales_cursor;
b l el
END;
/
fer a
a n s
n -tr
o
an
connect trace/trace@TRACESERV

h s
a deฺ
alter session set tracefile_identifier='mytraceS4';

e )
s Gui
set timing on r ฺ
i ent
p ฺ m d
DECLARE
TYPE SalesCurTyp @ d g S t u
a IS
TYPE SalesListbiIS TABLEth issh.sales%ROWTYPE;
REF CURSOR;
OF
(ja uSalesCurTyp;
v_sales_cursor
i a se
b
sales_List
A toVARCHAR2(200);
SalesList;
o
ni-- Dynamic SQL statement with placeholder:
v_stmt_str

ntoBEGIN
e A
J o s v_stmt_str := 'select /* S4 */ * from sh.sales where
amount_sold>0';

-- Open cursor:
OPEN v_sales_cursor FOR v_stmt_str;

-- Fetch rows from result set one at a time:


LOOP
FETCH v_sales_cursor BULK COLLECT INTO Sales_List LIMIT 10000;
EXIT WHEN v_sales_cursor%NOTFOUND;
END LOOP;

-- Close cursor:
CLOSE v_sales_cursor;
END;
/

exit;

Oracle Database 11g: SQL Tuning Workshop A - 337


Practice 10-1: Tracing Applications (continued)
14) Execute the run_tracep5.sh script from your terminal session. Observe your
screen.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep5.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:33:59 2008


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

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
SQL>
i ce
SQL> connect trace/trace@TRACESERV
b l el
Connected.
fer a
SQL>
SQL> alter session set tracefile_identifier='mytraceP5';
a n s
o n -tr
an
Session altered.

SQL>
h s
a deฺ
e
SQL> insert into sales2 select * from )
s Gui union all select *
sh.sales
r ฺ
mi dent
from sales;

1837686 rows created. dg p ฺ


S tu
b i a@ this
(ja use
SQL> commit;

b i a to
A
Commit complete.
o
ni
toSQL>
A n SQL>

o s e SQL> connect trace/trace@TRACESERV


J Connected.
SQL>
SQL> alter session set tracefile_identifier='mytraceS5';

Session altered.

SQL>
SQL> insert into sales3 select * from sh.sales union all select *
from sales;

1837686 rows created.

SQL> commit;

Commit complete.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production

Oracle Database 11g: SQL Tuning Workshop A - 338


Practice 10-1: Tracing Applications (continued)
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$

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

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
n s e
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
i ce
b l el
sqlplus / as sysdba @run_tracep5.sql
fer a
--------------------------------------------------------------
a n s
o n -tr
an
set echo on

connect trace/trace@TRACESERV
h s
a deฺ
e )
s Gui
r ฺ
i ent
alter session set tracefile_identifier='mytraceP5';

p ฺ m d
d
insert into sales2 selectg * from u
S t sh.sales union all select * from
sales;
commit; b i a@ this
i a (ja use
A to
btrace/trace@TRACESERV
o
ni session set tracefile_identifier='mytraceS5';
connect

ntoalter
e A
J o s insert into sales3 select * from sh.sales union all select * from
sales;
commit;

exit;

15) Execute the run_tracep6.sh script from your terminal session. Observe your
screen.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep6.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:34:26 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:34:26 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop A - 339


Practice 10-1: Tracing Applications (continued)

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

and Real Application Testing options

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
n s e
SQL> connect trace/trace
i ce
SQL>
b l el
SQL> connect trace/trace@TRACESERV
Connected.
fer a
SQL>
a n s
SQL> update sales set amount_sold=amount_sold+1;
n -tr
o
an
Connected.
SQL>
h s
a deฺ
SQL> alter session set tracefile_identifier='mytraceP6';

e )
s Gui
Session altered. r ฺ
i ent
p ฺ m d
SQL>
d g S t u
b i a@ this
SQL> exec dbms_lock.sleep(30);

918843 rows (
a ja use
updated.
b i to
SQL> A
o
ni exec dbms_lock.sleep(60);
ntoSQL>
e A
J o s PL/SQL procedure successfully completed.

SQL>
SQL> set termout off
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$
PL/SQL procedure successfully completed.

SQL>
SQL> rollback;

Rollback complete.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining

Oracle Database 11g: SQL Tuning Workshop A - 340


Practice 10-1: Tracing Applications (continued)
and Real Application Testing options

[oracle@edrsr33p1-orcl Application_Tracing]$

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

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
n s e
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
i ce
b l el
sqlplus / as sysdba @run_tracep6a.sql &
fer a
sqlplus / as sysdba @run_tracep6b.sql
a n s
n -tr
o
an
--------------------------------------------------------------

h s
a deฺ
set echo on
e )
s Gui
r ฺ
connect trace/trace
p ฺ mi dent
d g Stu
b i a@ this
update sales set amount_sold=amount_sold+1;

i a (ja use
exec dbms_lock.sleep(60);

A b to
i o
rollback;
n
t o
n exit;
e A
Jos --------------------------------------------------------------

set echo on

connect trace/trace@TRACESERV

alter session set tracefile_identifier='mytraceP6';

exec dbms_lock.sleep(30);

set termout off

select cust_id, sum(amount_sold) from sales group by cust_id order


by cust_id;

set tournout on

exit;

Oracle Database 11g: SQL Tuning Workshop A - 341


Practice 10-1: Tracing Applications (continued)
16) Execute the run_tracep7.sh script from your terminal session. Observe your
screen.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./run_tracep7.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:36:10 2008


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

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
n s e
SQL>
i ce
SQL> connect trace/trace@TRACESERV
b l el
Connected.
fer a
SQL>
SQL> alter session set tracefile_identifier='mytraceP7';
a n s
o n -tr
an
Session altered.

SQL>
h s
a deฺ
SQL> declare
e )
s Gui
r ฺ
mi dent
2 c number := dbms_sql.open_cursor;
3 custid number;
amount number;dg p ฺ
4
S tu
5
a@ this
ignore integer;
b i
(ja use
6 begin
7
b i a
dbms_sql.parse(c,'select
o
t -- use bind var
cust_id, sum(amount_sold) from

i o A
sales where cust_id=2
dbms_sql.native);
group by cust_id order by cust_id' ,

nton 8 dbms_sql.define_column(c, 1, custid);


e A 9 dbms_sql.define_column(c, 2, amount);

J o s 10
11
ignore := dbms_sql.execute(c);
if dbms_sql.fetch_rows(c)>0 then
12 dbms_sql.column_value(c, 1, custid);
13 dbms_sql.column_value(c, 2, amount);
14 end if;
15 end;
16 /

PL/SQL procedure successfully completed.

SQL>
SQL> connect trace/trace
Connected.
SQL>
SQL> create index sales_cust_indx on sales(cust_id);

Index created.

SQL>
SQL> exit;

Oracle Database 11g: SQL Tuning Workshop A - 342


Practice 10-1: Tracing Applications (continued)
Disconnected from Oracle Database 11g Enterprise Edition Release
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

n s e
export
i ce
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
b l el
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
fer a
sqlplus / as sysdba @run_tracep7.sql
a n s
n - tr
o
an
--------------------------------------------------------------

set echo on
h s
a deฺ
connect trace/trace@TRACESERV rฺe
)
s Gui
ฺ m i ent
d p
g Stud
alter session set tracefile_identifier='mytraceP7';

declare b i a@ this
c number (
a jadbms_sql.open_cursor;
:=
u se
i to
b number;
custid number;
o A
ni ignore integer;
amount

ntobegin
e A
J o s dbms_sql.parse(c,'select cust_id, sum(amount_sold) from sales
where cust_id=2 group by cust_id order by cust_id' ,
dbms_sql.native); -- use bind var
dbms_sql.define_column(c, 1, custid);
dbms_sql.define_column(c, 2, amount);
ignore := dbms_sql.execute(c);
if dbms_sql.fetch_rows(c)>0 then
dbms_sql.column_value(c, 1, custid);
dbms_sql.column_value(c, 2, amount);
end if;
end;
/

connect trace/trace

create index sales_cust_indx on sales(cust_id);

exit;

17) Disable tracing for your database.

Oracle Database 11g: SQL Tuning Workshop A - 343


Practice 10-1: Tracing Applications (continued)
a) Go back to the Top Services page in your Enterprise Manager session.
b) Ensure that TRACESERV is selected, and click the Disable SQL trace button.
c) Back to the Top Services page, you should see a confirmation message near the
top of the Top Consumers page.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

18) Try to find out all the trace files that were generated to handle the previous seven
cases.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./show_mytraces.sh
orcl_ora_19237_mytraceP0.trc
orcl_ora_19237_mytraceP0.trm
orcl_ora_19313_mytraceP1.trc
orcl_ora_19313_mytraceP1.trm
orcl_ora_19355_mytraceS1.trc
n s e
ce
orcl_ora_19355_mytraceS1.trm
orcl_ora_19382_mytraceP2.trc
eli
orcl_ora_19382_mytraceP2.trm
a b l
orcl_ora_19467_mytraceS2.trc
fe r
orcl_ora_19467_mytraceS2.trm
a n s
orcl_ora_19474_mytraceP3.trc
n - tr
orcl_ora_19474_mytraceP3.trm
o
orcl_ora_19503_mytraceS3.trc
s an
orcl_ora_19503_mytraceS3.trm
) h a deฺ
orcl_ora_19534_mytraceP4.trc
orcl_ora_19534_mytraceP4.trm
r ฺ e s Gui
orcl_ora_19549_mytraceS4.trc
p ฺ mi dent
g Stu
orcl_ora_19549_mytraceS4.trm
d
a@ this
orcl_ora_19558_mytraceP5.trc
b i
orcl_ora_19558_mytraceP5.trm

i a (ja use
orcl_ora_19568_mytraceS5.trc
b to
orcl_ora_19568_mytraceS5.trm
A
orcl_ora_19583_mytraceP6.trc

o n io
orcl_ora_19583_mytraceP6.trm
nt orcl_ora_19634_mytraceP7.trc
e A orcl_ora_19634_mytraceP7.trm
J o s [oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

ls /u01/app/oracle/diag/rdbms/orcl/orcl/trace | grep mytrace

19) After you identify the location of those trace files, merge their content into one file
called mytrace.trc located in your
$HOME/solutions/Application_Tracing directory.
[oracle@edrsr33p1-orcl Application_Tracing]$ ./merge_traces.sh
[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

Oracle Database 11g: SQL Tuning Workshop A - 344


Practice 10-1: Tracing Applications (continued)
#!/bin/bash

cd /home/oracle/solutions/Application_Tracing

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

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

trcsess output=mytrace.trc service=TRACESERV


/u01/app/oracle/diag/rdbms/orcl/orcl/trace/*.trc

n s e
20) Use tkprof over the mytrace.trc file to generate a compiled trace output called
i ce
myreport.txt located into your
b l el
$HOME/solutions/Application_Tracing directory.
f er a
an s
- t r
[oracle@edrsr33p1-orcl Application_Tracing]$ ./tkprof_traces.sh

TKPROF: Release 11.1.0.6.0 - Production on Fri n on4 20:37:27 2008


Apr
s a
Copyright (c) 1982, 2007, Oracle. All h
) a
i d eฺ
rights reserved.

i r ฺes nt Gu
p ฺm ude
[oracle@edrsr33p1-orcl Application_Tracing]$
g
[oracle@edrsr33p1-orcl d
S t
Application_Tracing]$
@ s
j a b ia
e thi
--------------------------------------------------------------

b ia ( to us
oA
#!/bin/bash
n i
ntocd /home/oracle/solutions/Application_Tracing
e A
J o s export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

tkprof mytrace.trc myreport.txt

21) In addition, run TKPROF over the trace file that was generated for case 7 (step 16)
with the EXPLAIN option set to your TRACE account.
[oracle@edrsr33p1-orcl Application_Tracing]$ tkprof
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/*mytraceP7.trc
myreport2.txt explain=trace/trace

TKPROF: Release 11.1.0.6.0 - Production on Fri Apr 4 20:40:22 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop A - 345


Practice 10-1: Tracing Applications (continued)

[oracle@edrsr33p1-orcl Application_Tracing]$
[oracle@edrsr33p1-orcl Application_Tracing]$

22) After this is done, interpret the trace generated for case 1 (step 10). What do you
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

observe, and what are your conclusions?


a) This case tries to illustrate the consequences of using manually sized SQL area
parameters, such as HASH_AREA_SIZE. The first statement was executed using
a very small HASH_AREA_SIZE value. The immediate consequence was the
number of temporary segments needed to execute the statement. Later, the same
statement was executed, but this time using the default SQL area parameters,
which were sized automatically by the system to handle the needs. You can see a
high disk value as well as a high number of waits for temporary segments for the
n s e
first execution, compared to the second one. i ce
b l el
select /*+ ORDERED USE_HASH(s2) */ count(*)
fe r a
from
a n s
-tr
sales s1, sales s2 where s1.cust_id=s2.cust_id

o n
call count cpu elapsed
s a n query current
disk
rows
)
------- ------ -------- ----------s----------
eฺ
ha id----------
r ฺ e G u ----------

mi0.00dent 0
----------
Parse 1 0.00
p
g Stuฺ 2 0
0 d
Execute 1
b i [email protected]
t h is 0.00 0 0 0

(j2a u78.23
se
0
Fetch
b i a o
79.16 806500 8874 0
1
i o A ------ t-------- ---------- ---------- ---------- ----------
-------
n
Anto----------
total 4 78.23 79.16 806500 8876 0
e
Jos
1

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation


------- ---------------------------------------------------
1 SORT AGGREGATE (cr=8874 pr=806500 pw=806500 time=0 us)
172878975 HASH JOIN (cr=8874 pr=806500 pw=806500 time=1723284 us
cost=1392520 size=5737160286 card=220660011)
918843 TABLE ACCESS FULL SALES (cr=4437 pr=4433 pw=4433
time=7413 us cost=1363 size=14132716 card=1087132)
918843 TABLE ACCESS FULL SALES (cr=4437 pr=4433 pw=4433
time=7485 us cost=1363 size=14132716 card=1087132)

Elapsed times include waiting on following events:


Event waited on Times Max. Wait
Total Waited

Oracle Database 11g: SQL Tuning Workshop A - 346


Practice 10-1: Tracing Applications (continued)
---------------------------------------- Waited ---------- ---
---------
SQL*Net message to client 2 0.00
0.00
reliable message 1 0.00
0.00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

enq: KO - fast object checkpoint 1 0.00


0.00
direct path read 86 0.00
0.00
direct path write temp 2524 0.00
0.00
direct path read temp 797634 0.00
1.70
SQL*Net message from client 2 0.00
0.00
n s e
********************************************************************
i ce
************
b l el
fer a
a n s
o n -tr
s an
h a deฺ
select /*+ ORDERED USE_HASH(s2) S1 */ count(*)
)
from
r ฺ e s Gui
mi dent
sales s1, sales s2 where s1.cust_id=s2.cust_id

p ฺ
g elapsed
d S tu
call
rows
count
b i a@ this
cpu disk query current

i a (ja --------
------- ------
u se ---------- ---------- ---------- ----------
b 1 to 0.00
----------
Parse A
0ni
o 0.00 0 2 0

ntoExecute
e A 1 0.00 0.00 0 0 0

J o s 0
Fetch 2 40.02 40.14 10028 8874 0
1
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 4 40.03 40.14 10028 8876 0
1

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation


------- ---------------------------------------------------
1 SORT AGGREGATE (cr=8874 pr=10028 pw=10028 time=0 us)
172878975 HASH JOIN (cr=8874 pr=10028 pw=10028 time=1430806 us
cost=6891 size=5737160286 card=220660011)
918843 TABLE ACCESS FULL SALES (cr=4437 pr=4433 pw=4433
time=6837 us cost=1363 size=14132716 card=1087132)
918843 TABLE ACCESS FULL SALES (cr=4437 pr=4433 pw=4433
time=7084 us cost=1363 size=14132716 card=1087132)

Oracle Database 11g: SQL Tuning Workshop A - 347


Practice 10-1: Tracing Applications (continued)

Elapsed times include waiting on following events:


Event waited on Times Max. Wait
Total Waited
---------------------------------------- Waited ---------- ---
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

---------
SQL*Net message to client 2 0.00
0.00
direct path read 86 0.00
0.00
direct path write temp 166 0.00
0.00
direct path read temp 166 0.00
0.00
SQL*Net message from client 2 0.00
n s e
0.00
ice
********************************************************************
b l el
************
fe ra
a n s
23) Interpret the trace generated for case 2 (step 11). What do you observe, and what are
your conclusions?
o n -tr
a) For this case, the almost same statement was run 5000 a ntimes, but each time a
different literal was used to execute the query.a
h s eฺinefficient
This caused the system to parse
almost the same statement 5000 times, which )
s Gu is i d
extremely although the
r ฺ e
statement. However, another p ฺ mi dwaseinformation
time precision is too low to give accurate
statement
t
nalso about the parse time of each
d
time using a bind variable. S tu was parsed only 5000
gThe statement executed
once,
times, but this
and executed
i a @ i s
5000 times. This
j abbehavior
e thmuch more efficient than the previous one.
is
(
ia to u s
b
select object_name
from A
o n io
dba_objects where object_id = 1
nt
e A
J o s call count cpu elapsed disk query current
rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.01 0.01 0 0 0
0
Execute 1 0.00 0.00 0 0 0
0
Fetch 1 0.00 0.00 0 3 0
0
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 3 0.01 0.01 0 3 0
0

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182 (recursive depth: 1)

Rows Row Source Operation

Oracle Database 11g: SQL Tuning Workshop A - 348


Practice 10-1: Tracing Applications (continued)
------- ---------------------------------------------------
0 VIEW DBA_OBJECTS (cr=3 pr=0 pw=0 time=0 us cost=5 size=158
card=2)
0 UNION-ALL (cr=3 pr=0 pw=0 time=0 us)
0 FILTER (cr=3 pr=0 pw=0 time=0 us)
0 NESTED LOOPS (cr=3 pr=0 pw=0 time=0 us cost=5 size=109
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

card=1)
0 NESTED LOOPS (cr=3 pr=0 pw=0 time=0 us cost=4 size=105
card=1)
0 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=3 pr=0 pw=0
time=0 us cost=3 size=82 card=1)
1 INDEX RANGE SCAN I_OBJ1 (cr=2 pr=0 pw=0 time=0 us
cost=2 size=0 card=1)(object id 36)
0 INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=23 card=1)(object id 47)
0 INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
n s e
cost=1 size=4 card=1)(object id 47)
i ce
0 TABLE ACCESS BY INDEX ROWID IND$ (cr=0 pr=0 pw=0 time=0
b l el
us cost=2 size=8 card=1)
0
fe
INDEX UNIQUE SCAN I_IND1 (cr=0 pr=0 pw=0 time=0 us r a
cost=1 size=0 card=1)(object id 41)
a n s
0 -tr
NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=2 size=28
n
o
an
card=1)
0 INDEX FULL SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=20 card=1)(object id 47)
h s
a deฺ
0 )
s Gui
INDEX RANGE SCAN I_OBJ4 (cr=0 pr=0 pw=0 time=0 us
e
r ฺ
mi dent
cost=1 size=8 card=1)(object id 39)
0 ฺ
FILTER (cr=0 pr=0 pw=0 time=0 us)
p
g Stu
0
d
NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=1 size=83
card=1)
0 b i a@ this
INDEX FULL SCAN I_LINK1 (cr=0 pr=0 pw=0 time=0 us

i a (ja use
cost=0 size=79 card=1)(object id 134)
0b to
INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us

n i oA
cost=1 size=4 card=1)(object id 47)

nto********************************************************************
e A
J o s ************

select object_name
from
dba_objects where object_id = 2

call count cpu elapsed disk query current


rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 0 0
0
Execute 1 0.00 0.00 0 0 0
0
Fetch 1 0.00 0.00 0 5 0
1
------- ------ -------- ---------- ---------- ---------- ----------
----------

Oracle Database 11g: SQL Tuning Workshop A - 349


Practice 10-1: Tracing Applications (continued)
total 3 0.00 0.00 0 5 0
1

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182 (recursive depth: 1)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

...

select object_name
from
dba_objects where object_id = 5000

call count cpu elapsed disk query current


n s e
rows
i ce
------- ------ -------- ---------- ---------- ---------- ----------
b l el
----------
Parse 1 0.00 0.00 0 0
fe r a 0
0
a n s
Execute 1 0.00 0.00 0
o n -tr 0 0

a0 n
0
Fetch 1 0.00 0.00 5 0
1
h a s e ฺ
------- ------
s )
-------- ---------- ----------
u i d---------- ----------
---------- r ฺ
i0.00e tG 0
total 3 0.00
p ฺ m d e n 5 0
1
d g S t u
@ s
Misses in library
j a b e thi parse: 1
iacache during
ia (id:to182us (recursive depth: 1)
Optimizer mode: ALL_ROWS
Parsing buser

n i o A Row Source Operation


nto------- ---------------------------------------------------
Rows

e A
J o s 1 VIEW DBA_OBJECTS (cr=5 pr=0 pw=0 time=0 us cost=5 size=158
card=2)
1 UNION-ALL (cr=5 pr=0 pw=0 time=0 us)
1 FILTER (cr=5 pr=0 pw=0 time=0 us)
1 NESTED LOOPS (cr=5 pr=0 pw=0 time=0 us cost=5 size=109
card=1)
1 NESTED LOOPS (cr=4 pr=0 pw=0 time=0 us cost=4 size=105
card=1)
1 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=3 pr=0 pw=0
time=0 us cost=3 size=82 card=1)
1 INDEX RANGE SCAN I_OBJ1 (cr=2 pr=0 pw=0 time=0 us
cost=2 size=0 card=1)(object id 36)
1 INDEX RANGE SCAN I_USER2 (cr=1 pr=0 pw=0 time=0 us
cost=1 size=23 card=1)(object id 47)
1 INDEX RANGE SCAN I_USER2 (cr=1 pr=0 pw=0 time=0 us
cost=1 size=4 card=1)(object id 47)
0 TABLE ACCESS BY INDEX ROWID IND$ (cr=0 pr=0 pw=0 time=0
us cost=2 size=8 card=1)
0 INDEX UNIQUE SCAN I_IND1 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=0 card=1)(object id 41)

Oracle Database 11g: SQL Tuning Workshop A - 350


Practice 10-1: Tracing Applications (continued)
0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=2 size=28
card=1)
0 INDEX FULL SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=20 card=1)(object id 47)
0 INDEX RANGE SCAN I_OBJ4 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=8 card=1)(object id 39)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 FILTER (cr=0 pr=0 pw=0 time=0 us)


0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=1 size=83
card=1)
0 INDEX FULL SCAN I_LINK1 (cr=0 pr=0 pw=0 time=0 us
cost=0 size=79 card=1)(object id 134)
0 INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=4 card=1)(object id 47)

********************************************************************
************
n s e
i ce
b l el
fer a
a n s
o n -tr
s an
select object_name
) h a deฺ
from
r ฺ e s Gui
mi dent
dba_objects where object_id = :y

p ฺ
g elapsed
d S tu
call
rows
count
b i a@ this
cpu disk query current

i a (ja --------
------- ------
u se ---------- ---------- ---------- ----------
b 1 to 0.00
----------
Parse A
0ni
o 0.00 0 0 0

ntoExecute 5000
e A 0.20 0.18 0 0 0

J o s 0
Fetch 5000 0.24 0.24 0 26837 0
4928
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 10001 0.45 0.43 0 26837 0
4928

Misses in library cache during parse: 1


Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 182 (recursive depth: 1)

Rows Row Source Operation


------- ---------------------------------------------------
0 VIEW DBA_OBJECTS (cr=3 pr=0 pw=0 time=0 us cost=5 size=158
card=2)
0 UNION-ALL (cr=3 pr=0 pw=0 time=0 us)
0 FILTER (cr=3 pr=0 pw=0 time=0 us)
0 NESTED LOOPS (cr=3 pr=0 pw=0 time=0 us cost=5 size=109
card=1)

Oracle Database 11g: SQL Tuning Workshop A - 351


Practice 10-1: Tracing Applications (continued)
0 NESTED LOOPS (cr=3 pr=0 pw=0 time=0 us cost=4 size=105
card=1)
0 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=3 pr=0 pw=0
time=0 us cost=3 size=82 card=1)
1 INDEX RANGE SCAN I_OBJ1 (cr=2 pr=0 pw=0 time=0 us
cost=2 size=0 card=1)(object id 36)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

0 INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us


cost=1 size=23 card=1)(object id 47)
0 INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=4 card=1)(object id 47)
0 TABLE ACCESS BY INDEX ROWID IND$ (cr=0 pr=0 pw=0 time=0
us cost=2 size=8 card=1)
0 INDEX UNIQUE SCAN I_IND1 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=0 card=1)(object id 41)
0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=2 size=28
card=1)
n s e
0 INDEX FULL SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
ice
cost=1 size=20 card=1)(object id 47)
b l el
0 INDEX RANGE SCAN I_OBJ4 (cr=0 pr=0 pw=0 time=0 us
cost=1 size=8 card=1)(object id 39)
fe ra
0 FILTER (cr=0 pr=0 pw=0 time=0 us)
a n s
0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=1 size=83
o n -tr
an
card=1)
0 INDEX FULL SCAN I_LINK1 (cr=0 pr=0 pw=0 time=0 us
cost=0 size=79 card=1)(object id 134)
h s
a deฺ
0 )
s Gui
INDEX RANGE SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us
e
cost=1 size=4 card=1)(object id 47) r ฺ
i ent
p ฺ m d
d g t u
********************************************************************
S
************
b i a@ this
i a (jagenerated
24) Interpret the trace
u sefor case 3 (step 12). What do you observe, and what are
b
your conclusions?
A to
o
ni If you look closely at this case, you see that you access the complete table to
a)
t o
e An update only two rows. This is very inefficient and the alternate case is much better

Jos
as it uses an index to speed up the retrieval of the rows that need to be updated.
update sales set amount_sold=20000
where
prod_id=13 and cust_id=987

call count cpu elapsed disk query current


rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 1 0
0
Execute 1 0.09 0.09 2442 4441 4
2
Fetch 0 0.00 0.00 0 0 0
0
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 2 0.09 0.10 2442 4442 4
2

Oracle Database 11g: SQL Tuning Workshop A - 352


Practice 10-1: Tracing Applications (continued)
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation


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

------- ---------------------------------------------------
0 UPDATE SALES (cr=4441 pr=2442 pw=2442 time=0 us)
2 TABLE ACCESS FULL SALES (cr=4441 pr=2442 pw=2442 time=17
us cost=1366 size=4251 card=109)

Elapsed times include waiting on following events:


Event waited on Times Max. Wait
Total Waited
---------------------------------------- Waited ---------- ---
n s e
---------
i ce
db file scattered read 63 0.00
b l el
0.01
db file sequential read 3 0.00
fer a
0.00
a n s
SQL*Net message to client 1
n -tr
0.00
o
an
0.00
SQL*Net message from client 1 0.00
0.00
h s
a deฺ
)
s Gui
********************************************************************
e
r ฺ
mi dent
************

p ฺ
g Stu
d
b i a@ this
a
(jset se
b i
update salesa to u
amount_sold=30000
where A
i o
nprod_id=13
n t o and cust_id=987

e A
Jos call
rows
count cpu elapsed disk query current

------- ------ -------- ---------- ---------- ---------- ----------


----------
Parse 1 0.00 0.00 0 2 0
0
Execute 1 0.00 0.00 0 3 3
2
Fetch 0 0.00 0.00 0 0 0
0
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 2 0.00 0.00 0 5 3
2

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation

Oracle Database 11g: SQL Tuning Workshop A - 353


Practice 10-1: Tracing Applications (continued)
------- ---------------------------------------------------
0 UPDATE SALES (cr=3 pr=0 pw=0 time=0 us)
2 INDEX RANGE SCAN SALES_PROD_CUST_INDX (cr=3 pr=0 pw=0
time=2 us cost=3 size=78 card=2)(object id 75372)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Elapsed times include waiting on following events:


Event waited on Times Max. Wait
Total Waited
---------------------------------------- Waited ---------- ---
---------
SQL*Net message to client 1 0.00
0.00
SQL*Net message from client 1 0.00
0.00
********************************************************************
n s e
************
ice
b l
25) Interpret the trace generated for case 4 (step 13). What do you observe, and what areel
your conclusions? fe r a
a n s
o n -tr
a) In this case, the first statement does not use the fetching mechanism appropriately.
One fetch operation is done for every single row retrieved. This is also very
an
inefficient. The alternate case is doing 92 fetches to retrieve the same amount of
s
h a deฺ
rows. This technique is called array fetch.
)
r ฺ e s Gui
mi dent
select *
from
p ฺ
g Stu
d
sh.sales where amount_sold>0

b i a@ this
call
a (ja uscpu
count
i
e elapsed disk query current
rows b o
A ------ t-------- ---------- ---------- ---------- ----------
i o
-------
n
nto----------
e A Parse 1 0.02 0.02 0 0 0

J o s 0
Execute 1 0.00 0.00 0 0 0
0
Fetch 918844 5.89 5.93 0 918944 0
918843
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 918846 5.92 5.95 0 918944 0
918843

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182 (recursive depth: 1)

Rows Row Source Operation


------- ---------------------------------------------------
918843 PARTITION RANGE ALL PARTITION: 1 28 (cr=918944 pr=0 pw=0
time=41095 us cost=547 size=26646447 card=918843)
918843 TABLE ACCESS FULL SALES PARTITION: 1 28 (cr=918944 pr=0
pw=0 time=27743 us cost=547 size=26646447 card=918843)

Oracle Database 11g: SQL Tuning Workshop A - 354


Practice 10-1: Tracing Applications (continued)
********************************************************************
************
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

select /* S4 */ *
from
sh.sales where amount_sold>0

call count cpu elapsed disk query current


rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
n s e
Parse 1 0.00 0.00 0 0 0
i ce
0
b l e0 l
Execute
0
1 0.00 0.00 0 0
fe r a
Fetch 92 2.06 2.06 0 1811
a n s 0
918843
o n -tr ----------
a0 n 1811
------- ------ -------- ---------- ---------- ----------
----------
total 94 2.06 2.06
h a s e ฺ 0
918843
s) Gui d
r ฺ e
iparse:en1 t
Misses in library cache during
Optimizer mode: ALL_ROWS g p ฺ m d
Parsing user id: 182@ d S t u
b i a t h is
(recursive depth: 1)

Rows a
Row(jSource se
a to uOperation
------- bi---------------------------------------------------

n i o A PARTITION
918843 RANGE ALL PARTITION: 1 28 (cr=1811 pr=0 pw=0

nto 918843 TABLE ACCESS FULL SALES PARTITION: 1 28 (cr=1811 pr=0 pw=0
time=21132 us cost=547 size=26646447 card=918843)

e A
Jos
time=8617 us cost=547 size=26646447 card=918843)

********************************************************************
************

26) Interpret the trace generated for case 5 (step 14). What do you observe, and what are
your conclusions?
a) Here, the first statement incurs too many recursive calls to allocate extents to the
table. This is because the tablespace in which it is stored is not correctly set up for
extent allocations. The alternate case is much better as you can see the number of
disk values going way down compared to the first case. Also the number of
recursive statements in the second case should be much lower than the first one.
insert into sales2 select * from sh.sales union all select * from
sales

call count cpu elapsed disk query current


rows

Oracle Database 11g: SQL Tuning Workshop A - 355


Practice 10-1: Tracing Applications (continued)
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.01 0 2 0
0
Execute 1 5.02 5.41 4432 28134 148866
1837686
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Fetch 0 0.00 0.00 0 0 0


0
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 2 5.02 5.42 4432 28136 148866
1837686

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182
n s e
i ce
Rows Row Source Operation
b l el
------- ---------------------------------------------------
fe
0 LOAD TABLE CONVENTIONAL (cr=44323 pr=4433 pw=4433 time=0r a
us)
a n s
1837686 -tr
UNION-ALL (cr=6160 pr=4432 pw=4432 time=58107 us)
o n
an
918843 PARTITION RANGE ALL PARTITION: 1 28 (cr=1719 pr=0 pw=0
time=21204 us cost=545 size=26646447 card=918843)
918843
h s
a deฺ
TABLE ACCESS FULL SALES PARTITION: 1 28 (cr=1719 pr=0
)
s Gui
pw=0 time=8395 us cost=545 size=26646447 card=918843)
e
r ฺ
mi dent
918843 TABLE ACCESS FULL SALES (cr=4441 pr=4432 pw=4432

p
g Stuฺ
time=6976 us cost=1369 size=94580484 card=1087132)
d
b i
Elapsed times includea@waiting
t h is on following events:
Event waited
i a (jaon use Times Max. Wait
b
Total Waited
A to
n i o
---------------------------------------- Waited ---------- ---

nto db file scattered read


---------

e A 51 0.00

J o s 0.03
log file switch completion 2 0.00
0.00
log buffer space 8 0.09
0.23
SQL*Net message to client 1 0.00
0.00
SQL*Net message from client 1 0.00
0.00
********************************************************************
************

select file#
from
file$ where ts#=:1

call count cpu elapsed disk query current


rows
------- ------ -------- ---------- ---------- ---------- ----------
----------

Oracle Database 11g: SQL Tuning Workshop A - 356


Practice 10-1: Tracing Applications (continued)
Parse 1812 0.02 0.02 0 0 0
0
Execute 1812 0.02 0.03 0 0 0
0
Fetch 3624 0.05 0.05 0 7242 0
1812
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

------- ------ -------- ---------- ---------- ---------- ----------


----------
total 7248 0.11 0.10 0 7242 0
1812

Misses in library cache during parse: 1


Misses in library cache during execute: 1
Optimizer mode: CHOOSE
Parsing user id: SYS (recursive depth: 1)

n s e
Rows Row Source Operation
i ce
------- ---------------------------------------------------
b l el
1 TABLE ACCESS FULL FILE$ (cr=4 pr=0 pw=0 time=0 us cost=2
size=6 card=1)
fer a
a n s
- tr
********************************************************************
n
o
an
************

h s
a deฺ
e )
s Gui
r ฺ
p ฺ mi dent
d g Stu
b i a@ this
i a (ja use
Ab to
o n io
insert into sales3 select * from sh.sales union all select * from
sales
nt
e A
J o s call count cpu elapsed disk query current
rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 1 0
0
Execute 1 3.64 5.13 1087 22471 77748
1837686
Fetch 0 0.00 0.00 0 0 0
0
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 2 3.65 5.14 1087 22472 77748
1837686

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation

Oracle Database 11g: SQL Tuning Workshop A - 357


Practice 10-1: Tracing Applications (continued)
------- ---------------------------------------------------
0 LOAD TABLE CONVENTIONAL (cr=22544 pr=1087 pw=1087 time=0
us)
1837686 UNION-ALL (cr=6160 pr=1087 pw=1087 time=58277 us)
918843 PARTITION RANGE ALL PARTITION: 1 28 (cr=1719 pr=0 pw=0
time=21371 us cost=545 size=26646447 card=918843)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

918843 TABLE ACCESS FULL SALES PARTITION: 1 28 (cr=1719 pr=0


pw=0 time=8407 us cost=545 size=26646447 card=918843)
918843 TABLE ACCESS FULL SALES (cr=4441 pr=1087 pw=1087
time=7141 us cost=1369 size=94580484 card=1087132)

Elapsed times include waiting on following events:


Event waited on Times Max. Wait
Total Waited
---------------------------------------- Waited ---------- ---
n s e
---------
ice
buffer busy waits 3 0.00
b l el
0.00
undo segment extension 1 0.00
fe ra
0.00
a n s
log file switch completion 5
n - 0.08 tr
o
an
0.12
log buffer space 30 0.15
1.24
h s
a deฺ
db file scattered read
e )
s Gui 25 0.00
r ฺ
mi dent
0.00

p ฺ
log file switch (private strand flush incomplete)
g Stu
d 313 0.05
0.10
b i a@ this
SQL*Net message to client 1 0.00
0.00
i a (ja use
Ab to
SQL*Net message from client 1 0.00

o n io
0.00
********************************************************************
nt
e A ************

J o s 27) Interpret the trace generated for case 6 (step 15). What do you observe, and what are
your conclusions?
a) This case is more difficult to understand. Here, you select a table that is entirely
locked by another transaction. This forces the query to generate consistent read
blocks for almost the entire table causing undo segments to be accessed. This is
materialized by an important disk value, and almost no current blocks.
select cust_id, sum(amount_sold)
from
sales group by cust_id order by cust_id

call count cpu elapsed disk query current


rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 1 0
0

Oracle Database 11g: SQL Tuning Workshop A - 358


Practice 10-1: Tracing Applications (continued)
Execute 1 0.00 0.00 0 0 0
0
Fetch 472 2.38 2.39 5333 978282 0
7059
------- ------ -------- ---------- ---------- ---------- ----------
----------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

total 474 2.39 2.39 5333 978283 0


7059

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182

Rows Row Source Operation


------- ---------------------------------------------------
7059 SORT GROUP BY (cr=978282 pr=5333 pw=5333 time=64 us
n s e
cost=1422 size=28265432 card=1087132)
i ce
918843 TABLE ACCESS FULL SALES (cr=978282 pr=5333 pw=5333
b l el
time=17979 us cost=1369 size=28265432 card=1087132)
fe r a
a n s
Elapsed times include waiting on following events:
n - tr
o
an
Event waited on Times Max. Wait
Total Waited
----------------------------------------
h s
a deฺ
Waited ---------- ---
---------
e )
s Gui
r ฺ
mi dent
SQL*Net message to client 472 0.00
0.00
p ฺ
g Stu
d
db file sequential read 3827 0.00
0.03
b i a@ this
db file scattered read 37 0.00
0.01
i a (ja use
Ab to
latch: cache buffers lru chain 1 0.00

o io
0.00
nSQL*Net message from client 472 0.00
nt
e A 0.02

J o s ********************************************************************
************

28) Interpret the trace generated for case 7 (step 16). What do you observe, and what are
your conclusions?
a) For case 7, you should compare the content in myreport.txt with the content
in myreport2.txt. You should see that an index was added after the first trace
was generated. This situation can cause confusion especially if the trace does not
contain an execution plan to begin with. This is what you can see from within
myreport.txt:
select cust_id, sum(amount_sold)
from
sales where cust_id=2 group by cust_id order by cust_id

call count cpu elapsed disk query current


rows

Oracle Database 11g: SQL Tuning Workshop A - 359


Practice 10-1: Tracing Applications (continued)
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 1 0
0
Execute 1 0.00 0.00 0 0 0
0
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Fetch 1 0.05 0.05 0 4441 0


1
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 3 0.06 0.06 0 4442 0
1

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182 (recursive depth: 1)
n s e
i ce
Rows Row Source Operation
b l el
------- ---------------------------------------------------
fer
1 SORT GROUP BY NOSORT (cr=4441 pr=0 pw=0 time=0 us cost=1366a
size=1872 card=72)
a n s
176 -tr
TABLE ACCESS FULL SALES (cr=4441 pr=0 pw=0 time=153 us
o n
an
cost=1366 size=1872 card=72)

h s
a deฺ
e )
s Gui
r ฺ
i ent
ฺ m
********************************************************************
p
************
d g Stud
i
b) This is what youbseea@ h is
from myreport2.txt:
t
a (ja use
SQL ID : i51xxq509gnbbc
selectA b
cust_id, o
tsum(amount_sold)
i o
tonsales where cust_id=2 group by cust_id order by cust_id
from

A n
o s e
J call count cpu elapsed disk query current
rows
------- ------ -------- ---------- ---------- ---------- ----------
----------
Parse 1 0.00 0.00 0 1 0
0
Execute 2 0.00 0.00 0 0 0
0
Fetch 1 0.05 0.05 0 4441 0
1
------- ------ -------- ---------- ---------- ---------- ----------
----------
total 4 0.06 0.06 0 4442 0
1

Misses in library cache during parse: 1


Optimizer mode: ALL_ROWS
Parsing user id: 182 (TRACE) (recursive depth: 1)

Oracle Database 11g: SQL Tuning Workshop A - 360


Practice 10-1: Tracing Applications (continued)
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT GROUP BY NOSORT (cr=4441 pr=0 pw=0 time=0 us cost=1366
size=1872 card=72)
176 TABLE ACCESS FULL SALES (cr=4441 pr=0 pw=0 time=153 us
cost=1366 size=1872 card=72)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Rows Execution Plan


------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
1 SORT (GROUP BY NOSORT)
176 TABLE ACCESS (BY INDEX ROWID) OF 'SALES' (TABLE)
0 INDEX MODE: ANALYZED (RANGE SCAN) OF 'SALES_CUST_INDX'
(INDEX)

n s e
i ce
Elapsed times include waiting on following events:
b l el
Event waited on
Total Waited
Times Max. Wait
fer a
---------------------------------------- n s
Waited ---------- ---
a
---------
o n -tr
an
SQL*Net message to client 1 0.00
0.00
SQL*Net message from client
h s
a deฺ
1 0.00
0.00
e )
s Gui
r ฺ
mi dent
********************************************************************
************
p ฺ
g Stuby executing the at_cleanup.sh script
29) You can now clean up your d
environment
from your terminal b i a@ this
window.
i a (ja useApplication_Tracing]$ ./at_cleanup.sh
b to
[oracle@edrsr33p1-orcl
A
ni o
n t oSQL*Plus: Release 11.1.0.6.0 - Production on Fri Apr 4 20:45:24 2008

e A Copyright (c) 1982, 2007, Oracle. All rights reserved.


Jos
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL>
SQL> drop user trace cascade;

User dropped.

SQL>
SQL> drop tablespace tracetbs including contents and datafiles;

Tablespace dropped.

SQL>
SQL> drop tablespace tracetbs3 including contents and datafiles;

Oracle Database 11g: SQL Tuning Workshop A - 361


Practice 10-1: Tracing Applications (continued)
Tablespace dropped.

SQL>
SQL> exit;
Disconnected from Oracle Database 11g Enterprise Edition Release
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Application_Tracing]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Application_Tracing
n s e
i ce
export ORACLE_SID=orcl
b l el
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
fer a
a n s
export
o n -tr
an
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
h s
a deฺ
)
s Gui
rm /u01/app/oracle/diag/rdbms/orcl/orcl/trace/*mytrace*.*
e
r ฺ
rm mytrace.trc
p ฺ mi dent
d g Stu
rm myreport.txt
b i a@ this
i a (ja use
rm myreport2.txt

A b to
o
ni $ORACLE_HOME/network/admin/tnsnames.ora.bak1
rm $ORACLE_HOME/network/admin/tnsnames.ora

ntomv
e A
J o s $ORACLE_HOME/network/admin/tnsnames.ora

sqlplus / as sysdba @at_cleanup.sql

--------------------------------------------------------------

set echo on

drop user trace cascade;

drop tablespace tracetbs including contents and datafiles;

drop tablespace tracetbs3 including contents and datafiles;

exit;

Oracle Database 11g: SQL Tuning Workshop A - 362


Practices for Lesson 11
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
b i a@ this
i a (ja use
Ab to
o n io
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 363


Practice 11-1: Proactively Tuning High-Load SQL Statements
In this practice, you use the SQL Tuning Advisor to help you tune problematic SQL
statements.
1) Connect as SYSDBA through Database Control and navigate to the Performance tab
of the Database Control Home page. On the Performance tabbed page, make sure that
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

the View Data field is set to Real Time: 15 second Refresh. After this is done, open a
terminal emulator window connected as the oracle user. When this is done, change
your current directory to your lab directory: cd
$HOME/solutions/SQL_Tuning_Advisor. Then enter the following
command from the OS prompt: ./setup_dina.sh
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./setup_dina.sh
n s e
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./setup_dina.sh
ice
b l el
PL/SQL procedure successfully completed.
fe r a
a n s
Grant succeeded.
n - tr
o
s an
Session altered.
) h a deฺ
r ฺ e s Gui
ฺ i ent
mcompleted.
p
g Stud
PL/SQL procedure successfully
d
a@ this completed.
PL/SQL procedurebisuccessfully

i a (ja use
A b to
i
Usero altered.

nton
e A
J o s User altered.

Index dropped.

drop index sales_time_idx


*
ERROR at line 1:
ORA-01418: specified index does not exist

Index created.

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$

--------------------------------------------------------------

#!/bin/bash

Oracle Database 11g: SQL Tuning Workshop A - 364


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
cd /home/oracle/solutions/SQL_Tuning_Advisor

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

sqlplus -s /NOLOG <<EOF

set echo on

connect / as sysdba
n s e
i ce
exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();
b l el
fer a
grant dba to SH;
a n s
n -tr
rem -- event to allow setting very short Flushing interval
o
s a n forever, level
alter session set events '13508 trace name context
1';
) h a deฺ
r ฺ e s Gui
p
rem -- change INTERVAL setting ฺ mito 2dminutes
e nt
g Sto
rem -- change RETENTIONdsetting tu 6 hours (total of 180 snapshots)
@ his
ia360);
execute dbms_workload_repository.modify_snapshot_settings(interval
=> 2,retentiona=>
j b e t
(
ia to u s
b
n i
rem o--Aplay with ADDM sensitiveness
ntoexec
e A dbms_advisor.set_default_task_parameter('ADDM','DB_ACTIVITY_MIN',30)

J o s ;

alter user sh account unlock;


alter user sh identified by sh;

connect sh/sh

drop index sales_time_bix;


drop index sales_time_idx;
create index sales_time_idx on sales(time_id) compute statistics;

EOF

2) After this is executed, execute the start_dinas.sh script by using the following
command: ./start_dinas.sh
This script starts the workload used for this lab.

Oracle Database 11g: SQL Tuning Workshop A - 365


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./start_dinas.sh
Started stream with pid=30479
Started stream with pid=30480
Started stream with pid=30481
Started stream with pid=30482
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Started stream with pid=30485


Started stream with pid=30486
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/SQL_Tuning_Advisor
n s e
i ce
el
export ORACLE_SID=orcl

a b l
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
fer
a n s
export
n -tr
o
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
an
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
s
) h a deฺ
STREAM_NUM=0
r ฺ e s Gui
MAX_STREAM=6
p ฺ mi dent
PIDLST=""
d g Stu
while [ $STREAM_NUM
b i a@ h is
-lt $MAX_STREAM
t ]; do

i
# one morea (ja use
let A b to
STREAM_NUM="STREAM_NUM+1"
o
nt oni# start one more stream
eA sqlplus -S sh/sh @dina.sql &

Jos # remember PID


PIDLST="$! $PIDLST"

echo "Started stream with pid=$!"

done

#
# Save PID List
#
echo $PIDLST > /tmp/dina_pids

--------------------------------------------------------------

DECLARE
n number;
BEGIN
for i in 1..1000 loop
select /*+ ORDERED USE_NL(c) FULL(c) FULL(s)*/ count(*) into n
from sales s, customers c

Oracle Database 11g: SQL Tuning Workshop A - 366


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
where c.cust_id = s.cust_id and CUST_FIRST_NAME='Dina'
order by time_id;
end loop;
END;
/
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3) When the start_dinas.sh script completes, observe the Performance tabbed


page for around 10 minutes (5 snapshot icons). What are your conclusions?
a) You should see that the workload activity goes up very quickly. Because the CPU
used by the workload is very close to the maximum CPU available on your
system, there must be an issue with this workload. Because the most important
area corresponding to a wait class is the Other wait class, the issue must be
n s e
associated to that class. Note that the snapshot interval is now around two ice
minutes.
b l el
4) Fix the problem. fe r a
a n s
o n -tr
a) The fastest way to determine the problem is by looking at an Automatic Database
Diagnostic Monitor (ADDM) report analysis executed during the problematic
an
period. Then, by following its analysis, ADDM should guide you through the
s
process of fixing the problem. ) h a deฺ
b) Using the Database Control Home ipage, r ฺesthere G ui different ways to identify
ฺm udent
are two
the correct ADDM analysisgtask: p t
d to theSproblematic
i @
i) If the time corresponding
a detected h i s time period corresponds with the
j
latest ADDM a b run e t by Database Control, you should find the link
( u s
A bia section
corresponding
Summary
toto the
of
correct performance analysis directly in the Diagnostic
the Database Control Home page. Note that you should
n i o wait around 8 to 10 minutes
Anto before the Diagnostic Summary section is
refreshed with the correct ADDM analysis. If you are in this case, click the
e
Jos link corresponding to the number of findings right next to the ADDM
Findings row. This takes you to the corresponding Automatic Database
Diagnostic Monitor (ADDM) page.
ii) If not, you should open the Advisor Central page and search for the correct
ADDM task. This is how you can retrieve the task from the Advisor Central
page:
(1) On the Database Control Home page, click the Advisor Central link.
(2) On the Advisor Central page, in the search section, select ADDM from
the Advisory Type drop-down list, and Last 24 Hours from the
Advisor Runs drop-down list.
(3) After this is done, click Go.
(4) Then select the ADDM task corresponding to the time of the
problematic period.

Oracle Database 11g: SQL Tuning Workshop A - 367


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
(5) This takes you to the corresponding Automatic Database Diagnostic
Monitor (ADDM) page.
c) On the Automatic Database Diagnostic Monitor (ADDM) page, you should see
two main findings: CPU Usage and Top SQL by DB Time. Both should be close
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

to 100%. If they are not, ensure that what you see is the latest ADDM analysis.
d) Click the “Top SQL by DB Time” link.
e) On the “Performance Finding Details: Top SQL by DB Time” page, click Show
All Details link. You should see something similar to the following: Run SQL
Tuning Advisor on the SQL statement with SQL_ID "5mxdwvuf9j3vp". Next to
this recommendation, click the Run Advisor Now button.
f) Wait on the Processing: SQL Tuning Advisor Task SQL_TUNING_… page for a n s e
ice
while.
b l el
g) You are automatically directed to the Recommendations for SQL
fe r a
n
ID:5mxdwvuf9j3vp” page from where you should see two recommendations-one
a s
for a SQL Profile and one for an index creation.
o n -tr
h) You can investigate the consequence of implementing
s a nthe recommended profile
by comparing execution plans before and after
) h i eฺ
aprofiledimplementation. You can
do so by clicking the “Compare explain
SQL Profile row. Because the potential i r ฺesbenefit
n t of
u icon for the corresponding
plan” eyeglass
G using the proposed SQL profile
p ฺ m d e
g Costs),
is very high (see both computed
d S tu you implement the SQL profile.
i
i) On the Compare Explain
b a@ Planst h ispage, click the Recommendations for SQL
(ja ulocator
ID:5mxdwvuf9j3vp
i a se link.
j) Back A to
bto the Recommendations for SQL ID:5mxdwvuf9j3vp page, ensure that the
n i o
SQL Profile row is selected, and click Implement.
n t o
e A k) On Confirmation page, click Yes.
Jos l) On Recommendations for SQL ID:5mxdwvuf9j3vp page, you should see the
following message at its top: “Confirmation: The recommended SQL Profile has
been created successfully”.
m) Click Return.
n) On SQL Tuning Results:TASK, click the Database Instance: orcl locator link.
o) Back to the Database Home Page, click the Performance tab.
5) After following the SQL Tuning Advisor recommendation to implement a SQL
Profile, how can you quickly verify that the problem is solved?
a) On the Performance tabbed page, you expect to see a dramatic drop for the Other
wait class in the Average Active Sessions graph. However, when you view the
graph you see that not has changed.
6) How do you interpret the result you see, and how would you ensure that the new
profile is taken into account?

Oracle Database 11g: SQL Tuning Workshop A - 368


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
a) A profile is taken into account the next time you execute the corresponding
statement. Because the SQL statement for which you created a profile takes a long
time to execute, you should wait for a long time to see the benefit. To quickly see
the benefit, stop and restart your workload. This executes the same SQL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

statements again, and the profile should be used automatically this time.
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./stop_dinas.sh
Killing stream with pid=30486
DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

n s e
Killing stream with pid=30485
ice
DECLARE
b l el
*
fe ra
ERROR at line 1:
ORA-00028: your session has been killed
a n s
n - tr
o
Killing stream with pid=30482
s an
DECLARE
) h a deฺ
*
r ฺ e s Gui
mi killed nt
ERROR at line 1:
ORA-00028: your session has ฺbeen e
d p
g Stu d
b i a@ t h is
(ja use
Killing stream with pid=30481
DECLARE
b i a to
*
o A
ni
ERROR at line 1:

ntoORA-00028: your session has been killed

e A
J o s Killing stream with pid=30480
DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

Killing stream with pid=30479


DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./start_dinas.sh


Started stream with pid=31731

Oracle Database 11g: SQL Tuning Workshop A - 369


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Started stream with pid=31732
Started stream with pid=31733
Started stream with pid=31734
Started stream with pid=31735
Started stream with pid=31740
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/SQL_Tuning_Advisor

export ORACLE_SID=orcl
n s e
i ce
el
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

a b l
export
fer
a n s
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

-tr
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

o n
PIDLST=`cat /tmp/dina_pids`
s an
) h a deฺ
#
r ฺ e s Gui
# Kill all these processes
# p ฺ mi dent
for PID in $PIDLST; do dg tu
S
b ia@@kill_dina.sql
echo "Killing stream
sqlplus / asasysdba
withis
t h pid=$PID"

sqlplus a ( u s e
j @/tmp/drop_dina.sql $PID >> /tmp/stop_dina.log 2>&1

b i $PID >>to/tmp/stop_dina.log >>


/nolog /tmp/stop_dina.log 2>&1

oA
kill -9 2>&1
n i
done
nto
e A --------------------------------------------------------------

J o s set head off


set timing off
set feedback off;
set pagesize 0
set verify off

spool /tmp/drop_dina.sql;

select 'connect / as sysdba;' from dual;

select 'alter system kill session ''' || sid || ',' || serial# ||


''';'
from v$session
where process=&1;

select 'exit;' from dual;

spool off

exit;

Oracle Database 11g: SQL Tuning Workshop A - 370


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
b) Go back to the Performance tabbed page.
c) On the Performance page, you should now see the benefit of the SQL Profile.
There is no longer any Other waits in the Average Active Sessions graph.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7) How would you make sure that the SQL Profile was implemented?
a) On the Average Active Sessions graph, click the Other wait category in the
caption.
b) On the Active Sessions Waiting: Other page, you should see that your statement
waits for the latch free event. You can see that from the ACTIVITY(%) column.
c) Click the 5mxdwvuf9j3vp SQL Id link in the Top SQL table.
d) On the SQL Details: 5mxdwvuf9j3vp page, click the Plan Control tab in the n s e
i ce
Details section.
b l el
r
e) You should see that this statement is associated to a SQL Profile that was
fe a
n s
manually implemented. It is part of the DEFAULT category and is ENABLED.
a
n -tr
8) Clean up your environment by executing the following commands from your
o
command-line window:
s an
./stop_dians.sh
) h a deฺ
./cleanup_dina.sh
r ฺ e s Gui
p ฺ mi dent ./stop_dinas.sh
[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$
d g Stu
Killing stream with pid=31740
DECLARE
b i a@ this
(ja1: use
*

b
ORA-00028:i a
ERROR at line
your o
tsession has been killed
A
io stream with pid=31735
t o n
Killing
A n DECLARE

o s e *
J ERROR at line 1:
ORA-00028: your session has been killed

Killing stream with pid=31734


DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

Killing stream with pid=31733


DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

Killing stream with pid=31732


DECLARE
*
ERROR at line 1:
ORA-00028: your session has been killed

Oracle Database 11g: SQL Tuning Workshop A - 371


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Killing stream with pid=31731
DECLARE
*
ERROR at line 1:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

ORA-00028: your session has been killed

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$

[oracle@edrsr33p1-orcl SQL_Tuning_Advisor]$ ./cleanup_dina.sh

Revoke succeeded.
n s e
i ce
el
specify password for SH as parameter 1:

a b l
specify default tablespace for SH as parameter 2:
fer
a n s
-tr
specify temporary tablespace for SH as parameter 3:

o n
specify password for SYS as parameter 4:
s an
specify directory path for the data files
) h i d eฺ 5:
a as parameter
i r es files
ฺlog t G u
ฺm uden
writeable directory path for the as parameter 6:

g p
d 7:St
specify version as parameter
i a @ h is
b t
i a (ja use
Session altered.
A b to
o
ni dropped.
ntoUser
e A
J o s old
new
1: CREATE USER sh IDENTIFIED BY &pass
1: CREATE USER sh IDENTIFIED BY sh

User created.

old 1: ALTER USER sh DEFAULT TABLESPACE &tbs


new 1: ALTER USER sh DEFAULT TABLESPACE example
old 2: QUOTA UNLIMITED ON &tbs
new 2: QUOTA UNLIMITED ON example

User altered.

old 1: ALTER USER sh TEMPORARY TABLESPACE &ttbs


new 1: ALTER USER sh TEMPORARY TABLESPACE temp

User altered.

Grant succeeded.

Oracle Database 11g: SQL Tuning Workshop A - 372


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Grant succeeded.

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

Grant succeeded.

Grant succeeded.

PL/SQL procedure successfully completed.

Connected.
n s e
i ce
Grant succeeded.
b l el
fer a
old
s
1: CREATE OR REPLACE DIRECTORY data_file_dir AS '&data_dir'
a n
-tr
new 1: CREATE OR REPLACE DIRECTORY data_file_dir AS
n
'/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/'
o
Directory created.
s an
) h a deฺ
old 1: CREATE OR REPLACE DIRECTORY
r ฺ e s Gui AS '&log_dir'
log_file_dir
new i elog_file_dir
1: CREATE OR REPLACE DIRECTORY
ฺ m n t AS '/home/oracle/'

Directory created. d gp Stud


b i a@ this
i a (ja use
Grant succeeded.
A b to
o
ni succeeded.
ntoGrant
e A
J o s Grant succeeded.

Connected.

Session altered.

Session altered.

Table created.

Table created.

...

Table created.

Oracle Database 11g: SQL Tuning Workshop A - 373


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)

Creating constraints ...

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

...

Table altered.

specify password for SH as parameter 1:


n s e
i ce
el
specify path for data files as parameter 2:

a b l
specify path for log files as parameter 3:
fer
a n s
-tr
specify version as parameter 4:

o n
an
Looking for indexes that could slow down load ...
s
no rows selected
) h a deฺ
r ฺ e s Gui
loading TIMES using: p ฺ mi dent
d g Stu
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/time_v
3.ctl
b i a@ this
3.dat
i a (ja use
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/time_v

A b to
/home/oracle/time_v3.log
ni o
ntoSQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:23

e A 2008

J o s Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 1000.

Load completed - logical record count 1826.

loading COUNTRIES using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/coun_v
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/coun_v
3.dat
/home/oracle/coun_v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:24


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop A - 374


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Load completed - logical record count 23.

loading CUSTOMERS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/cust_v
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/cust1v
3.dat
/home/oracle/cust1v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:24


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
i ce
el
Save data point reached - logical record count 10000.
Save data point reached - logical record count 20000.
a b l
Save data point reached - logical record count 30000.
fer
Save data point reached - logical record count 40000.
a n s
-tr
Save data point reached - logical record count 50000.

o n
Load completed - logical record count 55500.
s an
) h a deฺ
loading PRODUCTS using:
r ฺ e s Gui
3.ctl p ฺ mi dent
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prod_v

d g Stu
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prod1v
3.dat
b i a@ this
i a (ja use
/home/oracle/prod1v3.log

A
SQL*Loader: to 11.1.0.6.0 - Production on Thu Mar 20 15:22:24
b Release
i o
ton
2008

A n Copyright (c) 1982, 2007, Oracle. All rights reserved.


o s e
J
Load completed - logical record count 72.

loading PROMOTIONS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prom_v
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prom1v
3.dat
/home/oracle/prom1v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:25


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 10.


Save data point reached - logical record count 20.
...
Save data point reached - logical record count 500.

Oracle Database 11g: SQL Tuning Workshop A - 375


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Load completed - logical record count 503.

loading CHANNELS using:


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

/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/chan_v
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/chan_v
3.dat
/home/oracle/chan_v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:25


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
i ce
b l el
Load completed - logical record count 5.
fer a
a n s
loading SALES using:
o n -tr
3.ctl
s an
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/sale_v

h a deฺ
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/sale1v
)
3.dat
r ฺ e s Gui
/home/oracle/sale1v3.log
p ฺ mi dent
d g S-tuProduction on Thu Mar 20 15:22:25
SQL*Loader: Release 11.1.0.6.0
2008
b i a@ this
Copyright a
i (ja1982,u2007,
(c) se Oracle. All rights reserved.
A b o
treached
n i
Save o data point - logical record count 100000.
nt o...

e A Save data point reached - logical record count 900000.

J o s Load completed - logical record count 916039.

loading COSTS using external table

Table created.

82112 rows created.

loading additonal SALES using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dmsal_
v3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dmsal_
v3.dat
/home/oracle/dmsal_v3.log

Oracle Database 11g: SQL Tuning Workshop A - 376


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:38
2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


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

Save data point reached - logical record count 100.


...
Save data point reached - logical record count 2800.

Load completed - logical record count 2804.

loading SUPPLEMENTARY DEMOGRAPHICS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dem_v3
.ctl
n s e
i ce
el
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dem1v3
.dat
a b l
er
/home/oracle/dem1v3.log
s f
n 15:22:38
SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar a
- t r 20
2008
n on
Copyright (c) 1982, 2007, Oracle. All rights
s a reserved.
) h a
i d eฺ
... i r ฺes nt Gu
Save data point reached - logical record count 10.

Save data point reached - p


g ฺm urecord
logical de count 4500.
d t
Scount 4500.
i a @ record
i s
ab se th
Load completed - logical

( j
b ia to u
oA
Commit complete.

o n i
nt
eA Enabling constraints ...

Jos Table altered.

...

Table altered.

Creating additional indexes ...

Index created.

...

Index created.

Oracle Database 11g: SQL Tuning Workshop A - 377


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Create dimensions ...

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

Commit complete.

PL/SQL procedure successfully completed.

no rows selected

Dimension created.
n s e
i ce
b l el
PL/SQL procedure successfully completed.
fer a
a n s
no rows selected n -tr
o
s an
Dimension created.
) h a deฺ
r ฺ e s Gui
ฺ i ent
mcompleted.
p
g Stud
PL/SQL procedure successfully
d
b i a@ this
i a (ja use
no rows selected

A b to
i o
ton
Dimension created.

A n
o s e PL/SQL procedure successfully completed.
J
no rows selected

Dimension created.

PL/SQL procedure successfully completed.

no rows selected

Creating MVs as tables ...

View created.

Table created.

Oracle Database 11g: SQL Tuning Workshop A - 378


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)

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

Index created.

Index created.

Index created.

Index created.
n s e
i ce
Creating materialized views ...
b l el
fer a
a n s
Materialized view created.
n -tr
o
Materialized view created.
s an
) h a deฺ
r ฺ e s Gui
Creating comments ...
p ฺ mi dent
Comment created. d g Stu
b i a@ this
...
i a (ja use
A b to
ni o
ntoComment created.

e A
J o s Creating OLAP metadata ...
<<<<< CREATE CWMLite Metadata for the Sales History Schema >>>>>
-
<<<<< CREATE CATALOG sh_cat for Sales History >>>>>
Catalog Dropped
CWM Collect Garbage
-
<<<<< CREATE the Sales CUBE >>>>>
Sales amount, Sales quantity
<TIMES CHANNELS PRODUCTS CUSTOMERS PROMOTIONS >
Drop SALES_CUBE prior to recreation
Cube Dropped
Add dimensions -
to SALES_CUBE and map the foreign keys
Create measures -
for SALES_CUBE and map to columns in the fact table
Set default aggregation method -
to SUM for all measures over TIME
Add SALES_CUBE to the catalog
SALES_CUBE successfully added to sh_cat

Oracle Database 11g: SQL Tuning Workshop A - 379


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
-
<<<<< CREATE the Cost CUBE >>>>>
Unit Cost, Unit Price < TIMES PRODUCTS CHANNELS PROMOTIONS >
Drop COST_CUBE prior to recreation
Cube Dropped
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Add dimensions -
to COST_CUBE and map the foreign keys
Create measures -
for COST_CUBE and map to columns in the fact table
Set default aggregation method -
to SUM for all measures over TIME
Add COST_CUBE to the catalog
COST_CUBE successfully added to sh_cat
-
<<<<< TIME DIMENSION >>>>>
n s e
i ce
el
Dimension - display name, description and plural name
Level - display name and description
a b l
Hierarchy - display name and description
fer
- default calculation hierarchy
a n s
- default display hierarchy
Level Attributes - name, display name, description n - tr
o
Drop dimension attributes prior to re-creation
s an
Create dimension attributes and add their level attributes
- Long Description created
) h a deฺ
- Short Description created
r ฺ e s Gui
p ฺ mi dent
- Period Number of Days created
- Period End Date created
d g Stu
Classify entity descriptor use
i a@ this
- Time dimension
b
i a (ja use
- Long description
- Day name
Ab to
- Calendar month description

o n io - Calendar quarter description


nt - Fiscal month description

e A - Fiscal quarter description

J o s - Short Description
- Day name
- Calendar month description
- Calendar quarter description
- Fiscal month description
- Fiscal quarter description
- Time Span
- Days in calendar month
- Days in calendar quarter
- Days in calendar year
- Days in fiscal month
- Days in fiscal quarter
- Days in fiscal year
- End Date
- End of calendar month
- End of calendar quarter
- End of calendar year
- End of fiscal month
- End of fiscal quarter
- End of fiscal year
-

Oracle Database 11g: SQL Tuning Workshop A - 380


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
<<<<< CUSTOMERS DIMENSION >>>>>
Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

- default display hierarchy


Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
n s e
i ce
el
No attribute to drop
No attribute to drop
a b l
No attribute to drop
fer
No attribute to drop
a n s
No attribute to drop
n - tr
Create dimension attributes and add their level attributes
o
- Long Description created
- Short Description created
s an
h
- Other Customer Information created
) a deฺ
Classify entity descriptor use
r ฺ e s Gui
- Long Description
- Short Descriptionp ฺ mi dent
- d g Stu
i a@ this
<<<<< PRODUCTS DIMENSION >>>>>
b
i a (ja use
Dimension - display name, description and plural name
Level - display name and description
Ab to
Hierarchy - display name and description

o n io - default calculation hierarchy


nt - default display hierarchy

e A Level Attributes - name, display name, description

J o s Drop dimension attributes prior to re-creation


No attribute to drop
Create dimension attributes and add their level attributes
- Long Description created
- Short Description created
Classify entity descriptor use
- Long Description
- Short Description
-
<<<<< PROMOTIONS DIMENSION >>>>>
Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
- default display hierarchy
Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
No attribute to drop
Create dimension attributes and add their level attributes
- Long Description created
- Short Description created

Oracle Database 11g: SQL Tuning Workshop A - 381


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
Classify entity descriptor use
- Long Description
- Short Description
-
<<<<< CHANNELS DIMENSION >>>>>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Dimension - display name, description and plural name


Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
- default display hierarchy
Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
No attribute to drop
Create dimension attributes and add their level attributes
- Long Description created
n s e
i ce
el
- Short Description created
Classify entity descriptor use
a b l
- Long Description
fer
- Short Description
a n s
-
<<<<< FINAL PROCESSING >>>>> n -tr
o
- Changes have been committed
s an
PL/SQL procedure successfully completed.
) h a deฺ
r ฺ e s Gui
Commit complete. p ฺ mi dent
d g Stu
b i a@... this
i a (ja use
gathering statistics

A b
PL/SQL procedure tosuccessfully completed.
i o
nton
e A PL/SQL procedure successfully completed.

J o s SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/SQL_Tuning_Advisor

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin

Oracle Database 11g: SQL Tuning Workshop A - 382


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)

#
# Cleanup ADDM snapshot settings
#
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

sqlplus -s /NOLOG <<EOF >> /tmp/cleanup_dina.log 2>&1

connect / as sysdba

rem -- change INTERVAL setting to 30 minute


execute dbms_workload_repository.modify_snapshot_settings(interval
=> 60);

rem -- change ADDM sensitiveness back to normal


exec
n s e
i ce
el
dbms_advisor.set_default_task_parameter('ADDM','DB_ACTIVITY_MIN',300
);
a b l
fer
connect sh/sh
a n s
drop index sales_time_idx; n - tr
o
create bitmap index sales_time_bix
s an
on sales(time_id)
) h a deฺ
tablespace example
r ฺ e s Gui
p ฺ mi dent
local nologging compute statistics;

d g Stu
EOF
b i a@ this
#
i a (ja use
# Cleanup
# io
to
Ab sql profile
n
tosqlplus -s /NOLOG <<EOF > /tmp/cleanup_dina.log 2>&1
A n
o s e connect / as sysdba
J
set head off
set timing off
set feedback off;
set pagesize 0

spool /tmp/drop_dyn.sql;

select q'#connect / as sysdba;#' from dual;

select q'#execute dbms_sqltune.drop_sql_profile('#' || name || q'#')


;#'
from dba_sql_profiles ;

select q'#execute dbms_advisor.delete_task('#' || task_name || q'#')


;#'
from user_advisor_tasks
where CREATED > SYSDATE-(1/24);

select q'#connect system/oracle;#' from dual;

Oracle Database 11g: SQL Tuning Workshop A - 383


Practice 11-1: Proactively Tuning High-Load SQL Statements
(continued)
select q'#execute dbms_advisor.delete_task('#' || task_name || q'#')
;#'
from user_advisor_tasks
where CREATED > SYSDATE-(1/24);
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

spool off

@/tmp/drop_dyn.sql

EOF

cp /home/oracle/solutions/SQL_Access_Advisor/sh/*
$ORACLE_HOME/demo/schema/sales_history
n s e
i ce
cd /home/oracle/solutions/SQL_Access_Advisor/sh
b l el
fer a
sqlplus -s /NOLOG <<EOF
a n s
set echo on
o n -tr
connect / as sysdba
s an
) h a deฺ
revoke dba from sh;
r ฺ e s Gui
p ฺ
@sh_main sh example temp oraclemi dent
d g Stu
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/
/home/oracle/ v3 a@ is
b i t h
EOF
i a (ja use
A b to
i o
nton
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 384


Practice 11-2: Using SQL Access Advisor
The following scenario illustrates the types of recommendations that can be made by
SQL Access Advisor. The scenario also uses the SQL Performance Analyzer to prove
that recommendations made by SQL Access Advisor are good.
1) From a terminal session connected as the oracle user, execute the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

sqlaccessadv_setup.sh script. This script generates the necessary data that


you use throughout this lab. In particular, it generates the SQL Tuning Set that is used
to represent the workload you want to analyze.
./sqlaccessadv_setup.sh

[oracle@edrsr33p1-orcl SQL_Access_Advisor]$ ./sqlaccessadv_setup.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 21:14:49 n s e


ice
el
2008

a b l
Copyright (c) 1982, 2007, Oracle. All rights reserved.
fe r
a n s
Connected to:
o n -tr
an
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
s
Production
) h a deฺ
s Gui
With the Partitioning, Oracle Label Security, OLAP, Data Mining

and Real Application Testing options
r
i ente
p ฺ mSQL>
SQL> SQL> d g StudSQL> SQL> SQL> SQL> Connected.
SQL> SQL> SQL> SQL> SQL> SQL>

Grant succeeded. ia@ is


b
ja use t h
SQL> SQL> a (
b i to
A
User altered.
o
ni SQL> Connected.
toSQL>
A n SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> 2 3 4 5
o s e 6 7 8 9 10 11 12 13 14 15 16 17 18 19
J 20 21 22 23 24
PL/SQL procedure successfully completed.

SQL> SQL> SQL> SQL> SQL> SQL> SQL> DROP TABLE temp_table purge
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
Table created.

SQL> SQL> SQL> SQL> SQL> SQL> SQL>


System altered.

SQL> BEGIN dbms_sqltune.drop_sqlset('SQLSET_MY_SQLACCESS_WORKLOAD');


END;

*
ERROR at line 1:

Oracle Database 11g: SQL Tuning Workshop A - 385


Practice 11-2: Using SQL Access Advisor (continued)
ORA-13754: "SQL Tuning Set" "SQLSET_MY_SQLACCESS_WORKLOAD" does not
exist for user "SH".
ORA-06512: at "SYS.DBMS_SQLTUNE_INTERNAL", line 8406
ORA-06512: at "SYS.DBMS_SQLTUNE", line 2949
ORA-06512: at line 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SQL> drop table tempjfv purge


*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> SQL>
Table created.

n s e
SQL> SQL> SQL> 2 3 4 5 6 7
i ce
PL/SQL procedure successfully completed.
b l el
SQL> SQL> drop table customersjfv purge
fer a
*
a n s
ERROR at line 1:
o n -tr
an
ORA-00942: table or view does not exist

h s
a deฺ
SQL>
e )
s Gui
Table created. r ฺ
i ent
p
3 g 4
ฺ m 5ud 6
SQL> SQL> SQL> 2
16 @ d S t 20 217 228 239 2410 11 12
13
27
14
28
15
29 b
30i
17 18
a 31 t32
h is 33 34 35 36 37 38
19 25
39
26
40
41 42 43(ja44 se 46 47
45 48 49
b i a
PL/SQL procedure
to u
successfully completed.
o A
t o niCOUNT(*)
SQL> SQL> SQL>
An
e
Jos
----------
5

1 row selected.

SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL>


PL/SQL procedure successfully completed.

SQL>
PL/SQL procedure successfully completed.

SQL> SQL>
PL/SQL procedure successfully completed.

SQL> SQL>
PL/SQL procedure successfully completed.

SQL> SQL>
LAST_ANAL
---------
18-MAR-08

Oracle Database 11g: SQL Tuning Workshop A - 386


Practice 11-2: Using SQL Access Advisor (continued)
18-MAR-08
18-MAR-08
18-MAR-08
22-AUG-07
18-MAR-08
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

18-MAR-08
18-MAR-08
18-MAR-08
18-MAR-08
18-MAR-08
18-MAR-08
18-MAR-08

14 rows selected.

n s e
SQL> SQL> SQL> SQL> Disconnected from Oracle Database 11g Enterprise
i ce
Edition Release 11.1.0.6.0 - Production
b l el
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
fer a
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$
a n s
o n -tr
an
--------------------------------------------------------------

#!/bin/bash
h s
a deฺ
e )
s Gui
r ฺ
i ent
cd /home/oracle/solutions/SQL_Access_Advisor

p ฺ m
export ORACLE_SID=orcl g
d S tud
b i a@ this
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export bia
(ja use
A to
ni o
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

ntol/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
e A
J o s sqlplus / as sysdba <<FIN!

SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 8000
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
SET LONG 1000

CONNECT / AS SYSDBA

grant dba to sh;

alter user sh identified by sh account unlock;

connect sh/sh

set serveroutput on size 32768;


set echo on;

Oracle Database 11g: SQL Tuning Workshop A - 387


Practice 11-2: Using SQL Access Advisor (continued)
variable norecs number;

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

declare
name varchar2(30);
cursor name_cur1 is
select task_name from user_advisor_templates
where task_name like '%SQLACCESS%';
begin
----------------------------------------------------------------
-----------
-- Get rid of templates, tasks and workloads.
----------------------------------------------------------------
n s e
-----------
i ce
b l el
open name_cur1;
fer a
loop
a n s
fetch name_cur1 into name;
o n -tr
an
exit when name_cur1%NOTFOUND;

h s
a deฺ
)
s Gui
dbms_advisor.update_task_attributes(name,null,null,'FALSE','FALSE');
e
r ฺ
mi dent
dbms_advisor.delete_task(name);
end loop;
p
g Stuฺ
close name_cur1;@d
b i a t h is
end;
i a (ja use
/
A b to
i o
nton
e A
J o s Rem make a temp table

DROP TABLE temp_table purge;


CREATE TABLE temp_table AS SELECT * FROM SYS.WRI\$_ADV_SQLW_STMTS
WHERE NULL IS NOT NULL;

Rem create a large number of pseudo-random (repeatable) queries in


the temporary table

alter system flush shared_pool;


execute dbms_sqltune.drop_sqlset('SQLSET_MY_SQLACCESS_WORKLOAD');

drop table tempjfv purge;

create table tempjfv (c number, d varchar2(1000));

Oracle Database 11g: SQL Tuning Workshop A - 388


Practice 11-2: Using SQL Access Advisor (continued)
begin
for i in 1..20000 loop
insert into tempjfv values(-
i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
end loop;
commit;
end;
/

n s e
drop table customersjfv purge;
i ce
create table customersjfv as select * from customers;
b l el
fe r a
DECLARE
a n s
sql_stmt varchar2(2000);
o n -tr
an
sqlsetname VARCHAR2(30);
sqlsetcur dbms_sqltune.sqlset_cursor;
refid NUMBER;
h s
a deฺ
k NUMBER := 0;
e )
s Gui
r ฺ
mi dent
num_queries NUMBER := 500;
BEGIN
p ฺ
g S2tu*/ ch.channel_class, c.cust_city,
sql_stmt := 'SELECT @ /* d
b i
t.calendar_quarter_desc,a t h is
QueryJFV
SUM(s.amount_sold) sales_amount FROM
sh.sales s, ( j a
sh.times e
sAND s.cust_id = c.cust_id AND ch
t, sh.customers c, sh.channels WHERE
s.time_idb i a
= t.time_id u
toc.cust_state_province = ''CA'' AND ch.channel_desc
s.channel_id =
A
io(''Internet'',''Catalog'') AND t.calendar_quarter_desc IN
ch.channel_id AND
o n
in
t (''1999-01'',''1999-02'') GROUP BY ch.channel_class, c.cust_city,
A n
o s e t.calendar_quarter_desc';
J insert into temp_table values(1,1,NULL,0,'SH','Access
Advisor','Workload',0,0,0,0,1,100,2,to_date('02-FEB-
2007'),3,0,sql_stmt,1);

sql_stmt := 'SELECT /* QueryJFV 3 */ ch.channel_class, c.cust_city,


t.calendar_quarter_desc, SUM(s.amount_sold) sales_amount FROM
sh.sales s, sh.times t, sh.customers c, sh.channels ch WHERE
s.time_id = t.time_id AND s.cust_id = c.cust_id AND s.channel_id =
ch.channel_id AND c.cust_state_province = ''CA'' AND ch.channel_desc
in (''Internet'',''Catalog'') AND t.calendar_quarter_desc IN
(''1999-03'',''1999-04'') GROUP BY ch.channel_class, c.cust_city,
t.calendar_quarter_desc';

insert into temp_table values(1,1,NULL,0,'SH','Access


Advisor','Workload',0,0,0,0,1,100,2,to_date('02-FEB-
2007'),3,0,sql_stmt,1);

Oracle Database 11g: SQL Tuning Workshop A - 389


Practice 11-2: Using SQL Access Advisor (continued)
sql_stmt := 'SELECT /* QueryJFV 4 */ c.country_id, c.cust_city,
c.cust_last_name FROM sh.customers c WHERE c.country_id in (52790,
52798) ORDER BY c.country_id, c.cust_city, c.cust_last_name';

insert into temp_table values(1,1,NULL,0,'SH','Access


Advisor','Workload',0,0,0,0,1,100,2,to_date('02-FEB-
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2007'),3,0,sql_stmt,1);

sql_stmt := 'select /* func_indx */ count(*) from tempjfv where


abs(c)=5';

insert into temp_table values(1,1,NULL,0,'SH','Access


Advisor','Workload',0,0,0,0,1,100,2,to_date('02-FEB-
2007'),3,0,sql_stmt,1);

n s e
sql_stmt := 'SELECT /* QueryJFV 5 */ * FROM sh.customersjfv WHERE
i ce
cust_state_province = ''CA''';
b l el
insert into temp_table values(1,1,NULL,0,'SH','Access
fer a
Advisor','Workload',0,0,0,0,1,100,2,to_date('02-FEB-
a n s
2007'),3,0,sql_stmt,1);
o n -tr
s an
h
sqlsetname := 'SQLSET_MY_SQLACCESS_WORKLOAD';
) a deฺ
r ฺ e G ui
s 'Generated
m i
dbms_sqltune.create_sqlset(sqlsetname,
n t STS');

OPEN sqlsetcur FOR


d g pฺ tude
SELECT
i a @ his Ssql_text, null, null, username,
module, ( j ab se t
SQLSET_ROW(null,null,

b ia to u action, elapsed_time, cpu_time, buffer_gets,


i oA
disk_reads,
n
ntooptimizer_cost, null,0,rows_processed, 0, executions, 0,
e A
J o s priority, command_type,
to_char(last_execution_date,'yyyy-mm-
dd/hh24:mi:ss'),
0,0,NULL,0,NULL,NULL
)
FROM temp_table;

dbms_sqltune.load_sqlset(sqlsetname, sqlsetcur);
END;
/

SELECT COUNT(*) FROM


TABLE(DBMS_SQLTUNE.SELECT_SQLSET('SQLSET_MY_SQLACCESS_WORKLOAD'));

Rem Cleanup anything left behind

Oracle Database 11g: SQL Tuning Workshop A - 390


Practice 11-2: Using SQL Access Advisor (continued)
execute dbms_advisor.delete_task('%');
execute dbms_advisor.delete_sqlwkld('%');

EXECUTE DBMS_STATS.UNLOCK_SCHEMA_STATS('SH');

execute dbms_stats.gather_schema_stats(ownname => 'SH',


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

estimate_percent=> DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => 'FOR


ALL COLUMNS SIZE AUTO', degree => 4 );

select distinct last_analyzed from dba_tab_statistics where


owner='SH';

REM EXECUTE DBMS_STATS.LOCK_SCHEMA_STATS('SH');

exit;

n s e
FIN!
ice
b l el
fe
2) Using Enterprise Manager, create a SQL Access Advisor tuning task based on the ra
captured workload held in the SH.SQLSET_MY_ACCESS_WORKLOAD SQL a n s
tuning set using the SQLACCESS_WAREHOUSE template.
o n -tr
n
a) Connect to Enterprise Manager Database Control asathe sh user (password: sh).
On the Home page, click the Advisor Centralhlink
s
a in the ฺ Links section.
eRelated
)
s Advisors i d
u link. Then on the SQL
b) On the Advisor Central page, clickithe r ฺeSQL t G
Advisors page, click the SQLpAccess
g ฺm uAdvisor
d en link.
d S t Options from a previously saved Task
c) On the Initial Options @ s
page, select Inherit
a selectththei SQLACCESS_WAREHOUSE template. After
ithen
or Template,ja b
and
i a
this is done, se
( click Continue.
u
OnA
d) io
b to
the Workload Source page, select Use an existing SQL Tuning Set and enter
n
Anto SH.SQLSET_MY_SQLACCESS_WORKLOAD in the SQL Tuning Set field.
(This SQL Tuning Set was generated earlier. It represents a warehouse workload
e
Jos that you want to analyze.) Click Next.
e) On the Recommendation Options page, ensure that all possible access structures
are selected, and that Comprehensive is selected. After this is done, click Next.
f) On the Schedule page, enter MY_SQLACCESS_TASK in the Task Name field.
Select the first Time Zone from the provided list (Click the torch icon.). After this
is done, click Next.
g) On the Review page, click Submit.
h) Back to the Advisor Central page, click Refresh until the Status of your task is
COMPLETED.
i) After this is done, click the MY_SQLACCESS_TASK link in the Results table.
3) After this is done, investigate the proposed recommendations:
a) Back to the Advisor Central page, click the MY_SQLACCESS_TASK link in the
Results table. The task should have COMPLETED as the status.

Oracle Database 11g: SQL Tuning Workshop A - 391


Practice 11-2: Using SQL Access Advisor (continued)
b) This takes you to the Results page. From this page, you can see the potential
benefit of implementing the SQL Access Advisor recommendations on the
workload. There should be a huge difference between the original and the new
costs. Click the Recommendation subtab.
c) On the Recommendations subtab, you can see the high-level overview of the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

recommendations. Basically, all possible types of recommendations were


generated for this workload (Indexes, Materialized Views, Materialized View
Logs, Partitions, and Others).
d) Ensure that all recommendations are selected, and click the Recommendation
Details button. This takes you to the Details page, where you can see more details
about each of the recommendations, as well as the corresponding SQL statements
from the workload that are affected by these recommendations. You should see e
the following recommendations: n s
ice
- Partition CUSTOMERS table.
b l el
- Create four materialized view log.
fe ra
- Create a materialized view. s
a n
- Create one bitmap index.
- Create a function-based index. o n -tr
- Create a B*-tree index on multiple columns.
s an
) h a deฺ
e) Click OK.
r ฺ e s Gui
p ฺ mi dent What happens, and why?
4) Try to implement the generated recommendations.
d
a) Back to the Recommendationsg subtab,
S tuclick the Schedule Implementation button.
i a@ this page, a warning is displayed indicating that the
b) On the SchedulebImplementation
wizard will
a ja try touimplement
(not se its recommendations because some of them are
b i t o
i o Aimportant changes
very that should be looked at closely by the administrator.
n
Antoc) Click the Show SQL button to look at the script you could use to implement all
recommendations. In fact, you already created this script and you will use it later
e
Jos in this lab. After you review the script, click Done.
d) Back to the Schedule Implementation page, click Cancel.
e) Click the Advisor Central locator link at the top of the “Results for Task” page.
f) On the Advisor Central page, select the SQL Access Advisor
MY_SQLACCESS_TASK task and click Delete.
g) On the Information page, click Yes.
5) Use Enterprise Manager to verify the performance improvement if you implement the
recommendations mentioned.
a) Click the Database tab at the top-right corner and then the “Software and
Support” tab. On the “Software and Support” tabbed page, click the SQL
Performance Analyzer link. You want to prove that implementing the
recommendations is beneficial.
b) On the SQL Performance Analyzer page, click the Guided Workflow link.

Oracle Database 11g: SQL Tuning Workshop A - 392


Practice 11-2: Using SQL Access Advisor (continued)
c) On the Guided Workflow page, click the Execute icon on the line corresponding
to step 1.
d) On the Create SQL Performance Analyzer Task page, enter MY_SPA_TASK in
the SQL Performance Analyzer Task Name field. Then, enter
SH.SQLSET_MY_SQLACCESS_WORKLOAD in the SQL Tuning Set Name field.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

After this is done, click Create.


e) Back to the Guided Workflow page, click the Execute icon for step 2.
f) On the Create Replay Trial page, enter MY_SQL_REPLAY_BEFORE in the
Replay Trial Name field, and ensure that you select the Trial environment
established check box. Then, click Submit.
g) Wait on the Guided Workflow page until step 2 is completed.
n s e
h) From your terminal session, connect as the sh user (password: sh) in the i ce
SQL*Plus session, and execute the implement.sql script. This script is a b l el
fe r a
precreated script corresponding to the recommendations previously generated by
your SQL Access Advisor session. a n s
o n -tr
an
cd /home/oracle/solutions/SQL_Access_Advisor
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$ ls
implement.sql revert.sh sqlaccessadv_setup.sh
h s
a deฺ
)
s Gui
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$ sqlplus sh/sh
e
r ฺ
i ent on Tue Mar 18 21:57:25
SQL*Plus: Release 11.1.0.6.0ฺm - Production
2008
d p
g Stud
Copyright (c) 1982,b i a@
2007,th is
( j a s e Oracle. All rights reserved.

b ia to u
i o ADatabase
Connected
n
to:

ntoOracle 11g Enterprise Edition Release 11.1.0.6.0 -

e A Production

J o s With the Partitioning, Oracle Label Security, OLAP, Data Mining


and Real Application Testing options

SQL> @implement
SQL>
SQL> Rem
SQL> Rem Creating new partitioned table
SQL> Rem
SQL> CREATE TABLE "SH"."CUSTOMERS1"
2 ( "CUST_ID" NUMBER,
3 "CUST_FIRST_NAME" VARCHAR2(20),
4 "CUST_LAST_NAME" VARCHAR2(40),
5 "CUST_GENDER" CHAR(1),
6 "CUST_YEAR_OF_BIRTH" NUMBER(4,0),
7 "CUST_MARITAL_STATUS" VARCHAR2(20),
8 "CUST_STREET_ADDRESS" VARCHAR2(40),
9 "CUST_POSTAL_CODE" VARCHAR2(10),
10 "CUST_CITY" VARCHAR2(30),
11 "CUST_CITY_ID" NUMBER,
12 "CUST_STATE_PROVINCE" VARCHAR2(40),
13 "CUST_STATE_PROVINCE_ID" NUMBER,

Oracle Database 11g: SQL Tuning Workshop A - 393


Practice 11-2: Using SQL Access Advisor (continued)
14 "COUNTRY_ID" NUMBER,
15 "CUST_MAIN_PHONE_NUMBER" VARCHAR2(25),
16 "CUST_INCOME_LEVEL" VARCHAR2(30),
17 "CUST_CREDIT_LIMIT" NUMBER,
18 "CUST_EMAIL" VARCHAR2(30),
19 "CUST_TOTAL" VARCHAR2(14),
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

20 "CUST_TOTAL_ID" NUMBER,
21 "CUST_SRC_ID" NUMBER,
22 "CUST_EFF_FROM" DATE,
23 "CUST_EFF_TO" DATE,
24 "CUST_VALID" VARCHAR2(1)
25 ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS
NOLOGGING
26 TABLESPACE "EXAMPLE"
27 PARTITION BY RANGE ("CUST_ID") INTERVAL( 3000) ( PARTITION
VALUES LESS THAN (3000)
n s e
28 );
i ce
b l el
Table created.
fer a
SQL>
a n s
SQL> Rem
o n -tr
an
SQL> Rem Copying comments to new partitioned table
SQL> Rem
SQL>
h s
a deฺ
COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_ID" IS 'primary key';

e )
s Gui
Comment created. r ฺ
i ent
p ฺ m d
SQL>
d g S t u
b i a@ "SH"."CUSTOMERS1"."CUST_FIRST_NAME"
SQL> COMMENT ON COLUMN
name of the customer'; t h is IS 'first

ja e
ia ( to us
Comment bcreated.

n i oA
ntoSQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_LAST_NAME" IS 'last
SQL>

e A
J o s name of the customer';

Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_GENDER" IS 'gender;
low cardinality attribute';

Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_YEAR_OF_BIRTH" IS
'customer year of birth';

Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_MARITAL_STATUS" IS
'customer marital status; low cardinality attribute';

Comment created.

Oracle Database 11g: SQL Tuning Workshop A - 394


Practice 11-2: Using SQL Access Advisor (continued)
SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_STREET_ADDRESS" IS
'customer street address';

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

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_POSTAL_CODE" IS
'postal code of the customer';

Comment created.

SQL>
SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_CITY" IS 'city where
n s e
the customer lives';
i ce
b l el
Comment created.
fer a
SQL>
a n s
o n -tr
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_STATE_PROVINCE" IS

an
'customer geography: state or province';

Comment created.
h s
a deฺ
e )
s Gui
SQL> r ฺ
i ent
p ฺ m
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."COUNTRY_ID"
d IS 'foreign
d
key to the countries tableg t u
(snowflake)';
S
@ s
Comment created.bia
j a e thi
SQL> b ia ( to us
i o A mainONphone
SQL> COMMENT
n
COLUMN "SH"."CUSTOMERS1"."CUST_MAIN_PHONE_NUMBER" IS

nto'customer number';

e A
J o s Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_INCOME_LEVEL" IS
'customer income level';

Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_CREDIT_LIMIT" IS
'customer credit limit';

Comment created.

SQL>
SQL> COMMENT ON COLUMN "SH"."CUSTOMERS1"."CUST_EMAIL" IS 'customer
email id';

Comment created.

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 395


Practice 11-2: Using SQL Access Advisor (continued)
SQL> COMMENT ON TABLE "SH"."CUSTOMERS1" IS 'dimension table';

Comment created.

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

SQL> Rem Copying constraints to new partitioned table


SQL> Rem
SQL> ALTER TABLE "SH"."CUSTOMERS1" ADD CONSTRAINT "CUSTOMERS_PK1"
PRIMARY KEY ("CUST_ID")
2 USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING
COMPUTE STATISTICS
3 TABLESPACE "EXAMPLE" ENABLE NOVALIDATE;

Table altered.

n s e
SQL>
i ce
SQL> Rem
b l el
SQL> Rem Copying referential constraints to new partitioned table
SQL> Rem
fer a
SQL> ALTER TABLE "SH"."CUSTOMERS1" ADD CONSTRAINT
a n s
"CUSTOMERS_COUNTRY_FK1" FOREIGN KEY ("COUNTRY_ID")
o n -tr
an
2 REFERENCES "SH"."COUNTRIES" ("COUNTRY_ID") ENABLE
NOVALIDATE;
h s
a deฺ
Table altered.
e )
s Gui
r ฺ
i ent
SQL>
p ฺ m d
SQL> Rem
d g S t u
SQL> Rem b i a@ tto
SQL> Rem Copying indexes
h isnew partitioned table
SQL> CREATE (
a ja uINDEX
BITMAP se "SH"."CUSTOMERS_GENDER_BIX1" ON
i o
b 10 tINITRANS
"SH"."CUSTOMERS1" ("CUST_GENDER")
i o A
2 PCTFREE 2 MAXTRANS 255 NOLOGGING COMPUTE STATISTICS
to n 3 TABLESPACE "EXAMPLE" LOCAL;
A n
o s e Index created.
J SQL>
SQL> Rem
SQL> Rem Copying object grants to new partitioned table
SQL> Rem
SQL> GRANT SELECT ON "SH"."CUSTOMERS1" TO "BI";

Grant succeeded.

SQL>
SQL> Rem
SQL> Rem Populating new partitioned table with data from original
table
SQL> Rem
SQL> INSERT /*+ APPEND */ INTO "SH"."CUSTOMERS1"
2 SELECT * FROM "SH"."CUSTOMERS";

55500 rows created.

SQL> COMMIT;

Oracle Database 11g: SQL Tuning Workshop A - 396


Practice 11-2: Using SQL Access Advisor (continued)
Commit complete.

SQL>
SQL> begin
2 dbms_stats.gather_table_stats('"SH"', '"CUSTOMERS1"', NULL,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

dbms_stats.auto_sample_size);
3 end;
4 /

PL/SQL procedure successfully completed.

SQL>
SQL> Rem
SQL> Rem Renaming tables to give new partitioned table the original
table name
n s e
SQL> Rem
i ce
SQL> ALTER TABLE "SH"."CUSTOMERS" RENAME TO "CUSTOMERS11";
b l el
Table altered.
fer a
a n s
SQL> ALTER TABLE "SH"."CUSTOMERS1" RENAME TO "CUSTOMERS";
o n -tr
Table altered.
s an
) h a deฺ
SQL>
r ฺ e s Gui
SQL> Rem

SQL> Rem Revalidating dimensionsm i for eusent with new partitioned table
SQL> Rem p
dg Stu d
SQL> ALTER DIMENSION@
b i a t h is
"SH"."CUSTOMERS_DIM" COMPILE;

i a (ja use
Dimension altered.

SQL> A
b to
n i o
ntoSQL>
e A SQL> CREATE MATERIALIZED VIEW LOG ON

J o s 2 "SH"."CUSTOMERS"
3 WITH ROWID,
SEQUENCE("CUST_ID","CUST_CITY","CUST_STATE_PROVINCE")
4 INCLUDING NEW VALUES;

Materialized view log created.

SQL>
SQL> CREATE MATERIALIZED VIEW LOG ON
2 "SH"."CHANNELS"
3 WITH ROWID,
SEQUENCE("CHANNEL_ID","CHANNEL_DESC","CHANNEL_CLASS")
4 INCLUDING NEW VALUES;

Materialized view log created.

SQL>
SQL> CREATE MATERIALIZED VIEW LOG ON
2 "SH"."TIMES"
3 WITH ROWID, SEQUENCE("TIME_ID","CALENDAR_QUARTER_DESC")
4 INCLUDING NEW VALUES;

Oracle Database 11g: SQL Tuning Workshop A - 397


Practice 11-2: Using SQL Access Advisor (continued)
Materialized view log created.

SQL>
SQL> CREATE MATERIALIZED VIEW LOG ON
2 "SH"."SALES"
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

3 WITH ROWID,
SEQUENCE("CUST_ID","TIME_ID","CHANNEL_ID","AMOUNT_SOLD")
4 INCLUDING NEW VALUES;

Materialized view log created.

SQL>
SQL> CREATE MATERIALIZED VIEW "SH"."MV_01DF0000"
2 REFRESH FAST WITH ROWID
3 ENABLE QUERY REWRITE
n s e
4 AS SELECT SH.CUSTOMERS.CUST_STATE_PROVINCE C1,
i ce
SH.CUSTOMERS.CUST_CITY C2, SH.CHANNELS.CHANNEL_CLASS
b l el
5 C3, SH.CHANNELS.CHANNEL_DESC C4, SH.TIMES.CALENDAR_QUARTER_DESC
C5, SUM("SH"."SALES"."AMOUNT_SOLD")
fer a
n
6 M1, COUNT("SH"."SALES"."AMOUNT_SOLD") M2, COUNT(*) M3 FROM
a s
SH.CUSTOMERS,
n -tr
o
an
7 SH.CHANNELS, SH.TIMES, SH.SALES WHERE SH.SALES.CHANNEL_ID =
SH.CHANNELS.CHANNEL_ID
h s
a deฺ
8 AND SH.SALES.TIME_ID = SH.TIMES.TIME_ID AND SH.SALES.CUST_ID =
SH.CUSTOMERS.CUST_ID
e )
s Gui
r ฺ
mi dent
9 AND (SH.TIMES.CALENDAR_QUARTER_DESC IN ('1999-04', '1999-03',
'1999-02'
p ฺ
g Stu
d
10 , '1999-01')) AND (SH.CHANNELS.CHANNEL_DESC IN ('Internet',
'Catalog'
b i a@ this
11 )) AND (SH.CUSTOMERS.CUST_STATE_PROVINCE = 'CA') GROUP BY

i a (ja use
SH.CUSTOMERS.CUST_STATE_PROVINCE,
b to
12 SH.CUSTOMERS.CUST_CITY, SH.CHANNELS.CHANNEL_CLASS,
A
o n io
SH.CHANNELS.CHANNEL_DESC,
13 SH.TIMES.CALENDAR_QUARTER_DESC;
nt
e A
J o s Materialized view created.

SQL>
SQL> begin
2
dbms_stats.gather_table_stats('"SH"','"MV_01DF0000"',NULL,dbms_stats
.auto_sample_size);
3 end;
4 /

PL/SQL procedure successfully completed.

SQL>
SQL> CREATE BITMAP INDEX "SH"."CUSTOMERSJFV_IDX_01DF0000"
2 ON "SH"."CUSTOMERSJFV"
3 ("CUST_STATE_PROVINCE")
4 COMPUTE STATISTICS;

Index created.

SQL>

Oracle Database 11g: SQL Tuning Workshop A - 398


Practice 11-2: Using SQL Access Advisor (continued)
SQL> CREATE INDEX "SH"."TEMPJFV_IDX_01DF0001"
2 ON "SH"."TEMPJFV"
3 (ABS("C"))
4 COMPUTE STATISTICS;

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

SQL>
SQL> CREATE INDEX "SH"."CUSTOMERS_IDX_01DF0002"
2 ON "SH"."CUSTOMERS"
3 ("COUNTRY_ID","CUST_CITY","CUST_LAST_NAME")
4 COMPUTE STATISTICS;

Index created.

SQL>
n s e
SQL> exit
ice
Disconnected from Oracle Database 11g Enterprise Edition Release
b l el
11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
fe r a
and Real Application Testing options
a n s
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$
o n -tr
i) Back to your Guided Workflow page, click the Execute a nicon corresponding to
step 3.
h s
a deฺ
e )
s Gui
j) On the Create Replay Trial page, enter
i r ฺ MY_SQL_REPLAY_AFTER
n t in the Replay
Trial Name field. Ensure that you
p m
ฺ ude
select the Trial environment established check
box, and click Submit. d g S t
@ s
k) Wait until step b
j a 3 is
e thi
iacompleted.
l) Backb toia
(
your Guided u s
Workflow Enterprise Manager page, click the Execute icon
A
corresponding
t o
to step 4.
n i o
Antom) On the Run Replay Trial Comparison page, ensure that you create a comparison
e between MY_SQL_REPLAY_BEFORE and MY_SQL_REPLAY_AFTER. Click
Jos Submit.
n) Wait until step 4 is completed.
o) Back to your Guided Workflow Enterprise Manager page, click the Execute icon
corresponding to step 5.
p) On the SQL Performance Analyzer Task Result page, you can clearly see that the
second trial is much faster than the original one. You should see that all five SQL
statements are improved in the second trial due a changed execution plan.
q) To get more details, analyze the differences in execution plan for all five
statements. You can do so directly from the SQL Performance Analyzer Task
Result page by clicking each SQL ID in the Top 10 table. Each time you click a
SQL ID, you can see in the SQL Details section all statistics differences between
the two trials as well as the differences in execution plans.

Oracle Database 11g: SQL Tuning Workshop A - 399


Practice 11-2: Using SQL Access Advisor (continued)
r) After this is done, go back to the SQL Performance Analyzer page (Home >
Software and Support > SQL Performance Analyzer), and delete
MY_SPA_TASK by selecting it and clicking Delete. On the Confirmation page,
click Delete.
s) Log out from Enterprise Manager.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

6) From a terminal session, execute the revert.sh script to return to the situation you
were in before you started the lab.
[oracle@edrsr33p1-orcl SQL_Access_Advisor]$ ./revert.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:02


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.


n s e
ice
b l el
Connected to:
fe r a
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
a n s
-tr
Production
n
With the Partitioning, Oracle Label Security, OLAP, Data Mining
o
an
and Real Application Testing options

SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL>ha


s eฺSQL> Connected.
) i d
SQL> SQL>
SQL> SQL>
Grant succeeded. i r ฺes nt Gu
g p ฺm ude
SQL> SQL> d S t
@ s
ia thi
User altered.

( j a b e
iaSQL>
SQL> SQL> Connected. u s
b
SQL> SQL> o
A 8 9t 10 11SQL>12SQL>13SQL>14SQL>15SQL>16 2 17 3 18 4 19 5
SQL> SQL>
6 io 7
n 21 22 23 24
to20
A n PL/SQL procedure successfully completed.
o s e
J SQL> SQL> SQL> SQL> SQL> SQL>
Table dropped.

SQL> SQL>
System altered.

SQL> SQL>
Table dropped.

SQL> SQL>
Table dropped.

SQL> SQL> SQL>


PL/SQL procedure successfully completed.

SQL>
PL/SQL procedure successfully completed.

SQL> SQL>
PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 400


Practice 11-2: Using SQL Access Advisor (continued)
SQL> SQL> SQL>
PL/SQL procedure successfully completed.

SQL> SQL> SQL> SQL>


Materialized view log dropped.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SQL>
Materialized view log dropped.

SQL> SQL>
Materialized view log dropped.

SQL> SQL>
Materialized view log dropped.

n s e
SQL> SQL>
i ce
Materialized view dropped.
b l el
SQL> SQL>
fer a
Index dropped.
a n s
o n -tr
an
SQL> SQL>
Table dropped.
h s
a deฺ
SQL> SQL>
e )
s Gui
Table dropped. r ฺ
i ent
p ฺ m d
SQL> SQL> Connected.
d g S t u
SQL> SQL>
b
Revoke succeeded. i a@ this
i a (jaRem use
A b $Header:tosh_main.sql 06-mar-2008.15:00:45 cbauwens Exp $
SQL> SQL> SQL>
o
i Rem
SQL> Rem
n
ntoSQL>
e A SQL> Rem sh_main.sql

J o s SQL>
SQL>
Rem
Rem Copyright (c) 2001, 2008, Oracle. All rights reserved.
SQL> Rem
SQL> Rem NAME
SQL> Rem sh_main.sql - Main schema creation and load script
...
SQL> Rem
SQL>
SQL> SET ECHO OFF

specify password for SH as parameter 1:

specify default tablespace for SH as parameter 2:

specify temporary tablespace for SH as parameter 3:

specify password for SYS as parameter 4:

specify directory path for the data files as parameter 5:

writeable directory path for the log files as parameter 6:

Oracle Database 11g: SQL Tuning Workshop A - 401


Practice 11-2: Using SQL Access Advisor (continued)
specify version as parameter 7:

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

User dropped.

old 1: CREATE USER sh IDENTIFIED BY &pass


new 1: CREATE USER sh IDENTIFIED BY sh

User created.

old 1: ALTER USER sh DEFAULT TABLESPACE &tbs


new 1: ALTER USER sh DEFAULT TABLESPACE example
n s e
old 2: QUOTA UNLIMITED ON &tbs
i ce
new 2: QUOTA UNLIMITED ON example
b l el
User altered.
fer a
a n s
old 1: ALTER USER sh TEMPORARY TABLESPACE &ttbs
n -tr
o
an
new 1: ALTER USER sh TEMPORARY TABLESPACE temp

User altered.
h s
a deฺ
e )
s Gui
r ฺ
Grant succeeded.
p ฺ mi dent
d g Stu
Grant succeeded.bia
@ his
j a e t
(
ia to u s
... A
b
o n io
nt
e A
J o s Grant succeeded.

Grant succeeded.

PL/SQL procedure successfully completed.

Connected.

Grant succeeded.

old 1: CREATE OR REPLACE DIRECTORY data_file_dir AS '&data_dir'


new 1: CREATE OR REPLACE DIRECTORY data_file_dir AS
'/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/'

Directory created.

old 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '&log_dir'


new 1: CREATE OR REPLACE DIRECTORY log_file_dir AS '/home/oracle/'

Oracle Database 11g: SQL Tuning Workshop A - 402


Practice 11-2: Using SQL Access Advisor (continued)
Directory created.

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

Grant succeeded.

Grant succeeded.

Connected.

Session altered.

n s e
Session altered.
i ce
b l el
Table created.
fer a
a n s
n -tr
o
an
Table created.

h s
a deฺ
...
e )
s Gui
r ฺ
i ent
p ฺ m
Table created.
d g Stud
b i a@ this
(ja us...
Creating constraints
i a
e
b
altered. t
Table A
o
i o
nton
e A
J o s ...

Table altered.

specify password for SH as parameter 1:

specify path for data files as parameter 2:

specify path for log files as parameter 3:

specify version as parameter 4:

Looking for indexes that could slow down load ...

no rows selected

loading TIMES using:

Oracle Database 11g: SQL Tuning Workshop A - 403


Practice 11-2: Using SQL Access Advisor (continued)
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/time_v
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/time_v
3.dat
/home/oracle/time_v3.log
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:23


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 1000.

Load completed - logical record count 1826.

n s e
loading COUNTRIES using:
i ce
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/coun_v
b l el
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/coun_v
fer a
3.dat
a n s
/home/oracle/coun_v3.log
o n -tr
SQL*Loader: Release 11.1.0.6.0 - Production a on n Thu Mar 20 15:22:24
2008
h a s eฺ
) i d
Copyright (c) 1982, 2007, Oracle.
i r ฺesAllnrights
t Gu reserved.
g p ฺm ude
t
d recordScount
i a @
Load completed - logical
h i s 23.
b
ja use t
ia ( tousing:
loading bCUSTOMERS
i oA
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/cust_v
n
nto/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/cust1v
3.ctl

e A
J o s 3.dat
/home/oracle/cust1v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:24


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 10000.


Save data point reached - logical record count 20000.
Save data point reached - logical record count 30000.
Save data point reached - logical record count 40000.
Save data point reached - logical record count 50000.

Load completed - logical record count 55500.

loading PRODUCTS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prod_v
3.ctl

Oracle Database 11g: SQL Tuning Workshop A - 404


Practice 11-2: Using SQL Access Advisor (continued)
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prod1v
3.dat
/home/oracle/prod1v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:24


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

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Load completed - logical record count 72.

loading PROMOTIONS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prom_v
3.ctl
n s e
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/prom1v
i ce
3.dat
b l el
/home/oracle/prom1v3.log
fer a
n s
SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:25
a
2008
o n -tr
Copyright (c) 1982, 2007, Oracle. All rights n
a reserved.
h s eฺ
acountd10.
Save data point reached - logical record )
s count
erecord i
u 20.
Save data point reached - logical
i r ฺ t G
...
g p ฺm den count 500.
logicalurecord
d
Save data point reached -
S t
a @
Load completed -bilogical
s
hi count 503.
j a e trecord
b ia ( to us
i o A CHANNELS using:
loading
n
nto3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/chan_v

e A
J o s /u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/chan_v
3.dat
/home/oracle/chan_v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:25


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Load completed - logical record count 5.

loading SALES using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/sale_v
3.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/sale1v
3.dat
/home/oracle/sale1v3.log

Oracle Database 11g: SQL Tuning Workshop A - 405


Practice 11-2: Using SQL Access Advisor (continued)
SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:25
2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 100000.


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

...
Save data point reached - logical record count 900000.

Load completed - logical record count 916039.

loading COSTS using external table

Table created.
n s e
i ce
b l el
82112 rows created.
fer a
a n s
loading additonal SALES using:
o n -tr
an
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dmsal_
v3.ctl
h s
a deฺ
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dmsal_
v3.dat
e )
s Gui
/home/oracle/dmsal_v3.log r ฺ
i ent
p ฺ m d
d g
SQL*Loader: Release 11.1.0.6.0
S
- u
t Production on Thu Mar 20 15:22:38
2008
b i a@ this
Copyright (c)
i a (ja1982,u2007,
se Oracle. All rights reserved.
A o
b point treached
i o
Save data
n
- logical record count 100.

ntoSave data point reached - logical record count 2800.


...

e A
J o s Load completed - logical record count 2804.

loading SUPPLEMENTARY DEMOGRAPHICS using:


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dem_v3
.ctl
/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/dem1v3
.dat
/home/oracle/dem1v3.log

SQL*Loader: Release 11.1.0.6.0 - Production on Thu Mar 20 15:22:38


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Save data point reached - logical record count 10.


...
Save data point reached - logical record count 4500.

Load completed - logical record count 4500.

Oracle Database 11g: SQL Tuning Workshop A - 406


Practice 11-2: Using SQL Access Advisor (continued)

Commit complete.

Enabling constraints ...


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

Table altered.

...

Table altered.

n s e
Creating additional indexes ...
i ce
b l el
Index created.
fer a
a n s
...
n - tr
o
s an
Index created.
) h a deฺ
r ฺ e s Gui
Create dimensions ...
p ฺ mi dent
Dimension created. @d
g Stu
b i a t h is
i a (ja use
b
Commit complete.
A to
o
ni procedure successfully completed.
ntoPL/SQL
e A
J o s
no rows selected

Dimension created.

PL/SQL procedure successfully completed.

no rows selected

Dimension created.

PL/SQL procedure successfully completed.

no rows selected

Oracle Database 11g: SQL Tuning Workshop A - 407


Practice 11-2: Using SQL Access Advisor (continued)

Dimension created.

PL/SQL procedure successfully completed.


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

no rows selected

Dimension created.

PL/SQL procedure successfully completed.

n s e
i ce
no rows selected
b l el
Creating MVs as tables ...
fer a
a n s
n -tr
o
an
View created.

h s
a deฺ
Table created.
e )
s Gui
r ฺ
i ent
p ฺ m
Table created.
d g Stud
b i a@ this
Index created.
i a (ja use
A b to
o
ni created.
n t oIndex

e A
Jos Index created.

Index created.

Creating materialized views ...

Materialized view created.

Materialized view created.

Creating comments ...

Comment created.

...

Oracle Database 11g: SQL Tuning Workshop A - 408


Practice 11-2: Using SQL Access Advisor (continued)

Comment created.

Creating OLAP metadata ...


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

<<<<< CREATE CWMLite Metadata for the Sales History Schema >>>>>
-
<<<<< CREATE CATALOG sh_cat for Sales History >>>>>
Catalog Dropped
CWM Collect Garbage
-
<<<<< CREATE the Sales CUBE >>>>>
Sales amount, Sales quantity
<TIMES CHANNELS PRODUCTS CUSTOMERS PROMOTIONS >
Drop SALES_CUBE prior to recreation
n s e
Cube Dropped
i ce
Add dimensions -
b l el
to SALES_CUBE and map the foreign keys
Create measures -
fer a
for SALES_CUBE and map to columns in the fact table
a n s
Set default aggregation method -
n - tr
o
an
to SUM for all measures over TIME
Add SALES_CUBE to the catalog
h s
a deฺ
SALES_CUBE successfully added to sh_cat
-
e )
s Gui
r ฺ
mi dent
<<<<< CREATE the Cost CUBE >>>>>

p ฺ
Unit Cost, Unit Price < TIMES PRODUCTS CHANNELS PROMOTIONS >
g Stu
d
Drop COST_CUBE prior to recreation
Cube Dropped
b i a@ this
Add dimensions -

i a (ja use
to COST_CUBE and map the foreign keys
b to
Create measures -
A
o n io for COST_CUBE and map to columns in the fact table
Set default aggregation method -
nt
e A to SUM for all measures over TIME

J o s Add COST_CUBE to the catalog


COST_CUBE successfully added to sh_cat
-
<<<<< TIME DIMENSION >>>>>
Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
- default display hierarchy
Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
Create dimension attributes and add their level attributes
- Long Description created
- Short Description created
- Period Number of Days created
- Period End Date created
Classify entity descriptor use
- Time dimension
- Long description
- Day name
- Calendar month description

Oracle Database 11g: SQL Tuning Workshop A - 409


Practice 11-2: Using SQL Access Advisor (continued)
- Calendar quarter description
- Fiscal month description
- Fiscal quarter description
- Short Description
- Day name
- Calendar month description
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

- Calendar quarter description


- Fiscal month description
- Fiscal quarter description
- Time Span
- Days in calendar month
- Days in calendar quarter
- Days in calendar year
- Days in fiscal month
- Days in fiscal quarter
- Days in fiscal year
n s e
- End Date
i ce
- End of calendar month
b l el
-
-
End of calendar quarter
End of calendar year
fer a
- End of fiscal month
a n s
- End of fiscal quarter
o n -tr
an
- End of fiscal year
-
<<<<< CUSTOMERS DIMENSION >>>>>
h s
a plural ฺ
ename
Dimension - display name, description) and i d
Level - display name and description
i r ฺ es t Gu
Hierarchy - display name andฺm
g p description
u d en
d hierarchy
- default calculation
t
hierarchy
S
@
- default display
i s
Level Attributesbi-aname, tdisplay
hprior name, description
Drop dimension j a
(to dropus e
attributes to re-creation
i a
b to tdrop
No attribute o
o A
i attribute to drop
No attribute
n
toNo
A n No attribute to drop

o s e No attribute to drop
J No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
No attribute to drop
Create dimension attributes and add their level attributes
- Long Description created
- Short Description created
- Other Customer Information created
Classify entity descriptor use
- Long Description
- Short Description
-
<<<<< PRODUCTS DIMENSION >>>>>
Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy

Oracle Database 11g: SQL Tuning Workshop A - 410


Practice 11-2: Using SQL Access Advisor (continued)
- default display hierarchy
Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
No attribute to drop
Create dimension attributes and add their level attributes
- Long Description created
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

- Short Description created


Classify entity descriptor use
- Long Description
- Short Description
-
<<<<< PROMOTIONS DIMENSION >>>>>
Dimension - display name, description and plural name
Level - display name and description
Hierarchy - display name and description
- default calculation hierarchy
n s e
- default display hierarchy
i ce
Level Attributes - name, display name, description
b l el
Drop dimension attributes prior to re-creation
No attribute to drop
fer a
Create dimension attributes and add their level attributes
a n s
- Long Description created
n - tr
o
an
- Short Description created
Classify entity descriptor use
- Long Description
h s
a deฺ
- Short Description
e )
s Gui
r ฺ
mi dent
-

p ฺ
<<<<< CHANNELS DIMENSION >>>>>
g Stu
d
Dimension - display name, description and plural name

b i a@ this
Level - display name and description
Hierarchy - display name and description

i a (ja use
- default calculation hierarchy
b to
- default display hierarchy
A
o n io
Level Attributes - name, display name, description
Drop dimension attributes prior to re-creation
nt
e A No attribute to drop

J o s Create dimension attributes and add their level attributes


- Long Description created
- Short Description created
Classify entity descriptor use
- Long Description
- Short Description
-
<<<<< FINAL PROCESSING >>>>>
- Changes have been committed

PL/SQL procedure successfully completed.

Commit complete.

gathering statistics ...

PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 411


Practice 11-2: Using SQL Access Advisor (continued)
PL/SQL procedure successfully completed.

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

[oracle@edrsr33p1-orcl SQL_Access_Advisor]$

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/SQL_Access_Advisor/sh

export ORACLE_SID=orcl
n s e
i ce
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
b l el
export
fer a
n s
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
a
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin:/bin
n -tr
cp * $ORACLE_HOME/demo/schema/sales_history a n
o
h a s eฺ
sqlplus / as sysdba <<FIN!
e s ) u id

ir ent G
SET ECHO ON ฺ m
p tud
SET FEEDBACK 1
d g
SET NUMWIDTH 10
i a @ his S
SET LINESIZE 8000
SET TRIMSPOOL ( j aONb se t
ia to u
SET TAB bOFF
i A
oLONG
SET PAGESIZE 100
to n
SET 1000
A n
o s e CONNECT / AS SYSDBA
J grant dba to sh;

alter user sh identified by sh account unlock;

connect sh/sh

set serveroutput on size 32768;


set echo on;
variable norecs number;

Rem Clean up

declare
name varchar2(30);
cursor name_cur1 is
select task_name from user_advisor_templates
where task_name like '%SQLACCESS%';

Oracle Database 11g: SQL Tuning Workshop A - 412


Practice 11-2: Using SQL Access Advisor (continued)
begin
----------------------------------------------------------------
-----------
-- Get rid of templates, tasks and workloads.
----------------------------------------------------------------
-----------
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

open name_cur1;

loop
fetch name_cur1 into name;
exit when name_cur1%NOTFOUND;

dbms_advisor.update_task_attributes(name,null,null,'FALSE','FALSE');
dbms_advisor.delete_task(name);
n s e
end loop;
i ce
b l el
close name_cur1;
fer a
end;
a n s
/
n - tr
o
s an
Rem make a temp table
) h a deฺ
r ฺ e s Gui
DROP TABLE temp_table purge;ฺmi e nt
d p
g Stu d
b i a@ this
alter system flush shared_pool;

(ja upurge;
drop table tempjfv
i a se
A to
b customersjfv
i o
drop table
n
purge;

nto
e A
J o s execute dbms_advisor.delete_task('%');
execute dbms_advisor.delete_sqlwkld('%');

execute dbms_sqltune.drop_sqlset('SQLSET_MY_SQLACCESS_WORKLOAD');

EXECUTE DBMS_STATS.UNLOCK_SCHEMA_STATS('SH');

DROP MATERIALIZED VIEW LOG ON "SH"."CUSTOMERS";

DROP MATERIALIZED VIEW LOG ON "SH"."CHANNELS";

DROP MATERIALIZED VIEW LOG ON "SH"."TIMES";

DROP MATERIALIZED VIEW LOG ON "SH"."SALES";

DROP MATERIALIZED VIEW "SH"."MV_01DF0000";

DROP INDEX "SH"."CUSTOMERS_IDX_01DF0002";

Oracle Database 11g: SQL Tuning Workshop A - 413


Practice 11-2: Using SQL Access Advisor (continued)
DROP TABLE "SH"."CUSTOMERS" PURGE;

DROP TABLE "SH"."CUSTOMERS11" CASCADE CONSTRAINTS PURGE;

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

revoke dba from sh;

@sh_main sh example temp oracle


/u01/app/oracle/product/11.1.0/db_1/demo/schema/sales_history/
/home/oracle/ v3

exit;

FIN!
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
nt
e A
J o s

Oracle Database 11g: SQL Tuning Workshop A - 414


Practice 11-3: Using Automatic SQL Tuning
In this practice, you manually launch Automatic SQL Tuning to automatically tune a
small application workload. You then investigate the outcomes and configuration
possibilities.
1) On the Server page, click Automated Maintenance Tasks, check that Status is set to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Enabled, and click Configure. Click the Configure button next to Automatic SQL
Tuning. Select Yes for “Automatic Implementation of SQL Profiles.” Then, click
Apply. Execute the ast_setup.sh script from a terminal window connected as the
oracle user. This script creates the AST user used throughout this practice, turns off
automatic maintenance tasks, and drops any existing profiles on queries executed by
the AST user.

n s e
$ cd /home/oracle/solutions/Automatic_SQL_Tuning
ice
b l el
$ ./ast_setup.sh
f er a
an s
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$ ./ast_setup.sh
- t r
n 18 15:31:49
SQL*Plus: Release 11.1.0.6.0 - Production on Tue n oMar
2008
s a
Copyright (c) 1982, 2007, Oracle. s ) a eฺ
hrights idreserved.
i r ฺe nt Gu
All

g p ฺm ude
Connected to:
Oracle Database 11g @
d S t
b i a t h i
Enterprises Edition Release 11.1.0.6.0 -

(ja useOracle Label Security, OLAP, Data Mining


Production
i a
With the Partitioning,
to Testing options
b Application
A
and Real
o
ni SQL> SQL> SQL> drop user ast cascade
toSQL>
A n *
o s e ERROR at line 1:
J ORA-01918: user 'AST' does not exist

SQL> SQL>
User created.

SQL> SQL>
Grant succeeded.

SQL> SQL>
System altered.

SQL> SQL> SQL> SQL> SQL> SQL>


System altered.

SQL> SQL> SQL> SQL> SQL> SQL>


PL/SQL procedure successfully completed.

SQL> SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7 8 9


PL/SQL procedure successfully completed.

Oracle Database 11g: SQL Tuning Workshop A - 415


Practice 11-3: Using Automatic SQL Tuning (continued)
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition
Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

--------------------------------------------------------------

#!/bin/bash

cd /home/oracle/solutions/Automatic_SQL_Tuning

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
n s e
i ce
export
b l el
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin
fer a
a n s
sqlplus / as sysdba <<FIN!
n -tr
o
set echo on
s an
) h a deฺ
drop user ast cascade;
r ฺ e s Gui
create user ast identified by ฺ i ent
mast;
d g p t u d
grant dba to ast; @
a is S
b i t h
a shared_pool;
alter system(jflush
i a u se
A b to
--
n o
i Turn off AUTOTASK
nto--
e A --

J o s alter system set "_enable_automatic_maintenance"=0;

--
-- Clear out old executions of auto-sqltune
--

exec dbms_sqltune.reset_tuning_task('SYS_AUTO_SQL_TUNING_TASK');

--
-- Drop any profiles on AST queries
--

declare
cursor prof_names is
select name from dba_sql_profiles where sql_text like '%AST%';
begin
for prof_rec in prof_names loop
dbms_sqltune.drop_sql_profile(prof_rec.name);
end loop;
end;

Oracle Database 11g: SQL Tuning Workshop A - 416


Practice 11-3: Using Automatic SQL Tuning (continued)
/

FIN!

2) In preparation for the practice, you should execute a workload. Execute the
run_workload_stream.sh script. This script executes, multiple times, a query that
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

is not correctly optimized. The query in question uses hints that force the optimizer to
pick a suboptimal execution plan. The script execute for approximately 30 seconds.

./run_workload_stream.sh

[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$
./run_workload_stream.sh
Tue Mar 18 15:38:05 GMT-7 2008
n s e
ice
el
SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 15:38:05
2008
a b l
fe r
Copyright (c) 1982, 2007, Oracle. All rights reserved.
a n s
o n -tr
Connected to:
s a n11.1.0.6.0 -
Oracle Database 11g Enterprise Edition Release
Production
)
With the Partitioning, Oracle LabelsSecurity,
eฺ Data Mining
ha idOLAP,
i r ฺe nt Gu
and Real Application Testing options

g p ฺm ude
SQL> SQL> SQL> SQL> d S t
no rows selected a@ s
j a b i
e thi
SQL>
ia ( to us
no rows bselected
n i oA
ntoSQL>
e A no rows selected

J o s ...

SQL>
no rows selected

SQL>
no rows selected

SQL>
no rows selected

SQL>
no rows selected

SQL>
no rows selected

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining

Oracle Database 11g: SQL Tuning Workshop A - 417


Practice 11-3: Using Automatic SQL Tuning (continued)
and Real Application Testing options
Tue Mar 18 15:38:23 GMT-7 2008
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$

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

#!/bin/bash

cd /home/oracle/solutions/Automatic_SQL_Tuning

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
n s e
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin
i ce
b l el
date
fer a
sqlplus ast/ast <<FIN!
a n s
o n -tr
set echo on
n
a c.cust_id,
select /*+ USE_NL(s c) FULL(s) FULL(c) AST
h s
a deฺ c where s.cust_id
*/
sum(s.quantity_sold) from sh.sales s,) sh.customers
r ฺ e G ui
sby c.cust_id;
= c.cust_id and c.cust_id < 2 group
ฺ m i ent

d p
g Stud
select /*+ USE_NL(sb i a@ t h is FULL(c) AST */ c.cust_id,
c) FULL(s)
ja usfrom
(and
sum(s.quantity_sold)
a
e sh.sales s, sh.customers c where s.cust_id
b i
= c.cust_id
to
c.cust_id < 2 group by c.cust_id;
io A
t o n
FIN!
A n
o s e date
J 3) Automatic SQL Tuning is implemented using an automated task that runs during
maintenance windows. However, you do not wait for the next maintenance window to
open. Instead, you force the opening of your next maintenance window now. This
automatically triggers the Automatic SQL Tuning task. Execute the run_ast.sh
script to open your next maintenance window now. The script’s execution takes a
couple of minutes.

./run_ast.sh

[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$ ./run_ast.sh


Tue Mar 18 15:43:48 GMT-7 2008

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 15:43:48


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop A - 418


Practice 11-3: Using Automatic SQL Tuning (continued)
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL> SQL> SQL> SQL>


PL/SQL procedure successfully completed.

SQL> SQL> SQL> SQL> 2 3 4


PL/SQL procedure successfully completed.

SQL> SQL>
WINDOW
--------------------------------------------------------------------
n s e
------------
i ce
TUESDAY_WINDOW
b l el
SQL> SQL> SQL> SQL> SQL> SQL> 2
fer a
System altered.
a n s
o n -tr
an
SQL> SQL> >
PL/SQL procedure successfully completed.
h s
a deฺ
SQL> SQL> >
e )
s Gui
r ฺ
i ent
PL/SQL procedure successfully completed.

p ฺ m d
SQL> SQL>
d g S t u
b i a@ this completed.
PL/SQL procedure successfully

SQL> SQL> SQL>


i a (jaSQL> u se SQL> completed.
SQL>
b
PL/SQL procedure
A tosuccessfully

n o
i SQL> 2 3 4 5 6 7 8 9 10
toSQL> 11 12 13

e An 14 15 16 17 18 19 20 21 22

Jos
PL/SQL procedure successfully completed.

SQL> SQL> 2
System altered.

SQL> SQL> SQL> SQL> SQL> SQL> SQL> >


PL/SQL procedure successfully completed.

SQL> SQL> >


PL/SQL procedure successfully completed.

SQL> SQL> SQL> Disconnected from Oracle Database 11g Enterprise


Edition Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Tue Mar 18 15:44:50 GMT-7 2008
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$

--------------------------------------------------------------

#!/bin/bash

Oracle Database 11g: SQL Tuning Workshop A - 419


Practice 11-3: Using Automatic SQL Tuning (continued)
cd /home/oracle/solutions/Automatic_SQL_Tuning

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

date

sqlplus / as sysdba <<FIN!

set echo on
n s e
i ce
exec dbms_workload_repository.create_snapshot;
b l el
variable window varchar2(20);
fer a
a n s
begin
o n -tr
an
select upper(to_char(sysdate,'fmday'))||'_WINDOW' into :window from
dual;
end;
h s
a deฺ
/
e )
s Gui
r ฺ
print window;
p ฺ mi dent
d g Stu
--
b i a@ thmaintenance
-- Open the corresponding is window, but with other clients
j a e
ia ( to us
disabled
-- b
n i o Asystem set "_enable_automatic_maintenance"=1
nto/
alter

e A
J o s exec dbms_auto_task_admin.disable( -
'auto optimizer stats collection', null, :window);

exec dbms_auto_task_admin.disable( -
'auto space advisor', null, :window);

exec dbms_scheduler.open_window(:window, null, true);

--
-- Close the maintenance window when sqltune is done
--

exec dbms_lock.sleep(60);

declare
running number;
begin

loop
select count(*)

Oracle Database 11g: SQL Tuning Workshop A - 420


Practice 11-3: Using Automatic SQL Tuning (continued)
into running
from dba_advisor_executions
where task_name = 'SYS_AUTO_SQL_TUNING_TASK' and
status = 'EXECUTING';

if (running = 0) then
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

exit;
end if;

dbms_lock.sleep(60);
end loop;

dbms_scheduler.close_window(:window);

end;
/
n s e
ice
alter system set "_enable_automatic_maintenance"=0
b l el
/
fe r a
--
a n s
-
-- Re-enable the other guys so they look like they are enabled in
n tr
o
an
EM.
-- Still they will be disabled because we have set the underscore.
--
h s
a deฺ
e )
s Gui
r ฺ
mi denull, nt :window);
exec dbms_auto_task_admin.enable( -

p ฺ
'auto optimizer stats collection',
g Stu-
d
b a@ null,
'auto space advisor', t h is :window);
exec dbms_auto_task_admin.enable(
i
i a (ja use
FIN! A
b to
i o
nton
e A date

J o s 4) Execute the run_workload_stream.sh script again. What do you observe?


a) You should see that the execution time for run_workload_stream.sh is much
faster than the original execution. This is probably due to the fact that Automatic
SQL Tuning implemented a profile for your statement automatically.

./run_workload_stream.sh

[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$
./run_workload_stream.sh
Tue Mar 18 15:46:42 GMT-7 2008

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 15:46:42


2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Connected to:

Oracle Database 11g: SQL Tuning Workshop A - 421


Practice 11-3: Using Automatic SQL Tuning (continued)
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options

SQL> SQL> SQL> SQL>


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

no rows selected

SQL>
no rows selected

SQL>
no rows selected

...

n s e
SQL>
i ce
no rows selected
b l el
SQL>
fer a
no rows selected
a n s
o n -tr
an
SQL>
no rows selected
h s ฺ
a 11gdeEnterprise
)
s Gu
SQL> SQL> Disconnected from Oracle Database
Release 11.1.0.6.0 - Production rฺe
i Edition

With the Partitioning, Oracle ฺ m i Security,


Label e nt OLAP, Data Mining
g p
d 2008St
and Real Application Testing u d
options
Tue Mar 18 15:46:42 @
b i
GMT-7
[oracle@edrsr33p1-orcl t h is
a Automatic_SQL_Tuning]$
i a (ja use
b to
------------------------------------------------------------
A
o
ni /home/oracle/solutions/AST
#!/bin/bash

ntocd
e A
J o s export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin

date

sqlplus ast/ast <<FIN!

set echo on

select /*+ USE_NL(s c) FULL(s) FULL(c) AST */ c.cust_id,


sum(s.quantity_sold) from sh.sales s, sh.customers c where s.cust_id
= c.cust_id and c.cust_id < 2 group by c.cust_id;

Oracle Database 11g: SQL Tuning Workshop A - 422


Practice 11-3: Using Automatic SQL Tuning (continued)
select /*+ USE_NL(s c) FULL(s) FULL(c) AST */ c.cust_id,
sum(s.quantity_sold) from sh.sales s, sh.customers c where s.cust_id
= c.cust_id and c.cust_id < 2 group by c.cust_id;

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

date

5) Force the creation of an Automatic Workload Repository (AWR) snapshot.

./create_snapshot.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 15:51:19


2008

n s e
ce
Copyright (c) 1982, 2007, Oracle. All rights reserved.

eli
a b l
Connected to:
fe r
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
a n s
Production
o n -tr
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
s an
) h a deฺ
SQL> SQL> SQL> SQL>
r
PL/SQL procedure successfully completed.ฺ e s Gui
p ฺ mi Database
Oracle d e nt 11g Enterprise Edition
SQL> SQL> Disconnected from
d g Stu
a@ Oracle
Release 11.1.0.6.0 - Production
b i
With the Partitioning,
t h is Label Security, OLAP, Data Mining
( j a
and Real Application
s eAutomatic_SQL_Tuning]$
Testing options
bia to
[oracle@edrsr33p1-orcl u
io A
t o n
--------------------------------------------------------------

A n
o s e #!/bin/bash
J cd /home/oracle/solutions/Automatic_SQL_Tuning

export ORACLE_SID=orcl

export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

export
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

sqlplus / as sysdba <<FIN!

set echo on

exec dbms_workload_repository.create_snapshot;

FIN!

Oracle Database 11g: SQL Tuning Workshop A - 423


Practice 11-3: Using Automatic SQL Tuning (continued)
6) How would you confirm that a SQL Profile was automatically implemented?
a) In Oracle Enterprise Manager, locate the Automatic SQL Tuning summary page
under Server > Automated Maintenance Tasks > Automatic SQL Tuning. The
task has already run in one maintenance window and has results ready to be
viewed.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

b) View the tuning results.


c) Look at the graphs on the Task Activity Summary page.
d) Focus on understanding the pie chart and the bar graph next to it. You should be
able to get a feeling for the general finding breakdown, as well as the number of
SQL profiles implemented by the task.
e) Click View Report to see a detailed SQL-level report. Find the SQL that ran in the
n s e
AST schema. Note the green check mark meaning that the profile was
i ce
implemented.
b l el
f) Click the corresponding option button and then View Recommendations. fe r a
a n s
o n -tr
g) Click the Compare Explain Plans eyeglass icon for the SQL Profile entry.
h) View the old and new explain plans for the query. a n
i) Then click the “Recommendations for SQL_ID” h a slocatorelink
ฺ to return to the
) i d
previous screen.
i r ฺes nt Gu
j) Investigate a SQL profile. While
g p ฺmstill uondthe
e “Recommendations for SQL_ID”
d t
i a @ his S SQL Details page for this SQL.
page, click the SQL text to go to the
k) This takes you j e t History tab. Note the link to
atob the Tuning
(
ia to u
SYS_AUTO_SQL_TUNING_TASK s that is there to show that the SQL was tuned
b
n i obyAthis tuning task.
Antol) Look at the Plan Control subpage and note that a profile was created
e automatically for this SQL. The AUTO type means it was automatically created.
Jos m) Click the Statistics tab to take a look at the execution history for this SQL.
n) Depending on the speed of your machine, you may not see two hash values. If
that is the case, ignore this step and the following one. Select Real Time: Manual
Refresh from the View Data and then each of possible two Plan Hash Values from
the corresponding drop-down list. Choose one after the other and wait for the
page to refresh each time.
o) Depending on the speed of your environment, you should see one statement with
a relatively high elapsed time per execution, and one with very low elapsed time
per execution. This shows the improved plan. If you select All from the Plan Hash
Values drop-down list, you might not be able to see the execution corresponding
to the statement after tuning in the Summary graph. This might be because the
workload was too short to execute.
7) Generate a text report for more indepth information. From the command line, execute
the get_task_report.sh script. What do you observe?

Oracle Database 11g: SQL Tuning Workshop A - 424


Practice 11-3: Using Automatic SQL Tuning (continued)
a) Note the first queries that fetch execution name and object number from the
advisor schema, followed by the final query that gets the text report. In the text
report, look for the section about the SQL profile finding and peruse the
Validation Results section. This shows you the execution statistics observed
during test-execute and allows you to get a better idea about the profile’s quality.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

You can also use the report_auto_tuning_task API to get reports that span
multiple executions of the task.

./get_task_report.sh

[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$ ./get_task_report.sh

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Mar 18 16:02:18


2008
n s e
i ce
Copyright (c) 1982, 2007, Oracle. All rights reserved.
b l el
fe r a
a n s
-tr
Connected to:
n
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 -
o
an
Production

and Real Application Testing options h s


With the Partitioning, Oracle Label Security, OLAP, Data Mining
a deฺ
e )
sSQL> SQL>ui
r ฺ G
Session altered. p ฺ mi dent
SQL> SQL> SQL> SQL> SQL> SQL> SQL>

d g Stu
3a@4 is STATUS
EXECUTION_NAMEabi
SQL> SQL> 2
j e t h EXECUTION_START
(
a to u s
EXEC_1 bi
------------------------------ ----------- -------------------

o A COMPLETED 03/18/2008 15:43:54


ni SQL> SQL> SQL> 2 3 4 5 6 7
toSQL>
A n PL/SQL procedure successfully completed.
o s e
J SQL> SQL>
LAST_EXEC
--------------------------------------------------------------------
------------
EXEC_1

SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7


8 9 10
PL/SQL procedure successfully completed.

SQL> SQL>
OBJ_ID
----------
3

SQL> SQL> SQL> SQL> SQL> SQL> 2 3 GENERAL INFORMATION SECTION


--------------------------------------------------------------------
-----------
Tuning Task Name : SYS_AUTO_SQL_TUNING_TASK
Tuning Task Owner : SYS

Oracle Database 11g: SQL Tuning Workshop A - 425


Practice 11-3: Using Automatic SQL Tuning (continued)
Workload Type : Automatic High-Load SQL
Workload
Scope : COMPREHENSIVE
Global Time Limit(seconds) : 3600
Per-SQL Time Limit(seconds) : 1200
Completion Status : COMPLETED
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Started at : 03/18/2008 15:43:54


Completed at : 03/18/2008 15:44:14
Number of Candidate SQLs : 4
Cumulative Elapsed Time of SQL (s) : 27

--------------------------------------------------------------------
-----------
Object ID : 3
Schema Name: AST
SQL ID : by9m5m597zh19
n s e
SQL Text : select /*+ USE_NL(s c) FULL(s) FULL(c) AST */
i ce
c.cust_id,
b l el
where
sum(s.quantity_sold) from sh.sales s, sh.customers c
fe ra
n
s.cust_id = c.cust_id and c.cust_id < 2 group by
a s
c.cust_id
o n -tr
an
--------------------------------------------------------------------
s
-----------
) h a deฺ
FINDINGS SECTION (2 findings)
r ฺ e s Gui
mi dent
--------------------------------------------------------------------
-----------
p ฺ
g explain
d S tu plans section below)
1- SQL Profile Finding
b i a@ this
(see
--------------------------------------------------------

i a (ja"SYS_SQLPROF_01463043cc730000"
A potentially better
u seexecution plan was found for this statement.

A b
SQL profile
to was created
o statement.
ithis
automatically
n
for

nt o
e A
J o s Recommendation (estimated benefit: 98.62%)
------------------------------------------
- An automatically-created SQL profile is present on the system.
Name: SYS_SQLPROF_01463043cc730000
Status: ENABLED

Validation results
------------------
The SQL profile was tested by executing both its plan and the
original plan
and measuring their respective execution statistics. A plan may
have been
only partially executed if the other could be run to completion in
less time.

Original Plan With SQL Profile %


Improved
------------- ---------------- --------
--
Completion Status: COMPLETE COMPLETE

Oracle Database 11g: SQL Tuning Workshop A - 426


Practice 11-3: Using Automatic SQL Tuning (continued)
Elapsed Time(ms): 182 0
100%
CPU Time(ms): 182 0
100%
User I/O Time(ms): 0 0
Buffer Gets: 3177 44
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

98.61%
Disk Reads: 0 0
Direct Writes: 0 0
Rows Processed: 0 0
Fetches: 0 0
Executions: 1 1

2- Index Finding (see explain plans section below)


--------------------------------------------------
The execution plan of this statement can be improved by creating
n s e
one or more
i ce
indices.
b l el
Recommendation (estimated benefit: 90.97%)
fer a
------------------------------------------
a n s
o n -tr
- Consider running the Access Advisor to improve the physical

an
schema design
or creating the recommended index.
h s
a deฺ
create index SH.IDX$$_00010001 on SH.SALES("CUST_ID");

e )
s Gui
Rationale r ฺ
i ent
---------
p ฺ m d significantly improves the
d g
Creating the recommended
t u
indices
S
execution plan
b i a@ However,
of this statement. t h is it might be preferable to run
"Access Advisor"
a ja use
(representative
usingi
b This to
a SQL workload as opposed to a single
A
iowill allow to get comprehensive index recommendations which
statement.
o n
t takes into
A n
o s e account index maintenance overhead and additional space
J consumption.

--------------------------------------------------------------------
-----------
EXPLAIN PLANS SECTION
--------------------------------------------------------------------
-----------

1- Original With Adjusted Cost


------------------------------
Plan hash value: 4005616876

--------------------------------------------------------------------
------------
-------------------
| Id | Operation | Name | Rows | Bytes | Cost
(%CPU)| Time
| Pstart| Pstop |
--------------------------------------------------------------------
------------

Oracle Database 11g: SQL Tuning Workshop A - 427


Practice 11-3: Using Automatic SQL Tuning (continued)
-------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 902
(2)| 00:00:1
1 | | |
| 1 | HASH GROUP BY | | 1 | 13 | 902
(2)| 00:00:1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

1 | | |
| 2 | NESTED LOOPS | | 1 | 13 | 901
(2)| 00:00:1
1 | | |
|* 3 | TABLE ACCESS FULL | CUSTOMERS | 1 | 5 | 405
(1)| 00:00:0
5 | | |
| 4 | PARTITION RANGE ALL| | 1 | 8 | 495
(3)| 00:00:0
6 | 1 | 28 |
n s e
|* 5 | TABLE ACCESS FULL | SALES | 1 | 8 | 495
i ce
(3)| 00:00:0
b l el
6 | 1 | 28 |
fer a
--------------------------------------------------------------------
------------
a n s
-------------------
o n -tr
Predicate Information (identified by operation
s a nid):
h a deฺ
---------------------------------------------------
)
3 - filter("C"."CUST_ID"<2) rฺe
s Gui
5 - filter("S"."CUST_ID"<2 ฺ i "S"."CUST_ID"="C"."CUST_ID")
mAND e nt
p
g Stu d
2- Using SQL Profile@d
b i
-------------------- a t h is
i a (ja 3070788227
Plan hash value:
u se
A b to
i o
--------------------------------------------------------------------
n
nto------------
e A ---------------------------------------

J o s | Id | Operation
| Bytes |
| Name | Rows

Cost (%CPU)| Time | Pstart| Pstop |


--------------------------------------------------------------------
------------
---------------------------------------
| 0 | SELECT STATEMENT | |
1 | 13 |
55 (2)| 00:00:01 | | |
| 1 | HASH GROUP BY | |
1 | 13 |
55 (2)| 00:00:01 | | |
| 2 | NESTED LOOPS | |
1 | 13 |
54 (0)| 00:00:01 | | |
| 3 | PARTITION RANGE ALL | |
1 | 8 |
54 (0)| 00:00:01 | 1 | 28 |
| 4 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES |
1 | 8 |
54 (0)| 00:00:01 | 1 | 28 |

Oracle Database 11g: SQL Tuning Workshop A - 428


Practice 11-3: Using Automatic SQL Tuning (continued)
| 5 | BITMAP CONVERSION TO ROWIDS | |
| |
| | | |
|* 6 | BITMAP INDEX RANGE SCAN | SALES_CUST_BIX |
| |
| | 1 | 28 |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

|* 7 | INDEX UNIQUE SCAN | CUSTOMERS_PK |


1 | 5 |
0 (0)| 00:00:01 | | |
--------------------------------------------------------------------
------------
---------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

n s e
6 - access("S"."CUST_ID"<2)
i ce
filter("S"."CUST_ID"<2)
b l el
7 - access("S"."CUST_ID"="C"."CUST_ID")
filter("C"."CUST_ID"<2)
fer a
a n s
3- Using New Indices
o n -tr
an
--------------------
Plan hash value: 1871796534
h s
a deฺ
)
s Gui
--------------------------------------------------------------------
e
------------ r ฺ
i ent
ฺ m
---------------------------------------
p d
| Id | Operation
d g S t u | Name | Rows
| Bytes |
Cost (%CPU)| Time b i a@| Pstart|
t h is Pstop |
i a (ja use
--------------------------------------------------------------------
b
------------
A to
i o
---------------------------------------

nto|1n | 0 |13SELECT STATEMENT | |

e A |

J o s |
5 (0)| 00:00:01 |
1 | SORT GROUP BY NOSORT
| |
| |
1 | 13 |
5 (0)| 00:00:01 | | |
| 2 | NESTED LOOPS | |
| |
| | | |
| 3 | NESTED LOOPS | |
1 | 13 |
5 (0)| 00:00:01 | | |
|* 4 | INDEX RANGE SCAN | CUSTOMERS_PK |
1 | 5 |
2 (0)| 00:00:01 | | |
|* 5 | INDEX RANGE SCAN | IDX$$_00010001 |
1 | |
2 (0)| 00:00:01 | | |
| 6 | TABLE ACCESS BY GLOBAL INDEX ROWID| SALES |
1 | 8 |
3 (0)| 00:00:01 | ROWID | ROWID |
--------------------------------------------------------------------
------------

Oracle Database 11g: SQL Tuning Workshop A - 429


Practice 11-3: Using Automatic SQL Tuning (continued)
---------------------------------------

Predicate Information (identified by operation id):


---------------------------------------------------

4 - access("C"."CUST_ID"<2)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

5 - access("S"."CUST_ID"="C"."CUST_ID")
filter("S"."CUST_ID"<2)

--------------------------------------------------------------------
-----------

SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition


Release 11.1.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
n s e
and Real Application Testing options
i ce
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$
b l el
fe
-------------------------------------------------------------- r a
a n s
#!/bin/bash
o n -tr
cd /home/oracle/solutions/Automatic_SQL_Tuning
s an
) h a deฺ
export ORACLE_SID=orcl
r ฺ e s Gui
ฺ mi dent
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
p
d g Stu
export
b i a@ this
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca

i a (ja use
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

A to <<FIN!
b/ as sysdba
o
ni echo on
sqlplus

ntoset
e A
J o s set long 1000000000
set longchunksize 1000

--
-- Check the execution names
--
alter session set nls_date_format = 'MM/DD/YYYY HH24:MI:SS';

select execution_name, status, execution_start


from dba_advisor_executions
where task_name = 'SYS_AUTO_SQL_TUNING_TASK'
order by execution_start;

variable last_exec varchar2(30);

begin
select max(execution_name) keep (dense_rank last order by
execution_start)
into :last_exec
from dba_advisor_executions
where task_name = 'SYS_AUTO_SQL_TUNING_TASK';

Oracle Database 11g: SQL Tuning Workshop A - 430


Practice 11-3: Using Automatic SQL Tuning (continued)
end;
/

print :last_exec

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

-- Find the object ID for query AST with sql_id by9m5m597zh19


--

variable obj_id number;

begin
select object_id
into :obj_id
from dba_advisor_objects
where task_name = 'SYS_AUTO_SQL_TUNING_TASK' and
n s e
execution_name = :last_exec and
i ce
type = 'SQL' and
b l el
end;
attr1 = 'by9m5m597zh19';
fe r a
/
a n s
o n -tr
an
print :obj_id

--
h s eฺ
a onedquery
-- Get a text report to drill down on) this i
--
i r ฺ es t Gu
set pagesize 0
g p ฺm uden
d 'TEXT', t 'TYPICAL', 'ALL', :obj_id)
select dbms_sqltune.report_auto_tuning_task(
S
@ s
thi
:last_exec, :last_exec,
from dual;
a b ia
j e
FIN! b ia ( to us
n i o A how to configure Automatic SQL Tuning using Enterprise Manager.
8) Investigate
to
e An a) Back in EM, go to the Automated Maintenance Tasks page.
Jos b) The chart here shows times in the past when each client was executed, and times
in the future when they are scheduled to run again.
c) Modify the graph’s begin and end points with the widgets at the upper right.
d) Click the Configure button.
e) This brings you to the Automated Maintenance Tasks Configuration page.
f) From this page, you can disable individual clients and change which windows
they run in.
g) Disable the Automatic SQL Tuning client entirely, click Apply, and then click the
locator link to return to the last page.
h) Note that no light blue bars appear for Automatic SQL Tuning in the future.
i) Return to the configuration page, enable the task again, and click Apply to undo
your changes.

Oracle Database 11g: SQL Tuning Workshop A - 431


Practice 11-3: Using Automatic SQL Tuning (continued)
j) Click the Automatic SQL Tuning link on the Automated Maintenance Tasks
Configuration page.
k) This takes you to the page where you can configure the task itself, and set beyond
when it will run.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

l) Note that there are more fine-grained controls here, such as one that allows the
task to run but not implement profiles, and one that allows you to control the
maximum number of profiles created per run.
9) Investigate how to configure Automatic SQL Tuning using PL/SQL. From your
terminal session, execute the manual_config.sh script. What does it do?
a) Note the first action. You changed the total time limit for the task. Instead of
running for an unlimited amount of time (still bound by the maintenance window
boundaries), it now runs for a maximum of one hour. The n s e
i ce
execute_tuning_task API call runs the task immediately, in the foreground.
Use this to run the task yourself whenever you want. b l el
fe r a
a n s
./manual_config.sh
o n -tr
an
--------------------------------------------------------------
s
) h a deฺ
#!/bin/bash
r ฺ e s Gui
p ฺ mi dent
cd /home/oracle/solutions/Automatic_SQL_Tuning
d g Stu
b i a@ this
export ORACLE_SID=orcl

i a (ja use
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1

A b to
ni o
export

ntoPATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
e A l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin

J o s sqlplus / as sysdba <<FIN!

connect / as sysdba

set echo on

--
-- Configure the task to run for at most 30 minutes. The value of
the
-- TIME_LIMIT parameter determines the total time allowed for a task
execution.
--

select parameter_value
from dba_advisor_parameters
where task_name = 'SYS_AUTO_SQL_TUNING_TASK' and
parameter_name = 'TIME_LIMIT';

exec dbms_sqltune.set_tuning_task_parameter( -
'SYS_AUTO_SQL_TUNING_TASK', 'TIME_LIMIT', 1800);

Oracle Database 11g: SQL Tuning Workshop A - 432


Practice 11-3: Using Automatic SQL Tuning (continued)
select parameter_value
from dba_advisor_parameters
where task_name = 'SYS_AUTO_SQL_TUNING_TASK' and
parameter_name = 'TIME_LIMIT';

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

-- Run the task immediately


--

exec dbms_sqltune.execute_tuning_task('SYS_AUTO_SQL_TUNING_TASK');

FIN!

10) Note: In your case, the task executes quickly because the workload to take into
account is really small. However, you could use the interrupt_task.sh script
from another session to stop the task, should it last too long. n s e
i ce
[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$ cat interrupt_task.sh
b l el
#!/bin/bash
fe r a
a n s
-tr
cd /home/oracle/solutions/Automatic_SQL_Tuning

o n
an
export ORACLE_SID=orcl
s
a deฺ
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
h
e )
s Gui
export
r ฺ
ฺ mi dent
PATH=/u01/app/oracle/product/11.1.0/db_1/bin:/bin:/usr/bin:/usr/loca
p
g Stu
l/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_11/bin
d
sqlplus / as sysdba
b i a@
<<FIN! is
th
a
connect / as(jsysdbause
A biona to
setoecho
ni
to--
A n
o s e -- Interrupt the task
J --

exec dbms_sqltune.interrupt_tuning_task('SYS_AUTO_SQL_TUNING_TASK');

FIN!

[oracle@edrsr33p1-orcl Automatic_SQL_Tuning]$

11) Ensure that you disable automatic implementation of SQL profiles to clean up your
environment.

a) On the EM Server page, click Automated Maintenance Tasks.


b) Check that Status is set to Enabled, and click Configure.
c) Click the Configure button next to Automatic SQL Tuning.
d) Select No for “Automatic Implementation of SQL Profiles.”
e) Then, click Apply.

Oracle Database 11g: SQL Tuning Workshop A - 433


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

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

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

i el
ce n s e
A
Access path 3-16, 3-17, 3-19, 3-23, 3-24, 3-28, 3-32, 4-2, 4-3,
4-4, 4-6, 4-8, 4-10, 4-14, 4-17, 4-29, 4-31, 4-33, 4-43, 4-47,
4-56, 4-71, 4-72, 5-49, 6-8, 6-14, 9-2, 9-9, 9-10, 9-11, 9-12,
9-13, 9-14, 9-17, 9-30, 11-5, 11-7, 11-10, 11-15
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Action 1-11, 1-12, 1-22, 1-23, 2-7, 2-9, 2-13, 2-14, 2-15, 2-17,
2-18, 2-19, 3-3, 3-12, 3-25, 3-30, 4-3, 4-6, 4-30, 4-70, 6-14,
7-20, 7-22, 7-32, 8-8, 10-4, 10-8, 10-9, 10-10, 10-13, 10-14, 11-3,
11-15, 11-21, 11-24, 11-25
Adaptive Cursor Sharing 8-2, 8-12, 8-13, 8-14, 8-15, 8-16, 8-17,
8-18, 8-19, 8-20, 8-21 n s e
i ce
ADDM 2-6, 2-8, 2-9, 2-10, 2-11, 2-14, 2-15, 5-27, 5-28, 11-4,
b l el
11-9, 11-12, 11-14, 11-26, 11-38 fer a
a n s
ADR 10-5, 10-14, 10-15
o n -tr
AGGREGATE 1-6, 1-17, 1-18, 2-19, 3-5, 3-20, 3-21, 3-29,n3-30,
4-20, 4-27, 4-30, 4-31, 4-48, 4-52, 4-53, 5-26, a s a5-47,
5-38, eฺ 6-10,
)h id
10-19, 10-25, 10-28, 10-29
i r ฺes nt Gu
ALL_ROWS 3-32, 3-33, 5-14, 9-4, 9-8,
g p ฺm9-10,ud10-26,
e 10-28, 10-29,
d t
11-7 @
ia
b 4-12, sS
hi
t4-20, 4-24, 4-27, 4-37, 4-38,
ALTER 1-8, 3-3, 3-11,
( j a3-28, s e
4-50, 5-11,b ia 5-44,
5-25, t o u7-8, 7-22, 7-26, 8-11, 8-12, 9-10, 10-12,
n
11-11 i oA
n to
A
eALTER SESSION 3-3, 5-44, 8-11, 8-12, 9-10, 10-12
Jos ALTER SYSTEM 3-3, 7-22, 8-11
AND-EQUAL 4-25
Anti Join 4-61, 4-65
APPEND 2-20, 4-68, 9-23
Architecture 1-1, 1-2, 1-3, 1-12, 1-19, 1-20, 8-14, 8-15
Ascending 4-10, 4-11, 4-15, 4-16, 4-17, 9-12, 9-14
ASH 1-14, 1-17, 1-19, 2-6, 2-8, 2-9, 3-27, 3-28, 3-30, 4-4,
4-5, 4-11, 4-24, 4-44, 4-45, 4-46, 4-47, 4-48, 4-51, 4-54, 4-58,
4-59, 4-62, 4-63, 4-64, 4-65, 4-67, 4-70, 5-11, 5-12, 5-14, 5-20,
5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27, 5-30, 5-32, 5-37, 5-44,
5-47, 5-49, 6-6, 6-12, 6-13, 6-16, 8-3, 9-11, 9-22, 9-29, 11-18,
11-26

Oracle Database 11g: SQL Tuning Workshop Index - 2


A
AST 1-5, 1-7, 1-16, 2-3, 2-6, 2-18, 2-19, 3-5, 3-11, 3-12,
3-16, 3-17, 3-25, 3-28, 3-29, 3-30, 3-33, 4-4, 4-5, 4-6, 4-7,
4-9, 4-10, 4-13, 4-19, 4-20, 4-21, 4-24, 4-27, 4-28, 4-29, 4-32,
4-36, 4-40, 4-45, 4-46, 4-67, 4-69, 5-4, 5-9, 5-11, 5-13, 5-19,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

5-24, 5-25, 5-26, 5-27, 5-35, 5-36, 5-37, 5-38, 5-39, 5-44, 5-45,
5-46, 6-7, 6-10, 6-14, 7-16, 7-19, 7-26, 7-33, 8-3, 8-8, 8-10,
9-3, 9-7, 9-10, 9-11, 9-14, 9-15, 9-20, 9-21, 9-22, 9-23, 9-24,
9-28, 9-30, 9-31, 10-17, 10-27, 11-3, 11-5, 11-7, 11-16, 11-17, 11-18,
11-27, 11-28, 11-29, 11-30, 11-31, 11-33, 11-37
AUDIT 3-3 n s e
lice
Automatic 1-8, 1-14, 1-15, 1-16, 1-17, 1-18, 1-24, 2-6, 2-7,
a b le
2-8, 2-10, 2-11, 2-12, 2-15, 2-20, 3-30, 3-33, 4-10, 5-7, 5-10, fer
s
5-15, 5-27, 5-28, 5-29, 5-30, 5-35, 6-7, 6-10, 7-3, 7-4, 7-13, tran
o n -
a n
7-15, 7-21, 7-24, 7-25, 7-26, 7-27, 7-34, 7-36, 8-12, 8-13, 8-16,
8-19, 10-6, 10-8, 11-2, 11-3, 11-4, 11-5, 11-6, a s eฺ 11-11,
11-7, 11-8,
)h id
i r ฺes 11-30,
11-12, 11-15, 11-26, 11-27, 11-28, 11-29,
t G u 11-32, 11-33, 11-34,
11-31,
11-35, 11-36, 11-37, 11-38
g p ฺm uden
St 11-30, 11-31, 11-32,
@d11-27,s 11-28,
Automatic SQL tuning 2-6, 11-2,
11-33, 11-34, 11-35,j
ia
ab11-36, e hi
t 11-38
11-37,
(
iaOptimizer s
u11-3, 11-5, 11-6, 11-7
Automatic Tuningb t o
n
AUTOTRACE i o A5-4, 5-6, 5-15, 5-16, 5-17, 5-18, 5-19, 9-15, 10-25
n to
A
eAWR 2-6, 2-8, 2-9, 2-12, 5-4, 5-6, 5-21, 5-27, 5-28, 5-29,
Jos 5-30, 5-31, 5-32, 5-33, 5-34, 11-12, 11-14, 11-27, 11-32
awrsqrpt 2-8, 5-34
B
B*tree 3-19, 4-10, 4-11, 4-12, 4-18, 4-26, 4-27, 4-30, 4-46, 7-4,
7-8
background process 1-2, 1-3, 1-4, 1-6, 1-8, 1-15, 1-18, 1-20,
1-25, 10-12
BIND 2-14, 2-16, 2-22, 2-23, 2-24, 2-25, 3-6, 3-7, 3-11, 3-12,
3-13, 4-23, 5-14, 5-15, 6-8, 8-1, 8-2, 8-4, 8-5, 8-6, 8-7,
8-8, 8-11, 8-12, 8-13, 8-14, 8-15, 8-16, 8-17, 8-18, 8-19, 8-20,
8-21, 9-24, 10-10, 10-11, 10-12, 10-15, 10-16, 10-17, 11-12
bind peeking 5-15, 8-2, 8-13, 8-14, 8-20, 8-21

Oracle Database 11g: SQL Tuning Workshop Index - 3


B
Bind variable 2-14, 2-16, 2-22, 2-23, 2-24, 2-25, 3-7, 5-15,
6-8, 8-1, 8-2, 8-4, 8-5, 8-6, 8-7, 8-8, 8-11, 8-12, 8-13,
8-14, 8-16, 8-18, 8-19, 8-20, 9-24, 10-17
Bind-aware 8-14, 8-15, 8-16, 8-17
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Bind-sensitive 8-14, 8-16


Bitmap 1-14, 3-30, 4-4, 4-10, 4-11, 4-18, 4-20, 4-30, 4-31, 4-32,
4-33, 4-34, 4-35, 4-36, 4-39, 5-26, 6-5, 6-7, 6-8, 6-9, 6-10,
6-11, 6-13, 6-15, 6-16, 6-18, 6-19, 6-20, 6-21, 7-9, 9-12, 9-14,
9-15, 9-16
BITMAP AND 4-33, 4-34, 6-11, 6-13, 6-16, 9-16 n s e
i ce
BITMAP CONVERSION 4-31, 4-33, 4-34, 6-13, 6-16, 9-16
b l el
Bitmap create area 1-14 fe ra
n s
BITMAP INDEX 4-4, 4-10, 4-11, 4-20, 4-30, 4-31, 4-32, 4-33, 4-34,
n - tra
no
4-35, 4-39, 6-5, 6-7, 6-8, 6-9, 6-10, 6-11, 6-13, 6-15, 6-16,
a
6-18, 6-19, 6-20, 6-21, 7-9, 9-12, 9-15, 9-16 as eฺ
) h i d
Bitmap Join Index 4-11, 4-35, 6-15, 6-16, 6-18,
i r ฺes 6-19,
t G u 6-21,
6-20,
9-12
g p ฺm uden
BITMAP KEY ITERATION 4-34, d6-10, 6-13,
S t 6-16
@ i s
ia 4-34,th6-11,
BITMAP MERGE 1-14, ( j a b
3-30, e 6-13, 6-16, 9-16
i a u s
b 1-14to
Bitmap mergeAarea
BITMAP i o
nMINUS 4-34
n t o
e A
BITMAP OR 6-16, 9-16
J o s Branch 4-12, 4-20, 4-21, 4-22, 4-46, 4-68, 5-41, 5-48
Buffer cache 1-6, 1-7, 1-10, 1-11, 1-15, 1-16, 1-18, 2-15, 3-5,
3-27, 3-30, 3-32, 4-5, 4-45, 4-49, 5-18, 9-24
BUFFER SORT 4-49, 4-60, 6-13, 6-16
C
CACHE 1-6, 1-7, 1-9, 1-10, 1-11, 1-12, 1-15, 1-16, 1-18, 2-15,
3-5, 3-9, 3-14, 3-27, 3-29, 3-30, 3-31, 3-32, 4-5, 4-45, 4-49,
4-70, 4-72, 5-4, 5-18, 5-20, 5-23, 5-24, 5-25, 5-32, 7-20, 8-3,
8-4, 8-16, 8-19, 9-24, 9-25, 10-16, 10-23, 10-26, 10-28, 10-29, 11-12,
11-17, 11-21
Cardinality 2-3, 3-24, 3-26, 5-3, 5-38, 7-5, 7-12, 7-24, 7-31,
9-24, 11-7

Oracle Database 11g: SQL Tuning Workshop Index - 4


C
Cartesian 2-3, 4-49, 4-60, 5-49, 11-11
Child 5-3, 5-20, 5-21, 5-23, 5-24, 5-25, 5-40, 5-41, 5-43, 5-44,
5-45, 5-46, 5-48, 8-12, 8-13, 8-15
Client 1-4, 1-12, 3-5, 3-8, 3-9, 3-10, 5-18, 5-19, 10-3, 10-4,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

10-7, 10-8, 10-9, 10-10, 10-13, 10-14, 10-15


CLIENT_IDENTIFIER 10-8, 10-10, 10-14
CLOSE 1-5, 3-3, 3-6, 3-7, 3-12, 3-13, 3-28, 5-44, 9-5, 10-16,
10-27
Cluster 1-3, 1-22, 2-12, 2-19, 4-4, 4-6, 4-8, 4-10, 4-13,
4-44, 4-45, 4-46, 4-47, 4-56, 7-4, 7-6, 7-8, 7-9, 9-11, 10-6 n s e
i ce
Clustering factor 4-45, 4-46, 7-4, 7-6, 7-8, 7-9
b l el
COMMENT 2-24, 3-3, 9-3, 9-5, 9-6 fer a
a n s
COMMIT 1-11, 1-22, 2-13, 3-3, 3-12, 10-16, 10-18
n - tr
Composite 4-10, 4-21, 4-23, 4-28, 4-36, 4-44, 4-55, 5-37 n o
a
CONCATENATION 4-50, 4-66, 4-68, 9-17 as ฺ
)h ide
CONNECT 1-3, 1-4, 1-5, 1-6, 1-7, 1-19, 2-13,
i r ฺes2-14,t G u
2-22,

g ฺm 9-27,
2-25, 3-3, 3-5, 3-21, 4-12, 5-10,p5-15,
u d en10-6, 10-7, 10-8,
t
10-20 @d s S
ia hi
j ab 2-14,
Connection 1-4, 1-5, (2-13, e t 2-25, 10-6, 10-7, 10-8
2-22,
ia 2-20, s
u3-20, 4-10, 4-14, 4-20, 4-27, 4-35,
Constraint 2-18,
A b 2-19, t o
n
4-39,i o4-40, 7-7, 11-3
n to
A file 1-15, 1-19
econtrol
Jos CONTROL_MANAGEMENT_PACK_ACCESS 5-35, 9-25
Cost 1-14, 2-3, 2-4, 3-16, 3-18, 3-19, 3-24, 3-26, 3-27, 3-28,
3-30, 3-32, 3-33, 3-34, 4-4, 4-5, 4-7, 4-8, 4-9, 4-11, 4-14,
4-15, 4-16, 4-17, 4-18, 4-19, 4-20, 4-23, 4-25, 4-26, 4-29, 4-31,
4-34, 4-39, 4-40, 4-45, 4-46, 4-47, 4-49, 4-50, 4-51, 4-52, 4-53,
4-54, 4-55, 4-58, 4-59, 4-60, 4-64, 5-3, 5-11, 5-12, 5-14, 5-25,
5-32, 5-38, 6-3, 6-7, 6-12, 6-14, 6-17, 7-3, 7-5, 7-6, 7-8,
7-9, 7-16, 7-20, 7-31, 7-32, 9-8, 9-10, 9-12, 9-15, 9-17, 9-19,
9-24, 9-25, 9-29, 10-17, 11-7, 11-16, 11-17, 11-21, 11-22, 11-25, 11-30
COUNT STOPKEY 4-52

Oracle Database 11g: SQL Tuning Workshop Index - 5


C
CPU 1-8, 2-3, 2-12, 3-27, 4-5, 4-8, 4-26, 4-30, 4-31, 4-47,
4-51, 4-52, 4-58, 4-60, 4-64, 5-11, 5-12, 5-14, 5-25, 5-27, 5-32,
5-35, 5-37, 5-38, 5-39, 7-3, 7-4, 7-20, 7-26, 8-13, 8-17, 9-29,
10-16, 10-24, 10-25, 10-28, 10-29, 11-4, 11-14, 11-21, 11-29, 11-30
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

CREATE 1-4, 1-5, 1-6, 1-8, 1-14, 1-21, 1-22, 1-24, 2-11, 2-19,
2-20, 2-24, 3-3, 3-5, 3-7, 3-8, 3-9, 3-11, 3-18, 3-21, 3-22,
3-30, 4-6, 4-10, 4-14, 4-15, 4-16, 4-17, 4-18, 4-19, 4-20, 4-23,
4-24, 4-26, 4-27, 4-28, 4-29, 4-35, 4-36, 4-38, 4-39, 4-40, 4-41,
4-44, 4-45, 4-46, 4-47, 4-51, 4-67, 5-7, 5-10, 5-15, 5-29, 5-33,
6-7, 6-14, 6-15, 6-18, 6-19, 6-20, 6-21, 7-4, 7-11, 7-13, 7-15, n s e
lice
7-16, 7-17, 7-18, 7-19, 7-21, 7-26, 7-29, 9-28, 10-7, 10-8, 10-10,
a b le
fer
10-20, 10-27, 10-29, 10-31, 11-5, 11-7, 11-8, 11-9, 11-10, 11-12, 11-14,
s
11-15, 11-16, 11-21, 11-25, 11-27, 11-28, 11-29, 11-36
- t r an
Cube 8-14, 8-15, 8-16 n on
s a3-12,
Cursor 1-14, 2-14, 2-16, 2-22, 2-23, 2-24, 2-25, 3-7,a3-8, eฺ
)h
es 5-24,
3-13, 3-29, 5-4, 5-6, 5-20, 5-21, 5-22, ฺ5-23,
id
u 5-26,
5-25,
i r t G
5-32, 5-44, 7-26, 8-2, 8-3, 8-4, p
g 8-8, en 8-11, 8-12,
ฺm8-9,ud8-10,
St 8-20, 8-21, 9-24, 10-16,
@d 8-18,s 8-19,
8-13, 8-14, 8-15, 8-16, 8-17,
10-17, 10-27, 11-12, j
ia
ab11-29 e thi
(
ia 2-25, s
u 8-2, 8-3, 8-4, 8-8, 8-9,
Cursor Sharing A b2-16, t o 3-29,
n
8-10, i o8-12, 8-13, 8-14, 8-15, 8-16, 8-17, 8-18, 8-19, 8-20, 8-21
n to
A
eCURSOR_SHARING 2-24, 3-29, 8-11, 8-12, 8-19, 8-21, 9-24, 11-29
Jos CURSOR_SHARING_EXACT 8-11, 9-24
D
Data block 1-7, 1-22, 1-23, 2-15, 4-4, 4-7, 4-12, 4-21, 4-44,
4-47, 4-49, 4-56, 5-18, 7-5, 7-6, 7-7, 7-28, 10-25, 10-29
Data dictionary cache 1-9, 3-5, 10-26
data file 1-7, 1-10, 1-11, 1-15, 1-19, 1-21, 1-23, 10-25
Database writer 1-7, 1-10, 1-15
DB_FILE_MULTIBLOCK_READ_COUNT 3-29, 4-5, 4-6, 4-20, 7-5
DBA_HIST_ACTIVE_SESS_HISTORY 5-31
DBA_HIST_BASELINE DBA_HIST_DATABASE_INSTANCE 5-31
DBA_HIST_SNAPSHOT 5-29, 5-30, 5-31
DBA_HIST_SQL_PLAN 5-4, 5-31

Oracle Database 11g: SQL Tuning Workshop Index - 6


D
DBA_HIST_WR_CONTROL 5-30, 5-31
DBA_IND_STATISTICS 7-6, 7-8
DBA_TAB_COL_STATISTICS 3-25, 7-10
DBA_TAB_STATISTICS 3-25, 7-5, 7-34
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

DBA_TABLES 3-25
DBMS_APPICATION_INFO 10-4
DBMS_MONITOR 5-4, 10-4, 10-10, 10-11, 10-12, 10-13, 10-15
DBMS_SERVICE 10-4
DBMS_SESSION 10-4, 10-8, 10-10, 10-12, 10-15
DBMS_SQLTUNE 5-35, 5-37, 11-7, 11-30 n s e
i ce
DBMS_STATS 2-15, 7-3, 7-13, 7-15, 7-17, 7-18, 7-19, 7-20, 7-22,
b l el
7-23, 7-24, 7-25, 7-26, 7-28, 7-29, 7-30, 7-34, 11-6 fe ra
n s
DBMS_WORKLOAD_REPOSITORY 5-28, 5-29, 5-30, 5-33, 11-32
n - tra
DBMS_XPLAN 5-6, 5-11, 5-12, 5-14, 5-25, 5-32, 5-33, 5-34,
a no5-44,
9-29
h a s eฺ
DECLARE 3-3, 4-26, 7-16 e s ) u id

ir 3-26, n G
t3-34,
DEFINE 1-19, 3-3, 3-6, 3-7, 3-10, 3-12, ฺ m3-22, e 4-10,
d gp Stud
4-18, 4-27, 4-29, 4-44, 4-54,
b i a@ 4-55,
t h is 6-2, 6-22, 7-5, 7-19, 7-20,
i a (ja 10-5,
8-5, 8-6, 9-24, 9-26,
u se10-7, 10-9, 10-10, 11-20, 11-31, 11-32
DELETE 1-8,A 1-19, to 3-12, 4-5, 4-18, 5-3, 5-15, 5-36,
b 3-3, 3-11,
o
ni 7-26, 7-28, 7-33, 7-34, 8-15, 9-5, 9-6, 9-28, 10-20, 10-25,
7-25,
t o
e An10-29
J os Descending 4-10, 4-11, 4-13, 4-15, 4-16, 4-17, 9-12, 9-13, 10-19
DESCRIBE 1-2, 1-25, 2-2, 2-26, 3-2, 3-3, 3-6, 3-7, 3-10,
3-35, 3-36, 4-16, 6-10, 6-11, 7-3, 7-26, 7-27, 9-21, 10-5, 11-19
DIAGNOSTIC_DEST 10-5
Dimension 2-12, 4-61, 6-3, 6-4, 6-6, 6-7, 6-8, 6-9, 6-10,
6-11, 6-12, 6-13, 6-14, 6-15, 6-16, 6-17, 6-18, 6-20, 6-21, 10-6,
11-17, 11-21
Dispatcher 1-4, 1-5
Domain 4-10, 4-11, 9-12, 9-14
DRIVING_SITE 9-22
DROP 1-8, 3-3, 4-10, 4-27, 4-37, 4-38, 4-39, 4-40, 5-10, 5-29,
5-30, 10-20, 10-27, 11-16, 11-17

Oracle Database 11g: SQL Tuning Workshop Index - 7


D
dynamic sampling 3-25, 3-33, 3-34, 7-2, 7-17, 7-19, 7-24, 7-31,
7-32, 7-33, 7-34, 7-35, 9-24, 11-6, 11-7
Dynamic SQL 2-14, 2-16
DYNAMIC_SAMPLING 3-25, 3-32, 3-33, 7-17, 7-31, 7-32, 7-33, 9-24,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

9-25
E
Equi 1-7, 1-16, 1-17, 2-2, 2-5, 2-6, 2-7, 2-8, 2-14, 2-15,
2-17, 2-18, 2-19, 2-20, 2-23, 2-25, 3-7, 3-10, 3-18, 3-19, 3-20,
3-21, 3-22, 3-23, 4-7, 4-10, 4-28, 4-30, 4-35, 4-36, 4-39, 4-40,
n s e
4-48, 4-56, 4-58, 4-59, 4-61, 4-62, 4-65, 5-3, 5-15, 5-18, 5-19,
lice
5-24, 5-34, 6-2, 6-4, 6-7, 6-19, 6-20, 6-22, 7-5, 7-6, 7-11,
a b le
7-17, 7-25, 7-27, 9-13, 9-24, 9-26, 10-20, 10-25, 10-28, 10-29, 11-4, s f er
11-8, 11-11, 11-16, 11-22, 11-27, 11-29 - t r an
Estimator 3-24, 3-25, 3-26, 3-27, 7-11 n on
s a
EXECUTE 1-10, 2-14, 2-15, 2-23, 2-25, 3-3, 3-4, h 3-6, 3-7, eฺ
a
e s ) u id
3-8, 3-9, 3-12, 3-13, 3-14, 3-19, 3-20, i r ฺ
3-24,
n t G
3-27, 4-11, 4-18,
4-50, 4-51, 4-58, 4-69, 4-70, g 5-4,
m
pฺ 5-7,t5-9, de5-10, 5-11, 5-18,
d u
S 5-41, 5-42, 5-43, 5-45,
5-20, 5-25, 5-26, 5-32, i a @ 5-38,
5-35, i s 5-40,
j ab 7-23, e th
5-46, 5-47, 5-48,( 7-22,
ia 9-11, u s 7-32, 8-4, 8-7, 8-8, 8-9, 8-10,
A
8-11, 8-14,b8-15, t o 9-22, 9-24, 9-26, 10-10, 10-16, 10-17, 10-24,
i o
ton 10-26, 10-27, 10-28, 10-29, 11-7, 11-30, 11-32, 11-36
10-25,
e An plan 2-3, 2-10, 2-11, 2-13, 2-15, 2-20, 2-21, 2-22,
Execution
J os 3-16, 3-17, 3-18, 3-19, 3-20, 3-24, 3-26, 3-28, 3-32, 3-33, 4-2,
4-14, 4-16, 4-50, 4-55, 4-56, 4-59, 4-70, 5-1, 5-2, 5-3, 5-4,
5-5, 5-6, 5-7, 5-8, 5-9, 5-10, 5-11, 5-14, 5-15, 5-16, 5-17,
5-20, 5-24, 5-25, 5-31, 5-32, 5-33, 5-38, 5-39, 5-40, 5-41, 5-42,
5-43, 5-44, 5-45, 5-46, 5-47, 5-48, 5-49, 5-50, 5-51, 5-52, 6-3,
6-6, 6-14, 7-3, 7-31, 7-35, 8-3, 8-4, 8-9, 8-10, 8-13, 8-14,
8-15, 8-18, 9-3, 9-7, 9-8, 9-10, 9-14, 9-15, 9-16, 9-20, 9-22,
9-24, 9-26, 9-29, 9-30, 9-32, 10-16, 10-17, 10-20, 10-23, 10-26, 10-27,
10-30, 11-3, 11-4, 11-6, 11-26, 11-29, 11-30
Expansion 3-18, 3-19, 4-68, 9-17

Oracle Database 11g: SQL Tuning Workshop Index - 8


E
EXPLAIN PLAN 4-5, 4-51, 4-70, 5-3, 5-4, 5-6, 5-7, 5-8, 5-9,
5-10, 5-11, 5-12, 5-13, 5-15, 5-20, 5-40, 5-41, 5-44, 5-45, 5-50,
9-3, 9-29, 10-20, 10-27, 10-29
Expression Statistics 7-19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Extended statistics 7-4


Extent 1-21, 1-22, 1-23, 2-15, 5-48
F
FACT 2-13, 3-16, 3-29, 4-40, 4-45, 4-46, 4-61, 6-3, 6-4, 6-7,
6-8, 6-9, 6-10, 6-11, 6-12, 6-14, 6-15, 6-17, 6-18, 6-19, 7-4,
n s e
7-6, 7-8, 7-9, 7-14, 7-20, 7-29, 9-19, 9-20, 11-6, 11-7, 11-28
lice
Fact table 6-3, 6-7, 6-8, 6-9, 6-10, 6-11, 6-12, 6-14, 6-15,
a b le
6-17, 6-18, 9-19 s f er
Fast full 3-29, 3-30, 4-4, 4-9, 4-13, 4-19, 4-20, 4-24, 4-27, - t r an
4-29, 4-36, 4-69, 8-10 n on
s a
FETCH 3-3, 3-6, 3-7, 3-8, 3-10, 3-12, 3-13, 4-14, h
) a
4-15, 4-56,
i d eฺ
4-58, 4-59, 4-67, 5-17, 6-10, 7-8, 10-16,
i r ฺes10-17,
t G u 10-24, 10-25,
10-18,
10-28, 10-29 g p ฺm uden
d 4-17, S t
filter 3-25, 3-26, 4-5, 4-6, 4-14,
i a @ 4-15,
h s
i 4-18, 4-19, 4-20,
b
a 4-31, t
4-21, 4-23, 4-25,
i a (j4-29, u se 4-32, 4-45, 4-46, 4-47, 4-49, 4-50,
A
4-52, 4-55, to 4-58, 4-59, 4-60, 4-62, 4-64, 4-66, 4-67,
b4-56, 4-57,
o
ni 5-3, 5-11, 5-12, 5-25, 5-42, 5-44, 5-49, 6-6, 6-7, 6-9,
o
4-68,
t
e An6-10, 6-14, 9-22, 10-18, 11-12, 11-17, 11-21
Jos FILTER 3-25, 3-26, 4-5, 4-6, 4-14, 4-15, 4-17, 4-18, 4-19, 4-20,
4-21, 4-23, 4-25, 4-29, 4-31, 4-32, 4-45, 4-46, 4-47, 4-49, 4-50,
4-52, 4-55, 4-56, 4-57, 4-58, 4-59, 4-60, 4-62, 4-64, 4-66, 4-67,
4-68, 5-3, 5-11, 5-12, 5-25, 5-42, 5-44, 5-49, 6-6, 6-7, 6-9,
6-10, 6-14, 9-22, 10-18, 11-12, 11-17, 11-21
FIRST ROW 4-35, 4-46, 4-53, 4-58, 4-59, 4-60, 5-45, 9-10, 9-21,
9-22, 11-5
FIRST_ROWS 3-32, 3-33, 9-10, 11-7
foreground process 1-4, 5-18, 10-12
Foreign key 2-20, 4-35, 6-3, 6-4, 6-7, 6-9, 6-18
Frequency 5-28, 7-10, 7-11, 7-12, 7-13, 7-17, 7-25, 7-27

Oracle Database 11g: SQL Tuning Workshop Index - 9


F
FULL 1-22, 2-10, 2-14, 2-15, 2-17, 3-17, 3-19, 3-28, 3-29, 3-30,
3-32, 4-3, 4-4, 4-5, 4-6, 4-9, 4-13, 4-19, 4-20, 4-24, 4-26,
4-27, 4-29, 4-34, 4-36, 4-45, 4-46, 4-47, 4-49, 4-51, 4-52, 4-53,
4-55, 4-56, 4-57, 4-58, 4-59, 4-60, 4-61, 4-62, 4-63, 4-64, 4-65,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4-67, 4-69, 4-70, 5-11, 5-12, 5-14, 5-25, 5-28, 5-30, 5-32, 5-33,
5-38, 5-39, 5-42, 5-44, 5-47, 5-49, 6-6, 6-13, 6-14, 6-16, 7-6,
7-8, 7-34, 8-5, 8-8, 8-10, 8-18, 9-7, 9-11, 9-12, 9-14, 9-19,
9-22, 9-24, 9-27, 9-29, 9-30, 10-17, 10-27, 10-28, 11-10, 11-18, 11-25,
11-27
Full Table Scan 2-14, 2-15, 3-17, 3-19, 3-29, 3-30, 3-32, 4-4, n s e
lice
4-5, 4-6, 4-9, 4-20, 4-45, 5-42, 5-44, 5-49, 6-14, 7-6, 7-8,
a b le
8-8, 8-18, 9-11, 9-12, 9-14, 9-22, 9-24, 9-29, 10-28, 11-10 fer
s
Function based 2-4, 4-10, 4-41
- t r an
no Function-based 4-6, 4-11, 4-17, 4-18, 7-19, 9-14
n
a
s eฺ
G h a
e s ) u id

ir ent G
Gather 2-11, 4-11, 5-2, 5-3, 5-10, 5-22, 5-26, 5-51, 6-7, 7-2,
ฺ m
gp 7-15,
7-3, 7-4, 7-5, 7-6, 7-9, 7-11, 7-13,
d S tud7-17, 7-19, 7-20,
7-21, 7-22, 7-23, 7-24,i @ 7-26,
a7-25, h is 7-27, 7-28, 7-29, 7-30, 7-31,
b
a 11-28, t
7-34, 7-36, 11-5,
i a (j11-6, u se 11-29
A
Generator 3-24,b3-28 to
o
ni 1-6, 1-14, 1-22, 5-7, 5-10, 5-37, 7-25, 7-26, 9-28,
Global
t o1-3,
e An10-10, 10-11, 11-10, 11-18, 11-30
Jos GRANT 2-25, 3-3, 3-34, 5-15, 5-29
GROUP BY 2-4, 3-21, 3-25, 3-26, 4-15, 4-48, 4-51, 4-67, 4-70,
6-5, 6-6, 6-13, 6-16, 9-18, 9-22, 9-30
H
Hard parse 2-23, 3-14, 3-18, 8-7, 8-13, 8-14, 8-15, 10-23
HASH 1-14, 1-17, 3-27, 3-28, 3-30, 4-4, 4-5, 4-24, 4-44, 4-45,
4-46, 4-47, 4-48, 4-51, 4-54, 4-58, 4-59, 4-62, 4-63, 4-64, 4-65,
4-67, 4-70, 5-11, 5-12, 5-14, 5-20, 5-21, 5-22, 5-23, 5-24, 5-25,
5-26, 5-32, 5-37, 5-44, 5-47, 5-49, 6-6, 6-12, 6-13, 6-16, 8-3,
9-11, 9-22, 9-29, 11-18
Hash Cluster 4-4, 4-44, 4-45, 4-46, 4-47

Oracle Database 11g: SQL Tuning Workshop Index - 10


H
Hash Join 1-14, 3-28, 4-5, 4-24, 4-51, 4-54, 4-58, 4-59, 4-62,
4-63, 4-64, 4-65, 5-22, 5-32, 5-47, 6-6, 6-12, 6-13, 6-16, 9-22,
9-29
Hash join area 1-14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Height-Balanced 7-14, 7-15


High-load 2-11, 10-4, 11-3, 11-4, 11-9, 11-12, 11-14, 11-26
Hint 3-24, 3-30, 4-6, 4-9, 4-17, 4-20, 4-24, 4-25, 4-26, 4-65,
4-70, 5-14, 5-20, 5-22, 5-26, 5-35, 5-47, 6-8, 6-17, 7-31, 8-11,
9-1, 9-2, 9-3, 9-4, 9-5, 9-6, 9-7, 9-8, 9-9, 9-10, 9-11,
9-12, 9-13, 9-14, 9-15, 9-16, 9-17, 9-18, 9-19, 9-20, 9-21, 9-22, n s e
i ce
9-23, 9-24, 9-25, 9-26, 9-27, 9-28, 9-29, 9-30, 9-31, 9-32, 10-26,
b l el
10-27, 11-2, 11-37 fe ra
a n s
Histogram 2-8, 2-9, 3-25, 3-26, 7-4, 7-10, 7-11, 7-12, 7-13,
o n -tr
n 8-16,
7-14, 7-15, 7-16, 7-17, 7-26, 7-29, 7-34, 8-10, 8-11, 8-14,
a
8-18
h as eฺ
Host 1-4, 2-9, 10-7 e s ) u id

ir ent G
ฺ m
I
d gp Stud
b a@ 2-11,
Index 1-22, 1-23, 2-3, 2-4,i2-10,
t h is2-13, 2-15, 2-16, 2-17,
2-18, 2-20, 2-25, a 3-16,
(j3-11, se 3-17, 3-19, 3-21, 3-22, 3-23, 3-28,
i a u
to 4-3, 4-4, 4-5, 4-6, 4-7, 4-9, 4-10,
b3-32, 3-34,
A
3-29, 3-30,
o
o ni 4-12, 4-13, 4-14, 4-15, 4-16, 4-17, 4-18, 4-19, 4-20, 4-21,
4-11,
t
e An4-22, 4-23, 4-24, 4-25, 4-26, 4-27, 4-28, 4-29, 4-30, 4-31, 4-32,
J os 4-33, 4-34, 4-35, 4-36, 4-37, 4-38, 4-39, 4-40, 4-41, 4-42, 4-44,
4-45, 4-46, 4-47, 4-48, 4-49, 4-50, 4-51, 4-52, 4-53, 4-54, 4-55,
4-56, 4-57, 4-58, 4-59, 4-60, 4-63, 4-65, 4-67, 4-68, 4-69, 5-25,
5-42, 5-44, 5-45, 5-46, 5-49, 5-50, 6-5, 6-7, 6-8, 6-9, 6-10,
6-11, 6-13, 6-15, 6-16, 6-18, 6-19, 6-20, 6-21, 7-4, 7-6, 7-7,
7-8, 7-9, 7-11, 7-17, 7-19, 7-20, 7-24, 7-26, 7-28, 7-29, 7-30,
7-31, 7-33, 7-34, 8-3, 8-8, 8-10, 8-11, 8-18, 9-3, 9-4, 9-8,
9-9, 9-11, 9-12, 9-13, 9-14, 9-15, 9-16, 9-20, 9-21, 9-22, 9-23,
9-24, 9-25, 9-28, 9-29, 9-30, 10-14, 10-26, 10-27, 10-28, 10-29, 11-5,
11-8, 11-9, 11-10, 11-11, 11-16, 11-17, 11-18, 11-28, 11-33
Index Cluster 4-44, 4-45, 4-46, 7-8, 7-9
Index join 4-4, 4-13, 4-24, 9-13

Oracle Database 11g: SQL Tuning Workshop Index - 11


I
Index Organized Table 4-10, 4-11
Index Scan 4-4, 4-7, 4-13, 4-16, 4-19, 4-20, 4-21, 6-16, 7-6,
9-12, 9-13, 9-14
Index-organized 1-22, 2-20, 4-4, 4-28, 4-29
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

INDEX_ASC 9-12, 9-14


INDEX_COMBINE 9-12, 9-14, 9-15, 9-16
INDEX_DESC 4-17, 9-12, 9-13, 9-14
INDEX_FFS 4-9, 4-20, 9-12, 9-14
INDEX_JOIN 4-24, 9-13
INDEX_SS 4-23, 9-12, 9-14 n s e
i ce
Initialization parameter 1-6, 1-18, 2-14, 2-15, 2-24, 2-25, 3-24,
b l el
3-25, 3-30, 4-20, 4-37, 4-70, 5-22, 5-35, 6-7, 7-26, 7-31, 7-32, fer a
a n s
7-35, 8-11, 8-21, 9-25, 10-5, 10-12, 10-16, 10-30
o n -tr
Inlist Iterator 4-33, 4-50
a n
s
INSERT 1-8, 3-3, 3-11, 4-39, 5-3, 5-4, 5-7, 5-8, 5-9,a5-10, eฺ
)h
es 10-19,
5-11, 5-15, 6-14, 7-33, 9-5, 9-6, 9-23, ฺ10-4,
id
u10-20, 10-25,
i r t G
10-26, 10-29, 11-36
g p ฺm uden
INTERSECT 4-48, 4-66, 4-69, d 9-5,S9-27 t
@6-11, s
j
ia
ab4-38se
Invisible 3-32, 3-34, 4-37, thi
(
ia 4-29, u
IOT 2-20, 4-11,
A b
4-28, t o 4-46
nio
J
n t o
e A pool 1-6, 1-13, 1-16, 1-18, 3-5
Java
Jo s
Join 1-14, 1-17, 2-3, 2-4, 2-19, 2-20, 2-21, 3-20, 3-21, 3-24,
3-25, 3-26, 3-28, 3-30, 4-2, 4-3, 4-4, 4-5, 4-8, 4-11, 4-13,
4-24, 4-35, 4-39, 4-45, 4-48, 4-49, 4-51, 4-54, 4-55, 4-56, 4-57,
4-58, 4-59, 4-60, 4-61, 4-62, 4-63, 4-64, 4-65, 4-69, 4-71, 5-3,
5-21, 5-22, 5-24, 5-25, 5-26, 5-32, 5-38, 5-42, 5-43, 5-45, 5-46,
5-47, 5-49, 6-3, 6-4, 6-6, 6-7, 6-8, 6-10, 6-11, 6-12, 6-13,
6-14, 6-15, 6-16, 6-18, 6-19, 6-20, 6-21, 7-3, 7-5, 7-11, 7-16,
7-17, 7-33, 9-2, 9-9, 9-10, 9-12, 9-13, 9-17, 9-20, 9-21, 9-22,
9-24, 9-27, 9-29, 9-30, 11-18
K
Key compression 4-10, 4-11

Oracle Database 11g: SQL Tuning Workshop Index - 12


L
Large pool 1-6, 1-12, 1-16, 1-18, 11-22
LEADING 2-3, 4-11, 4-15, 4-19, 4-21, 4-23, 4-36, 6-19, 9-4,
9-20, 9-30
Leaf 4-12, 4-20, 4-21, 4-22, 4-28, 5-14, 5-40, 7-4, 7-6, 7-7,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

7-8, 7-24
Library cache 1-9, 1-10, 1-11, 3-5, 3-9, 3-14, 5-4, 5-20,
5-23, 5-24, 8-3, 8-4, 10-16, 10-23, 10-26, 10-28, 10-29
Listener 1-4, 1-5, 10-6
Literal 2-4, 2-5, 2-23, 2-25, 3-29, 8-3, 8-4, 8-7, 8-9,
8-11, 8-12, 8-13, 8-18, 8-19, 9-24 n s e
i ce
Lock 1-6, 1-7, 1-9, 1-10, 1-21, 1-22, 1-23, 2-9, 2-13, 2-15,
b l el
3-12, 3-15, 3-18, 3-21, 3-22, 3-24, 3-27, 3-28, 3-29, 3-30, 3-32, fer a
a n s
4-4, 4-5, 4-6, 4-7, 4-8, 4-9, 4-11, 4-12, 4-13, 4-19, 4-20,
o n -tr
n 4-52,
4-21, 4-22, 4-28, 4-30, 4-44, 4-45, 4-46, 4-47, 4-49, 4-51,
a
as7-6, 7-7,
4-56, 4-57, 5-3, 5-12, 5-18, 5-19, 5-48, 7-4, 7-5,
)h eฺ
es 7-34,
7-8, 7-9, 7-24, 7-25, 7-28, 7-30, 7-32, ฺ7-33,
id
u 9-5,
9-4,
i r t G
9-6, 9-10, 9-17, 9-18, 9-24, 9-26,
g p ฺm
9-27, 9-28,
u d en9-29, 10-4, 10-25,
t
10-29 @d s S
Locking Statistics 7-34j abia
e thi
( us
Lookup table A bia9-24 to
6-3,
nio
M
n t o
e A
Materialized view 2-3, 2-10, 2-19, 9-17, 11-16, 11-17, 11-18
Jo s
Measures 3-24, 6-3
MERGE 1-14, 3-3, 3-11, 3-21, 3-22, 3-28, 3-30, 3-34, 4-25, 4-34,
4-48, 4-49, 4-51, 4-52, 4-54, 4-58, 4-60, 4-64, 4-65, 4-69, 5-25,
6-8, 6-10, 6-11, 6-13, 6-16, 7-33, 8-14, 8-15, 9-5, 9-6, 9-12,
9-14, 9-16, 9-17, 9-18, 9-21, 9-22, 9-24, 9-26, 9-27, 9-30, 10-13
Min/Max 4-13, 4-53
MINUS 4-32, 4-34, 4-48, 4-66, 4-69, 9-5, 9-27
MMAN 1-15, 1-18, 4-12, 5-4, 5-6, 5-7, 5-8, 5-9, 5-11, 5-12,
5-16, 5-44, 7-22, 8-5, 8-11, 8-12, 10-2, 10-8, 10-10, 10-18, 10-19,
10-23, 10-24, 10-25, 10-26, 10-27, 10-29, 10-30, 11-4, 11-8, 11-16

Oracle Database 11g: SQL Tuning Workshop Index - 13


M
MMON 1-15, 1-21, 2-3, 2-4, 2-5, 2-6, 2-11, 2-14, 2-15, 2-25,
3-23, 3-24, 4-4, 4-10, 4-15, 4-21, 4-26, 4-44, 4-48, 4-62, 4-63,
7-20, 8-9, 10-4, 10-6, 11-26
Module 2-9, 4-37, 10-4, 10-8, 10-9, 10-10, 10-13, 10-14, 11-4, 11-21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

MONITOR 1-3, 1-15, 2-6, 2-7, 2-8, 2-9, 2-10, 2-11, 5-4,
5-15, 5-19, 5-29, 5-35, 5-36, 5-37, 5-38, 5-39, 5-50, 5-52, 7-29,
8-17, 9-25, 10-4, 10-5, 10-8, 10-10, 10-11, 10-12, 10-13, 10-15, 10-29,
11-4, 11-26
Multicolumn Statistics 7-17, 7-18
n s e
N
lice
N-ary 4-3
a b le
Nested Loops 3-26, 3-32, 4-47, 4-54, 4-55, 4-56, 4-57, 4-62, 4-63, s f er
4-64, 4-65, 4-67, 5-3, 5-42, 5-43, 5-44, 5-45, 5-46, 7-5, 9-20,
- t r an
9-21 n on
s a
NO_EXPAND 9-17
) h a
i d eฺ
NO_FACT 6-17, 9-19 i r ฺes nt Gu
NO_INDEX 9-12, 9-14 g p ฺm ude
d S t
NO_MERGE 4-51, 4-52, 9-18 a @ i s
NO_MONITOR 5-35,(j9-25 abi se th
b ia to u
i o A
NO_QUERY_TRANSFORMATION 9-17
ton
NO_RESULT_CACHE 4-70, 9-25
e An
NO_USE_HASH 9-22
J os NO_USE_MERGE 9-22
NO_USE_NL 9-21
NOAPPEND 9-23
NOAUDIT 3-3
Non-equi 4-61, 4-62
NULL 3-13, 4-15, 4-19, 4-20, 4-24, 4-26, 4-27, 4-29, 4-30, 4-39,
4-41, 4-42, 4-63, 4-65, 4-68, 5-11, 5-12, 5-14, 5-25, 5-30, 5-32,
5-44, 7-4, 7-10, 7-17, 7-26, 7-27, 7-30, 7-34, 9-29
O
OPEN 1-19, 1-24, 2-14, 2-23, 3-3, 3-6, 3-8, 3-13, 4-63, 10-12,
11-15
OPT_PARAM 9-25

Oracle Database 11g: SQL Tuning Workshop Index - 14


O
optimizer statistics 2-3, 2-11, 2-25, 7-1, 7-2, 7-3, 7-4,
7-25, 7-27, 7-28, 7-29, 7-35, 11-28
OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES 3-32, 3-33, 8-19
OPTIMIZER_DYNAMIC_SAMPLING 3-25, 3-32, 3-33, 7-31, 7-32, 7-33,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

9-25
OPTIMIZER_FEATURES_ENABLED 3-32, 3-35
OPTIMIZER_INDEX_CACHING 3-32, 9-25
OPTIMIZER_INDEX_COST_ADJ 3-32, 9-25
OPTIMIZER_MODE 3-32, 3-33, 9-10
OPTIMIZER_USE_INVISIBLE_INDEXES 3-32, 3-34, 4-37, 4-38 n s e
i ce
OPTIMIZER_USE_PENDING_STATISTICS 3-32, 3-34
b l el
OPTIMIZER_USE_SQL_PLAN_BASELINES 3-32, 3-33 fer a
ORDER BY 4-15, 4-16, 4-17, 4-18, 4-19, 4-46, 4-48, 7-13, 7-15, tr a n s
o n -
9-30, 10-19
a n
s eฺ
ORDERED 3-12, 4-19, 4-20, 4-21, 4-23, 4-46, 4-48,a5-41, 5-47,
)h id
7-6, 9-20, 9-21, 9-23, 9-24, 11-21, 11-25
i r ฺes nt G u
ORDERED_PREDICATES 9-23, 9-24
g p ฺm ude
t
OS block 1-21 @d s S
Outer Join 4-61, 4-63(jab
ia
e thi
ia u s
P A b t o
i o
ton 1-3, 4-6, 7-26, 7-29, 7-30
parallelism
e An
PARALLELIZE 3-6, 3-7, 3-11, 4-40, 7-26
J os Parent 2-21, 3-20, 4-65, 5-3, 5-10, 5-23, 5-24, 5-40, 5-41, 5-43,
5-45, 5-46, 5-48, 9-5, 11-8
PARSE 1-10, 2-14, 2-23, 2-25, 3-5, 3-6, 3-7, 3-9, 3-13, 3-14,
3-15, 3-18, 5-23, 7-20, 8-4, 8-7, 8-8, 8-13, 8-14, 8-15, 10-16,
10-17, 10-18, 10-23, 10-24, 10-25, 10-26, 10-27, 10-28, 10-29
Partition 1-22, 2-3, 2-10, 2-19, 3-11, 4-3, 4-4, 5-3, 5-12,
6-8, 6-13, 7-26, 7-28, 7-30, 9-12, 9-13, 9-14, 11-16, 11-17, 11-18
Peeking 5-15, 8-2, 8-7, 8-8, 8-13, 8-14, 8-20, 8-21
PGA 1-6, 1-14, 1-15, 1-17, 1-18, 3-5, 3-29, 3-30, 4-49, 5-26,
6-10
PGA_AGGREGATE_TARGET 1-17, 3-29, 3-30, 5-26, 6-10

Oracle Database 11g: SQL Tuning Workshop Index - 15


P
PLAN_TABLE 5-4, 5-6, 5-7, 5-10, 5-11, 5-12, 5-13, 5-14, 5-15,
5-20, 5-21, 5-25, 5-32, 5-33, 5-40, 10-20
PLUSTRACE 5-15
Predicate 2-3, 2-25, 3-18, 3-22, 3-25, 3-26, 3-27, 3-34, 4-5,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4-11, 4-14, 4-15, 4-16, 4-17, 4-18, 4-19, 4-20, 4-23, 4-24, 4-25,
4-26, 4-29, 4-30, 4-31, 4-32, 4-33, 4-34, 4-41, 4-49, 4-50, 4-51,
4-52, 4-53, 4-54, 4-55, 4-58, 4-59, 4-60, 4-61, 4-64, 4-67, 4-68,
5-11, 5-12, 5-14, 5-25, 5-42, 5-49, 6-6, 6-10, 6-14, 6-16, 7-3,
7-6, 7-10, 7-14, 7-16, 7-17, 7-18, 7-19, 7-24, 7-31, 7-32, 8-14,
8-15, 8-16, 9-15, 9-21, 9-23, 9-24, 9-26, 11-7, 11-11, 11-18 n s e
i ce
Preference 7-2, 7-25, 7-26, 7-35
b l el
PREPARE 3-3 fer a
Primary key 2-20, 3-15, 3-20, 4-11, 4-14, 4-27, 4-28, 4-29, 4-35, tra n s
o n -
4-40, 5-37, 5-46, 6-3, 6-9, 6-18, 7-7, 8-9
a n
h Private SQL area 3-4, 3-8, 3-14 a s eฺ
e s ) u id PUSH_PRED 9-24

ir ent G
ฺ m PUSH_SUBQ 9-24
d gp Stud
Q
b i a@ this
(ja3-22,us3-24,
Query block 3-18, 3-21,
i a
e 3-28, 4-51, 5-12, 9-4, 9-17,
A
9-18, 9-26, to 9-29
b9-27, 9-28,
ni o
R n t o
e A
Jos
Range 3-25, 3-30, 3-32, 4-4, 4-5, 4-6, 4-8, 4-10, 4-13, 4-15,
4-16, 4-17, 4-18, 4-25, 4-26, 4-28, 4-31, 4-34, 4-36, 4-39, 4-49,
4-50, 4-53, 4-56, 4-57, 4-63, 4-65, 4-67, 4-68, 5-29, 5-30, 5-42,
5-44, 5-45, 6-13, 6-16, 7-8, 7-9, 7-10, 7-12, 7-14, 7-26, 8-8,
8-9, 8-10, 8-16, 8-18, 9-8, 9-12, 9-13, 9-14, 9-16, 9-29, 10-26,
10-29, 11-14, 11-18
Redo log buffer 1-6, 1-8, 1-10, 1-11
redo log file 1-8, 1-11, 1-15, 1-19, 2-15
RENAME 3-3, 5-10
Result cache 1-9, 1-15, 3-5, 3-30, 3-31, 4-70, 4-72, 9-25
Result Set 2-5, 4-54, 4-70, 5-44, 5-47, 6-7, 6-9, 6-11, 6-12,
6-14
RESULT_CACHE 3-29, 3-30, 3-31, 4-70, 9-25

Oracle Database 11g: SQL Tuning Workshop Index - 16


R
RESULT_CACHE_MAX_RESULT 3-29, 3-31
RESULT_CACHE_MAX_SIZE 3-29, 3-31
RESULT_CACHE_MODE 3-29, 3-30, 4-70, 9-25
RESULT_CACHE_REMOTE_EXPIRATION 3-29, 3-31
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

Reverse key 4-10, 4-11


REVOKE 3-3
REWRITE 2-11, 2-27, 3-18, 6-9, 9-17, 11-11, 11-17
ROLLBACK 1-10, 2-13, 3-3, 3-12, 10-16, 10-18
Root 4-12, 4-20, 4-21, 5-40, 7-6, 10-5
Row cache 1-9 n s e
i ce
ROWID 4-4, 4-6, 4-7, 4-12, 4-13, 4-14, 4-15, 4-16, 4-17, 4-18,
b l el
fe
4-19, 4-23, 4-24, 4-25, 4-28, 4-30, 4-31, 4-33, 4-34, 4-36, 4-46, ra
n s
n - tra
4-47, 4-49, 4-50, 4-55, 4-56, 4-57, 4-59, 4-63, 4-68, 5-25, 5-42,
o
5-44, 5-45, 5-46, 6-11, 6-13, 6-15, 6-16, 7-5, 7-8, 9-8,n9-11,
a
s eฺ
9-16, 10-26, 10-29
h a
ROWID Scan 4-4, 4-7 e s ) u id

ir 11-16 n tG

Rule 2-12, 2-24, 5-41, 5-42, 5-44, 9-6, m
11-11, e
d gp Stud
S
b i a@ this
Sample Table Scan 4-4,
i a (ja 4-8,u4-9
se
SAVEPOINT A b 3-12 to
3-3,
o
ni 1-17, 2-12, 2-13
o
Scalability
t
e An 1-21, 1-22, 2-13, 2-15, 2-18, 2-24, 2-25, 3-3, 3-16, 3-17,
Schema
J os 3-24, 5-10, 5-15, 5-20, 6-2, 6-3, 6-4, 6-5, 6-6, 6-22, 7-25,
7-26, 7-27, 7-28, 7-29, 7-30, 7-34, 9-11, 9-21, 10-19, 10-20, 11-12,
11-16, 11-21, 11-38
Segment 1-15, 1-21, 1-22, 1-23, 2-8, 2-15, 3-29, 4-12, 4-20,
4-28, 4-29, 5-27, 5-48, 10-25

Oracle Database 11g: SQL Tuning Workshop Index - 17


S
SELECT 1-4, 2-3, 2-4, 2-5, 2-11, 2-19, 2-20, 3-3, 3-10, 3-11,
3-12, 3-13, 3-16, 3-17, 3-19, 3-20, 3-21, 3-22, 3-23, 3-24, 3-25,
3-26, 3-28, 3-32, 4-3, 4-5, 4-6, 4-7, 4-8, 4-11, 4-14, 4-15,
4-16, 4-17, 4-18, 4-19, 4-20, 4-21, 4-23, 4-24, 4-25, 4-26, 4-27,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4-29, 4-31, 4-32, 4-33, 4-36, 4-38, 4-39, 4-41, 4-47, 4-48, 4-49,
4-50, 4-51, 4-52, 4-53, 4-54, 4-55, 4-56, 4-57, 4-58, 4-59, 4-60,
4-62, 4-63, 4-64, 4-65, 4-67, 4-68, 4-70, 5-3, 5-6, 5-9, 5-10,
5-11, 5-12, 5-14, 5-15, 5-18, 5-25, 5-32, 5-33, 5-34, 5-37, 5-38,
5-42, 5-44, 5-45, 5-47, 5-48, 5-49, 6-5, 6-6, 6-9, 6-12, 6-14,
6-16, 6-18, 6-19, 6-20, 6-21, 7-3, 7-6, 7-8, 7-10, 7-11, 7-13, n s e
i ce
7-14, 7-15, 7-16, 7-17, 7-18, 7-19, 7-24, 7-31, 7-32, 7-34, 8-3,
b l el
8-4, 8-5, 8-7, 8-8, 8-9, 8-10, 8-12, 8-14, 8-15, 8-16, 8-18, fe r a
a
9-3, 9-5, 9-6, 9-8, 9-10, 9-11, 9-12, 9-13, 9-14, 9-15, 9-16, tr n s
o n -
a n
9-18, 9-20, 9-21, 9-22, 9-23, 9-24, 9-27, 9-28, 9-29, 9-30, 10-7,
10-12, 10-13, 10-15, 10-17, 10-20, 10-25, 10-28, s
a 10-29,eฺ11-7, 11-8, 11-12,
)h id
11-13, 11-14, 11-15, 11-19, 11-20, 11-24,
i r ฺes 11-25,
t G u 11-34, 11-36
11-33,
n 6-14,
Selectivity 2-3, 2-11, 3-24, 3-25, 3-26,
g p ฺm
4-3, 4-6,
u d e4-36,
d 7-17, t
7-3, 7-8, 7-10, 7-11, 7-14,
ia @7-16,
hi
t 11-7 s S 7-19, 7-24, 7-31, 7-32,
j ab 8-18,
8-8, 8-14, 8-15, (8-16, s e9-24,
Semi join 4-61, b ia to u
4-64
n io A1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 1-10, 1-11,
serveroprocess
t
n1-12,
e A 3-11, 3-30, 10-3
Jos Service 1-4, 1-20, 2-8, 2-9, 5-31, 10-3, 10-4, 10-6, 10-7,
10-8, 10-9, 10-10, 10-13, 10-14, 10-31
SERVICE_NAME 10-7, 10-8, 10-14
SERVICE_NAMES 10-8
Session 1-4, 1-5, 1-6, 1-12, 1-13, 1-14, 1-17, 2-8, 2-9,
3-3, 3-30, 3-33, 4-37, 5-20, 5-21, 5-25, 5-27, 5-30, 5-31, 5-35,
5-36, 5-37, 5-38, 5-39, 5-44, 7-33, 8-5, 8-11, 8-12, 9-6, 9-10,
9-27, 10-2, 10-3, 10-4, 10-6, 10-8, 10-10, 10-11, 10-12, 10-13, 10-14,
10-15, 10-17, 10-18, 10-20, 10-30, 11-19, 11-20
SESSION_ID 10-8, 10-11, 10-14
SET ROLE 3-3
SET TRANSACTION 3-3

Oracle Database 11g: SQL Tuning Workshop Index - 18


S
SGA 1-3, 1-4, 1-6, 1-7, 1-8, 1-9, 1-10, 1-11, 1-12, 1-15,
1-16, 1-18, 2-6, 2-8, 3-5, 3-30, 3-31, 4-49, 11-21
SGA_TARGET 1-16
Shared pool 1-6, 1-9, 1-10, 1-11, 1-12, 1-15, 1-16, 1-18, 2-14,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

2-22, 2-23, 3-5, 4-70, 5-24


Shared server 1-4, 1-12, 1-14, 6-10, 10-3
Shared SQL area 1-10, 3-4, 3-5, 3-9, 3-14, 3-15, 5-23
Single-table hash cluster 4-46
Skip 4-4, 4-13, 4-21, 4-22, 4-23, 4-36, 4-58, 9-14, 9-17
Small table 4-6, 4-39, 5-49 n s e
i ce
Snapshot 2-6, 5-27, 5-28, 5-29, 5-30, 5-31, 5-33, 5-34, 11-12,
b l el
11-14, 11-32 fer a
a n s
Snowflake 6-4, 6-21
o n -tr
Soft parse 2-23, 3-14, 8-14, 8-15, 10-23
a n
s eฺ
SORT 1-14, 1-17, 2-5, 2-20, 3-26, 3-27, 3-28, 3-30,a4-11, 4-15,
)h id
4-17, 4-19, 4-20, 4-24, 4-27, 4-31, 4-46,
i r ฺes4-48,t G u 4-52, 4-53,
4-49,

g p d en5-19, 5-22, 5-24,


ฺm5-3,u5-18,
4-54, 4-58, 4-60, 4-65, 4-67, 4-69,
St 9-10, 9-22, 10-18, 10-19,
@d 6-16,s 9-3,
5-25, 5-26, 5-38, 5-47, 6-13,
j
10-21, 10-22, 10-28,
ia
ab10-29e t hi
(
ia to u s
Sort area 1-14,
A b4-49
n
Sort-mergei oJoin 4-48, 4-54, 4-58, 9-22
n to
A hash cluster 4-46
eSorted
Jos SPA 1-4, 1-5, 1-8, 1-21, 1-22, 1-23, 1-24, 2-6, 2-8, 2-9,
2-14, 2-15, 2-21, 2-24, 2-25, 3-5, 3-9, 4-5, 4-10, 4-11, 4-35,
4-39, 4-40, 4-46, 4-65, 5-4, 5-5, 5-12, 5-31, 5-34, 5-36, 5-39,
5-48, 6-4, 7-5, 7-14, 7-21, 7-28, 9-3, 9-5, 9-6, 9-23, 10-8,
10-14, 10-16, 10-26, 11-4, 11-8, 11-22, 11-25
SQL Access Advisor 2-10, 5-50, 11-2, 11-10, 11-16, 11-17, 11-18, 11-19,
11-20, 11-21, 11-22, 11-23, 11-24, 11-25, 11-37, 11-38
SQL Management Base 3-33, 5-4, 5-5
SQL monitor 2-10, 5-35, 5-36, 5-37, 5-38, 5-39, 5-50, 5-52, 9-25
SQL profile 2-6, 5-5, 11-5, 11-7, 11-8, 11-9, 11-15, 11-26, 11-27,
11-28, 11-29, 11-30, 11-32, 11-33, 11-36

Oracle Database 11g: SQL Tuning Workshop Index - 19


S
SQL tuning advisor 2-6, 2-10, 2-14, 5-50, 11-2, 11-3, 11-4, 11-5,
11-8, 11-9, 11-12, 11-13, 11-14, 11-15, 11-26, 11-27, 11-28, 11-34, 11-36,
11-37, 11-38
SQL tuning loop 11-9, 11-26
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

SQL tuning set 2-10, 5-4, 5-6, 11-12, 11-14, 11-17, 11-21, 11-32
Stale 2-3, 3-31, 7-3, 7-5, 7-6, 7-25, 7-26, 7-27, 11-5, 11-6,
11-28, 11-29
Star Join 4-61
star query 6-2, 6-3, 6-5, 6-7, 6-12, 6-14, 6-16, 6-17, 6-22
star schema 6-2, 6-3, 6-4, 6-6, 6-22 n s e
i ce
Star Transformation 3-18, 4-35, 6-1, 6-2, 6-5, 6-6, 6-7,
b l el
6-8, 6-9, 6-12, 6-13, 6-14, 6-15, 6-16, 6-17, 6-22, 6-23, 9-17, fer a
a n s
9-19
o n -tr
n 9-25
STAR_TRANSFORMATION 3-29, 3-30, 6-7, 6-17, 9-4, 9-19,
a
STAR_TRANSFORMATION_ENABLED 3-29, 3-30,a6-7, s 9-25eฺ
Statistics 1-16, 1-18, 2-3, 2-6, 2-7, 2-8, 2-9,ฺe
)h
s 2-15,
2-11,
id
u
i r t G
2-25, 3-16, 3-17, 3-18, 3-24, 3-25,
g p en 3-33, 3-34, 4-11,
ฺm3-26,ud3-32,
@d5-16,s5-17,
4-41, 4-42, 5-4, 5-13, 5-15, St 5-18, 5-19, 5-20, 5-22,
5-23, 5-24, 5-26, j
ia
ab 5-28,
5-27, e hi
t 5-31, 5-35, 5-37, 5-44, 5-48,
5-29,
( s
u 7-6, 7-7, 7-8, 7-9, 7-10,
6-7, 7-1,A bia7-3, 7-4,
7-2, to 7-5,
n
7-11,
o io7-12, 7-13, 7-15, 7-17, 7-18, 7-19, 7-20, 7-21, 7-22, 7-23,
t
e An7-24, 7-25, 7-26, 7-27, 7-28, 7-29, 7-30, 7-31, 7-34, 7-35, 7-36,
Jos 8-10, 8-11, 8-14, 8-15, 8-17, 8-18, 9-3, 9-10, 9-24, 10-2, 10-10,
10-16, 10-17, 10-18, 10-20, 10-23, 10-24, 10-25, 10-26, 10-28, 10-30, 11-5,
11-6, 11-7, 11-8, 11-9, 11-12, 11-14, 11-15, 11-24, 11-26, 11-28, 11-29,
11-32, 11-33
STATISTICS_LEVEL 1-16, 1-18, 5-22, 5-26, 5-35, 5-44, 7-27, 10-16,
11-32
STATS$SQL_PLAN 5-4
Streams pool 1-6, 1-13, 1-16, 1-18, 3-5
Suboptimal 2-3, 2-21, 7-11, 7-19, 8-8, 11-6
Subquery 2-4, 3-11, 3-18, 3-20, 4-61, 4-64, 4-65, 5-42, 6-8,
7-33, 9-5, 9-17, 9-18, 9-24, 9-27
SYSAUX 1-24, 5-5

Oracle Database 11g: SQL Tuning Workshop Index - 20


S
SYSTEM 1-3, 1-4, 1-6, 1-8, 1-11, 1-15, 1-16, 1-17, 1-18, 1-23,
1-24, 2-8, 2-9, 2-11, 2-12, 2-13, 2-14, 2-15, 2-16, 2-22, 2-23,
2-25, 3-3, 3-18, 3-26, 3-30, 3-31, 4-3, 4-8, 4-10, 4-14, 4-15,
4-16, 4-21, 4-22, 4-23, 4-25, 4-29, 4-30, 4-35, 4-37, 4-45, 4-47,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

4-49, 4-52, 4-57, 4-59, 4-63, 5-10, 5-17, 5-18, 5-27, 5-28, 5-31,
5-44, 5-46, 5-47, 6-9, 6-14, 7-2, 7-3, 7-4, 7-11, 7-13, 7-15,
7-20, 7-21, 7-22, 7-23, 7-24, 7-26, 7-27, 7-28, 7-29, 7-33, 7-35,
7-36, 8-8, 8-11, 8-12, 8-14, 8-15, 8-19, 9-3, 9-22, 9-24, 9-29,
10-6, 10-8, 10-9, 10-16, 10-20, 11-3, 11-6, 11-8, 11-9, 11-14, 11-17,
11-26, 11-34, 11-36 n s e
lice
System statistics 2-11, 7-2, 7-4, 7-20, 7-21, 7-22, 7-23, 7-27,
a b le
7-28, 7-35, 7-36, 9-24 fer s
T - t r an
n
no
Table 1-9, 1-21, 1-22, 1-23, 1-24, 2-3, 2-5, 2-14, 2-15, 2-16, a
2-17, 2-18, 2-19, 2-20, 2-21, 2-25, 3-10, 3-11, h a s eฺ 3-17,
3-12, 3-16,
e s ) u id
3-18, 3-19, 3-20, 3-21, 3-22, 3-25, 3-26,
i r ฺ 3-28,
n t G 3-30, 3-31,
3-29,
m
ฺ 4-7,ud4-8, e 4-9, 4-10,
3-32, 3-33, 3-35, 4-3, 4-4, 4-5,
d gp4-6, t
S 4-18, 4-19, 4-20, 4-21,
4-11, 4-12, 4-13, 4-14,i a @ 4-16,
4-15, i s 4-17,
j ab 4-27, e th
(
4-23, 4-24, 4-25, 4-26,
ia 4-37, u s 4-28, 4-29, 4-30, 4-31, 4-32, 4-33,
A b
4-34, 4-35, 4-36, t o 4-39, 4-40, 4-44, 4-45, 4-46, 4-47, 4-49,
i o
n ton 4-51, 4-52, 4-54, 4-55, 4-56, 4-57, 4-58, 4-59, 4-60, 4-61,
4-50,
e A 4-62, 4-63, 4-64, 4-65, 4-67, 4-68, 4-70, 5-3, 5-4, 5-5, 5-6,
J o s
5-7, 5-8, 5-9, 5-10, 5-11, 5-12, 5-13, 5-14, 5-15, 5-18, 5-20,
5-21, 5-25, 5-31, 5-32, 5-33, 5-34, 5-38, 5-39, 5-40, 5-42, 5-44,
5-45, 5-46, 5-47, 5-48, 5-49, 6-3, 6-4, 6-5, 6-6, 6-7, 6-8,
6-9, 6-10, 6-11, 6-12, 6-13, 6-14, 6-15, 6-16, 6-17, 6-18, 6-20,
6-21, 7-4, 7-5, 7-6, 7-7, 7-8, 7-9, 7-13, 7-15, 7-17, 7-18,
7-19, 7-20, 7-21, 7-22, 7-24, 7-25, 7-26, 7-27, 7-28, 7-29, 7-30,
7-31, 7-32, 7-33, 7-34, 8-4, 8-8, 8-9, 8-10, 8-11, 8-18, 9-3,
9-4, 9-5, 9-6, 9-8, 9-9, 9-10, 9-11, 9-12, 9-13, 9-14, 9-16,
9-19, 9-20, 9-21, 9-22, 9-23, 9-24, 9-26, 9-27, 9-28, 9-29, 9-30,
10-5, 10-17, 10-18, 10-19, 10-20, 10-21, 10-22, 10-25, 10-26, 10-28, 10-29,
11-5, 11-6, 11-8, 11-9, 11-10, 11-17, 11-18, 11-21, 11-25, 11-36

Oracle Database 11g: SQL Tuning Workshop Index - 21


T
Tablespace 1-21, 1-22, 1-23, 1-24, 2-15, 2-25, 4-39, 4-40, 5-5,
5-31, 5-48, 7-21, 11-25
Temporary table 1-21, 1-22, 4-49, 5-7, 5-10, 6-14, 7-31
tkprof 2-8, 5-10, 5-48, 7-35, 10-2, 10-4, 10-13, 10-17, 10-18, 10-19,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

10-20, 10-21, 10-22, 10-23, 10-24, 10-25, 10-26, 10-27, 10-28, 10-29, 10-30,
10-31
trace file 1-19, 1-20, 2-6, 2-9, 3-28, 3-37, 5-4, 5-5, 5-14,
5-40, 5-47, 5-48, 7-35, 10-2, 10-3, 10-5, 10-8, 10-10, 10-12, 10-13,
10-14, 10-15, 10-16, 10-17, 10-18, 10-19, 10-20, 10-27, 10-30
Transformer 3-18, 3-19, 3-20, 3-21, 3-22, 3-23 n s e
lice
Transitivity 3-18, 3-23
a b le
trcsess 2-8, 10-2, 10-4, 10-13, 10-14, 10-15, 10-18, 10-30, 10-31
sfer
TRUNCATE 3-3
- t r an
no Tuning mode 11-3
n
a
s eฺ
U h a
e s ) u id
UGA 4-49 ฺ
ir ent G
ฺ m
d gp 4-50,
UNION 2-3, 2-4, 2-5, 3-19, 3-22, 4-48,
S
d 4-68, 4-69,
tu4-66,
5-48, 9-5, 9-17, 9-27,ia @ 11-11
9-28,
h is
b
ja 4-50, t
UNION ALL 2-3, 2-5,(3-19,
i a u se4-66, 4-68, 4-69, 5-48, 9-17,
A
9-27, 11-11b to
o
i 2-5, 2-20, 3-8, 3-20, 4-4, 4-10, 4-11, 4-13, 4-14,
n1-11,
UNIQUE
t o
e An4-15, 4-27, 4-29, 4-39, 4-40, 4-48, 4-50, 4-55, 4-56, 4-60, 4-69,
Jos 5-21, 5-24, 5-37, 5-42, 5-44, 5-45, 5-46, 7-7, 7-9, 7-16, 8-11,
9-8, 10-4
Unique 1-11, 2-5, 2-20, 3-8, 3-20, 4-4, 4-10, 4-11, 4-13, 4-14,
4-15, 4-27, 4-29, 4-39, 4-40, 4-48, 4-50, 4-55, 4-56, 4-60, 4-69,
5-21, 5-24, 5-37, 5-42, 5-44, 5-45, 5-46, 7-7, 7-9, 7-16, 8-11,
9-8, 10-4
Unique key 2-20, 3-20, 4-40
UNNEST 3-18, 3-20, 3-34, 4-64, 9-4, 9-17
UPDATE 1-8, 3-3, 3-11, 3-12, 4-7, 4-37, 5-3, 5-15, 5-23, 7-3,
7-20, 7-21, 7-22, 7-33, 9-5, 9-6, 9-8, 9-28, 10-4, 10-25, 10-29
USE_CONCAT 9-17
USE_HASH 5-47, 9-22

Oracle Database 11g: SQL Tuning Workshop Index - 22


U
USE_MERGE 9-21, 9-22, 9-30
USE_NL 5-45, 9-4, 9-20, 9-21, 9-22, 9-30
USE_NL_WITH_INDEX 9-21
user process 1-3, 1-4, 1-5, 1-11, 1-15, 3-9, 3-14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

V
V$ACTIVE_SESSION_HISTORY 5-31, 5-35, 5-36, 5-38
V$DIAG_INFO 10-5
V$SESSION 5-21, 5-25, 5-35, 5-36, 5-39, 10-8, 10-11, 10-14
V$SESSION_LONGOPS 5-35, 5-36, 5-39
n s e
V$SQL 5-4, 5-13, 5-14, 5-20, 5-21, 5-22, 5-23, 5-24, 5-25, 5-26,
lice
5-32, 5-35, 5-36, 5-37, 5-38, 5-40, 5-44, 5-48, 8-3, 8-14, 8-16,
a b le
8-17, 11-17 s f er
V$SQL_CS_HISTOGRAM 8-16 - t r an
V$SQL_CS_SELECTIVITY 8-16 n on
s a
V$SQL_CS_STATISTICS 8-17
) h a
i d eฺ
V$SQL_MONITOR 5-4, 5-35, 5-36, 5-37 irฺe
s Gu
V$SQL_PLAN 5-4, 5-13, 5-14, 5-20, p ฺ m 5-22,
5-21, d e nt 5-24, 5-25,
5-23,
d g Stu
5-26, 5-35, 5-36, 5-38,
i @ 5-44,
a5-40, h is 5-48
b t
V$SQL_PLAN_MONITOR
i a (ja 5-4, u se5-35, 5-36, 5-38
A b
V$SQL_PLAN_STATISTICS to 5-13, 5-20, 5-22, 5-23, 5-24, 5-48
i o
ton
V$SQL_PLAN_STATISTICS_ALL 5-13, 5-20, 5-22, 5-23, 5-24
e An
V$SQL_WORKAREA 5-20, 5-22, 5-23, 5-24
J os V$SQLAREA 5-21, 5-23, 5-24, 5-25
View 1-2, 1-3, 1-6, 1-20, 1-22, 1-26, 2-3, 2-6, 2-8, 2-9,
2-10, 2-11, 2-16, 2-19, 2-21, 2-25, 2-27, 3-6, 3-14, 3-15, 3-18,
3-21, 3-22, 3-25, 3-26, 3-32, 3-34, 3-37, 4-8, 4-10, 4-24, 4-35,
4-37, 4-38, 4-43, 4-50, 4-51, 4-52, 4-54, 4-72, 5-4, 5-6, 5-13,
5-15, 5-20, 5-21, 5-22, 5-23, 5-24, 5-27, 5-29, 5-30, 5-31, 5-32,
5-35, 5-36, 5-38, 5-39, 5-49, 5-52, 6-6, 6-8, 6-15, 6-18, 6-23,
7-4, 7-9, 7-11, 7-13, 7-15, 7-17, 7-18, 7-19, 7-25, 7-26, 7-28,
7-31, 7-33, 7-34, 7-35, 7-36, 8-13, 8-14, 8-16, 8-17, 8-21, 9-3,
9-4, 9-17, 9-18, 9-24, 9-25, 9-26, 9-27, 9-28, 9-29, 9-31, 9-32,
10-6, 10-9, 10-12, 10-14, 10-18, 10-30, 10-31, 11-5, 11-8, 11-15, 11-16,
11-17, 11-18, 11-19, 11-20, 11-23, 11-24, 11-25, 11-28, 11-33, 11-34, 11-38

Oracle Database 11g: SQL Tuning Workshop Index - 23


V
View merging 3-18, 3-21, 3-34, 9-17, 9-18
W
Wait 2-8, 2-9, 2-12, 2-15, 5-33, 5-35, 5-37, 5-38, 10-10, 10-11,
10-12, 10-15, 10-16, 10-17, 10-19, 11-36
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2010, Oracle and/or its affiliatesฺ

WHENEVER 3-3, 4-62, 7-3, 7-25

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

Oracle Database 11g: SQL Tuning Workshop Index - 24

You might also like