You are on page 1of 194

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
tG
db e11g:
Oracle Database

n
n
ha aStData
ud Warehouse
Administer
k
m
i his
m
a
t
Guide
e
shActivity
s
(
N to u
A
KH ense
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

D70064GC20
Edition 2.0
December 2010
D71192

Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Disclaimer

This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and
print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way.
Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
Restricted Rights Notice
If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United
States Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS
The U.S. Governments rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted
by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

Authors
n
ha Stud
k
Brian Pottle, Lauran K. Serhal, Mark Fuller
im his
m
sha use t
(
AN e to
Technical Contributors and Reviewers
H
KMariaeBillings,
ns Herbert Bradbury, Yanti Chang, Jean-Pierre
Hermann Baer, Kenji Baugham,
M
c
li
Dijcks, Gerlinde Frenzen,
Uwe Hesse, Yash Jain, Sean Kim, Paul Lane, Gwen
LAJoellGoodman,
e
A
Lazenby, Lakshmi
Sujatha Patchamatla, Paul Rayner, Raza Siddiqui, S Matt
L Narapareddi,
abValny,
r
U
e
Taylor, Jenny
Tsai,
Branislav
Jean-Francois Verrier, Andreas Walter, James Womack
f
M
I
s
n
M
A -tra
H
S
D
non
Trademark Notice

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

This book was published using:

Oracle Tutor

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Table of Contents

Practices for Lesson 1 .............................................................................................................................. 1-1


Overview of Practices for Lesson 1 .......................................................................................................... 1-3
Practice 1-1: Identifying the Available SQL Developer Resources ............................................................. 1-4
Practice 1-2: Creating and Using New SQL Developer Database Connections .......................................... 1-6
Practice 1-3: Browsing Your HR and SH Schemas Tables........................................................................ 1-9
Practice 1-4: Setting SQL Developer Preferences and Accessing the Oracle Database 11g, Release 2
Online Documentation Library............................................................................................................... 1-11
Practices for Lesson 2 .............................................................................................................................. 2-1
Overview of Practices for Lesson 2 .......................................................................................................... 2-3
Practices for Lesson 3 .............................................................................................................................. 3-1
Overview of Practices for Lesson 3 .......................................................................................................... 3-3
Practice 3-1: Data Warehouse Tuning Considerations .............................................................................. 3-4

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
Practices for Lesson 5 ..............................................................................................................................
5-1
a
d
h
u
k
t
Overview of Practices for Lesson 5 .......................................................................................................... 5-3
im his S
Practice 5-1: Parallelism Concepts ..........................................................................................................
5-4
m
a
t
h
e
(s us
Practices for Lesson 6 ..............................................................................................................................
6-1
N
o
A
t
Overview of Practices for Lesson 6 ..........................................................................................................
6-3
H nse
Practice 6-1: Parallel Operations inK
Data Warehouses..............................................................................
6-4
e
M
c
Practice 6-2: Partitioned Tables
LA andleDMLli ................................................................................................ 6-8
A
L 7 ..............................................................................................................................
Practices for Lesson
7-1
ab
r
U
e
f
M
OverviewIof Practicess
for Lesson 7 .......................................................................................................... 7-3
anAsynchronous Distributed Change Data Capture........................................................ 7-4
AM7-1:-Using
Practice
r
t
H
n Loading Data from Transportable Tablespaces .................................................................... 7-23
DS Practice
no7-2:
Practices for Lesson 4 .............................................................................................................................. 4-1
Overview of Practices for Lesson 4 .......................................................................................................... 4-3
Practice 4-1: Partitioning Basics .............................................................................................................. 4-4
Practice 4-2: Partition Maintenance Commands ....................................................................................... 4-7

Practices for Lesson 8 .............................................................................................................................. 8-1


Overview of Practices for Lesson 8 .......................................................................................................... 8-3
Practice 8-1: ETL Loading Using SQL*Loader .......................................................................................... 8-4
Practice 8-2: ETL Loading Using External Tables ..................................................................................... 8-7
Practices for Lesson 9 .............................................................................................................................. 9-1
Overview of Practices for Lesson 9 .......................................................................................................... 9-3
Practice 9-1: ETL Transformation ............................................................................................................ 9-4
Practice 9-2: Using Merge and External Tables ........................................................................................ 9-12
Practices for Lesson 10 ............................................................................................................................ 10-1
Overview of Practices for Lesson 10 ........................................................................................................ 10-3
Practice 10-1: Materialized Views ............................................................................................................ 10-4
Practices for Lesson 11 ............................................................................................................................ 11-1
Overview of Practices for Lesson 11 ........................................................................................................ 11-3
Practice 11-1: Refreshing Materialized Views........................................................................................... 11-4
Practices for Lesson 12 ............................................................................................................................ 12-1
Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Oracle Database 11g: Administer a Data Warehouse Table of Contents


i

Overview of Practices for Lesson 12 ........................................................................................................ 12-3


Practice 12-1: Working with Dimensions .................................................................................................. 12-4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices for Lesson 13 ............................................................................................................................ 13-1


Overview of Practices for Lesson 13 ........................................................................................................ 13-3
Practice 13-1: Query Rewrite................................................................................................................... 13-4

Practices for Lesson 14 ............................................................................................................................ 14-1


Practices for Lesson 14 ........................................................................................................................... 14-3
Practice 14-1: Using SQL Access Advisor, Compression, and Resumable Session Tasks ......................... 14-4

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Oracle Database 11g: Administer a Data Warehouse Table of Contents


ii

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G1
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
1
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 2

Overview of Practices for Lesson 1


Practices Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In these practices, you will review the available SQL Developer resources, create new database
connections for the HR and SH users, and browse some of the sample data.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 3

Practice 1-1: Identifying the Available SQL Developer Resources


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice you review the available SQL Developer resources.

Tasks
1.
2.

Familiarize yourself with Oracle SQL Developer, as needed using Appendix B: Using SQL
Developer.
Access the online SQL Developer home page at:
http://www.oracle.com/technology/products/database/sql_developer/index.html
The SQL Developer home page is displayed as follows:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
Bookmark the page for future access.
3.

Access the online SQL Developer home page at:

http://st-curriculum.oracle.com/tutorial/SQLDeveloper/index.htm
Note: This tutorial covers the use of SQL Developer 1.5.
The following page is displayed:

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
i as
4. Preview and experiment with the available links and demonstrations in the tutorial

malinks.
needed, especially the Creating a Database Connection and AccessinggData
e
d
i
uplus + sign
a@
To review the section on how to create a database connection,
click the
G
b
t
d
next to the What to Do First link to display the Creating
a Database
n Connection
n click
e
a
d
link. To review the Creating a Database Connection
topic,
the
h
u topics link. To
k
t
S
m
review the section on accessing data, click the
next to the Accessing
i plush+
is sign
m
a
Data link to display the list of available
topics.
To
review
any
of the topics, click the
t
sh use
(
topics link.
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 5

Practice 1-2: Creating and Using New SQL Developer Database


Connections
Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice you create new database connections for the SH, HR and SYS users.

Tasks
1.

Start SQL Developer.


Click the SQL Developer icon on your desktop.

s
a
h
)

om
c

l
ai
Choices or Values
Setting
m
g
Connection Name
SH Schema
ide
@
u
a
Username
sh
db ent G

Password
n
sh
ha Stud
k
Save Password
Select the check box.
mas provided
iPC
Hostname
Enter the host name for your
m
his by the instructor.
a
t
Port
1521
h
e
(s us
SID
orcl
N
A e to
H
K iconenonsthe Connections tabbed page, and then select
M
Right-click the Connections
ic the shortcut menu. The New/Select Database
lfrom
LA option
the New Connection
e
A
l
Lwindowraisbdisplayed. Use the preceding information provided to create
Connection
U
feconnection.
M
I database
the new
s
n
M
ANote: To-tdisplay
ra the properties of the newly created connection, right-click the
H
n
S
name, and then select Properties from the shortcut menu. The following
D connection
no
2.

Create a database connection for the sh schema using the following information:

is a sample of the newly created database connection for the SH schema using a
local connection:

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
e new
g usingidthe
3. Test the new connection. If the Status is Success, connect to the database
@
u
a
connection.
b nt G
dTest

a. In the New/Select Database Connection window, click


the
n
a tudebutton.
h
k
b. If the Status is Success, click the Connect button.
im his S
m
4. Create a new database connection named a
HR
Schema.
t
e
sh in the
s
(
a. Right-click the SH Schema connection
Object
Navigation tree, and select the
u
o
Properties menu option. AN
t
H nse name and hr as the username and password,
b. Enter HR Schema as K
the connection
M
ce the new connection.
A This
i
l
and then clickLSave.
creates
e new HR Schema connection.
A testblthe
c. Repeat L
step 3 tora
U fe
M
I
M trans
A
H
DS non

5.

Create a new database connection named SYS.


a. Right-click the HR Schema connection in the Object Navigation tree, and select the
Properties menu option.
b. Enter SYS as the connection name, sys as the username, and oracle as the
password.
c. In the Role drop down list, select SYSDBA.
d. Click Save to create the new connection.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

e. Repeat step 3 to test the new SYS connection.


Result: You should now have three connections, as shown in the following:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 8

Practice 1-3: Browsing Your HR and SH Schemas Tables


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice, you browse some of the tables in the HR and SH schemas.

Assumptions
You have completed practice 1-2.

Tasks
1.

Browse the structure of the EMPLOYEES table in the HR schema and display its data.
a. Expand the HR schema connection by clicking the plus sign next to it.
b. Expand the Tables icon by clicking the plus sign next to it.
c. Display the structure of the EMPLOYEES table.
Double-click the EMPLOYEES table. The Columns tab displays the columns in the
EMPLOYEES table as follows:

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

2.
3.

Browse the EMPLOYEES table and display its data.


To display the employees data, click the Data tab. The EMPLOYEES table data is displayed
as follows:

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

4.

Use the SQL Worksheet to select the last names and salaries of all employees whose
annual salary is greater than $10,000. Use both the Execute Statement (F9) and the Run
Script (F5) icons to execute the SELECT statement. Review the results of both methods of
executing the SELECT statements on the appropriate tabbed pages.
Note: Take a few minutes to familiarize yourself with the data, or consult Appendix A:
Table Descriptions, which provides the description and data for all tables in the HR and SH
schemas that you use in this course.
Display the SQL Worksheet using either of the following two methods: Select Tools >
SQL Worksheet or click the Open SQL Worksheet icon. The Select Connection
window is displayed. Using the HR Database Connection, enter the following
statement in the SQL Worksheet area, and then click the Run Script (F5) icon.
SELECT *
FROM employees
WHERE SALARY > 10000;

s
a
h
)

om
c

l
ai
5. Browse the structure of the SALES table in the SH schema and display itsm
data.
g
ide
a. Double-click the SH Schema database connection.
@
u
a
b. Expand the Tables icon by clicking the plus sign next toit.
db ent G
n
c. Display the structure of the SALES table.
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 10

Practice 1-4: Setting SQL Developer Preferences and Accessing the


Oracle Database 11g, Release 2 Online Documentation Library

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In this practice you set some SQL Developer options and learn how to access the Oracle
Database 11g, Release 2 online documentation library.

Assumptions
You have completed practice 1-2.

Tasks
1.

In the SQL Developer menu, select Tools > Preferences.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
2.

Click the Database > Worksheet option. In the Select default path to look for scripts text
box, browse for the /labs folder, and then click Open. This folder contains the labs needed
for some of the practices used in this course.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
de
i
@
u
a
3. In the Preferences window, select Database > Advanced. To display
values as
G
b NULL
t
dbox.

n
blanks, remove the (null) from the Display Null Values As
text
n
ha Stude
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
4.

In the Preferences window, navigate back to Database > Worksheet. Enter 10000 in the
Max Rows to print in a script text box, and then click OK to exit the Preferences window.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G
5. Familiarize yourself with the /labs folder and the /solutions
t
dbfolder:

n
n
e
awindow
a. Click the Open tool under the main menu. The Open
is
Navigate to
ddisplayed.
hscript
u
k
t
the /home/oracle/labs folder. Then select
any
file
and
click
Open.
(Note: Do
m is S
i
m
not execute any script at this point.) a
th
h
e
s
b. Perform the same technique while( navigating
N to usto the /home/oracle/solutions
A
folder.
H windows
se in the SQL Worksheet area.
Kcode
c. Dismiss the displayed
n
e
M
A e 11g,
lic Release 2 documentation Web page at:
6. Access the OracleLDatabase
A
l
L
ab
http://www.oracle.com/pls/db112/homepage
r
U
e
f
M
s for future access.
7. Bookmark
npage
MI trthe
a
A
8.H Display -the complete list of books available for Oracle Database 11g, Release 2.
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 13

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 1


Chapter 1 - Page 14

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G2
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
2
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 2


Chapter 2 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 2


Chapter 2 - Page 2

Overview of Practices for Lesson 2


Practices for Lesson 2

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

There are no practices for this lesson.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 2


Chapter 2 - Page 3

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 2


Chapter 2 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G3
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
3
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 2

Overview of Practices for Lesson 3

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In these practices, you will initially use a typical star query without any SQL processing
optimization and analyze the execution plan of a query using DBMS_XPLAN.DISPLAY to view
the general cost of the query by looking at values for the amount of data accessed by the query
(Bytes) and the execution time (Time). Next, you will enable star transformation while specifying
not to use the TEMP tables, clean out the plan table, and analyze the execution plan of the
same query as in step 1, and then describe the results of the execution plan and compare the
results to those in the first practice step.
Finally, you will enable star transformation and allow the star transformation to use temporary
tables. You will clean out the plan table and analyze the execution plan of the same query and
describe the results of the execution plan and compare the plan to the star transformation plan
for the second query. Finally, you will compare the general costs of the query to that in the first
and second steps.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 3

Practice 3-1: Data Warehouse Tuning Considerations


Tasks

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

1.

The first task performed here is a typical star query without any SQL processing
optimization. Using SQL Developer, connect as sh/sh to disable star transformation, and
analyze the execution plan of the following query using DBMS_XPLAN.DISPLAY.
The results serve as a baseline of sorts. Make note of the general cost of the query by
looking at values for the amount of data accessed by the query (Bytes) and the execution
time (Time).
Note: The NOREWRITE hint is used to avoid possible interference with any existing
materialized views. The code shown below can be viewed and executed using SQL
Developer, from the solution script file sol_03_01_01.sql.

s
a
h
)

Explain plan for


SELECT /*+ norewrite */ t.calendar_month_desc,
p.prod_subcategory, c.cust_city, sum(s.amount_sold)
AS dollars FROM sales s, times t, products p, customers c
WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id
AND s.cust_id = c.cust_id
AND c.cust_city='Ravensburg'
AND p.prod_category in ('Hardware','Photo')
AND t.fiscal_year in (2000,1999)
GROUP BY p.prod_subcategory, calendar_month_desc, c.cust_city;

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
SET LINESIZE 160KH
ns
SELECT * FROMM
table(DBMS_XPLAN.DISPLAY);
e
c
LA le li
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

The output in SQL Developer looks like this:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e tosolution script to avoid typing the code for
You can execute the sol_03_01_01.sql
H
K execute
this practice step. You can
nsthe solution script using either SQL Developer or
e
M
c
Aas thee SHli user.
SQL*Plus loggedLin
A
l using SQL Developer:
b
To executeL
a solution
script
a
U fer
M
I
Open
thenfile
s from the /oracle/home/solutions folder as follows: Click the
MOpentrtool
a
A
display the Open dialog. Then, use the navigation bar shortcut that
H oyou
-createdtopreviously
n
S

in order to navigate to the solutions folder.


D
n

Select the appropriate script file (sol_03_01_01.sql, in this case) and click
Open.

Select the appropriate database connection (the SH Schema, in this case).

Click the Run Script (F5) icon on the SQL Worksheet toolbar to run all of the
commands in the script; or click Run Statement (F9) to run a single statement.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Logged in as the SH user, enable star transformation, but specify that no TEMP tables be
used. Clean out the plan table and analyze the execution plan of the following query.
The code for this task, also found in sol_03_01_02.sql, is as follows:
ALTER SESSION SET star_transformation_enabled=TEMP_DISABLE;
DELETE FROM plan_table;
COMMIT;
EXPLAIN PLAN FOR
SELECT /*+ norewrite */ t.calendar_month_desc,
p.prod_subcategory, c.cust_city, sum(s.amount_sold)
AS dollars
FROM sales s, times t, products p, customers c
WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id
AND s.cust_id = c.cust_id
AND c.cust_city='Ravensburg'

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
a tuplan
d and compare the
When you finish this query, describe the results of the
hexecution
k
S
m
results to those in the first exercise. (Note: The code
for
this
task
is
also found in
i his
m
sol_03_01_02.sql.)
ha se t
s
(
u similar to this:
The execution plan output in SQL N
Developer looks
o
A
t
KH ense
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

AND p.prod_category in ('Hardware','Photo')


AND t.fiscal_year in (2000,1999)
GROUP BY p.prod_subcategory, calendar_month_desc, c.cust_city;

You can execute the sol_03_01_02.sql solution script to avoid typing the code in
this practice step logged in as the SH user.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

The plan shown above represents a typical star query transformation. The records in
the fact table, satisfying the querys WHERE condition, are found by only scanning the
bitmap index structures rather than the whole large fact table SALES. In a first,
internal recursive step, all records of the three dimension tables PRODUCTS, TIMES,
and CUSTOMERS, which satisfy the WHERE condition, are selected. You can see this
table access of the dimension tables underneath the BITMAP KEY ITERATION row
sources. The key values of these records are used as predicates for probing against
the bitmap index structures of the SALES fact table itself. Note that the predicates of
the query, on the CUSTOMERS, PRODUCTS, and TIMES dimension tables, are used for a
high-selective preselection of only the relevant records of your SALES table, using
the bitmap index structures sales_prod_bix, sales_cust_bix, and
sales_time_bix. In a second step, the result set is joined back with all the
dimension tables to get the final query result.
These are all operations after PARTITION RANGE SUBQUERY.
When comparing this execution with the results of the first query, you can see that
the star transformation performed in the second query is responsible for significant
savings in execution time and bytes.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Now, enable star transformation and allow the star transformation to use temporary tables.
Clean out the plan table and analyze the execution plan of the following query: (The code is
found in sol_03_01_03.sql.)
ALTER SESSION SET star_transformation_enabled=TRUE;
DELETE FROM plan_table;
COMMIT;
EXPLAIN PLAN FOR
SELECT /*+ norewrite */ t.calendar_month_desc,
p.prod_subcategory, c.cust_city, sum(s.amount_sold) AS dollars
FROM sales s, times t, products p, customers c
WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id
AND s.cust_id = c.cust_id
AND c.cust_city='Ravensburg'
AND p.prod_category in ('Hardware','Photo')
AND t.fiscal_year in (2000,1999)
GROUP BY p.prod_subcategory, calendar_month_desc, c.cust_city;

s
a
h
)

om
c

l
ai
m
gthe planidtoethe star
When you finish, describe the results of the execution plan and compare
@
u
a
G
b
transformation plan for the second query. Finally, compare the
general
costs
of the query to
t
d
n den
those in the first and second exercises.
a
h
klook similar
tuto this (your output may
The execution plan output in SQL Developer should
S
m
i
m this
differ slightly):
a
h
s use
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_03_01_03.sql script to avoid typing the code in this
practice step logged in as the SH user.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

The plan looks similar to the one shown in step 3-1.2, although not identical. The
difference between the plans is that the execution plan now uses a table named
SYS_TEMP_xxx (which is not part of the query) for satisfying the SQL statement. Note
that this name is system generated and varies. What does this mean? The optimizer
evaluated the selectivity of the WHERE conditions and the size of the PRODUCTS,
TIMES, and CUSTOMERS dimension tables. It detected that there is a high selectivity
on the CUSTOMERS table and that this table is large enough to justify the cost of
creating a temporary table. It is cheaper than accessing the CUSTOMERS table twice,
as was done in the second plan without temporary table usage. The TEMP table is
then used instead of the CUSTOMERS table itself. The creation of this temporary table
and the data insertion are shown in the plan (see area shaded in red).
When you compare the cost of this query with that of the previous queries, you see
that there is a slight savings in time over the second query but bytes accessed by
the two are nearly identical. It is important to remember, however, that because of
the small amount of data used, you do not see great benefits of this transformation
because you hardly exceed the database or file system cache, so that input/output
(I/O) disadvantages of the other mechanisms are mostly eliminated.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 3


Chapter 3 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G4
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
4
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 2

Overview of Practices for Lesson 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In these practices, you will examine static partition pruning using a filter condition, dynamic
partition pruning when doing a join, and perform common warehouse partition maintenance
operations.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 3

Practice 4-1: Partitioning Basics


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice you will examine static partition pruning and dynamic partition pruning.

Tasks
1.

The SELECT statement shown here demonstrates static partition pruning with a predicate
condition on the partition key column. Use explain plan and DBMS_XPLAN.DISPLAY to
view the execution plan for the SQL statements below:
DELETE FROM plan_table;
COMMIT;
explain plan for
SELECT c.channel_desc, sum(amount_sold)
FROM sales s, channels c
WHERE s.channel_id = c.channel_id
AND s.time_id >= to_date('04-JAN-2000','DD-MON-YYYY')
AND s.time_id <= to_date('22-FEB-2000','DD-MON-YYYY')
GROUP BY channel_desc;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
SELECT *
db ent G

FROM table(DBMS_XPLAN.DISPLAY);
n
ha Stud
k
im
How can you tell that partition pruning has taken
place?his
m
a
h
s use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_04_01_01.sql solution script to avoid typing the code in
this practice step using either SQL Developer or SQL*Plus logged in as the SH user.
Look at the PSTART and PSTOP columns to see information about partition pruning.
You see that the query has to access only one partition for getting the result, namely
partition #13.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

What is the name of the partition that was accessed? Use the user_tab_partitions
table to identify the name of the partition.

s
a
h
)

om
c

l
ai
m
e
g
idcode
@typing
u
You can execute the sol_04_01_02.sql solution script tob
avoid
the
in
a
Gthe SH user.
t
d

this practice step using either SQL*Plus or SQL Developer


logged
in
as
n
han StudeThe name suggests
The name of partition #13 of the SALES table is SALES_Q1_2000.
k
m is
the type of data the partition may contain.mi
a e th
h
s
uspartitioned on the TIME_ID column.
N ( is trange
o
3. The SALES table of the sample A
schema
se dimension table over TIME_ID allows dynamic
KH
Joining the SALES fact table
with the
times
n
e
M
c the execution plan of the SELECT statement below.
partition pruning to take
A place.
liView
L
e
A
l
DELETELFROM plan_table;
ab
r
U
COMMIT;
e
f
M
I
s FOR
PLAN
n
MEXPLAIN
a
A
r
norewrite */
H oSELECT
-t /*+
n
S
t.calendar_month_desc

D
n
,
p.prod_subcategory
,
c.cust_city
,
sum(s.amount_sold) AS dollars
FROM
sales s
,
times t
,
products p
,
customers c
WHERE
s.time_id = t.time_id
AND
s.prod_id = p.prod_id
AND
s.cust_id = c.cust_id
AND
c.cust_city='Ravensburg'
AND
p.prod_category in ('Hardware')
AND
t.fiscal_year in (2000,1999)
GROUP BY p.prod_subcategory, calendar_month_desc
,
c.cust_city;
SELECT * FROM
TABLE(DBMS_XPLAN.DISPLAY);

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

How can you tell that dynamic partition pruning has taken place, from the SQL Developer
output? (Your output may be slightly different than the partial output shown below.)

You can execute the sol_04_01_03.sql solution script to avoid typing the code in
this practice step using either SQL*Plus or SQL Developer logged in as the SH user.
Unlike static partition pruning, you do not see any absolute numbers in the PSTART
and PSTOP columns, but instead KEY(SQ) values. This indicates that dynamic
partition pruning takes place for this query at run time and not at parse time.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 6

Practice 4-2: Partition Maintenance Commands


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice you create a new partition.

Tasks
1.

Using the SH user, execute the following SQL statement. What happens and Why?
INSERT INTO sales
VALUES(13, 2280, '02-JAN-2002', 2,999,1,1232.16);

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_04_02_01.sql solution script to avoid typing the code in
this practice step logged in as the SH user.
The row being inserted has a TIME_ID value of 02-JAN-2002, which is outside the
range of the last partition of the SALES table. (Note: Rolling window operations are
typical in a data warehouse and require future partitions to be created before you use
them.)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 7

2.

Create a new partition to accept the previous row. The new partition should have the
following attributes:
a. Partition Name: SALES_Q1_2002

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

b.

Partition Boundary: TO_DATE(' 2002-04-01 00:00:00', 'SYYYY-MM-DD


HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
b
L therasol_04_02_02.sql
U
You can
executefe
solution script to avoid typing the code in
M
I
s
anstep logged in as the SH user.
AthisMpractice
r
t
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Reattempt to insert the row again and verify that it is inserted without error.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G the code in
You can execute the sol_04_02_03.sql solution scriptd
tob
avoid typing
t
n
n
this practice step logged in as the SH user.
ha Stude
k
After you create the partition successfully, you
insert the row without error.
imcanhnow
s
i
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 4


Chapter 4 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G5
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
5
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 2

Overview of Practices for Lesson 5


Practices Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In these practices, you will examine parallel-related parameters, determine the degree of
parallelism used, and modify the degree of parallelism for a query.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 3

Practice 5-1: Parallelism Concepts

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In this practice you will examine parallel-related parameters, determine the degree of parallelism
used, and modify the degree of parallelism for a query. For this entire lab, use the SYS account
(with dba privileges).

Tasks
1.

Check the initialization parameters relating to parallelism (parameters containing the string
parallel).
The output in SQL Developer looks like this:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_05_01_01.sql solution script to avoid typing the code in
this practice step using SQL Developer or SQL*Plus logged in as the SYS user.
Make a note of the value for parallel_max_servers.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Logged in as the SYS user, determine the dictionary DOP of the SALES table.

s
a
h
)

om
c

l
ai
m
e in
g theidcode
You can execute the sol_05_01_02.sql solution script to avoid
typingu
@
a
Gthe SYS user.
this practice step using either SQL Developer or SQL*Plus
in
as
t
dblogged

n
n
ha Stude
k
imto thehDEFAULT
3. Modify the dictionary DOP of the SH.SALES table
DOP, and verify that
is
m
a
t
your operation was successful.
sh use
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_05_01_03.sql solution script to avoid typing the code in
this practice step using either SQL Developer or SQL*Plus logged in as the SYS user.
4.

Determine the list of Oracle background processes that currently run your instance. As
shown below, use a terminal window and connect to SQL Plus as the SYS user.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
I
s the code from the sol_05_01_04.sql solution script to avoid
n
Mcan
You
copy/paste
a
A
r
H typing
-t code in this practice step using SQL*Plus logged in as the SYS user. (The
n
the
S

o
D
n
host command does not work with SQL Developer.)
Note that there are no running parallel execution server processes.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Logged in as the SYS user, execute the following SQL statement:


SELECT /*+ FULL(s) */ count(*) FROM sh.sales s;
Determine the value of the DEFAULT DOP for this statement. Assume that the requested
DOP for this operation is not downgraded.

s
a
h
)

om
c

l
ai
m
g
de
iterminal
@
u
Hint: Copy/paste the host ps command found in sol_05_01_05.sql
to
the
a
db ent G

window.
n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
...
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can copy/paste the host command from the sol_05_01_05.sql solution script
to avoid typing the code in this practice step using SQL*Plus logged in as the SYS
user. (The host command does not work with SQL Developer.)
You can determine the number of parallel execution servers that were used to
parallelize the full table scan of the SALES table by using the UNIX ps command to
list the processes used. As shown above, four parallel execution servers (ora_p00n)
were spawned during the operation. Because the database did not downgrade the
requested DOP, this implies that the DEFAULT DOP used for this operation is 4.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Logged in as the SYS user, determine a way to change the value of the DEFAULT DOP for
the previous operation.
After it is found, verify that your solution works by executing the SQL statement from step 5,
and then checking the background processes.
As was discussed in this lesson, the DEFAULT DOP depends on the values of the
PARALLEL_THREADS_PER_CPU and CPU_COUNT initialization parameters. Changing
their values should change the value of the DEFAULT DOP. You should avoid
modifying the CPU_COUNT parameter in general. You can change the initialization
parameter by using the ALTER SYSTEM command.
Execute the first two statements individually either in SQL Developer (using F9) or in
SQL*Plus. The host command must be run in SQL*Plus.
For example, in SQL Developer:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In SQL*Plus, the output from the host ps command is shown here:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Now you can see the difference. Here, the number of parallel execution servers used
to parallelize the operation is eight. Note, however, that this result may be different
depending on the machine used to run this lab because the default degree of
parallelism is also based on the number of CPUs of your machine.
DEFAULT DOP = parallel_threads_per_cpu (4) * cpu_count (2) = 8 total.
You can execute the first two statements in the sol_05_01_06.sql solution script in
SQL Developer logged in as the SYS user. You can copy/paste the host command
from this script to avoid typing using SQL*Plus (logged in as the SYS user). The host
command does not work with SQL Developer.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7.

In reality, by just looking at the output in the previous step, it is not possible to prove that
the DOP used for parallelizing the operation is really 4. Although the output from steps 5
and 6 seems to prove it, you need to verify one more thing to be sure. What else do you
need to verify (assuming that the requested DOP is not downgraded)?
You need to ensure that the operation does not invoke interparallelism. If that is the
case, the number of parallel execution servers is twice as high as the requested
DOP. This can be verified by looking at the OTHER_TAG column of the V$SQL_PLAN
table. For the corresponding operation, this column should not contain any
PARALLEL_TO_PARALLEL operation.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H You can- execute the sol_05_01_07.sql solution script to avoid typing the code in
DS non

this practice step using either SQL Developer or SQL*Plus logged in as the SYS user.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

8.

Change the dictionary DOP of the SALES table to 30. Set the initialization parameter
PARALLEL_MIN_PERCENT to 100 and execute the SQL statement you executed in steps 5
and 6. What happens, and why?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_05_01_08.sql solution script to avoid typing the code in
this practice step using either SQL Developer or SQL*Plus logged in as the SYS user.
Recall from step 3 the PARALLEL_MAX_SERVERS setting. This represents the
maximum number of parallel execution servers that can run on this instance. By
changing the dictionary DOP of the SALES table to 30, the SQL statement is indirectly
requesting a DOP of 30 for its operation. Because PARALLEL_MAX_SERVERS is set so
close to this number, the DOP of 30 cannot be satisfied.
9.

Change the dictionary DOP of the SALES table back to its original value of 1. Change the
PARALLEL_MIN_PERCENT parameter for your session to 0.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Verify that the operations are successful by comparing your output to that shown here.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
A e lic
L
A
l
You can execute
the sol_05_01_09.sql
solution script to avoid typing the code in
b
L
a
r
U
this practice
feusing either SQL Developer or SQL*Plus logged in as the SYS user.
IM nstep
s
M
A -tra
H
S
D
non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 5


Chapter 5 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G6
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
6
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 2

Overview of Practices for Lesson 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In these practices, you will enable parallel operations at the session level, create partitioned
tables, perform parallel DML, and examine the effects of parallel DML on subsequent
operations.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 3

Practice 6-1: Parallel Operations in Data Warehouses


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice you will enable parallel operations at the session level.

Assumptions
Complete all steps in practice 5-1, ensuring that practice 5-1, step 9, is completed successfully.

Tasks
1.

Connect as the user SH and execute the following SQL statement:


SELECT /*+ FULL(s) FULL(t) */ count(*)
FROM
sales s, times t
WHERE s.time_id = t.time_id;
How do you verify whether this statement has been executed in parallel?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Look at the OTHER_TAG column of V$SQL_PLAN. Apparently, the statement has


executed serially because no tag contains the word PARALLEL. (Note: if your output
shows any parallel tags, re-run the sol_05_01_09.sql solution script.)
You can execute the sol_06_01_01.sql solution script to avoid typing the code in
this practice step.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Verify the dictionary DOP of the SALES and TIMES tables, as well as the parallel statuses
of your session, and explain why the previous statement was executed serially.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
I execute
s the sol_06_01_02.sql solution script to avoid typing the code in
You
can
n
M
a
A
r
H thisopractice
-t step.
n
S

The
D
n statement in step 1 is not executed in parallel because: a) The dictionary DOP of

both tables is set to 1; b) Your session has only parallel query set to ENABLED; and c)
The executed statement does not contain any parallel hint.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Force the statement in step 1 to execute in parallel with a DOP of 5 without including a hint
in the query. After you have found a solution, execute the statement again and verify that
the statement was executed in parallel.
To parallelize the statement without including a hint, you can force your session to
parallelize all subsequent queries executed in your session (provided no
parallelization rule is violated). You can do so by issuing the ALTER SESSION FORCE
PARALLEL ... command. You can see that the PQ_STATUS of your session is set to
FORCED.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

After you execute the statement again, you can verify parallel execution by looking at
the OTHER_TAG column of V$SQL_PLAN for the corresponding statement. This
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

column now shows various PARALLEL_TO tags, which proves the parallel execution.
You can also look at the list of parallel execution servers spawned during this
execution.
You can execute the sol_06_01_03.sql solution script to avoid typing the code in
this practice step.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 7

Practice 6-2: Partitioned Tables and DML


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice, you create partitioned tables, perform parallel DML, and examine the effects of
parallel DML on subsequent operations.

Assumptions
You have completed practice 6-1.

Tasks
1.

Connected as the user SH, create the LITTLE_SALES partitioned table by executing the
following SQL statement:
CREATE TABLE little_sales
PARTITION BY HASH (time_id)
(PARTITION LS1,PARTITION LS2)
PARALLEL
AS
SELECT * FROM sales WHERE 1=2;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G DOP is set to
Note that the LITTLE_SALES table has only two partitions, and
t
dbthe dictionary

n
n
a tuPARALLEL
DEFAULT. Recall that the default mode of a session ish
DISABLE
DML.
de
k
S into the LITTLE_SALES
Then, execute the following SQL statement to insert
rows
im some
s
i
m
h
table:
sha use t
(
INSERT INTO little_sales
AN e to
SELECT *
H
K ens
FROM sales
M
WHERE ROWNUM
lic
LA< 5000;
e
A
l
Finally:
L
ab
r
U
e
f
M
a. Commit
your
I
s changes.
n
M
a
A
r
List-the
t processes spawned during the last statements execution (see
H b. osol_06_02_01.sql
n
S

for the linux command). What is your conclusion?


D
n

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

The LITTLE_SALES table has only two partitions, but multiple parallel execution
servers are started to execute the INSERT statement in parallel (your output may
show more than five). Multiple parallel execution servers insert into LITTLE_SALES
table because the SALES table scan is done serially (parallel query is disabled for the
session). Thus, the parallel execution servers insert into the same partition.
Use the sol_06_02_01.sql solution script to avoid typing the code in this practice
step using SQL*Plus logged in as the SH user.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Force parallel DML in your session with a degree of parallelism set to 5. When done, delete
all rows from the LITTLE_SALES table where PROD_ID is an even number. Do not commit
your changes. Try to retrieve rows from the LITTLE_SALES table. What happens and why?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

After your transaction has modified an object in parallel, it is impossible to query the
content of that object until the parallel changes are committed or rolled back. If you
try, you get an ORA-12838 error message.
You can execute the sol_06_02_02.sql solution script to avoid typing the code in
this practice step using either SQL Developer or SQL*Plus logged in as the SH user.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

From a second session connected as SYS, count the number of rows in the
SH.LITTLE_SALES table. Then, delete rows from SH.LITTLE_SALES with an odd
PROD_ID value. What happens, and why?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
As explained
during
this lesson, locking for PDML statements is different from
I
s
n
M
DML statements. Although it is still possible to query all rows of the
Alocking-for
traserialtable
H
LITTLE_SALES
from another session, it is impossible to modify rows that are
n
S
D
no
not modified by the first session. This is because the execution coordinator of the
first session holds an exclusive lock on each partition.
Stop the process by clicking the red x in either the worksheet tab or the Script
Output tab.
You can execute the sol_06_02_03_session2.sql solution script to avoid typing
the code in this practice step using either SQL Developer or SQL*Plus logged in as
the SYS user.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

4.

In the second session (connected as sys), roll back your changes and exit. In the first
session (connected as sh), rollback your changes, drop the LITTLE_SALES table, delete
any rows in the PLAN_TABLE table, and then commit.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_06_02_04_session2.sql and sol_06_02_04.sql


solution scripts to avoid typing the code in this practice step using either SQL
Developer or SQL*Plus.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 6


Chapter 6 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G7
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
7
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 2

Overview of Practices for Lesson 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In these practices, you will perform a guided exercise to examine asynchronous distributed
change data capture.
You use two databases and transfer DML changes from a source database to a staging
database. The source database is named src1 and the staging database is named stage1.

In the source database, a publisher is created to propagate any DML changes made to
the sh.products table.

The changes are propagated into a publishers account using streams technology to
the staging database and stored into a change table called products_ct.

Finally, you create a subscriber account in the staging database and then create a
subscription view to access a limited subset of the data that is published.

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 3

Practice 7-1: Using Asynchronous Distributed Change Data Capture

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In this practice you follow instructions for a guided set of exercises. A solution result file that
contains the output of the script, sol_07_01_02_results.txt, has been created as well.
You may consult this file to see the expected results if any problems occur while you run the lab
script.

Tasks
1.

You need to start up the two other databases on your server to complete lab seven. The
databases are named src1 and stage1. It is recommended that you shut down the
existing database, orcl, to minimize overhead.
(See the next two screen images for output illustrations.)

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

After starting the src1 and stage1 databases, exit SQL*Plus.

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

You can copy/paste code from the sol_07_01_01.sql solution script to avoid
typing the code in this practice step while using SQL*Plus.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 5

2.

The remainder of this practice is a guided lab.


In a terminal window, change to the /home/oracle/labs directory (see the screen
image example below). It is suggested that you maximize the size of your terminal
window to prevent the results from scrolling off the screen between each pause.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

As you run each lab piece and study the results, always note the account that is used
to perform the step and which database it is performed in.
Start the lab by executing the sqlplus /nolog command and then execute the
lab_07_01_02.sql script as follows:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
imto study
A pause has been created to give time form
you
isthe results. For detailed
h
a
t
information, see Oracle Database Data
e Guide 11g Release 1 (11.2).
shWarehousing
s
(
u
o
AN from
tthe
H
e
The compatible parameter
change
documentation was commented out, as it
K ens
M
is irreversible in 10g
A and later.
licThe streams_pool_size parameter change was
L
commented out
becausele
the automatic SGA memory-tuning feature is turned on. The
A
L reboots
abso that the changes can take effect.
databaseU
now
r
e
f
M
s continue.
Press
to
n
MI [Enter]
a
A
r
H on-t
S

D
n

3.

Press [Enter] to continue with the guided practice.


Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

SQL> REM ***************************************************


SQL> REM Step 2 Stage DB: Parameters
SQL> REM ***************************************************
SQL> connect sys/oracle@stage1 as sysdba;
Connected.
SQL> REM alter system set compatible=11.0 scope=spfile;
SQL> alter system set global_names=true scope=spfile;
System altered.
SQL> REM alter system set java_pool_size=100000000 scope=spfile;
SQL> alter system set open_links=40 scope=spfile;
System altered.

s
a
h
)

om
c

l
System altered.
ai
m
g
ide
@
u
SQL> alter system set parallel_max_servers=45 scope=spfile;
a
db ent G

n
System altered.
ha Stud
k
im his
m
SQL> alter system set processes=100
a scope=spfile;
t
h
e
s
s
(
N to u
System altered.
A
KH ense
M
SQL> alter system
scope=spfile;
lic
LA setle sessions=100
A
L
ab
System altered.
r
U
e
f
M
ns system set streams_pool_size=70M scope=spfile;
MIREMtraalter
A
SQL>
H onalter
- system set undo_retention=3600 scope=spfile;
DS SQL>
n
SQL> alter system set job_queue_processes=20 scope=spfile;

System altered.
SQL> pause;
A pause has been created to give you time to study the results.
The compatible parameter change from the documentation was commented out, as it
is irreversible in 10g and later. The streams_pool_size parameter change was
commented out because the automatic SGA memory-tuning feature is turned on. The
database now reboots so that the changes can take effect.
Press [Enter] to continue.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

SQL> shutdown immediate;


Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup;
ORACLE instance started.
Total System Global Area 263639040 bytes
Fixed Size
1299284 bytes
Variable Size
113249452 bytes
Database Buffers
142606336 bytes
Redo Buffers
6483968 bytes
Database mounted.
Database opened.
SQL> pause;

s
a
h
)

om
c

l
ai
m
g
SQL> REM ************************************************
ide
@
u
a
SQL> REM Step 3 Source DB: Adjustments
db ent G

SQL> REM ************************************************


n
a tud
SQL> connect sys/oracle@src1 as sysdba;kh
im his S
Connected.
m
SQL> ALTER DATABASE FORCE LOGGING;
sha use t
(
AN e to
Database altered.
H
K ens
M
SQL> ALTER DATABASE
licSUPPLEMENTAL LOG DATA;
LA lADD
e
A
L
ab
r
DatabaseUaltered.
e
f
M
I
s
n
M
a
A
r
HSQL>onALTER
-t TABLE sh.products ADD SUPPLEMENTAL LOG GROUP
S

log_group_products(PROD_ID,
PROD_NAME, PROD_LIST_PRICE) ALWAYS;
D
n
4.

Press [Enter] to continue with the guided practice.

Table altered.
SQL> ALTER TABLE sh.products ADD SUPPLEMENTAL LOG DATA (ALL)
COLUMNS;
Table altered.
SQL> pause;
Adjustments are made to the source database using a DBA account. The
supplemental logging feature is turned on for the whole database in general. For the
source table, supplemental logging is turned on including the columns that are to be
propagated to the staging database.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Press [Enter] to continue with the guided practice.


SQL> REM *************************************************
SQL> REM Step 4 Source DB: Create & grant privileges to publisher.
SQL> REM *************************************************
SQL> connect sys/oracle@src1 as sysdba;
Connected.
SQL> CREATE USER source_cdcpub IDENTIFIED BY oracle QUOTA UNLIMITED
ON SYSTEM QUOTA UNLIMITED ON SYSAUX;
User created.
SQL> GRANT CREATE SESSION TO source_cdcpub;
Grant succeeded.

s
a
h
)

om
c

l
Grant succeeded.
ai
m
g
ide
u
SQL> GRANT CREATE DATABASE LINK TO source_cdcpub;a@
db ent G

n
Grant succeeded.
ha Stud
k
im TOhsource_cdcpub;
is
m
SQL> GRANT EXECUTE on DBMS_CDC_PUBLISH
a
t
h
e
(s us
N
Grant succeeded.
A e to
H
K ens TO source_cdcpub;
M
SQL> GRANT EXECUTE_CATALOG_ROLE
LA le lic
A
L
Grant succeeded.
ab
r
U
e
f
M
s
n
MIGRANT
a
SQL>
SELECT_CATALOG_ROLE
TO source_cdcpub;
A
r
t
H
n
S
D
no
SQL> GRANT DBA TO source_cdcpub;

Grant succeeded.

SQL> EXECUTE DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE( GRANTEE=>


'source_cdcpub');
PL/SQL procedure successfully completed.
SQL> pause;
A publisher account, source_cdcpub, is created in the source database. Privileges
must be granted to the publisher. The publisher requires dba privileges and admin
privileges for streams technology.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Press [Enter] to continue with the guided practice.


SQL> REM *************************************************
SQL> REM Step 5 Stage DB: Create & grant privileges to publisher.
SQL> REM **********************************************************
SQL> connect sys/oracle@stage1 as sysdba;
Connected.
SQL> CREATE USER staging_cdcpub IDENTIFIED BY oracle DEFAULT
TABLESPACE SYSTEM QUOTA UNLIMITED ON SYSTEM;
User created.
SQL> GRANT CREATE SESSION TO staging_cdcpub;
Grant succeeded.

s
a
h
)

SQL> GRANT CREATE TABLE TO staging_cdcpub;

om
c

l
SQL> GRANT CREATE TABLESPACE TO staging_cdcpub;
ai
m
g
Grant succeeded.
ide
@
u
a
SQL> GRANT UNLIMITED TABLESPACE TO staging_cdcpub;
db ent G

n
ha Stud
Grant succeeded.
k
im his
SQL> GRANT SELECT_CATALOG_ROLE TO
staging_cdcpub;
m
sha use t
(
Grant succeeded.
AN e tTOo staging_cdcpub;
H
SQL> GRANT EXECUTE_CATALOG_ROLE
K ens
M
Grant succeeded.
LA le lic
A
SQL> GRANTL CONNECT,
ab RESOURCE, DBA TO staging_cdcpub;
r
U
e
f
M
s
Grant
n
MI succeeded.
a
A
r
HSQL>onGRANT
-t CREATE SEQUENCE TO staging_cdcpub;
S

D
n
Grant succeeded.

Grant succeeded.

SQL> EXECUTE DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(grantee =>


'staging_cdcpub');
PL/SQL procedure successfully completed.
SQL> GRANT CREATE DATABASE LINK TO staging_cdcpub;
Grant succeeded.
SQL> pause;
A publisher account, staging_cdcpub, is created on the source database. Privileges
must be granted to the publisher. The publisher requires the dba privilege and admin
privileges for streams technology. Because this is a distributed setup, the publisher
role is spread to both databases.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7.

Press [Enter] to continue with the guided practice.


SQL> REM ************************************************
SQL> REM Step 6 Source DB Publisher: Create Database Link
SQL> REM ************************************************
SQL> connect source_cdcpub/oracle@src1;
Connected.
SQL> CREATE DATABASE LINK stage1 CONNECT TO staging_cdcpub
IDENTIFIED BY oracle USING 'stage1';
Database link created.
SQL> pause;
Create a database link from the source database to the staging database. The link
name is the same name as the database name and is created by the publisher
account.

s
a
h
)

om
c

l
ai
m
g
8. Press [Enter] to continue with the guided practice.
ide
@
u
a
db ent G

SQL> REM ************************************************


n
ha S
SQL> REM Step 7 Staging DB Publisher: Create
Database
ud Link
k
t
m
i his
SQL> REM ************************************************
m
a
SQL> connect staging_cdcpub/oracle@stage1;
sh use t
(
Connected.
ANsrc1eCONNECT
to
SQL> CREATE DATABASE LINK
TO source_cdcpub IDENTIFIED
H
s
K
BY oracle USING 'src1';
M licen
A
ALcreated.
le
Database link
b
L
a
U fer
SQL> pause;
M
I
M trans
A
H
DS non
Create a database link from the staging database to the source database. The link
name is the same name as the database name and is created by the publisher
account.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

9.

Press [Enter] to continue with the guided practice. (There is a short pause while the PL/SQL
procedure executes.)
SQL> REM **************************************************
SQL> REM Step 8 Staging DB Publisher: Identify the change source
database and create the change sources
SQL> REM **************************************************
SQL> connect staging_cdcpub/oracle@stage1;
Connected.
SQL> BEGIN
2
DBMS_CDC_PUBLISH.CREATE_HOTLOG_CHANGE_SOURCE(
3
change_source_name => 'CHICAGO',
4
description => 'test source',
5
source_database => 'SRC1.US.ORACLE.COM');
6 END;
7 /

s
a
h
)

om
c

l
ai
m
g
SQL> pause;
ide
@
u
a
b nt G
d

n
You now identify the change source database as SRC1.US.ORACLE.COM
create a
de has and
hain this
u
k
t
change source named CHICAGO. The change source
example
a
naming
S
imof thehisource
s
m
convention of Chicago, indicating the location
database system.
a
t
h
e
(s us
N
10. Press [Enter] to continue with the
Aguidedepractice.
to (Again, a short pause during execution.)
H
K ens
M
A e lic
SQL> REM *************************************************
L
A
l DB Publisher: Create change sets.
SQL> REM Step
9 Staging
b
L
a
U*************************************************
SQL> REM
er
f
M
I
s
SQL>
connect
staging_cdcpub/oracle@stage1;
AM -tran
Connected.
H
DS SQL>
nonBEGIN
PL/SQL procedure successfully completed.

2
3
4
5
6
7
8

DBMS_CDC_PUBLISH.CREATE_CHANGE_SET(
change_set_name => 'CHICAGO_DAILY',
description => 'change set for product info',
change_source_name => 'CHICAGO',
stop_on_ddl => 'y');
END;
/

PL/SQL procedure successfully completed.


SQL> pause;
You now create a change set that is named CHICAGO_DAILY and associated with the
change source that is named CHICAGO.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 13

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

11. Press [Enter] to continue with the guided practice.

SQL> REM ************************************************


SQL> REM Step 10 Create tablespace to hold change tables.
SQL> REM ************************************************
SQL> connect sys/oracle@stage1 as sysdba;
Connected.
SQL> create tablespace TS_CHICAGO_DAILY datafile
'/u01/app/oracle/oradata/stage1/ts_chicago_daily01.dbf'
2
size 20M;
Tablespace created.
SQL> pause;

s
a
h
You now create a tablespace in the staging database to hold the change tables. This
)
step does not occur in the documentation, only assumed to exist. The datafileom
storage location is chosen to be consistent with other datafiles that already
ilcexist.
a
m de
g
@ Gui
12. Press [Enter] to continue with the guided practice.
a
b
d ent
n
a
h Stud
SQL> REM *************************************************
k
m
i Create
SQL> REM Step 11 Staging DB Publisher:
is the change tables.
m
h
a
t
SQL> REM *************************************************
sh use
(
SQL> connect staging_cdcpub/oracle@stage1;
AN e to
Connected.
H
K ens
SQL> BEGIN
M
2
DBMS_CDC_PUBLISH.CREATE_CHANGE_TABLE(
A e lic
L
A
3
owner bl
=> 'staging_cdcpub',
L
a
r
U
4
change_table_name
=> 'products_ct',
e
f
M
I
s
5
change_set_name
=>
'CHICAGO_DAILY',
n source_schema
M
a
A
r
t
6
=>
'SH',
H
source_table
=> 'PRODUCTS',
DS n7 on
8
column_type_list => 'PROD_ID NUMBER(6), PROD_NAME
VARCHAR2(50), PROD_LIST_PRICE NUMBER(8,2),
9
JOB_ID VARCHAR2(10), DEPARTMENT_ID NUMBER(4)',
10
capture_values
=> 'both',
11
rs_id
=> 'y',
12
row_id
=> 'n',
13
user_id
=> 'n',
14
timestamp
=> 'n',
15
object_id
=> 'n',
16
source_colmap
=> 'n',
17
target_colmap
=> 'y',
18
options_string
=> 'TABLESPACE TS_CHICAGO_DAILY');
19 END;
20 /
PL/SQL procedure successfully completed.
SQL> pause;
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 14

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

You now create a change table that is named PRODUCTS_CT. The change table is
constructed with only a subset of the many columns in the source table that is
named PRODUCTS. These are the columns that have been decided upon to propagate
to the staging system and represent the published data source.

13. Press [Enter] to continue with the guided practice.


SQL> REM *************************************************
SQL> REM Step 12 Staging DB Publisher: Enable the change source.
SQL> REM *************************************************
SQL> connect staging_cdcpub/oracle@stage1;
Connected.
SQL> BEGIN
2
DBMS_CDC_PUBLISH.ALTER_HOTLOG_CHANGE_SOURCE(
3
change_source_name => 'CHICAGO',
4
enable_source
=> 'Y');
5 END;
6 /

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
PL/SQL procedure successfully completed.
db ent G

n
ha Stud
k
SQL> pause;
im his
m
t
ha sbye default,
sdisabled
(
Change sources that are created are
so you now enable the
N to u
change source for usage.
A
KH ense
M
ic guided practice.
lthe
14. Press [Enter] to continue
LA with
e
A
l
L
ab
r
U
e
f
M
SQL> IREM ************************************************
ns 13 Staging DB Publisher: Enable the change set.
M REMtraStep
SQL>
A
H onREM
- ************************************************
DS SQL>
n
SQL> connect staging_cdcpub/oracle@stage1;
Connected.
SQL> BEGIN
2
DBMS_CDC_PUBLISH.ALTER_CHANGE_SET(
3
change_set_name => 'CHICAGO_DAILY',
4
enable_capture => 'y');
5 END;
6 /
PL/SQL procedure successfully completed.
SQL> pause;

Change sets are created disabled by default, so you now enable the change set for
usage. At this point, the Streams capture and apply processes are started.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 15

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

15. Press [Enter] to continue with the guided practice.

SQL> REM *************************************************


SQL> REM Step 14 Source DB: Create a new row in table and then force
log switch
SQL> REM *************************************************
SQL> connect sys/oracle@src1 as sysdba;
Connected.
SQL> INSERT INTO SH.PRODUCTS
2
(
PROD_ID,PROD_NAME,
3
PROD_DESC,PROD_SUBCATEGORY,
4
PROD_SUBCATEGORY_ID,PROD_SUBCATEGORY_DESC,
5
PROD_CATEGORY,PROD_CATEGORY_ID,
6
PROD_CATEGORY_DESC,PROD_WEIGHT_CLASS,
7
PROD_UNIT_OF_MEASURE,PROD_PACK_SIZE,
8
SUPPLIER_ID,PROD_STATUS,
9
PROD_LIST_PRICE,PROD_MIN_PRICE,
10
PROD_TOTAL,PROD_TOTAL_ID,
11
PROD_SRC_ID,PROD_EFF_FROM,
12
PROD_EFF_TO,PROD_VALID
13
)
14 VALUES
15
(
150,'Laser Mouse',
16
'Laser 200dpi mouse','Accessories',
17
2031,'Accessories',
18
'Peripherals and Accessories',203,
19
'Peripherals and Accessories',1,
20
'U','P',
21
1,'ordered',
22
32.48,32.48,
23
'TOTAL',1,
24
NULL,'1-JAN-1998',
25
NULL,'A'
26
);

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
1 row created.
SQL> COMMIT;
Commit complete.
SQL> alter system switch logfile;
System altered.
SQL> host sleep 85
SQL> pause;

You now perform a DML command to manipulate data and allow the propagation to
take place and send the row to the staging database. The DML statement is
committed and a log switch is forced. A sleep command has been added to give time
for the row to arrive on the staging database. The SYS account was used to insert the
row, but any account that has the appropriate privileges could have performed the
insert. A DBA account is needed to perform the log switch.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 16

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

16. Press [Enter] to continue with the guided practice.

SQL> REM ************************************************


SQL> REM Step 15 Staging DB: Verify Changed row exist in change
tables.
SQL> REM ************************************************
SQL> connect staging_cdcpub/oracle@stage1;
Connected.
SQL> select operation$,prod_id,prod_name from products_ct;
OP
PROD_ID PROD_NAME
-- ---------- -------------------------------------------------I
150 Laser Mouse

s
a
h
)

1 row selected.

om
c

l
ai
m
g
ide
@
After the pause, you now verify that the row has been propagated
to theu
staging
a
G in the
b notnexist
t
database server and exists in the change table. This step
ddoes

n
acheck.tuThis
documentation, but was added here as a verification
deconcludes the setup
h
k
of the publisher model.
im his S
m
a et
shpractice.
(
17. Press [Enter] to continue with the guided
N to us
A
KH ense
SQL> REM *************************************************
M
A Staging
licDB: Create a subscriber account.
SQL> REM Step L
16
e
A
l
SQL> REM *************************************************
rab
UL sys/oracle@stage1
SQL> Iconnect
as sysdba;
e
f
M
s
n
M
Connected.
A CREATE
tra USER subscriber1 IDENTIFIED BY oracle DEFAULT TABLESPACE
H
SQL> n
S
D USERS
no QUOTA UNLIMITED ON USERS;
SQL> pause;

User created.
SQL> GRANT CREATE SESSION TO subscriber1;
Grant succeeded.
SQL> GRANT EXECUTE ON DBMS_CDC_SUBSCRIBE to subscriber1;
Grant succeeded.
SQL> pause;
A subscriber account is now created on the staging database to access the
published data. There can be multiple subscribers if needed. You grant privileges to
the subscriber to allow login and to access the packages needed.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 17

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

18. Press [Enter] to continue with the guided practice.

SQL> REM *************************************************


SQL> REM Step 17 Staging DB Publisher: Grant access to subscribers.
SQL> REM *************************************************
SQL> connect staging_cdcpub/oracle@stage1;
Connected.
SQL> GRANT SELECT ON staging_cdcpub.products_ct TO subscriber1;
Grant succeeded.
SQL> pause;
You now use the publisher account on the staging database to grant access to the
staging table for each subscriber that needs access. Instead of granting access to
the whole table, limited access can be granted at the column level exposing only a
subset of the published data to each subscriber. Different subscribers can have
different access levels.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
19. Press [Enter] to continue with the guided practice.
db ent G

n
ha Stud
SQL> REM *************************************************
k
im for
SQL> REM Step 18 Find the source tables
iswhich the subscriber has
m
h
a
t
access privileges
sh use
(
SQL> REM *************************************************
AN e to
SQL> connect subscriber1/oracle@stage1;
H
K ens
Connected.
M
A all_source_tables;
SQL> select * L
from
lic
e
A
l
L
ab
r
U
no rows
selected
e
f
M
MI trans
A
HSQL>onpause;
S

D
n
You now use the subscriber account and check to see what source tables are
accessible to create a subscription to them.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 18

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

20. Press [Enter] to continue with the guided practice.

SQL> REM ***********************************************


SQL> REM Step 19 Find the change set names and columns for which the
subscriber has access privileges.
SQL> REM ***********************************************
SQL> connect subscriber1/oracle@stage1;
Connected.
SQL> SELECT UNIQUE CHANGE_SET_NAME, COLUMN_NAME, PUB_ID
2 FROM ALL_PUBLISHED_COLUMNS
3 WHERE SOURCE_SCHEMA_NAME ='SH' AND SOURCE_TABLE_NAME =
'PRODUCTS';
no rows selected

s
a
h
)

SQL> pause;

om
c

l
ai
m
g
ide
@
u
a
db ent G
21. Press [Enter] to continue with the guided practice.

n
ha Stud
k
im his
SQL> REM ***************************************************
m
SQL> REM Step 20 Create a subscription.
sha use t
(
SQL> REM ***************************************************
AN e to
SQL> connect subscriber1/oracle@stage1;
H
K ens
Connected.
M
SQL> BEGIN
A e lic
L
A
l
2
DBMS_CDC_SUBSCRIBE.CREATE_SUBSCRIPTION(
b
L
a
U change_set_name
3
=> 'CHICAGO_DAILY',
er
f
M
I
s
4
description
=>
data for PRODUCTS',
n subscription_name => 'Change
M
a
A
r
t
5
'PRODUCTS_SUB');
H
DS n6 onEND;

You now use the subscriber account to identify the names of the change sets and
the columns that are accessible to create a subscription to them.

PL/SQL procedure successfully completed.


SQL> pause;
You now use the subscriber account to create a subscription to the published data.
The subscription is named PRODUCTS_SUB and references the CHICAGO_DAILY
change set.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 19

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

22. Press [Enter] to continue with the guided practice.

SQL> REM *************************************************


SQL> REM Step 21 Subscribe to a source table and the columns in the
source table.
SQL> REM *************************************************
SQL> connect subscriber1/oracle@stage1;
Connected.
SQL> BEGIN
2
DBMS_CDC_SUBSCRIBE.SUBSCRIBE(
3
subscription_name => 'PRODUCTS_SUB',
4
source_schema
=> 'SH',
5
source_table
=> 'PRODUCTS',
6
column_list
=> 'PROD_ID, PROD_NAME,
PROD_LIST_PRICE',
7
subscriber_view
=> 'PRODUCTS_VIEW');
8 END;
9 /

s
a
h
)

om
c

l
ai
m
g
PL/SQL procedure successfully completed.
ide
@
u
a
db ent G

SQL> pause;
n
ha Stud
k
im his
You now create a subscription view thataism
namedtPRODUCTS_VIEW.
This view only
h
e
accesses a subset of the columns that
s published.
(s haveubeen
N
o
t
HtheAguided
e
s
K
23. Press [Enter] to continue with
practice.
M licen
A
AL able
SQL> REM *************************************************
L
UStep f22
SQL> REM
er Activate the Subscription
M
I
s
n
SQL>
a*************************************************
AM REM
r
t
H
SQL>
connect
subscriber1/oracle@stage1;
DS Connected.
non
SQL> BEGIN
2
DBMS_CDC_SUBSCRIBE.ACTIVATE_SUBSCRIPTION(
3
subscription_name => 'PRODUCTS_SUB');
4 END;
5 /
PL/SQL procedure successfully completed.
SQL> pause;
You now activate the subscription before it can be used.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 20

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

24. Press [Enter] to continue with the guided practice.

SQL> REM *************************************************


SQL> REM Step 23 Get the next set of change data;
SQL> REM *************************************************
SQL> connect subscriber1/oracle@stage1;
Connected.
SQL> BEGIN
2
DBMS_CDC_SUBSCRIBE.EXTEND_WINDOW(
3
subscription_name => 'PRODUCTS_SUB');
4 END;
5 /
PL/SQL procedure successfully completed.

s
a
h
)

SQL> pause;

om
c

l
ai
m
g
ide
@
u
a
db ent G
25. Press [Enter] to continue with the guided practice.

n
ha Stud
k
im his
SQL> REM *************************************************
m
SQL> REM Step 24 Read and queryha
e t of the subscriber
s theucontents
s
(
views;
AN e to
SQL> REM *************************************************
H
K ens
SQL> connect subscriber1/oracle@stage1;
M
Connected.
A e lic
L
A
l PROD_NAME, PROD_LIST_PRICE FROM PRODUCTS_VIEW;
SQL> SELECT PROD_ID,
b
L
a
U fer
M
I
s
PROD_ID
PROD_NAME
PROD_LIST_PRICE
n
M
a
A
r
t
---------------------------------------------------------------H--------n
S

o
D
n
You now get the next set of change data that is available by extending the
subscription window until it is no longer needed.

150 Laser Mouse

32.48

1 row selected.
SQL> exit
When this last segment is completed, exit SQL*Plus.
You now use the subscriber account and query your subscription view to access the
data that has been made available. When you finish using change data, a subscriber
must call the DBMS_CDC_SUBSCRIBE.PURGE_WINDOW procedure. This
indicates to Change Data Capture and the publisher that the change data is no longer
needed by this subscriber. The DBMS_CDC_SUBSCRIBE.PURGE_WINDOW
procedure does not physically remove rows from the change tables; however, the
data is removed logically from the subscriber view and can no longer be selected.
This concludes the subscriber setup and the guided lab exercise.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 21

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

26. Shutdown the databases, src1 and stage1, as they are no longer needed. You also need
to restart the orcl database to continue with the labs. Then, exit SQL*Plus.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
You can copy/paste code from the sol_07_01_26.sql solution script to avoid
typing the code in this practice step while using SQL*Plus.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 22

Practice 7-2: Loading Data from Transportable Tablespaces


Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice, you examine how to use transportable tablespaces to extract and transport
data.

Assumptions
You have completed practice 7-1. Execute the SQL statements below as user SYS using the
orcl database.

Tasks
1.

Assume that you want to move the January 2000 sales data from your data warehouse to
the SALES table in your data mart. The data must be placed into a separate tablespace to
be transported. So, create a tablespace called tt_temp_sales to hold the January sales
data. After you create the tablespace, create a table called temp_jan_sales.

s
a
h
)

om
c

l
ai
m
g
CREATE TABLE sh.temp_jan_sales NOLOGGING TABLESPACE
ide
@
u
a
tt_temp_sales
db ent G

n
AS SELECT * FROM sh.sales
ha '01-FEB-2000';
ud
WHERE time_id BETWEEN '31-DEC-1999'kAND
t
S
m
i his
m
a
sh use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
CREATE TABLESPACE tt_temp_sales DATAFILE
'/tmp/tt_temp_sales.dbf' SIZE 30M REUSE autoextend on;

You can execute the sol_07_02_01.sql solution script to avoid typing the code in
this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 23

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

To prevent any changes to the tt_temp_sales tablespace, set it to read-only by


executing the following SQL statement:
ALTER TABLESPACE tt_temp_sales READ ONLY;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
a totavoid
hscript
ud typing the code in
You can execute the sol_07_02_02.sql solution
k
S
m
i his
this practice step.
m
a
e tdata to read from socket, simply
Note: If you receive an error that states
shNoumore
s
(
exit and reopen SQL Developer
to cleartthe
o error. Then, reexecute the solution script.
AN
H
e
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 24

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

By using SQL Developer or SQL Plus, create a directory called tt_dump_dir pointing to
/home/oracle/wkdir where the log and dump files for the Data Pump export are written.
Then, copy/paste the command used to run the expdp utility into SQL Plus as shown
below. This command is used to export the tt_transfer tablespace metadata.
The contents of the sol_07_02_03.sql solution script are shown here:
CREATE DIRECTORY tt_dump_dir as '/home/oracle/wkdir/';
!expdp \'sh/sh\' DIRECTORY=tt_dump_dir
DUMPFILE=meta_tt_transfer.dmp
TRANSPORT_TABLESPACES=tt_temp_sales
The output of the export command is shown here:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Note: You may have to press Enter to get back to the SQL prompt.
Use the sol_07_02_03.sql solution script to avoid typing the code in this practice
step using both SQL Developer and SQL*Plus.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 25

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

4.

You do not actually have another server for your data mart, so you simulate this by copying
the data and dump file to the /home/oracle/datamart directory. Verify that the files
have been successfully copied.

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

You can copy/paste code from the sol_07_02_04.sql solution script to avoid
typing the code in this practice step while using SQL*Plus.
This simulates a transfer to a different server.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 26

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Using SQL Developer, drop the tt_temp_sales tablespace and try to access the
sh.temp_jan_sales table.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
I
s
n
M
a
A
r
H Youocan
-texecute the sol_07_02_05.sql solution script to avoid typing the code in
n
S

this
practice
step.
D
n

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 27

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Using SQL Developer, create a directory called dm_dump_dir pointing to


/home/oracle/datamart (used by your data mart) that will be used by the Data Pump
import. (Note: Only execute the CREATE DIRECTORY command.)
CREATE DIRECTORY dm_dump_dir as '/home/oracle/datamart/';
After the directory is created, in SQL Plus use the impdp utility as shown below to make
the tt_transfer tablespace accessible to your data warehouse.
!impdp \'sh/sh\' DIRECTORY=dm_dump_dir
DUMPFILE=meta_tt_transfer.dmp logfile=imp_tt.log
TRANSPORT_DATAFILES=/home/oracle/datamart/tt_temp_sales.dbf

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can copy/paste code from the sol_07_02_06.sql solution script to avoid
typing the code in this practice step using SQL*Plus.
7.

When the import is finished, verify that the temp_jan_sales table is accessible. If you
can access the temp_jan_sales table, you are ready to load the data into your data
warehouses SALES table.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 28

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im script
You can execute the sol_07_02_07.sql m
solution
is to avoid typing the code in
h
a
t
this practice step.
sh use
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 29

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

8.

After you have loaded the temp_jan_sales data, roll back the transaction and drop the
tt_temp_sales tablespace, the tt_dump_dir directory, and the dm_dump_dir
directory.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
A e lic
Lthe
You can execute
sol_07_02_08.sql
solution script to avoid typing the code in
A
l
b
L
a
this practice
U step.fer
M
I
M trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 7


Chapter 7 - Page 30

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G8
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
8
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 2

Overview of Practices for Lesson 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In this practice, you will see some of the differences between loading data with SQL*Loader and
loading data with external tables. Here, you use the salesDec01.dat file, which has been
extracted from an OLTP database. This data needs to be loaded into the data warehouse.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 3

Practice 8-1: ETL Loading Using SQL*Loader

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

You apply a transformation during this data load because the data is not in the right format. The
QUANTITY_SOLD and AMOUNT_SOLD columns must be summed, grouping on all other columns,
before loading the data into the target table. Because SQL*Loader cannot sum values while
loading, the data must first be loaded into a staging table and then transformed while being
inserted into the target table.

Tasks
1.

In a terminal window, change to the /home/oracle/labs directory. Then, in SQL*Plus


connect as sh/sh and then execute the lab_08_01_01.sql script.
This script creates the SALES_DEC01 staging table, which has the same structure as the
SALES table. The script also modifies the channel_id column to accept CHAR data
because the raw data is not in exactly the same format as that of the SALES fact table.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

The code in the lab_08_01_01.sql script is displayed as follows:


CREATE
SELECT
FROM
WHERE

TABLE sales_dec01 AS
*
sales
1=0;

SELECT COUNT(*)
FROM sales_dec01;
ALTER TABLE sales_dec01 MODIFY (channel_id CHAR(2) null)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

At the SQL prompt in the same terminal window, load the data from the salesDec01.dat
file into the SALES_DEC01 staging table, using the lab_08_01_02.sql script. This script
calls the sales_dec01.ctl control file.
At the SQL> prompt, enter @lab_08_02_01.sql.

s
a
h
)

om
c

l
i
The code in the lab_08_01_02.sql script is displayed as follows: a

m
e
g
d
i
u
a@direct=true
G
b
t
d
sqlldr sh/sh control=$HOME/labs/sales_dec01.ctl
n den
a
log=$HOME/labs/sales_dec01.log
h
k
tu
S
m
i
m this
a
h
s use
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Logged in as SH, use SQL Developer to load the data from the SALES_DEC01 staging table
into the SALES target table. Use an INSERT, as the data needs to be transformed.
The QUANTITY_SOLD and AMOUNT_SOLD columns must be summed, grouped by all other
columns. In addition, the CHAR values for channel_id must be converted to NUMBER as is
the same column in the SALES table.
The code is shown here:
INSERT /*+ APPEND */ INTO sales
( PROD_ID, CUST_ID, TIME_ID, CHANNEL_ID,
PROMO_ID, QUANTITY_SOLD, AMOUNT_SOLD )
SELECT
PROD_ID, CUST_ID, TIME_ID,
case CHANNEL_ID
when 'S' then 3
when 'T' then 9
when 'C' then 5
when 'I' then 4
when 'P' then 2
else 99
end,
PROMO_ID,
sum(QUANTITY_SOLD),
sum(AMOUNT_SOLD)
FROM sales_dec01
GROUP BY prod_id,time_id,cust_id,channel_id,promo_id;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H You can- execute the sol_08_01_03.sql solutions script to avoid typing the code in
DS non
this practice step.

4.

After you have successfully loaded the data into the SALES table, drop the staging table
sales_dec01 as follows:
DROP TABLE sales_dec01;

You can execute the sol_08_02_04.sql solution script to avoid typing the code in
this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 6

Practice 8-2: ETL Loading Using External Tables

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In this practice you use external tables to load data into a data warehouse. The table is called
sales_delta_XT and it uses data contained in the salesDec01.dat file used in the previous
SQL*Loader practice.

Assumptions
You have completed practice 8-1.

Tasks
1.

Before you can create an external table, you must create a directory object in the database
that points to the directory on the file system where the data files reside. Optionally, you can
separate the location for the log, bad, and discard files from the location of the data files.
To create the directories, execute the following SQL statements logged in as the SH user:
DROP DIRECTORY data_dir;
CREATE DIRECTORY data_dir AS '/home/oracle/labs';
DROP DIRECTORY log_dir;
CREATE DIRECTORY log_dir AS '/tmp';

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_08_02_01.sql solution script to avoid typing the code in
this practice step logged in as the SH user.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

When you create an external table, you define two parts of information:
The metadata information for the table representation inside the database
The HOW access parameter definition to extract the data from the external file
After the creation of this meta information, the external data can be accessed from within
the database, without the necessity of an initial load.
Execute the solution script sol_08_02_02.sql as user SH to create the external table
(code shown here):
CREATE TABLE sales_delta_XT
(
PROD_ID NUMBER,
CUST_ID NUMBER,
TIME_ID DATE,
CHANNEL_ID CHAR(2),
PROMO_ID NUMBER,
QUANTITY_SOLD NUMBER(3),
AMOUNT_SOLD NUMBER(10,2)
)
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY data_dir
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
BADFILE log_dir:'sh_sales.bad'
LOGFILE log_dir:'sh_sales.log_xt'
FIELDS TERMINATED BY "|" LDRTRIM
(prod_id, cust_id,
time_id CHAR(11) DATE_FORMAT DATE MASK "DD-MON-YYYY",
channel_id, promo_id, quantity_sold, amount_sold
)
)
location
(
'salesDec01.dat'
)
)REJECT LIMIT UNLIMITED NOPARALLEL;

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 8

3.

The data in the external file can now be accessed without any further action. To
demonstrate this capability, execute the following SQL commands logged in as the SH user:

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

SELECT COUNT(*) FROM sales_delta_xt;


SELECT MAX(time_id) FROM sales_delta_xt;

s
a
h
)

om
c

l
ai
m
You can execute the sol_08_02_03.sql solution script, logged ingas the SH
ideuser, to
@
u
a
avoid typing the code in this practice step.
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

4.

Load the data serially from the sales_delta_xt external table into the SALES fact table.
Roll back the operation when finished.
Use the sol_08_02_04.sql solution script, logged in as the SH user.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Using the Data Pump driver, create an external table called sales_ch from the SALES
table that contains all the records that have a channel_id of 4. Use the same data_dir
directory that you created in step 1. Name the Data Pump output file as 'sales_ch.exp'.
Logged in as the SH user, execute the sol_08_02_05.sql script or the following CREATE
TABLE statement to create the external table:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H The SELECT
- statement ensures that you can access the sales_ch table.
DS non
Locate the sales_ch.exp file that is being used by the new table (and created in the
process).

6.

Drop the data_dir and log_dir directories.

You can execute the sol_08_02_06.sql solution script, logged in as the SH user, to
avoid typing the code in this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 8


Chapter 8 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G9
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
9
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 2

Overview of Practices for Lesson 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In these practices, you will examine SQL plans comparing a multitable insert to a union-all insert
operation, perform a multitable conditional insert, use the merge statement with external tables,
and examine DML error logging.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 3

Practice 9-1: ETL Transformation

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In this practice you assume that your company gets some nonrelational data structures from
one of its partner companies, which sells its products for a special advertisement campaign. The
data structure is a denormalized, nonrelational record structure from a proprietary mainframe
system consisting of one record per customer and product per week. Those data structures
have to be inserted into your data warehouse. Because sales record data is stored per
customer and product per day, you have to transform the incoming data.
As part of the transformation, the nonrelational data structure must be transformed from one
record per week into seven records, each consisting of the information of a business day. In
addition, the data warehouse keeps track of all new customers with a credit limit above a certain
figure. Those customers should be tracked separately.
Here, you implement those business transformations by leveraging your databases multitable
insert capabilities.

s
a
h
)

om
c

l
ai it looks
1. You should investigate execution plan for the multitable insert and compare
how
m
e to
d
a union all equivalent statement. Logged in as the SH user, execute@
theg
i
u
apivoting
G
b
lab_09_01_01.sql script to look at the explain plan for thed
new
multitable insert.
t
n den
a
h
k
tu
S
EXPLAIN PLAN FOR
m
i
m this
a
INSERT ALL
h
s use
(
INTO sales
AN e to
VALUES(product_id, customer_id,weekly_start_date,2,9999,
H
q_sun,sales_sun)K
ns
e
c
INTO sales AM
i
lcustomer_id,weekly_start_date+1,2,9999,
L
e
A
l
VALUES(product_id,
L
ab
r
U
q_mon,sales_mon)
e
f
M
I sales
s
n
MINTO
a
A
r
customer_id,weekly_start_date+2,2,9999,
H oVALUES(product_id,
-t
n
S

q_tue,sales_tue)
D
n
Tasks

INTO sales
VALUES(product_id, customer_id,weekly_start_date+3,2,9999,
q_wed,sales_wed)
INTO sales
VALUES(product_id, customer_id,weekly_start_date+4,2,9999,
q_thu,sales_thu)
INTO sales
VALUES(product_id, customer_id,weekly_start_date+5,2,9999,
q_fri,sales_fri)
INTO sales
VALUES(product_id, customer_id,weekly_start_date+6,2,9999,
q_sat,sales_sat)
SELECT * FROM sales_input_table;
SET linesize 140
SELECT * from table(dbms_xplan.display);

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im logged
You can execute the lab_09_01_01.sql m
script,
is in as the SH user, to avoid
h
a
t
typing the code in this practice step.sh
e
s
(
u
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Now, show the execution plan for the insert based on a UNION ALL set operation.
Compare the plans for the insert methods and explain the differences. Logged in as the SH
user, execute the script lab_09_01_02.sql to avoid typing. The code in this script is:
EXPLAIN PLAN FOR
INSERT INTO sales
(prod_id, cust_id, time_id,
channel_id,promo_id,amount_sold,quantity_sold)
SELECT product_id,
customer_id,weekly_start_date,2,9999,sales_sun,q_sun
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+1,2,9999,sales_mon,q_mon
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+2,2,9999,sales_tue,q_tue
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+3,2,9999,sales_wed,q_wed
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+4,2,9999,sales_thu,q_thu
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+5,2,9999,sales_fri,q_fri
FROM sales_input_table
UNION ALL
SELECT product_id,
customer_id,weekly_start_date+6,2,9999,sales_sat,q_sat
FROM sales_input_table;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
SELECT * from table(dbms_xplan.display);

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
In the first exercise (pivoting multitable insert), the input source
table
is
scanned
G
t
db within

only once. The complexity of the denormalization is handled


the several
n
n
INSERT INTO branches, thus avoiding multiple scans.
ha Stude
k
im
When you look at the second exercise (using
UNION iALL),
can see that the input
m
a complexity
th sof theyou
h
source table is scanned seven times.sThe
denormalization is
e
us
handled within the several SELECT
N (operations.
o
A
t
Compare the Bytes and Cost
columns,
can see that the pivoting multitable
se you
KtoHthe
n
e
M
method is much superior
second
INSERT
(using the UNION ALL clause).
A e lic
L
A thealab_09_01_02.sql
l
You can execute
script, logged in as the SH user, to avoid
b
L
r
U
typing the
code inethis
practice step.
IM nsf
M
A -tra
H
S
D
non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

In this multitable insert, you insert data into several tables with different table structures
based on several different conditions. Logged in as the SH user, create an intermediate
table called customers_new with the same attributes as the CUSTOMERS table, where
cust_id is between 2000 and 5000.

s
a
h
)

om
c

l
ai
m
g
de
iSH
@
u
a
You can execute the sol_09_01_03.sql solution script, logged
in
as
the
b nt G user, to
d

avoid typing the code in this practice step.


han Stude
k
im
is table so that the data
4. Logged in as the SH user, disable the constraints
on the
m
hSALES
a
t
h
e
loaded into the staging table in step 1(can
s be deleted
s from the SALES table. Use the
u
N
following SQL:
o
A et
H
ALTER TABLE sales
DISABLE
K
ns CONSTRAINT sales_customer_fk;
e
M
c
DELETE FROM
li WHERE cust_id BETWEEN 2000 AND 5000;
LAcustomers
e
A
l
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_09_01_04.sql solution script, logged in as the SH user, to
avoid typing the code in this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

As the SH user, create an empty table called CUSTOMERS_SPECIAL from the CUSTOMERS
table to hold the special promotion information. Use the following SQL statements:
CREATE TABLE customers_special AS
SELECT cust_id, cust_credit_limit
FROM customers
WHERE rownum < 1;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G SQL
b nt Using
6. Issue a multitable insert into several tables with different tabledstructures.

n
Developer logged on as the SH user, execute the sol_09_01_06.sql
script,
as shown
a tude
h
k
here:
im his S
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the sol_09_01_05.sql solution script, logged in as the SH user, to
avoid typing the code in this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7.

Check and see how the data was distributed among the various tables. Run the following
SQL statements:
SELECT COUNT(*) FROM customers;
SELECT COUNT(*) FROM customers_special;
SELECT MIN(cust_credit_limit) FROM customers_special;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
a logged
hscript,
You can execute the sol_09_01_07.sql solution
ud in as the SH user, to
k
t
S
m
avoid typing the code in this practice step. i
m this
a
h
s use
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

8.

Reset your environment by executing the following SQL statements as the SH user:
ALTER TABLE sales
MODIFY CONSTRAINT sales_customer_fk
RELY ENABLE NOVALIDATE;
DROP TABLE customers_special;
DROP TABLE customers_new;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
I execute
s the sol_09_01_08.sql solution script, logged in as the SH user, to
You
can
n
M
a
A
r
typing
H avoid
-t the code in this practice step.
n
S

o
D
n

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 11

Practice 9-2: Using Merge and External Tables

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Overview

In addition to the previous scenario, your company has to update its product information in the
data warehouse periodically with data from its products database. Unfortunately, you get
product delta information without the possibility to distinguish between new and updated
information, so you have to figure this out on the data warehouse site.
You use the SQL MERGE command to update or insert the data conditionally. To do this, perform
the following steps:

Create an external table (and directories) for the external products information.
Perform an Upsert, using the SQL MERGE functionality.

Show the execution plan of the new MERGE command.

Perform an Upsert using two separate SQL commands (before Oracle9i functionality).

s
a
h
)

om
c

l
ai
m
g
ide
Tasks
@
u
a
db entforGthe directory
1. Create the directories for the external table (use /home/oracle/labs

n
pointer). Then, create an external table called products_delta.
ha Stud
k
im hscript
Logged in as the SH use, execute the lab_09_02_01
is to perform this task.
m
a
t
Note: It is acceptable for DROP commands
ean error.
sh to return
s
(
u
AN e to
H
DROP DIRECTORY data_dir;
K ens
M
DROP DIRECTORY
log_dir;
LA le lic
A
LDIRECTORY
CREATE
ab data_dir AS '/home/oracle/labs/';
r
U
e
f
M
DIRECTORY
log_dir AS '/home/oracle/labs/';
I
s
n
MCREATE
a
A
r
H oDROP
-t TABLE products_delta;
n
S

D
n
Assumptions

You have completed practice 9-1.

CREATE TABLE products_delta


(
PROD_ID NUMBER(6),
PROD_NAME VARCHAR2(50),
PROD_DESC VARCHAR2(4000),
PROD_SUBCATEGORY VARCHAR2(50),
PROD_SUBCATEGORY_ID NUMBER,
PROD_SUBCATEGORY_DESC VARCHAR2(2000),
PROD_CATEGORY VARCHAR2(50),
PROD_CATEGORY_ID NUMBER,
PROD_CATEGORY_DESC VARCHAR2(2000),
PROD_WEIGHT_CLASS NUMBER(2),
PROD_UNIT_OF_MEASURE VARCHAR2(20),
PROD_PACK_SIZE VARCHAR2(30),
SUPPLIER_ID NUMBER(6),
PROD_STATUS VARCHAR2(20),
PROD_LIST_PRICE NUMBER(8,2),
PROD_MIN_PRICE NUMBER(8,2),

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

PROD_TOTAL VARCHAR2(13),
PROD_TOTAL_ID NUMBER,
PROD_SRC_ID NUMBER,
PROD_EFF_FROM DATE,
PROD_EFF_TO DATE,
PROD_VALID CHAR(1)
)
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY data_dir
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
NOBADFILE
NOLOGFILE
FIELDS TERMINATED BY "|" LDRTRIM
)
location
('prodDelta.dat')
)
REJECT LIMIT UNLIMITED NOPARALLEL;

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 13

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You can execute the lab_09_02_01.sql script, logged in as the SH user, to avoid
typing the code in this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 14

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Load the data using the SQL MERGE command by executing the lab_09_02_02.sql
script as the SH user.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 15

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 9


Chapter 9 - Page 16

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G10
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
10
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 2

Overview of Practices for Lesson 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In this practice, you will create materialized views on prebuilt tables and estimate the number of
rows of a potential materialized view. You also identify the objects that are created when you
create new materialized views.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 3

Practice 10-1: Materialized Views


Tasks

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

1.

Connect as sh/sh, and estimate the number of rows a materialized view corresponding to
the following query contains:
SELECT c.cust_id, SUM(amount_sold) AS dollar_sales,
COUNT(amount_sold) AS cnt_dollars, COUNT(*) AS cnt
FROM
sales s, customers c
WHERE s.cust_id= c.cust_id
GROUP BY c.cust_id;

set echo on
set serveroutput on;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
DECLARE
no_of_rows NUMBER;
mv_size NUMBER;
BEGIN
no_of_rows :=555;
mv_size :=5555;
DBMS_MVIEW.estimate_mview_size('MV1',
'SELECT c.cust_id, SUM(amount_sold) AS dollar_sales,
COUNT(amount_sold) AS cnt_dollars, COUNT(*) AS cnt
FROM sales s, customers c
WHERE s.cust_id= c.cust_id
GROUP BY c.cust_id' , no_of_rows, mv_size);
DBMS_OUTPUT.put_line ( '');
DBMS_OUTPUT.put_line ( 'Complete MV');
DBMS_OUTPUT.put_line ( 'No of Rows: ' || no_of_rows );
DBMS_OUTPUT.put_line ( 'MV Size: ' || mv_size );
DBMS_OUTPUT.put_line ( '');
END;
/

Logged in as the SH user, you can execute the sol_10_01_01.sql solution script to
avoid typing the code for this practice step.
2.

In the step, you compare the estimated size you obtained in step 1 with the actual number
of rows in the new CUST_ID_SALES_AGGR table that you create.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

a.

Execute the lab_10_01_02.sql script to create the CUST_ID_SALES_AGGR table.


This table contains the data corresponding to the query in step 1.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
b.

What is your conclusion?


The estimated size from the ESTIMATE_MVIEW_SIZE procedure is accurate,
provided you have up-to-date statistics on the relevant objects.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Determine the number of objects in the SH schema.

s
a
h
)

om
c

l
ai
m
g
ideof the
4. Assume that the CUST_ID_SALES_AGGR table indeed corresponds@
with the result
u
a
G
query in step 1. Use this knowledge to create a materializedview
db onethentprebuilt
n
CUST_ID_SALES_AGGR table. Ensure that this materialized
view can
d be used for future
ha S
u
k
t
query rewrites and is fast refreshable on demand.
im his
m
sha use t
(
ANcust_id_sales_aggr;
to
DROP MATERIALIZED VIEW
H
e
K ens
M
CREATE MATERIALIZED
LA le licVIEW cust_id_sales_aggr
A
L
ab
r
ON PREBUILT
TABLE
U
e
f
M
I
s
FORCE
n
MREFRESH
a
A
r
H oENABLE
-t QUERY REWRITE
n
S

AS
D
n
SELECT c.cust_id, SUM(amount_sold) AS dollar_sales,
COUNT(amount_sold) AS cnt_dollars, COUNT(*) AS cnt
FROM sales s, customers c
WHERE s.cust_id= c.cust_id
GROUP BY c.cust_id;

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
t
e
sha the
s
(
Logged in as the SH user, you can execute
sol_10_01_04.sql
solution script to
u
N step.
o
A
t
avoid typing the code for this
practice
KH ense
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Count the number of objects in the SH schema again, to check for new objects. From the
data dictionary, identify the objects having CUST_ID_SALES_AGGR as name. What type of
objects are these? What is your conclusion?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

One additional object is created for the materialized view. Because the materialized
view is defined on a prebuilt table, a container table is not needed. Note that both
objects have the same name.
Logged in as the SH user, you can execute the sol_10_01_05.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Create a new materialized view named MV1. This materialized view should be defined
exactly the same way as CUST_ID_SALES_AGGR (created in the previous step), except
that MV1 should not be defined on the prebuilt table CUST_ID_SALES_AGGR, and its
SELECT list should not contain the following two expressions: COUNT(amount_sold),
COUNT(*). Count the number of objects in the SH schema again. What new objects have
been created? What is your conclusion?
DROP MATERIALIZED VIEW mv1;
CREATE MATERIALIZED VIEW mv1
REFRESH FORCE
ENABLE QUERY REWRITE
AS
SELECT c.cust_id, SUM(amount_sold) AS dollar_sales
FROM sales s, customers c
WHERE s.cust_id= c.cust_id
GROUP BY c.cust_id;

s
a
h
)

om
c

l
ai
m
g
COL name FORMAT A30
ide
@
u
a
db ent G

SELECT SUBSTR(object_name,1,30) Name, object_type


n
a tud
FROM user_objects
h
k
WHERE object_name LIKE '%MV1%';
im his S
m
ha se t
s
(
u like this:
The end of the SQL Developer output
look
N should
o
A
t
KH ense
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
SELECT COUNT(*) FROM user_objects;

The new materialized view is not defined on a prebuilt table. A container table and an
index are created to enhance fast refresh performance.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7.

Compare the staleness status of the materialized views you have created. What is your
conclusion?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K eview
ns on a prebuilt table, the contents of the table are
When you create a materialized
M
c
li does not guarantee that the contents are fresh.
LA Database
not validated. A
Oracle
e
l
b you can execute the sol_10_01_07.sql solution script to
L the SHrauser,
Logged in
as
U
e
f
M
avoidI typing n
M tra thes code for this practice step.
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

8.

Logged in as the SH user, execute the lab_10_01_08.sql script. This script adds one
row to the CUSTOMERS table, as shown here:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non
Then, check the staleness status and the compile status of both the materialized views.
What is your conclusion?

The COMPILE_STATE of both the materialized views is NEEDS_COMPILE. This status is


also reflected in the STALENESS column.
Logged in as the SH user, you can execute the sol_10_01_08.sql solution script to
avoid typing the code for the second part of this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

9.

How can you refresh the staleness status of both the materialized views to reflect their
current status? When refreshed, check the status. What happens, and why?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

You must recompile both views to see an accurate STALENESS status.


Logged in as the SH user, you can execute the sol_10_01_09.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 10


Chapter 10 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G11
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
11
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 2

Overview of Practices for Lesson 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In this practice, you will connect to the SH schema and verify the staleness and compile status
of the MV1 and CUST_ID_SALES_AGGR materialized views.
You then:

Perform a complete refresh for both of the materialized views and check the status of
each of the materialized views
Create the MV_CAPABILITIES_TABLE table and verify the fast refresh capabilities of
the MV1 materialized view

Take the necessary corrective actions for CUST_ID_SALES_AGGR to be immediately


fast refreshable in the maximum number of cases

Create the COSTS_MV materialized view by using a SELECT statement and ensure that
the new materialized view is refreshed FAST ON DEMAND, and can be used for future
query rewrites
Add two partitions to the COSTS table: COSTS_Q1_2004 and COSTS_Q2_2004

s
a
h
)

om
c

l
ai

m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 3

Practice 11-1: Refreshing Materialized Views


Assumptions

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

You have completed the practices for the lesson titled Materialized Views.

Tasks
1.

Connect as sh/sh, and verify the staleness, and compile the status of the MV1 and
CUST_ID_SALES_AGGR materialized views. Then perform a complete refresh for both the
materialized views and check the status of each materialized view again. What is your
conclusion?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

After the complete refresh is done, both the materialized views are now FRESH. That
is, the Oracle Database server is sure that the materialized views reflect their base
tables.
Logged in as the SH user, you can execute the sol_11_01_01.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Using SQL Developer, log in as the SH user and execute the following statements to create
the MV_CAPABILITIES_TABLE table.
Note: The $ORACLE_HOME/rdbms/admin/utlxmv.sql script is run by the second
statement.
DROP TABLE mv_capabilities_table;
@?/rdbms/admin/utlxmv.sql

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
Verify the fast refresh capabilities of the MV1 materialized
im hisview by running the
m
sol_11_01_02.sql solution script as the
et
shaSH uuser.
s
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

The output must look like this:

anonymous block completed


PCT: N
:
PCT_TABLE: N
CUSTOMERS: relation is not a partitioned table
PCT_TABLE: N
SALES: no partition key or PMARKER or join dependent expression
in select list
PCT_TABLE_REWRITE: N
CUSTOMERS: relation is not a partitioned table
PCT_TABLE_REWRITE: N
SALES: no partition key or PMARKER in select list
REFRESH_COMPLETE: Y
:
REFRESH_FAST: N
:
REFRESH_FAST_AFTER_ANY_DML: N
: see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled
REFRESH_FAST_AFTER_INSERT: N
SH.SALES: the detail table does not have a materialized view log
REFRESH_FAST_AFTER_INSERT: N
SH.CUSTOMERS: the detail table does not have a materialized view
log
REFRESH_FAST_AFTER_ONETAB_DML: N
DOLLAR_SALES: SUM(expr) without COUNT(expr)
REFRESH_FAST_AFTER_ONETAB_DML: N
: see the reason why REFRESH_FAST_AFTER_INSERT is disabled
REFRESH_FAST_AFTER_ONETAB_DML: N
: COUNT(*) is not present in the select list
REFRESH_FAST_AFTER_ONETAB_DML: N
: SUM(expr) without COUNT(expr)
REFRESH_FAST_PCT: N
: PCT is not possible on any of the detail tables in the
materialized view
REWRITE: Y
:
REWRITE_FULL_TEXT_MATCH: Y
:
REWRITE_GENERAL: Y
:
REWRITE_PARTIAL_TEXT_MATCH: Y
:
REWRITE_PCT: N
: general rewrite is not possible or PCT is not possible on any
of the detail tables
SET ECHO OFF

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Take the necessary corrective actions for CUST_ID_SALES_AGGR to be immediately fast


refreshable in the maximum number of cases. Verify that you made the right decisions.
Logged in as the SH user, you can execute the sol_11_01_03.sql solution script to
avoid typing the code for this practice step. The output is shown here:

set echo on
DROP MATERIALIZED VIEW LOG ON sales;
CREATE MATERIALIZED VIEW LOG ON sales
WITH ROWID, SEQUENCE
(prod_id, cust_id, time_id, channel_id, promo_id,
quantity_sold, amount_sold)
INCLUDING NEW VALUES ;

s
a
h
)

om
c

l
ai
CREATE MATERIALIZED VIEW LOG ON customers
m
g
ide
WITH ROWID, SEQUENCE
@
u
a
b nt G
(cust_id,cust_first_name,cust_last_name,cust_gender,
d

n
cust_year_of_birth,cust_marital_status, a
h Stude
k
cust_street_address,cust_postal_code,cust_city,
im his
cust_city_id, cust_state_province,cust_state_province_id,
m
ha se t
country_id,
s
(
cust_main_phone_number,cust_income_level,
N to u
A
cust_credit_limit,cust_email,cust_total,
cust_total_id,
H nse
K
cust_src_id, cust_eff_from,
cust_eff_to,
cust_valid)
AM e lice
INCLUDING NEW L
VALUES;
A abl
L
U fer
exec IDBMS_MVIEW.REFRESH('CUST_ID_SALES_AGGR','C',M
M trans
'',FALSE,TRUE,0,0,0,FALSE);
A
H onTABLE mv_capabilities_table;
DS TRUNCATE
n
DROP MATERIALIZED VIEW LOG ON customers;

exec dbms_mview.explain_mview('cust_id_sales_aggr');
set serveroutput on
begin
dbms_output.enable(9000);
for crec in (select capability_name, possible, related_text, msgtxt
from mv_capabilities_table order by 1) loop
dbms_output.put_line(crec.capability_name ||': '||crec.possible);
dbms_output.put_line(crec.related_text||': '||crec.msgtxt);
end loop;
end;
/
DROP MATERIALIZED VIEW LOG ON sales
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Error starting at line 3 in command:


DROP MATERIALIZED VIEW LOG ON sales
Error report:
SQL Error: ORA-12002: there is no materialized view log on table
"SH"."SALES"
12002. 00000 - "there is no materialized view log on table
\"%s\".\"%s\""
*Cause:
There was no materialized view log on the master table.
*Action:
Create a materialized view log on the master table.
CREATE MATERIALIZED VIEW LOG ON sales
WITH ROWID, SEQUENCE
(prod_id, cust_id, time_id, channel_id, promo_id,
quantity_sold, amount_sold)
INCLUDING NEW VALUES

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
Error starting at line 11 in command:
db ent G

n
DROP MATERIALIZED VIEW LOG ON customers ha
ud
k
t
Error report:
S
m
i his view log on table
SQL Error: ORA-12002: there is no
materialized
m
a
"SH"."CUSTOMERS"
sh use t
(
12002. 00000 - "there is N
view log on table
A no materialized
to
H
\"%s\".\"%s\""
e
K ens
*Cause:
There M
was no materialized
view log on the master table.
c
A a materialized
i
l
L
*Action:
Create
view
log on the master table.
e LOG ON customers
A ablVIEW
L
CREATE MATERIALIZED
U SEQUENCE
er
WITH IROWID,
f
M
s
M tran
(cust_id,cust_first_name,cust_last_name,cust_gender,
Acust_year_of_birth,cust_marital_status,
H
DS non
CREATE MATERIALIZED succeeded.
DROP MATERIALIZED VIEW LOG ON customers

cust_street_address,cust_postal_code,cust_city,
cust_city_id, cust_state_province,cust_state_province_id,
country_id,
cust_main_phone_number,cust_income_level,
cust_credit_limit,cust_email,cust_total, cust_total_id,
cust_src_id, cust_eff_from, cust_eff_to, cust_valid)
INCLUDING NEW VALUES
CREATE MATERIALIZED succeeded.
exec DBMS_MVIEW.REFRESH('CUST_ID_SALES_AGGR','C','',FALSE,TRUE,0,0,0,FALSE);
anonymous block completed
TRUNCATE TABLE mv_capabilities_table
TRUNCATE TABLE mv_capabilities_table succeeded.
exec dbms_mview.explain_mview('cust_id_sales_aggr');

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

anonymous block completed


set serveroutput on

begin
dbms_output.enable(9000);
for crec in (select capability_name, possible, related_text, msgtxt
from mv_capabilities_table order by 1) loop
dbms_output.put_line(crec.capability_name ||': '||crec.possible);
dbms_output.put_line(crec.related_text||': '||crec.msgtxt);
end loop;
end;
anonymous block completed
PCT: N
:
PCT_TABLE: N
CUSTOMERS: relation is not a partitioned table
PCT_TABLE: N
SALES: no partition key or PMARKER or join dependent expression in
select list
PCT_TABLE_REWRITE: N
CUSTOMERS: relation is not a partitioned table
PCT_TABLE_REWRITE: N
SALES: no partition key or PMARKER in select list
REFRESH_COMPLETE: Y
:
REFRESH_FAST: Y
:
REFRESH_FAST_AFTER_ANY_DML: Y
:
REFRESH_FAST_AFTER_INSERT: Y
:
REFRESH_FAST_AFTER_ONETAB_DML: Y
:
REFRESH_FAST_PCT: N
: PCT is not possible on any of the detail tables in the
materialized view
REWRITE: Y
:
REWRITE_FULL_TEXT_MATCH: Y
:
REWRITE_GENERAL: Y
:
REWRITE_PARTIAL_TEXT_MATCH: Y
:
REWRITE_PCT: N
: general rewrite is not possible or PCT is not possible on any of
the detail tables

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 9

Conclusions:
It is only necessary to create materialized view logs on both SALES and
CUSTOMERS for CUST_ID_SALES_AGGR to be fast refreshable in the maximum
number of cases.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

After you create the materialized view logs, you should refresh
CUST_ID_SALES_AGGR; otherwise, the Oracle server considers the materialized
view logs newer than the previous complete refresh, which prevents fast refresh.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 10

4.

Logged in as the SH user, execute the lab_11_01_04.sql script. This script prepares the
environment for the next steps.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

set echo on

ALTER TABLE costs DROP PARTITION costs_q1_2004;


ALTER TABLE costs DROP PARTITION costs_q2_2004;
ALTER TABLE costs DROP PARTITION costs_1_2004;
DROP MATERIALIZED VIEW LOG ON costs;
CREATE MATERIALIZED VIEW LOG ON costs
WITH ROWID, SEQUENCE
(prod_id, time_id, unit_cost, unit_price )
INCLUDING NEW VALUES ;

s
a
h
)

om
c

l
ai
m
g
DROP MATERIALIZED VIEW LOG ON products;
ide
@
u
a
db ent G

CREATE MATERIALIZED VIEW LOG ON products


n
ha Stud
WITH ROWID, SEQUENCE
k
im his
(prod_id,prod_name,prod_desc,prod_subcategory,prod_subcategory_id,
m
prod_subcategory_desc
sha use t
(
,prod_category,prod_category_desc,prod_weight_class,prod_unit_of_mea
AN e to
sure
H
K ens
,prod_pack_size,supplier_id,prod_status,prod_list_price,prod_min_pri
M
ce)
A e lic
LVALUES;
A
INCLUDING NEW
l
b
L
a
U fer
M
I
set
echo
ns
M traoff
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Create the COSTS_MV materialized view by using the following SELECT statement. Ensure
that this materialized view is refreshed FAST ON DEMAND, and can be used for future
query rewrites.
SELECT time_id, prod_subcategory,
SUM( unit_cost) AS sum_units,
COUNT(unit_cost) AS count_units, COUNT(*) AS cnt
FROM
costs c, products p
WHERE c.prod_id = p.prod_id
GROUP BY time_id, prod_subcategory;

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Logged in as the SH user, you can execute the sol_11_01_05.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 13

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Verify that the COSTS_MV materialized view is fast refreshable using PCT mechanisms. Why
is this materialized view PCT enabled on the COSTS table?

set echo on
col column_name format a30
select column_name from user_part_key_columns where name='COSTS';
TRUNCATE TABLE mv_capabilities_table;
exec dbms_mview.explain_mview('costs_mv');
set serveroutput on

s
a
h
)

begin
dbms_output.enable(9000);
for crec in (select capability_name, possible, related_text, msgtxt
from mv_capabilities_table order by 1) loop
dbms_output.put_line(crec.capability_name ||': '||crec.possible);
dbms_output.put_line(crec.related_text||': '||crec.msgtxt);
end loop;
end;
/

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
Execute the sol_11_01_06.sql
ANscript.
toResulting output looks like this:
H
e
K ens
M
col column_name
lic a30
LA lformat
e
A
L
ab Command Skipped: col column_name format a30:
r
U
e
line
3:
SQLPLUS
f
M
I cons
Monly
A
tra column_name from user_part_key_columns where name='COSTS'
H
n
S
select
D
no
COLUMN_NAME
--------------------------------------------------------TIME_ID
TRUNCATE TABLE mv_capabilities_table
TRUNCATE TABLE mv_capabilities_table succeeded.
exec dbms_mview.explain_mview('costs_mv');
anonymous block completed
set serveroutput on
begin
dbms_output.enable(9000);
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 14

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

for crec in (select capability_name, possible, related_text,


msgtxt
from mv_capabilities_table order by 1) loop
dbms_output.put_line(crec.capability_name ||':
'||crec.possible);
dbms_output.put_line(crec.related_text||': '||crec.msgtxt);
end loop;
end;
anonymous block completed
PCT: Y
:
PCT_TABLE: N
PRODUCTS: relation is not a partitioned table
PCT_TABLE: Y
COSTS:
PCT_TABLE_REWRITE: N
PRODUCTS: relation is not a partitioned table
PCT_TABLE_REWRITE: Y
COSTS:
REFRESH_COMPLETE: Y
:
REFRESH_FAST: Y
:
REFRESH_FAST_AFTER_ANY_DML: Y
:
REFRESH_FAST_AFTER_INSERT: Y
:
REFRESH_FAST_AFTER_ONETAB_DML: Y
:
REFRESH_FAST_PCT: Y
:
REWRITE: Y
:
REWRITE_FULL_TEXT_MATCH: Y
:
REWRITE_GENERAL: Y
:
REWRITE_PARTIAL_TEXT_MATCH: Y
:
REWRITE_PCT: Y
:

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 15

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Logged in as the SH user, you can execute the sol_11_01_06.sql solution script to
avoid typing the code for this practice step.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 16

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7. Add two partitions, COSTS_Q1_2004 and COSTS_Q2_2004, to the COSTS table, using
the following code:
COSTS_Q1_2004 should accept rows with a TIME_ID value less than
TO_DATE('01-APR-2004', 'DD-MON-YYYY').

COSTS_Q2_2004 should accept rows with a TIME_ID value less than


TO_DATE('01-JUL-2004', 'DD-MON-YYYY').

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 17

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

The script output of the sol_11_01_07.sql script looks like this:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H on-in as the SH user, you can execute the sol_11_01_07.sql solution script to
DS Logged
n
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 18

8.

Check the staleness and compile statuses of the COSTS_MV materialized view.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

SET ECHO ON
COL MVIEWNAME FORMAT A30

SELECT SUBSTR(mview_name,1,30) mviewname, staleness, compile_state


FROM user_mviews
WHERE mview_name = 'COSTS_MV';
ALTER MATERIALIZED VIEW costs_mv COMPILE;
SELECT SUBSTR(mview_name,1,30) mviewname, staleness, compile_state
FROM user_mviews
WHERE mview_name = 'COSTS_MV';

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Logged in as the SH user, you can execute the sol_11_01_08.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 19

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

9.

Logged in as the SH user, execute the lab_11_01_09.sql script.


This script adds rows to the new partitions of the COSTS table. Then, determine the new
staleness status, as well as the last refresh type used to refresh the COSTS_MV materialized
view.

SET ECHO ON
ALTER TABLE costs DISABLE CONSTRAINT costs_time_fk;
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT

INTO
INTO
INTO
INTO
INTO
INTO
INTO

costs
costs
costs
costs
costs
costs
costs

VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES

(13,
(20,
(20,
(13,
(13,
(13,
(20,

'02-JAN-04'
'02-FEB-04'
'03-MAR-04'
'21-APR-04'
'22-MAY-04'
'22-APR-04'
'12-JUN-04'

,
,
,
,
,
,
,

500,
501,
502,
503,
504,
505,
506,

2,
3,
5,
9,
2,
3,
5,

2.50, 25.00);
2, 34);
2, 34);
1, 35);
3, 36);
4, 37);
5, 34);

om
c

l
ai
m
g
ALTER MATERIALIZED VIEW costs_mv COMPILE;
ide
@
u
a
db ent G
COL mviewname format a30

n
d
ha Stucompile_state
k
SELECT SUBSTR(mview_name,1,30) mviewname,
staleness,
m
i his
FROM user_mviews
m
a
t
WHERE mview_name = 'COSTS_MV'; sh
e
s
(
AN e to u
SELECT 'Value Before: ',H
SUM(sum_units)
K ens
FROM costs_mv;
M
ic user_mviews WHERE mview_name='COSTS_MV';
LA le lFROM
SELECT last_refresh_type
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

COMMIT;

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 20

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 21

10. Fast-refresh COSTS_MV and check its staleness status and last refresh type again. What
are your conclusions?

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

EXEC DBMS_MVIEW.REFRESH('costs_mv','F');

SELECT last_refresh_type
FROM user_mviews
WHERE mview_name='COSTS_MV';
SELECT 'Value After: ', SUM(sum_units)
FROM costs_mv;
COL mviewname FORMAT A30
SELECT SUBSTR(mview_name,1,30) mviewname, staleness, compile_state
FROM user_mviews
WHERE mview_name = 'COSTS_MV';

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Logged in as the SH user, you can execute the sol_11_01_10.sql solution script to
avoid typing the code for this practice step. (Conclusions on the next page.)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 22

Initially, the refresh type was COMPLETE. This refresh corresponded to the initial build
of the COSTS_MV materialized view.

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

After inserting the new rows, COSTS_MV is considered to be STALE, which is normal.
Then, after fast refresh execution, COSTS_MV is now FRESH.

Note that the refresh time is considerably faster than during the previous complete
refresh. This is because a FAST refresh was executed as opposed to a COMPLETE
refresh. In this case, the Oracle Database server did not use a PCT refresh because
inserting rows is a DML operation that populates the COSTS materialized view log.
Therefore, a normal FAST refresh can be executed in this case.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 23

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

11. Drop the COSTS_Q2_2004 partition. Compile the COSTS_MV materialized view, and check
its staleness status. Refresh (FAST) the COSTS_MV materialized view again, and check the
last refresh type. What is your conclusion?

SET ECHO ON
ALTER TABLE costs DROP PARTITION costs_q2_2004;
ALTER MATERIALIZED VIEW costs_mv COMPILE;
COL mviewname FORMAT A30
SELECT SUBSTR(mview_name,1,30) mviewname, staleness, compile_state
FROM user_mviews
WHERE mview_name = 'COSTS_MV';

s
a
h
)

om
c

l
ai
m
g
ide
SELECT last_refresh_type
@
u
a
b nt G
FROM user_mviews
d

n
WHERE mview_name='COSTS_MV';
ha Stude
k
im his
SEELCT 'Value After: ', SUM(sum_units)
m
FROM costs_mv;
sha use t
(
AN emviewname,
to
SELECT SUBSTR(mview_name,1,30)
staleness, compile_state
H
s
n
FROM user_mviews M K
ce
A = 'COSTS_MV';
i
l
WHERE mview_name
L
A able
L
U fer
M
I
M trans
A
H
DS non
EXEC DBMS_MVIEW.REFRESH('costs_mv','F');

...

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 24

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
lic a fast DDL operation that does not populate
LA is normally
Dropping a partition
e
A
l
Lmaterialized
associated
ab view logs. Because PCT is enabled on the COSTS table, the
r
U
e
f
M
I Database
Oracle
s server is still able to perform a FAST refresh (called FAST_PCT) to
n
M
a
A
r
a-COMPLETE
refresh.
t
H avoid
n
S

o
Logged
D
n in as the SH user, you can execute the sol_11_01_11.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 25

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 11


Chapter 11 - Page 26

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G12
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
12
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 2

Overview of Practices for Lesson 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Practices Overview

In this practice, you will create a new multitable dimension object called CUSTOMERS_DIM. This
dimension is used to create hierarchies between the CUSTOMERS table and the COUNTRIES
table. You also create the DIMENSIONS_EXCEPTION table and validate the CUSTOMERS_DIM
dimension and identify any bad rows, if any.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 3

Practice 12-1: Working with Dimensions


Tasks

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

1.

Connected as the SH user, execute the lab_12_01_01.sql script.


This script creates a new multitable dimension object called CUSTOMERS_DIM. This
dimension creates hierarchies between the CUSTOMERS table and the COUNTRIES table.

SET ECHO ON
DROP DIMENSION customers_dim;
CREATE DIMENSION customers_dim
LEVEL customer IS (customers.cust_id)
LEVEL city
IS (customers.cust_city)
LEVEL state
IS (customers.cust_state_province)
LEVEL country
IS (countries.country_id)
LEVEL subregion IS (countries.country_subregion)
LEVEL region
IS (countries.country_region)
HIERARCHY geog_rollup (
customer CHILD OF
city
CHILD OF
state
CHILD OF
country
CHILD OF
subregion CHILD OF
region
JOIN KEY (customers.country_id) REFERENCES country)
ATTRIBUTE customer DETERMINES
(cust_first_name, cust_last_name, cust_gender,
cust_marital_status, cust_year_of_birth,
cust_income_level, cust_credit_limit,
cust_street_address, cust_postal_code,
cust_main_phone_number, cust_email)
ATTRIBUTE city DETERMINES (cust_city)
ATTRIBUTE state DETERMINES (cust_state_province, country_id)
ATTRIBUTE country DETERMINES (countries.country_name)
ATTRIBUTE subregion DETERMINES (countries.country_subregion)
ATTRIBUTE region DETERMINES (countries.country_region) ;

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

s
a
h
)

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

2.

Execute the utldim.sql script in $ORACLE_HOME/rdbms/admin to create the


DIMENSIONS_EXCEPTION table.
Using SQL Developer, connect to the SH schema and execute the following
statements in a SQL Worksheet. The code and the executed results are shown here:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G
b What
3. Validate the CUSTOMERS_DIM dimension and identify any baddrows.
is your
t

n
n
conclusion?
ha Stude
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

The CUSTOMERS_DIM dimension contains valid data.


Logged in as the SH user, you can execute the sol_12_01_03.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 12


Chapter 12 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G13
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
13
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 2

Overview of Practices for Lesson 13


Practices Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In these practices, you will:

Run a script to create the REWRITE_TABLE table by executing a script. You also
determine whether a given query can be rewritten.
Delete rows from the COST_Q1_2004 partition, commit the changes, and then check
again if query rewrite is possible for the query from the previous step.
Set the QUERY_REWRITE_INTEGRITY mode to TRUSTED in your session, and then
verify that the query can be rewritten using the COSTS_MV materialized view.
Drop the PRODUCTS_DIM dimension and determine whether query rewrite is still
possible for the query.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 3

Practice 13-1: Query Rewrite


Assumptions

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

You must have completed the practices for the lessons titled Refreshing Materialized Views
and Working with Dimensions.

Tasks
1.

Log in as SH and create the REWRITE_TABLE table by executing the utlxrw.sql script.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
s
Kyou can
Logged in as the SH user,
the sol_13_01_01.sql solution script to
nexecute
e
M
c
A forethisli practice step.
avoid typing the L
code
A abl
L
U fer
M
I
2. Connect
to the
SH
s schema, and then determine whether the following query can be
n
M
a
A
r
H rewritten:
-t
n
S

o
SELECT
p.prod_subcategory, sum(unit_cost)
D
n
FROM
WHERE
GROUP

costs c, products p
c.prod_id = p.prod_id
BY prod_subcategory;

Note: View the definition of the COSTS_MV materialized view to aid in your understanding.
SET ECHO ON
DECLARE
querytxt VARCHAR2(1500) :=
'SELECT p.prod_subcategory, sum(unit_cost) '||
'FROM costs c, products p ' ||
'WHERE c.prod_id = p.prod_id ' ||
'GROUP BY prod_subcategory';
BEGIN
DBMS_MVIEW.EXPLAIN_REWRITE(querytxt, NULL, 'ID1');
END;
/
SELECT message
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 4

SELECT message
FROM rewrite_table
WHERE statement_id = 'ID1'
ORDER BY sequence;

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

set long 2000

SELECT query
FROM user_mviews
WHERE mview_name = 'COSTS_MV';

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Although the query does not group the data by TIME_ID, it is still possible to use the
COSTS_MV materialized view because both queries have exactly the same join graph,
and it is possible to group data by PROD_SUBCATEGORY on the COSTS_MV materialized
view. In this case, it is not necessary to use constraints or dimensions to enable
query rewrite.
Logged in as the SH user, you can execute the sol_13_01_02.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

3.

Delete rows from the COST_Q1_2004 partition, and commit your changes. Check again if
query rewrite is possible for the query from step 2. If not, explain why.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
I
s
n
M
a
A
r
After
H Note:
-t using the EXPLAIN_REWRITE procedure, you do not need to recompile
n
S

o
COSTS_MV
to check its staleness status.
D
n

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G script to
Logged in as the SH user, you can execute the sol_13_01_03.sql
db entsolution

n
avoid typing the code for this practice step.
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

4.

Set the QUERY_REWRITE_INTEGRITY mode to TRUSTED in your session. Then, verify that
the following query can be rewritten using the COSTS_MV materialized view. Why is this
possible?
SELECT p.prod_category, sum(unit_cost)
FROM
costs c, products p
WHERE c.prod_id = p.prod_id
AND
c.time_id >= TO_DATE(''01-JAN-2000'',''DD-MON-YYYY'')
AND
c.time_id < TO_DATE(''01-JAN-2001'',''DD-MON-YYYY'')
GROUP BY prod_category;
Note: To use the relationships declared in PRODUCTS_DIM, you need to downgrade your
rewrite integrity level to TRUSTED.

ALTER SESSION SET query_rewrite_integrity = TRUSTED;

s
a
h
)

om
c

l
ai
m
g
DECLARE
ide
@
u
a
querytxt VARCHAR2(1500) :=
b nt G
d'||

'SELECT p.prod_category, sum(unit_cost)


n
ha Stude
'FROM costs c, products p ' ||
k
im' ||
'WHERE c.prod_id = p.prod_id AND
m
his
a
t
'c.time_id >= TO_DATE(''01-JAN-2000'',''DD-MON-YYYY'')
AND '
h
e
s us
(
||
AN e to
'c.time_id < TO_DATE(''01-JAN-2001'',''DD-MON-YYYY'')
' ||
H
s
K
'GROUP BY prod_category';
M licen
BEGIN
A
AL able
dbms_mview.Explain_Rewrite(querytxt,
NULL, 'ID1');
L
r
END; U
fe
s
/MIM
n
A -tra
H
S
SELECT
D
non message FROM rewrite_table
TRUNCATE TABLE rewrite_table;

WHERE

statement_id = 'ID1' ORDER BY sequence;

SELECT dimension_name FROM user_dimensions;

SET SERVEROUTPUT ON;


EXECUTE DBMS_DIMENSION.DESCRIBE_DIMENSION ('PRODUCTS_DIM');

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 8

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Query rewrite is possible because there is a PRODUCTS_DIM dimension declaring a 1to-N relationship between PROD_CATEGORY and PROD_SUBCATEGORY.
Without such knowledge, it is impossible for the Oracle optimizer to rewrite the
above query. The optimizer may determine that it is more expensive to perform query
rewrite as shown in this output.
Logged in as the SH user, you can execute the sol_13_01_04.sql solution script to
avoid typing the code for this practice step.

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

5.

Drop the PRODUCTS_DIM dimension and determine whether query rewrite is still possible
for the query shown in step 4.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
ic of the script output) show that the query rewrite
lbottom
The message results
LA (atlthe
e
A
b this query.
is no longerLpossibleafor
r
U
e
f
M
Logged
in as the
s SH user, you can execute the sol_13_01_05.sql solution script to
n
MI typing
a
A
avoid
the
code for this practice step.
r
t
H
n
S
D
no

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 13


Chapter 13 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
G14
t
dbLesson
Practices for

n
n
ha Stude
k
Chapter
14
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 1

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 2

Practices for Lesson 14


Practices Overview

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

In this practice, you use the SQL Access Advisor tool and implement tuning recommendations.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 3

Practice 14-1: Using SQL Access Advisor, Compression, and


Resumable Session Tasks
Tasks

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

1.

Using SQL*Plus, connect as the user SYS and flush the shared pool.

You can copy/paste code from the sol_14_01_01.sql solution script to avoid
typing the code in this practice step using SQL*Plus.

s
a
h
)

om
c

l
aithe SH user:
2. In SQL Developer, open and execute the following four lab scripts in order,
as
m
g
ide
1) lab_14_01_02_a.sql (Star query)
@
u
a
db ent G

2) lab_14_01_02_b.sql (Star query)


n
ha Stud
k
3) lab_14_01_02_c.sql (Star query)
im his
m
4) lab_14_01_02_d.sql (Order by)
sha use t
(
AN e to
H
Kin theefollowing
These four lab scripts result
ns output:
M
c
LA le li
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 5

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 6

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 7

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 8

3.

Check to ensure that Enterprise Manager is running by using the command line syntax:
emctl status dbconsole (see example below).

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

If Enterprise Manager is not started, then use the following syntax to start Enterprise
Manager: emctl start dbconsole

s
a
h
)

om
c

l
ai
m
g
de
ithe
@
u
4. Invoke Enterprise Manager and connect with sys as the username,
oracle
as
a
G
t
dbbelow.

password, and connect as SYSDBA on the login page, as shown


n
n
ha Studename>:1158/em.
Note: The URL to connect to Enterprise Manager isk
https://<machine
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 9

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Create a SQL Tuning set containing the four queries executed above, as specified here:
a. On the Database Control home page, click the Performance tab.
b. In the Additional Monitoring Links section at the bottom of the page, click the SQL
Tuning Sets hyperlink.
c. On the SQL Tuning Sets page, click the Create Button.
d. Name the SQL Tuning Set MY_STS_WORKLOAD and click the Next button.
e. On the Load Methods page, select the option to Load SQL Statements one time only
from the Cursor Cache. Click the Next button.
f. On the Filter Options page, use the SQL Text like filter condition and set the value to
SELECT /* QueryDW% and click Next.
g. On the Schedule page, accept the default job name and schedule the job to execute
immediately. Change the time zone to any value other than UTC(00:00) if needed.
Click Next to continue. The Review page appears:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H h. Click- Submit to execute the job.
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 10

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

Result: the Confirmation page appears.

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
haSetsSpage,
5. Using the Enterprise Manager tool from the SQL Tuning
udclick the Schedule
k
t
m
i his
SQL Access Advisor button.
m
a
a. Ensure that the Workload Source is
set to Useeant existing SQL Tuning Set and
h
s
(
contains the value SYS.MY_STS_WORKLOAD.
N to us Click the Next button.
A
KH ense
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

b.

On the Recommendation Options page, select the Indexes and Materialized Views
options. Also, select the Comprehensive mode for Scope. Click Next.
Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 11

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
c. On the Schedule page, ensure that the job is scheduled to run immediately.
all
ai Leave

m
other options with default values. Click the Next button.
g
idepage
@
u
d. Review the job details and submit job for execution. Result: The
Confirmation
a
db ent G
appears.

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 12

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

6.

Click the Database tab (top right) to return to the home page of Enterprise Manager. Then,
in the Related Links section at the bottom of the page, click the Advisor Central link. In the
Advisor Central page, click the View Result button for the SQL Access Advisor job that was
run in the previous step, as shown here:

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 13

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

7.

Review the Results for Tasks pages (Recommendations, SQL Statements). What do you
conclude?

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
A eis lable
ic to suggest performance improvement for all four
The SQL AccessLAdvisor
A
l
SQL statements
by creating
b materialized views, materialized view logs, indexes, and
Lstatistics.
a
r
U
gathering
of
IM nsfe
M
A -tra
H
n
S
and SQL Statement tabbed pages follow:
D The
noRecommendation

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 14

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 15

Unauthorized reproduction or distribution prohibited Copyright 2015, Oracle and/or its affiliates

s
a
h
)

om
c

l
ai
m
g
ide
@
u
a
db ent G

n
ha Stud
k
im his
m
sha use t
(
AN e to
H
K ens
M
LA le lic
A
L
ab
r
U
e
f
M
MI trans
A
H
DS non

Copyright 2010, Oracle and/or it affiliates. All rights reserved.

Practices for Lesson 14


Chapter 14 - Page 16

You might also like