Académique Documents
Professionnel Documents
Culture Documents
PART 2
2 Copyright 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda
Using the right tools Finding the right sample size Tell the optimizer everything Functions friends or foe Most useful hints to know
Sometimes you know more about your data than basic statistics show
Best way to get a good plan is to tell the Optimizer everything Otherwise it assumes, making an ass out of u and me
AND
MAKE BMW
color = 'RED;
MODEL 530xi COLOR RED Year 2013
AND
MAKE BMW BMW
make = BMW;
MODEL 530xi 530xi COLOR RED BLACK Year 2013 2011
BMW
530xi
BLUE
2012
Cardinality
make = BMW;
MODEL 530xi 530xi COLOR RED BLACK Year 2013 2011
BMW
530xi
BLUE
2012
Use SEED_COL_USAGE to have Oracle monitor workload Can monitor STS or live system Recommends what column groups are needed based on workload
10
With 11 column histogram (two per year and one for the garbage)
TABLE ACCESS (FULL) OF 'T1' (Cost=46 Card=36320 Bytes=290560)
11
Graphical Interpretation
Data for 2012
12
-- date -- numeric
-- char
daily
14
Histogram effects:
pre-histogram post-histogram
TABLE ACCESS (FULL) OF 'T1' (Cost=3 Card=397 Bytes=9131) TABLE ACCESS (FULL) OF 'T1' (Cost=3 Card=32 Bytes=736)
Graphical Interpretation
The real (char/number) data
16
Program Agenda
Using the right tools Finding the right sample size Tell the optimizer everything Functions friends or foe Most useful hints to know
17
18
19
Predicate is TO_CHAR(prod_id) Optimizer has no idea how function effects values in prod_id column Optimizer cant determine which rows will be accessed now
20
21
FROM
sales2
WHERE prod_id=to_number(139);
22
partition pruning
For function-based index to be considered, use that exact function as specified in
index
If multiple predicates involve the same columns, write predicates such that they
WHERE f(a) = b
AND a=c
Should be rewritten as
WHERE a = inv_f(b)
AND a=c
23
execute dbms_stats.gather_table_stats(user,'t1');
24
Predicate Information (identified by operation id): 1 - filter(MOD("ID1",3)=0) select * from t1 where mod(id1,3) != 0;
|*
1 |
500 |
5500 |
. 4 |
. 4 |
Predicate Information (identified by operation id): 1 - filter("T1"."M2"=0) select * from t1 where mod(id2,3) != 0; |* 1 | TABLE ACCESS FULL| T1 | 6667 | 73337 | . 4 |
26
Program Agenda
Using the right tools Finding the right sample size Tell the optimizer everything Functions friends or foe Most useful hints to know
27
between several possibilities A hint is a directive that will be followed when applicable
Can influence everything from the Optimizer mode used to each
28
preserved Records actual number of rows seen for each operation (step) in plan Execution statistis displayed in plan when format parameter of DMBS_XPLAN set to ALLSTATS LAST
Means display all execution statistics for last execution of this cursor
29
GATHER_PLAN_STATISTICS hint
30
GATHER_PLAN_STATISTICS Hint
SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST'));
Note: a lot of the data is zero in the A-rows column because we only show last executed cursor which is the QC. Need to use ALLSTATS ALL to see info on all parallel server cursors
31
GATHER_PLAN_STATISTICS Hint
SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS ALL'));
32
33
query
Useful way to prevent setting non-default parameter value system-wide Only the following Optimizer influencing init.ora parameters can be set:
OPTIMIZER_DYNAMIC_SAMPLING
OPTIMIZER_INDEX_CACHING OPTIMIZER_INDEX_COST_ADJ OPTIMIZER_USE_PENDING_STATISTICS Optimizer related underscore parameters
STAR_TRANSFORMATION_ENABLED
PARALLEL_DEGREE_POLICY PARALLEL_DEGREE_LIMIT
34
OPT_PARAM hint
Example
35
OPT_PARAM hint
Example
36
between optimizer versions Setting it to previous database version reverts the Optimizer to that version
Disables any functionality that was not present in that version
Easy way to work around unexpected behavior in a new release Hint allows you to revert the Optimizer for just a single statement
37
. |Id|Operation |Name |Cost |Pstart| Pstop| | 0|SELECT STATEMENT | | 48| | | | 1| PARTITION LIST INLIST | | 48|KEY(I)|KEY(I)| |*2| TABLE ACCESS BY LOCAL INDEX ROWID|PT1 | 48|KEY(I)|KEY(I)| |*3| INDEX RANGE SCAN |PT1_I1| 3|KEY(I)|KEY(I)|
38
NESTED LOOP
TABLE ACCESS (FULL) OF T7 TABLE ACCESS BY INDEX ROWID T8 INDEX RANGE SCAN T8_IND_SECOND -- wrong index
-- right index
39
40
Summary
Forgive the Optimizer its not out to get you
Its knowledge is limited to the information you provide it
More Information
White papers
Optimizer with Oracle Database 12c SQL Plan Management with Oracle Database 12c Understanding Optimizer Statistics with Oracle Database 12c
Blogs
http://jonathanlewis.wordpress.com/all-postings/ http://blogs.oracle.com/optimizer
Oracle.com
http://www.oracle.com/technetwork/database/focus-areas/bi-
datawarehousing/dbbi-tech-info-optmztn-092214.html
43 Copyright 2013, Oracle and/or its affiliates. All rights reserved.
44