Вы находитесь на странице: 1из 34

Updated for

Release 2!

Oracle 10g New Features:


SQL Tuning
Presented at VOUG
by Dave Anderson of SkillBuilders
March 2, 2006
www.skillbuilders.com
888-803-5607

2005 SkillBuilders, Inc.

SKILLBUILDERS

6.2

Agenda
Introduce several SQL-tuning related 10g
features
SQL

Tuning Advisor
SQL Profiles
SQL Access Advisor
Optimizer (CBO) enhancements
Hints
Statistics Gathering
PL/SQL Improvements
2005 SkillBuilders, Inc.

6.3

SQL Tuning Advisor


Executes Automatic Tuning Optimizer on an
SQL statement
Provides tuning advice
Missing

or stale statistics
Better paths

Can recommend creation of indexes

Restructuring

Syntax, semantics and design

SQL

SQL

Profiles

More in this later

2005 SkillBuilders, Inc.

6.4

STA Interfaces
10g Enterprise Manager
Easy GUI
Launch job with point and click
Implement recommendations with point and
click
Supplied package DBMS_SQLTUNE
R2 adds V$ADVISOR_PROGRESS
Monitor

STA progress
2005 SkillBuilders, Inc.

R2

6.5

Input to SQL Tuning Advisor


AWR or ADDM

Identify high-load SQL

Shared pool
Text
Multiple statements bound into SQL Tuning Set
Contains parsing schema and bind values
Contains execution count and average elapsed time
R2 supports export / import into another DB

R2

2005 SkillBuilders, Inc.

6.6

Running STA
Create tuning task

Stored persistently in data dictionary

Execute tuning task


Executes the optimizer in Plan Tuning Analysis mode
Gathers additional info
Creates a profile when warranted (more later)

Display report

Contains findings and possible solutions

Optionally delete task

2005 SkillBuilders, Inc.

6.7

STA Demo
Create a tuning task
Execute the task
Invokes

cost-based optimizer

Privileges
Privilegesreqd:
reqd:ADVISOR,
ADVISOR,
SELECT_CATALOG_ROLE,
SELECT_CATALOG_ROLE,
EXECUTE
EXECUTEon
on
DBMS_SQLTUNE
DBMS_SQLTUNE

Source
SourceofofSQL
SQLcan
canbe
beADDM
ADDM

SYSTEM>
SYSTEM> declare
declare
22
task_id
task_id varchar2(30);
varchar2(30);
33 begin
begin
44
task_id
task_id :=
:= dbms_sqltune.create_tuning_task(
dbms_sqltune.create_tuning_task(
55
sql_text=>'update
sql_text=>'update dave.t
dave.t set
set c1
c1 == 6000',
6000',
66
task_name=>'test2');
task_name=>'test2');
77
88
dbms_sqltune.execute_tuning_task('test2');
dbms_sqltune.execute_tuning_task('test2');
99 end;
end;
10
/
10 /
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
2005 SkillBuilders, Inc.

6.8

STA Demo
Display the report
SYSTEM>
SYSTEM> set
set long
long 1000
1000
SYSTEM>
set
linesize
SYSTEM> set linesize 120
120
SYSTEM>
select
dbms_sqltune.report_tuning_task('test2')
SYSTEM> select dbms_sqltune.report_tuning_task('test2') from
from dual;
dual;
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST2')
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST2')
----------------------------------------------------------------------------------------------------------------------------------GENERAL
11ststsection
GENERAL INFORMATION
INFORMATION SECTION
SECTION
sectionofofreport
reportgives
gives
-----------------------------------------------------------------general
information
-----------------------------------------------------------------general information
Tuning
Tuning Task
Task Name
Name :: test2
test2
Scope
:: COMPREHENSIVE
Scope
COMPREHENSIVE
Time
Limit(seconds):
1800
Time Limit(seconds): 1800
Completion
Completion Status
Status :: COMPLETED
COMPLETED
Started
at
:
08/02/2004
Started at
: 08/02/2004 17:49:29
17:49:29
Completed
at
:
08/02/2004
17:49:30
Completed at
: 08/02/2004 17:49:30
---------------------------------------------------------------------------------------------------------------------------------- 2005 SkillBuilders, Inc.

6.9

STA Demo
----------------------------------------------------------------------------------------------------------------------------------------------FINDINGS
FINDINGS SECTION
SECTION (1
(1 finding)
finding)
Findings
Findingssection
sectionidentifies
identifies
-----------------------------------------------------------------------------------------------------------------------------------------------

possible
possibleissues
issues

11- Statistics
Statistics Finding
Finding
----------------------------------------Table
Table "DAVE"."T"
"DAVE"."T" was
was not
not analyzed.
analyzed.
Recommendation
Recommendation

Recommendations
Recommendationssection
section
gives
possible
solutions
gives possible solutions

DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST2')
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST2')
--------------------------------------------------------------------------------------------------------------------------------------------Consider
Consider collecting
collecting optimizer
optimizer statistics
statistics for
for this
this table.
table.
execute
dbms_stats.gather_table_stats(ownname
=>
'DAVE',
execute dbms_stats.gather_table_stats(ownname => 'DAVE', tabname
tabname =>
=> 'T',
'T',
estimate_percent
=>
DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt
=>
'FOR
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => 'FOR

Clean
Cleanup
up

SYSTEM>
SYSTEM> exec
exec dbms_sqltune.drop_tuning_task('test2');
dbms_sqltune.drop_tuning_task('test2');
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
2005 SkillBuilders, Inc.

6.10

SQL Profiles
Additional statistics and information for CBO
Uses

previous execution stats for a query


Gets from AWR

Changes normal decision of CBO at run-time


Sort

of like a hint, without coding the hint


Useful for packaged apps like Peoplesoft or SAP

Created by SQL Tuning Advisor


When

run in COMPREHENSIVE mode


Stored in dictionary

2005 SkillBuilders, Inc.

6.11

SQL Profile Demo (1)


Run STA in comprehensive mode on query
SQL
SQL Text:
Text: select
select /*+
/*+ full(big)
full(big) */
*/ ** from
from big
big where
where object_id
object_id == 11
------------------------------------------------------------------------------------------------------------------------------------Forcing
FINDINGS
Forcingaafull
full
FINDINGS SECTION
SECTION (1
(1 finding)
finding)
scan
for
this
------------------------------------------------------------------scan for thistest
test
------------------------------------------------------------------11- SQL
SQL Profile
Profile Finding
Finding (see
(see explain
explain plans
plans section
section below)
below)
--------------------------------------------------------------------------------------------------------------AA potentially
potentially better
better execution
execution plan
plan was
was found
found for
for this
this statement.
statement.
Recommendation
Recommendation (estimated
(estimated benefit:
benefit: 99.66%)
99.66%)
----------------------------------------------------------------------------------Consider
Consider accepting
accepting the
the recommended
recommended SQL
SQL profile
profile
STA
STAcreated
createdaa
profile
profile
2005 SkillBuilders, Inc.

Mystery:
Mystery:how
how
totosee
the
see the
better
better
execution
execution
plan
planbefore
before
accepting?
accepting?

6.12

SQL Profile Demo (2)


Accept the profile
DAVE>
DAVE> var
var xx varchar2(30)
varchar2(30)
DAVE>
begin
DAVE> begin
22 :x
:x :=
:= dbms_sqltune.accept_sql_profile
dbms_sqltune.accept_sql_profile
(task_name=>'profile_test',
(task_name=>'profile_test',
name=>'profile_test_profile');
name=>'profile_test_profile');
33 end;
end;
44 //
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
Can
Canlimit
limituse
use
by
bysession
session
with
with
CATEGORY
CATEGORY
attributes
attributes
2005 SkillBuilders, Inc.

Doc
Docsays
saysthis
this
isisaaprocedure
procedure
its
itsactually
actually
aafunction.
function.

6.13

SQL Profile Demo (3)


Test is profile used?
DAVE>
DAVE> set
set autotrace
autotrace traceonly
traceonly
DAVE>
select
/*+
full(big)
DAVE> select /*+ full(big) */
*/ ** from
from big
big where
where object_id
object_id == 1;
1;
YES.
YES.Profile
Profileisis
used.
used.

no
no rows
rows selected
selected

Execution
Execution Plan
Plan
------------------------------------------------------------------------------------------------------------------00
SELECT
SELECT STATEMENT
STATEMENT Optimizer=ALL_ROWS
Optimizer=ALL_ROWS (Cost=2
(Cost=2 Card=1
Card=1
11
00 TABLE
ACCESS
(BY
INDEX
ROWID)
OF
'BIG'
(TABLE)
TABLE ACCESS (BY INDEX ROWID) OF 'BIG' (TABLE) (C
(C
22

11

INDEX
INDEX (RANGE
(RANGE SCAN)
SCAN) OF
OF 'BIG_IDX'
'BIG_IDX' (INDEX)
(INDEX) (Cost=1
(Cost=1

DAVE>
DAVE> exec
exec dbms_sqltune.drop_sql_profile('profile_test_profile')
dbms_sqltune.drop_sql_profile('profile_test_profile')
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
2005 SkillBuilders, Inc.

Cleanup
Cleanup

6.14

SQL Access Advisor


Tuning advice for a workload

SQL statements plus execution statistics

Recommends
Materialized views, view logs
Indexes

B-Tree, function-based, bitmap

Access via

Enterprise Manager

Advisor Central page

DBMS_ADVISOR

Supplied package

2005 SkillBuilders, Inc.

6.15

SQL Access Advisor


Workloads
Partial workload
Not

all queries or stats available

Only queries and executing user required

Will

not recommend dropping indexes

Full Workload
Believes

it knows all uses of objects


Will recommend drop if necessary

2005 SkillBuilders, Inc.

Access
Advisor
DEMO

6.16

CBO: Hash Group By


Hashing technique for GROUP BY
Returns rows grouped, but in random order
Must use ORDER BY for sorted results
Code

changes?

Release 2 BUG 4604970


Disable for now

R2

alter
alter session
session set
set "_gby_hash_aggregation_enabled"
"_gby_hash_aggregation_enabled" == FALSE;
FALSE;

2005 SkillBuilders, Inc.

6.17

CBO: Costed Transformations


CBO transformations
Subquery

unnesting
View merging

Before 10g
Unconditional

query transformation

10g
Cost

of original query compared to result of


transformation

2005 SkillBuilders, Inc.

6.18

9i Dynamic Sampling
Compile time sampling of data to determine
selectivity and volume
Useful when statistics old or non-existent
Useful for queries executed many times or
with long execution time
Recursive SQL issued to read random
sample of data blocks
Does not save stats in dictionary
2005 SkillBuilders, Inc.

6.19

9i Dynamic Sampling
Control with optimizer_dynamic_sampling
parameter or dynamic_sampling hint

0 no sampling
1 through 10 Various levels of sampling;
10 most aggressive, does most work

SQL>
SQL> select
select /*+
/*+ dynamic_sampling(t
dynamic_sampling(t 10)
10) */
*/
22 from
from tt
33 where
where lastname
lastname == 'T';
'T';
11 select
select /*+
/*+ dynamic_sampling(1)
dynamic_sampling(1) */
*/
22
t.lastname,
t.lastname, o.order_date
o.order_date
33 from
from t,
t, ord
ord oo
4*
4* where
where t.cust_no
t.cust_no == o.cust_no
o.cust_no
2005 SkillBuilders, Inc.

**

6.20

9i Dynamic Sampling
select
select /*+
/*+ dynamic_sampling(t
dynamic_sampling(t 10)
10) */
*/ **
from
from tt
where
where lastname
lastname == 'T'
'T'
call
count
cpu
elapsed
disk
query
current
rows
call
count
cpu
elapsed
disk
query
current
rows
---------------------------------------------------------------------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
11
0.04
0.07
118
123
00
00
Parse
0.04
0.07
118
123
Execute
1
0.00
0.00
0
0
0
00
Execute
1
0.00
0.00
0
0
0
Fetch
2
0.00
0.00
0
5
0
22
Fetch
2
0.00
0.00
0
5
0
------------- ----------- --------------- ------------------- ------------------- ------------------- ------------------- ------------------total
4
0.04
0.07
118
128
00
22
total
4
0.04
0.07
118
128

We can see the affect of


select
select /*+
/*+ ynamic_sampling(t
ynamic_sampling(t 10)
10) */
*/ **
from
sampling on PARSE
from tt
where
lastname
=
'T'
where lastname = 'T'
call
count
cpu
elapsed
disk
query
current
rows
call
count
cpu
elapsed
disk
query
current
rows
------------- ----------- --------------- ------------------- ------------------- ------------------- ------------------- ------------------Parse
11
0.00
0.00
00
00
00
00
Parse
0.00
0.00
Execute
1
0.00
0.00
0
0
0
0
Execute
1
0.00
0.00
0
0
0
0
Fetch
22
0.00
0.00
00
55
00
22
Fetch
0.00
0.00
------------- ----------- --------------- ------------------- ------------------- ------------------- ------------------- ------------------total
4
0.00
0.00
0
5
00
22
total
4
0.00
0.00
0
5
2005 SkillBuilders, Inc.

6.21

10g CBO: Dynamic Sampling


Default dynamic sampling level now 2, not 1
9i default is 1

2005 SkillBuilders, Inc.

6.22

10g Hints
Skip Scan
INDEX_SS,

INDEX_SS_ASC | DESC

Join
USE_NL_WITH_INDEX

Subquery
QB_NAME

User specified query block name

Transformation
NO_QUERY_TRANSFORMATION

2005 SkillBuilders, Inc.

6.23

Automatic Optimizer Statistics


DB installation creates a scheduled daily job
SQL>
SQL> select
select last_start_date,
last_start_date,
last_run_duration
last_run_duration
22 from
from dba_scheduler_jobs
dba_scheduler_jobs
33 where
where job_name
job_name == 'GATHER_STATS_JOB';
'GATHER_STATS_JOB';
31-JUL-04
31-JUL-04 10.00.03.904000
10.00.03.904000 AM
AM -04:00
-04:00
+000000000
+000000000 00:01:25.814000
00:01:25.814000

Collects only if stats stale or missing


Set STATISTICS_LEVEL = TYPICAL
All tables monitored with statistics level TYPICAL
Extension of 9i Table Monitoring feature

2005 SkillBuilders, Inc.

Part
Partofofnew
newJob
Job
Scheduler
feature.
Scheduler feature.

6.24

DBMS_STATS
Collect data dictionary statistics
SYSDBA>
SYSDBA> exec
exec dbms_stats.gather_dictionary_stats
dbms_stats.gather_dictionary_stats

Auto parallel for DBMS_STATS


Old stats auto-saved
DAVE>
DAVE> exec
exec dbms_stats.gather_schema_stats(user,
dbms_stats.gather_schema_stats(user, ->>
cascade=>TRUE,
cascade=>TRUE, ->>
method_opt=>
method_opt=> 'FOR
'FOR ALL
ALL INDEXED
INDEXED COLUMNS',
COLUMNS', ->>
options=>'GATHER
options=>'GATHER STALE',
STALE', ->>
degree=>dbms_stats.auto_degree)
degree=>dbms_stats.auto_degree)
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
2005 SkillBuilders, Inc.

6.25

Rule-Based Optimization
Still exists, but not supported
OPTIMIZER_MODE considerations
CHOOSE

or RULE causes ALERT.LOG warning


ALL_ROWS is the default

CHOOSE and RULE hints not supported


Migrate existing apps to CBO
Helpful

documentation:

Oracle Database Upgrade Guide


Metalink Doc 189702.1
Oracle Database Performance Tuning Guide
2005 SkillBuilders, Inc.

6.26

SQL*Plus AUTOTRACE
DAVE@linux3>
DAVE@linux3> select
select ** from
from big,small
big,small
22 where
big.object_id=small.object_id
where big.object_id=small.object_id
33 and
and big.owner
big.owner like
like 'A%';
'A%';

R2

Execution
Execution Plan
Plan
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|| Id
|| Name
|| Rows
Id || Operation
Operation
Name
Rows || Bytes
Bytes || Cost
Cost (%CPU)
(%CPU)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------|| 00 || SELECT
||
||
11 || 265
SELECT STATEMENT
STATEMENT
265 || 3707
3707 (3)
(3)
|| 11 || NESTED
LOOPS
|
|
1
|
265
|
3707
(3)
NESTED LOOPS
|
|
1 |
265 | 3707
(3)
|*
2
|
TABLE
ACCESS
FULL
|
BIG
|
1
|
186
|
3706
(3)
|* 2 |
TABLE ACCESS FULL
| BIG
|
1 |
186 | 3706
(3)
|| 33 || TABLE
ACCESS
BY
INDEX
ROWID|
SMALL
|
1
|
79
|
1
(0)
TABLE ACCESS BY INDEX ROWID| SMALL
|
1 |
79 |
1
(0)
|*
4
|
INDEX
UNIQUE
SCAN
|
SMALL_OBJECT_ID
|
1
|
|
0
(0)
|* 4 |
INDEX UNIQUE SCAN
| SMALL_OBJECT_ID |
1 |
|
0
(0)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------Predicate
Predicate Information
Information (identified
(identified by
by operation
operation id):
id):
----------------------------------------------------------------------------------------------------22 -- filter("BIG"."OWNER"
filter("BIG"."OWNER" LIKE
LIKE 'A%')
'A%')
44 -- access("BIG"."OBJECT_ID"="SMALL"."OBJECT_ID")
access("BIG"."OBJECT_ID"="SMALL"."OBJECT_ID")
2005 SkillBuilders, Inc.

6.27

PL/SQL Performance
New optimizing compiler
Better
integer

Oracle Corp: 2 times


faster than 8i, 1.5
times faster than 9i

performance
reuse of expression values
simplification of branching code
performance for some library calls
elimination of dead code

Looks for conversion possibilities


e.g.

Convert FOR LOOPS into BULK COLLECT

2005 SkillBuilders, Inc.

6.28

PL/SQL Performance
Initialization parameter controls level of optimization
LINUX>
LINUX> show
show parameter
parameter plsql_opt
plsql_opt
NAME
TYPE
VALUE
NAME
TYPE
VALUE
----------------------------------------------------------------------- --------------------- --------plsql_optimize_level
integer
22
plsql_optimize_level
integer

Can set when recompiling


LINUX>
LINUX> alter
alter procedure
procedure main
main compile
compile plsql_optimize_level=2;
plsql_optimize_level=2;
Procedure
Procedure altered.
altered.

Automatic
Automaticrecompilations
recompilationsuse
use
level
stored
with
program
level stored with program
2005 SkillBuilders, Inc.

6.29

PL/SQL Performance
Dictionary view reveals compile-time settings
LINUX>
LINUX> exec
exec print_table('select
print_table('select ** from
from
all_plsql_object_settings
all_plsql_object_settings where
where name
name == ''TEST''
''TEST'' ')
')
OWNER
:: DAVE
OWNER
DAVE
NAME
:: TEST
NAME
TEST
TYPE
:: PROCEDURE
TYPE
PROCEDURE
PLSQL_OPTIMIZE_LEVEL
:
2
PLSQL_OPTIMIZE_LEVEL
: 2
PLSQL_CODE_TYPE
:: INTERPRETED
PLSQL_CODE_TYPE
INTERPRETED

2005 SkillBuilders, Inc.

6.30

Optimization Comparison
ORACLE9i>
ORACLE9i> begin
begin
22
for
for xx in
in (( select
select ** from
from all_objects
all_objects where
where rownum
rownum << 10000)
10000)
33
loop
loop
44
null;
null;
55
end
end loop;
loop;
9i compiler provides
66 end;
end;
77 //
no conversion
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
select
select **
from
from
all_objects
all_objects where
where rownum
rownum << 10000
10000

10,000 individual
fetches

call
count
cpu
elapsed
disk
query
current
rows
call
count
cpu
elapsed
disk
query
current
rows
---------------------------------------------------------------------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
11
0.06
0.07
00
00
00
00
Parse
0.06
0.07
Execute
11
0.00
0.00
00
00
00
00
Execute
0.00
0.00
Fetch
10000
3.18
3.11
00
63481
00
9999
Fetch
10000
3.18
3.11
63481
9999
---------------------------------------------------------------------- ------ -------- ---------- ---------- ---------- ---------- ---------total
10002
3.24
3.18
00
63481
00
9999
total
10002
3.24
3.18
63481
9999
2005 SkillBuilders, Inc.

6.31

Optimization Comparison
ORACLE10G>
ORACLE10G> begin
begin
22
for
for xx in
in (( select
select ** from
from all_objects
all_objects where
where rownum
rownum << 10000)
10000)
33
loop
loop
44
null;
null;
55
end
end loop;
loop;
10g compiler converts
66 end;
end;
77 //
this into a bulk collect
PL/SQL
PL/SQL procedure
procedure successfully
successfully completed.
completed.
SELECT
SELECT **
FROM
FROM
ALL_OBJECTS
ALL_OBJECTS WHERE
WHERE ROWNUM
ROWNUM << 10000
10000

Arraysize = 100

Great reduction in
CPU, elapsed time
and logical I/O

call
count
cpu
elapsed
disk
query
current
rows
call
count
cpu
elapsed
disk
query
current
rows
---------------------------------------------------------------------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
11
0.00
0.00
00
00
00
00
Parse
0.00
0.00
Execute
11
0.00
0.00
00
00
00
00
Execute
0.00
0.00
Fetch
100
0.79
0.74
00
15812
00
9999
Fetch
100
0.79
0.74
15812
9999
---------------------------------------------------------------------- ------ -------- ---------- ---------- ---------- ---------- ---------total
102
0.79
0.75
00
15812
00
9999
total
102
0.79
0.75
15812
9999
2005 SkillBuilders, Inc.

6.32

COMMIT Enhancements
Can COMMIT w/o waiting for log write
Performance increase
BATCH groups redo streams for write

Can lose transaction


Might consider for data loads with frequent commits
SYSTEM@orcl>
SYSTEM@orcl> commit
commit write
write batch
batch nowait;
nowait;
Commit
Commit complete.
complete.
SYSTEM@orcl>
SYSTEM@orcl> show
show parameter
parameter commit_write
commit_write
NAME
TYPE
VALUE
NAME
TYPE
VALUE
----------------------------------------------------------------------- --------------------- --------commit_write
string
commit_write
string
2005 SkillBuilders, Inc.

6.33

Upcoming Oracle 10g Classes


Oracle 10g Administration
March

20 24, NYC
May 1 5, NYC

Oracle 10g New Features for Administrators


May

9 12, Richmond
May 15 18, NYC

Oracle 10g New Features for Developers


May

9 11, NYC

Call us to teach a class at


your site:
888-803-5607

2005 SkillBuilders, Inc.

6.34

The End
Thanks for listening!
Dave

Anderson
dave@skillbuilders.com
www.skillbuilders.com
Visit for tech papers,
download presentations

2005 SkillBuilders, Inc.

Вам также может понравиться