SlideShare ist ein Scribd-Unternehmen logo
1 von 119
Downloaden Sie, um offline zu lesen
22/07/2019
1
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Taming the beast
The Optimizer in 12c and beyond
Connor McDonald
1
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Connor McDonald
1
2
22/07/2019
2
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
4
3
4
22/07/2019
3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Stuff
youtube bit.ly/youtube-connor
blog bit.ly/blog-connor
twitter bit.ly/twitter-connor
400+ posts mainly on database & development
250 technical videos, new uploads every week
rants and raves on tech and the world :-)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
etc...
facebook bit.ly/facebook-connor
linkedin bit.ly/linkedin-connor
instagram bit.ly/instagram-connor
slideshare bit.ly/slideshare-connor
5
6
22/07/2019
4
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
In Memoriam
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
this session
8
7
8
22/07/2019
5
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
3 things
9
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12c(18c,19c) optimizer …
better performance
10
9
10
22/07/2019
6
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12c(18c,19c) optimizer …
worse performance
11
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
worse performance …
12
11
12
22/07/2019
7
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
13
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
14
13
14
22/07/2019
8
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
goal of the optimizer
15
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
simple
16
15
16
22/07/2019
9
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
good execution plan
17
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
so how did we end up here ?
18
17
18
22/07/2019
10
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
19
You will upgrade the
database with the new
optimizer features
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
LOL !LOL !
20
19
20
22/07/2019
11
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
LMAOLMAO!
21
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
That dude
is toast!
That dude
is toast!
22
21
22
22/07/2019
12
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Is this why I don't
have a desk ?
23
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
lots of fear
24
23
24
22/07/2019
13
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
lots of mis-information
25
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
only 3 issues
26
25
26
22/07/2019
14
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
ISSUE #1
27
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
poor statistics
28
27
28
22/07/2019
15
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
cardinality mis-estimates
29
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
for the optimizer...
30
29
30
22/07/2019
16
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
...cardinality is everything
31
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
32
31
32
22/07/2019
17
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
33
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
34
33
34
22/07/2019
18
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
better stats
35
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1) better histograms
36
35
36
22/07/2019
19
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
2015
37
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
38
37
38
22/07/2019
20
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
39
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Histograms are so
awesome. See
how much they
make me smile !
40
39
40
22/07/2019
21
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
41
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
No …
… honest :-)
42
41
42
22/07/2019
22
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the sucky part
43
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> create table T
2 as select * from dba_objects;
Table created.
SQL> exec dbms_stats.gather_table_stats('','T');
PL/SQL procedure successfully completed.
44
43
44
22/07/2019
23
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select COLUMN_NAME,NUM_DISTINCT,
2 ( select count(*)
3 from all_tab_histograms
4 where owner = a.owner
5 and table_name = a.table_name
6 and column_name = a.column_name )
7 from all_tab_cols a
8 where table_name = 'T'
9 order by column_id;
COLUMN_NAME NUM_DISTINCT HIST_CNT
------------------------------ ------------ ----------
OWNER 25 2
OBJECT_NAME 53000 2
SUBOBJECT_NAME 230 2
OBJECT_ID 90808 2 45
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
46
45
46
22/07/2019
24
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
47
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select intcol#,equality_preds,
2 range_preds,timestamp
3 from sys.col_usage$
4 where obj# = (select object_id
5 from obj
6 where object_name = 'T' );
INTCOL# EQUALITY_PREDS RANGE_PREDS TIMESTAMP
---------- -------------- ----------- ---------
1 1 0 24-SEP-17
48
47
48
22/07/2019
25
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> exec dbms_stats.gather_table_stats('','T');
SQL> select COLUMN_NAME,NUM_DISTINCT,
2 ( select count(*)
3 from all_tab_histograms
4 where owner = a.owner
5 and table_name = a.table_name
6 and column_name = a.column_name )
7 from all_tab_cols a
8 where table_name = 'T'
9 order by column_id;
COLUMN_NAME NUM_DISTINCT HIST_CNT
------------------------------ ------------ ----------
OWNER 25 25
OBJECT_NAME 53000 2
SUBOBJECT_NAME 230 2
OBJECT_ID 90808 2
49
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
we don't know what your "app" is
50
49
50
22/07/2019
26
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
still requires care
51
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12c improvements
52
51
52
22/07/2019
27
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select status, count(*)
2 from orders
3 group by status
4 order by 1;
STATUS COUNT(*)
-------------------- ----------
1-New 1000
2-Assigned 10
3-InProgress 4000
4-Processed 3
5-Shipped 800
6-Received 5000
7-Completed 15000
8-Archived 20000
53
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
skewed ...
54
53
54
22/07/2019
28
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
... need histogram
55
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
11g
56
55
56
22/07/2019
29
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('','ORDERS',
3 method_opt=>'for all columns size 15');
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select histogram from USER_TAB_COL_STATISTICS
2 where table_name = 'ORDERS';
HISTOGRAM
---------------
FREQUENCY
57
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5),
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0)
8 from user_histograms
9 where table_name = 'ORDERS'
10 order by 1;
ENDPOINT_NUMBER END_VAL FREQ
--------------- -------------------- ----------
1000 1-Nev 1000
1010 2-Ass 10
5010 3-InP 4000
5013 4-Pro 3
5813 5-Shi 800
10813 6-Rec 5000
25813 7-Com 15000
45813 8-Arc 20000
58
57
58
22/07/2019
30
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5),
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0)
8 from user_histograms
9 where table_name = 'ORDERS'
10 order by 1;
ENDPOINT_NUMBER END_VAL FREQ
--------------- -------------------- ----------
1000 1-Nev 1000
1010 2-Ass 10
5010 3-InP 4000
5013 4-Pro 3
5813 5-Shi 800
10813 6-Rec 5000
25813 7-Com 15000
45813 8-Arc 20000
59
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5),
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0)
8 from user_histograms
9 where table_name = 'ORDERS'
10 order by 1;
ENDPOINT_NUMBER END_VAL FREQ
--------------- -------------------- ----------
1000 1-Nev 1000
1010 2-Ass 10
5010 3-InP 4000
5013 4-Pro 3
5813 5-Shi 800
10813 6-Rec 5000
25813 7-Com 15000
45813 8-Arc 20000
60
59
60
22/07/2019
31
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from orders
2 where status = '5-Shipped';
----------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
----------------------------------------------------
| 0 | SELECT STATEMENT | | 800 | 8800 |
|* 1 | TABLE ACCESS FULL| ORDERS | 800 | 8800 |
----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='5-Shipped')
61
true = 800
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
so far … so good
62
61
62
22/07/2019
32
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
NDV > 254*
63
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('','ORDERS',
3 method_opt=>'for all columns size 6');
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select histogram from USER_TAB_COL_STATISTICS
2 where table_name = 'ORDERS';
HISTOGRAM
---------------
HEIGHT BALANCED
64
63
64
22/07/2019
33
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5),
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0)
8 from user_histograms
9 where table_name = 'ORDERS'
10 order by 1;
ENDPOINT_NUMBER END_VAL BKTS
--------------- -------------------- ----------
0 1-Nev 0
1 6-Rec 1
3 7-Com 2
6 8-Arc 3
65
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1-New
6-Received
7-Complete
8-Archived
8-Archived
8-Archived
7-Complete
66
65
66
22/07/2019
34
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from orders
2 where status = '8-Archived';
----------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
----------------------------------------------------
| 0 | SELECT STATEMENT | | 19089 | 205K|
|* 1 | TABLE ACCESS FULL| ORDERS | 19089 | 205K|
----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='8-Archived')
true = 20000
67
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from orders
2 where status = '7-Completed';
-----------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------
| 0 | SELECT STATEMENT | | 15271 | 164K|
|* 1 | TABLE ACCESS FULL| ORDERS | 15271 | 164K|
-----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='7-Completed')
true = 10000
68
67
68
22/07/2019
35
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
69
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1-New
6-Received
7-Complete
8-Archived
8-Archived
8-Archived
7-Complete
total ~ 45,000 ~7000 per bucket 70
69
70
22/07/2019
36
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1-New
6-Received
7-Complete
8-Archived
8-Archived
8-Archived
7-Complete
total = 45,000 ~7000 per bucket 71
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from orders
2 where status = '2-Assigned';
----------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
----------------------------------------------------
| 0 | SELECT STATEMENT | | 1273 | 14003 |
|* 1 | TABLE ACCESS FULL| ORDERS | 1273 | 14003 |
----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='2-Assigned')
true = 3
72
71
72
22/07/2019
37
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
height balanced = risk
73
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
11g
HEIGHT-BALANCED
FREQUENCY
74
73
74
22/07/2019
38
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Footnote: 11g
FOR ALL COLUMNS SIZE 254AUTO
75
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
76
75
76
22/07/2019
39
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('','ORDERS',
3 method_opt=>'for all columns size 15');
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select histogram from USER_TAB_COL_STATISTICS
2 where table_name = 'ORDERS';
HISTOGRAM
---------------
FREQUENCY
77
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('','ORDERS',
3 method_opt=>'for all columns size 6');
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select histogram from USER_TAB_COL_STATISTICS
2 where table_name = 'ORDERS';
HISTOGRAM
---------------
TOP-FREQUENCY
78
77
78
22/07/2019
40
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5)
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0)
8 from user_histograms
9 where table_name = 'ORDERS'
10 order by 1;
ENDPOINT_NUMBER END_VAL FREQ
--------------- -------------------- ----------
1000 1-Nev 1000
5000 3-InP 4000
5800 5-Shi 800
10800 6-Rec 5000
25800 7-Com 15000
45800 8-Arc 20000 79
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1-New
6-Received
3-InProgress
5-Shipped
8-Archived
7-Completed
80
79
80
22/07/2019
41
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
other values ?
81
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select num_rows
2 from user_tables
3 where table_name = 'ORDERS';
NUM_ROWS
----------
45813
SQL> select num_distinct
2 from user_tab_cols
3 where table_name = 'ORDERS';
NUM_DISTINCT
------------
8
FREQ
----------
800
1000
4000
5000
15000
20000
========
45800
( 45813 - 45800 ) / ( 8 - 6 ) = 7 approx.
82
81
82
22/07/2019
42
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from orders
2 where status = '4-Processed';
-----------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------
| 0 | SELECT STATEMENT | | 7 | 77 |
|* 1 | TABLE ACCESS FULL| ORDERS | 7 | 77 |
-----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='4-Processed')
83
true = 3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
what defines "TOP" ?
% top values > ((bkt-1)/bkt)* num rows
eg 1000 rows/ 12 buckets
12 most frequent > 11/12 * 1000
~920 rows
84
83
84
22/07/2019
43
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
what if TOP FREQ not possible ?
85
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert into orders select '1-New'
2 from dual connect by level <= 9000;
SQL> insert into orders select '2-Assigned'
2 from dual connect by level <= 9500;
SQL> insert into orders select '3-InProgress'
2 from dual connect by level <= 6000;
SQL> insert into orders select '4-Processed'
2 from dual connect by level <= 9500;
SQL> insert into orders select '5-Shipped'
2 from dual connect by level <= 8800;
SQL> insert into orders select '6-Received'
2 from dual connect by level <= 4500;
86
85
86
22/07/2019
44
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select status, count(*)
2 from orders
3 group by status
4 order by 1;
STATUS COUNT(*)
-------------------- ----------
1-New 10000
2-Assigned 9510
3-InProgress 10000
4-Processed 9503
5-Shipped 9600
6-Received 9500
7-Completed 15000
8-Archived 20000
87
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('','ORDERS',
3 method_opt=>'for all columns size 6');
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select histogram from USER_TAB_COL_STATISTICS
2 where table_name = 'ORDERS';
HISTOGRAM
---------------
HYBRID
88
87
88
22/07/2019
45
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 endpoint_number,
3 substr(
4 utl_raw.cast_to_varchar2(
5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5),
6 endpoint_number -
7 nvl(lag(endpoint_number) over ( order by endpoint_number),0),
8 endpoint_repeat_count
9 from user_histograms
10 where table_name = 'ORDERS'
11 order by 1;
ENDPOINT_NUMBER END_VAL FREQ ENDPOINT_REPEAT_COUNT
--------------- -------------------- ---------- ---------------------
590 1-Nev 590 590
1743 3-InP 1153 626
2314 4-Pro 571 571
2895 5-Shi 581 581
3449 6-Rec 554 554
5547 8-Arc 2098 1213 89
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1-New
6-Received
3-InProgress
5-Shipped
8-Archived
4-Processed
ENDPOINT_NUMBER END_VAL FREQ ENDPOINT_REPEAT_COUNT
--------------- -------------------- ---------- ---------------------
590 1-Nev 590 590
1743 3-InP 1153 626
2314 4-Pro 571 571
2895 5-Shi 581 581
3449 6-Rec 554 554
5547 8-Arc 2098 1213
2-Assigned
590 1153 571 581 554 2098
7-Completed
90
89
90
22/07/2019
46
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
8-Archived
END_VAL FREQ ENDPOINT_REPEAT_COUNT
---------------- -------- ---------------------
1-Nev 590 590
3-InP 1153 626
4-Pro 571 571
5-Shi 581 581
6-Rec 554 554
8-Arc 2098 1213
2098
7-Completed
"Bucket 8-Arc has 2098 occurrences...
1213 of them are the end value 8-Arc...
therefore 885 are > 6-Rec and < 8-Arc"
91
6-Received
554
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
key point
92
91
92
22/07/2019
47
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
must use auto sample size
93
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
still need care
94
93
94
22/07/2019
48
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
representative values
95
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
96
95
96
22/07/2019
49
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"today's hot deals"
97
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
gather statistics
SQL> select count(*)
2 from items
3 where type = 'HOT_DEAL'
COUNT(*)
----------
0
98
97
98
22/07/2019
50
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
activate "deals of the day"
SQL> update items
2 set type = 'HOT_DEAL'
3 where ...
SQL> select count(*)
2 from items
3 where type = 'HOT_DEAL'
COUNT(*)
----------
14
99
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
biggest day of the year !
SQL> select min(discount)
2 from items
3 where type = 'HOT_DEAL'
4 and ...
where type = 'HOT_DEAL'
SQL> select free_shipping
2 from items
3 where hot_deal = 'Y'
4 and ...
SQL> select ...
2 from ...
SQL> select ...
2 from ...
SQL> select ...
2 from ...
SQL> select ...
2 from ...SQL> select ...
2 from ...
100
99
100
22/07/2019
51
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
101
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
where type = 'HOT_DEAL'
102
101
102
22/07/2019
52
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
? 103
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select type, count(*)
2 from items
3 group by type;
TYPE COUNT(*)
------ ----------
ORDERED 21353
ARCHIVED 4213
NORMAL 513234
SOLD 528291
HOT_DEAL 0
104
103
104
22/07/2019
53
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
normally...
105
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"ok... I got 1,000,000 total rows...
and 7 distinct values.
I got a frequency histogram...
There's 4 popular values,
adding up to 910,000
That's 3 values left to cover the rest
3 / ( 1,000,000 - 910,000) = density
... I'm done!"
106
105
106
22/07/2019
54
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for ITEMS[ITEMS]
SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE
kkecdn: Single Table Predicate:"ITEMS"."TYPE"='HOT_DEAL'
Column (#2):
NewDensity:0.001974, OldDensity:0.000000 BktCnt:1067091.000000,
PopBktCnt:1067091.000000, PopValCnt:4, NDV:4
Column (#2): TYPE(VARCHAR2)
AvgLen: 7 NDV: 4 Nulls: 0 Density: 0.001974
Histogram: Freq #Bkts: 4 UncompBkts: 1067091 EndPtVals: 4
Using density:
0.001974 of col #2 as selectivity of pred having unreasonably low value
NDV / (1067091 - 1067091)"Aaggghhhh!"
107
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from items where type = 'HOT_DEAL';
----------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2107 | 23177 | 672 (2)|
|* 1 | TABLE ACCESS FULL| ITEMS | 2107 | 23177 | 672 (2)|
----------------------------------------------------------------
TYPE COUNT(*)
------ ----------
ORDERED 21353
ARCHIVED 4213 * 50%
NORMAL 513234
SOLD 528291
108
107
108
22/07/2019
55
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
representative values
... at the right time
109
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
19c can help
110
109
110
22/07/2019
56
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 111
TIME
Gather
Stale
overnight
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 112
Conventional
DML
Real-time
Statistics
Gathered Statistics
Lightweight
Statistics
111
112
22/07/2019
57
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 113
real time statistics
DML driven
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 114
cheap essential statistics
high/low values
row/column counts
113
114
22/07/2019
58
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 115
lightweight rapid gather
resource manager
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
2) extensions
116
115
116
22/07/2019
59
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
11g
117
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"extended" statistics
118
117
118
22/07/2019
60
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
aka "column groups"
119
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select *
2 from ADDRESS
3 where CITY = 'Pune'
4 and COUNTRY = 'Australia';
120
119
120
22/07/2019
61
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
121
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
automatic column groups
122
121
122
22/07/2019
62
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.2
123
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
expression tracking
124
123
124
22/07/2019
63
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select s.item_id,
2 s.category,
3 stddev(p.prod_list_price - p.prod_min_price)
4 from products p,
5 sales s
6 where ....;
SQL> select expression_text, evaluation_count, fixed_cost
2 from user_expression_statistics
2 where table_name = 'PRODUCTS';
EXPRESSION_TEXT EVALUATION_COUNT FIXED_COST
------------------------------------------ ---------------- ----------
STDDEV("PROD_LIST_PRICE"-"PROD_MIN_PRICE") 766 .000041667
125
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
ISSUE #2
126
125
126
22/07/2019
64
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
missing statistics
127
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
3) online gather
128
127
128
22/07/2019
65
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the old problem
129
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert /*+ APPEND */ into MY_TABLE
2 select *
3 from MY_WHOPPING_GREAT_FAT_TABLE;MY_WHOPPING_GREAT_FAT_TABLE;
130
129
130
22/07/2019
66
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
131
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
132
131
132
22/07/2019
67
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
133
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
134
133
134
22/07/2019
68
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
135
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert /*+ APPEND */ into MY_TABLE
2 select *
3 from MY_WHOPPING_GREAT_FAT_TABLE
4 ...
7102984123 rows created.
Elapsed: 06:12:34.00
136
135
136
22/07/2019
69
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
and then...
137
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select num_rows from user_tables
2 where table_name = 'MY_TABLE';
NUM_ROWS
----------
138
137
138
22/07/2019
70
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
so ...
139
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> begin
2 dbms_stats.gather_table_stats('',
3 tname=>'MY_TABLE'
4 ...
5 ...
6 end;
7 /
Elapsed: a really long time
140
139
140
22/07/2019
71
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
141
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
on load
142
141
142
22/07/2019
72
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> create table T (
2 ...
3 ...
4 ... );
Table created.
SQL> select num_rows
2 from user_tables
3 where table_name = 'T';
NUM_ROWS
----------
SQL> insert /*+ APPEND */ into T
2 select * from dba_objects;
78876 rows created.
SQL> commit;
Commit complete.
SQL> select num_rows
2 from user_tables
3 where table_name = 'T';
NUM_ROWS
----------
78876 143
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
things to note
144
143
144
22/07/2019
73
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
must be direct
insert /*+ APPEND */
create table as select
145
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert into T select * from dba_objects;
78876 rows created.
SQL> commit;
SQL> select num_rows from user_tables
2 where table_name = 'T';
NUM_ROWS
----------
146
145
146
22/07/2019
74
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
must be empty
147
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert into T values (....)
1 row created.
SQL> insert /*+ APPEND */ into T select * from dba_objects;
78876 rows created.
SQL> commit;
SQL> select num_rows from user_tables
2 where table_name = 'T';
NUM_ROWS
----------
148
147
148
22/07/2019
75
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
execution plan
149
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> insert /*+ APPEND */ into T
2 select * from t_src;
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 78877 | 9M| 428 (1)|
| 1 | LOAD AS SELECT | T | | | |
| 2 | OPTIMIZER STATISTICS GATHERING | | 78877 | 9M| 428 (1)|
| 3 | TABLE ACCESS FULL | T_SRC| 78877 | 9M| 428 (1)|
------------------------------------------------------------------------------
150
149
150
22/07/2019
76
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select column_name, notes
2 from user_tab_col_statistics
3 where table_name = 'T';
COLUMN_NAME NOTES
------------------------------ -------------
OWNER STATS_ON_LOAD
OBJECT_NAME STATS_ON_LOAD
SUBOBJECT_NAME STATS_ON_LOAD
OBJECT_ID STATS_ON_LOAD
DATA_OBJECT_ID STATS_ON_LOAD
OBJECT_TYPE STATS_ON_LOAD
CREATED STATS_ON_LOAD
...
...
151
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
note: no histograms
152
151
152
22/07/2019
77
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
3) session GTT
153
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
11g
154
153
154
22/07/2019
78
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> create global temporary table t ( x int, y int )
2 on commit preserve rows;
Table created.
SQL> insert into t
2 select rownum, rownum
3 from dual
4 connect by level <= 10000;
10000 rows created.
155
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> exec dbms_stats.gather_table_stats('','T');
SQL> set autotrace traceonly explain
SQL> select * from t;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)|
| 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)|
---------------------------------------------------------------
156
155
156
22/07/2019
79
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
looks great until ...
157
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> connect scott/tiger
Connected.
SQL> select count(*) from t;
COUNT(*)
----------
0
SQL> set autotrace traceonly explain
SQL> select * from t;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)|
| 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)|
---------------------------------------------------------------
158
157
158
22/07/2019
80
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
159
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> create global temporary table t ( x int, y int )
2 on commit preserve rows;
Table created.
SQL> insert into t
2 select rownum, rownum
3 from dual
4 connect by level <= 10000;
10000 rows created.
160
159
160
22/07/2019
81
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> exec dbms_stats.gather_table_stats('','T');
SQL> set autotrace traceonly explain
SQL> select * from t;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)|
| 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)|
---------------------------------------------------------------
as before
161
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> connect scott/tiger
Connected.
SQL> insert into t
2 select rownum, rownum
3 from dual
4 connect by level <= 50;
50 rows created.
SQL> exec dbms_stats.gather_table_stats('','T');
162
161
162
22/07/2019
82
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> set autotrace traceonly explain
SQL> select * from t;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50 | 300 | 2 (0)|
| 1 | TABLE ACCESS FULL| T | 50 | 300 | 2 (0)|
---------------------------------------------------------------
Note
-----
- Global temporary table session private statistics used
163
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
back to session 1
164
163
164
22/07/2019
83
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> set autotrace traceonly explain
SQL> select * from t;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)|
| 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)|
---------------------------------------------------------------
165
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
note: default = private
166
165
166
22/07/2019
84
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
so far ...
167
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
missing or poor statistics
168
167
168
22/07/2019
85
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
improved optimizer "inputs"
histograms
gather on load
private GTT
169
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
ISSUE #3
170
169
170
22/07/2019
86
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
statistics needed = "∞"
171
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
learn ... as we go
172
171
172
22/07/2019
87
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
learn ... from experiences
173
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Adaptive Query Optimization #1
174
learn as we go
173
174
22/07/2019
88
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> set autotrace traceonly explain
SQL> select ...
2 from order_iterms o,
3 product_info p
4 where o.unit_price = 15
5 and o.quantity > 1
6 and o.product_id = p.product_id
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 200 |
| 1 | NESTED LOOPS | | 12 | 200 |
| 2 | NESTED LOOPS | | 12 | 200 |
| 3 | TABLE ACCESS FULL | ORDER_ITEMS | 12 | 800 |
| 4 | INDEX UNIQUE SCAN | PRODUCT_PK | 1 | 40 |
| 5 | TABLE ACCESS BY ROWID | PRODUCT_INFO | 1 | 10 |
-----------------------------------------------------------------
175
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
176
what if there's 5000 ?
4 where o.unit_price = 15
5 and o.quantity > 1
6 and o.product_id = p.product_id
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 200 |
| 1 | NESTED LOOPS | | 12 | 200 |
| 2 | NESTED LOOPS | | 12 | 200 |
| 3 | TABLE ACCESS FULL | ORDER_ITEMS | 12 | 800 |
| 4 | INDEX UNIQUE SCAN | PRODUCT_PK | 1 | 40 |
| 5 | TABLE ACCESS BY ROWID | PRODUCT_INFO | 1 | 10 |
-----------------------------------------------------------------
175
176
22/07/2019
89
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the runtime plan ?
177
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select * from table(
2 dbms_xplan.display_cursor('...'));
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 16000 | 7 (0)|
| 1 | HASH JOIN | | 2000 | 16000 | 7 (0)|
| 2 | TABLE ACCESS FULL | ORDER_ITEMS | 10000 | 80000 | 7 (0)|
| 3 | TABLE ACCESS FULL | PRODUCT_INFO | 10000 | 80000 | 7 (0)|
--------------------------------------------------------------------------
178
177
178
22/07/2019
90
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> desc V$SQL
Name Null? Type
----------------------------------------- -------- ----------------------------
SQL_TEXT VARCHAR2(1000)
SQL_FULLTEXT CLOB
SQL_ID VARCHAR2(13)
SHARABLE_MEM NUMBER
PERSISTENT_MEM NUMBER
...
IS_BIND_SENSITIVE VARCHAR2(1)
IS_BIND_AWARE VARCHAR2(1)
...
IS_REOPTIMIZABLE VARCHAR2(1)
IS_RESOLVED_ADAPTIVE_PLAN VARCHAR2(1)
...
SQL>
179
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
cool
180
"dodged a bullet"
179
180
22/07/2019
91
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Adaptive Query Optimization #2
181
learn from experience
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select /*+ gather_plan_statistics */ ...
2 from order_iterms o,
3 product_info p
4 where o.sales_date > date '2017-01-01'
5 and o.amt > 50
6 and o.product_id = p.product_id
-------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 7500 | 250 |
| 1 | HASH JOIN | | 1 | 7500 | 250 |
| 2 | TABLE ACCESS FULL | ORDER_ITEMS | 1 | 7500 | 250 |
| 3 | TABLE ACCESS FULL | PRODUCT_INFO | 1 | 200 | 212 |
-------------------------------------------------------------------------
182
181
182
22/07/2019
92
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> desc V$SQL
Name Null? Type
----------------------------------------- -------- ----------------------------
SQL_TEXT VARCHAR2(1000)
SQL_FULLTEXT CLOB
SQL_ID VARCHAR2(13)
SHARABLE_MEM NUMBER
PERSISTENT_MEM NUMBER
...
IS_BIND_SENSITIVE VARCHAR2(1)
IS_BIND_AWARE VARCHAR2(1)
...
IS_REOPTIMIZABLE VARCHAR2(1)
IS_RESOLVED_ADAPTIVE_PLAN VARCHAR2(1)
...
SQL>
183
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select /*+ gather_plan_statistics */ ...
2 from order_iterms o,
3 product_info p
4 where o.sales_date > date '2017-01-01'
5 and o.amt > 50
6 and o.product_id = p.product_id
-------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 250 | 250 |
| 1 | HASH JOIN | | 1 | 250 | 250 |
| 2 | TABLE ACCESS FULL | ORDER_ITEMS | 1 | 250 | 250 |
| 3 | TABLE ACCESS FULL | PRODUCT_INFO | 1 | 212 | 212 |
-------------------------------------------------------------------------
Note:
- statistics feedback used for this statement
184
183
184
22/07/2019
93
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
seems cool
185
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
we mightta got carried away :-(
186
185
186
22/07/2019
94
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select
2 directive_id,
3 type,
4 reason
5 from DBA_SQL_PLAN_DIRECTIVES;
DIRECTIVE_ID TYPE REASON
---------------------- ----------------------- ------------------------------------
14774885086507357845 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
3814111798494124542 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
14157928931483804012 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
3273885094090423666 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
17607835686132426105 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE
28719982731298412 DYNAMIC_SAMPLING_RESULT SINGLE TABLE CARDINALITY MISESTIMATE
1591495495394657516 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
162577689324691883 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
13736133022354002565 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
7592221435558555884 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE
...
...
187
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> select ...
2 from order_iterms o,
3 product_info p
4 where o.sales_date > date '2017-01-01'
5 and o.product_id = p.product_id
Note:
- 3 Sql Plan Directives used for this statement
188
187
188
22/07/2019
95
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
same SQL ...
189
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
... different plans
190
189
190
22/07/2019
96
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
... then more different plans
191
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
... then even more different plans
192
191
192
22/07/2019
97
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
So ... this happens
193
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
upgrade to 12.1
194
193
194
22/07/2019
98
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"the plans keep changing"
199
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"um...stability?"
200
199
200
22/07/2019
99
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
just one switch
201
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
202
201
202
22/07/2019
100
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
options
203
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
optimizer_adaptive_features = false
204
203
204
22/07/2019
101
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.1 ... perhaps too aggressive
207
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.2 ... more control
208
207
208
22/07/2019
102
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
TRUE FALSE 209
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.1
patch 22652097
210
209
210
22/07/2019
103
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
which brings us to ...
211
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
upgrade scenarios
212
211
212
22/07/2019
104
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
there are only two
213
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
1) your system sucks :-)
214
upgrade to target version (12.2+)
explore full adaptive
213
214
22/07/2019
105
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
2) your system is OK
215
how can I lower risk ?
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL Plan Baselines
216
215
216
22/07/2019
106
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
apology #1
217
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
217
218
22/07/2019
107
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
rarely used
219
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
apology #2
220
evolve
accepted
repeatable
unaccepted
capture
signature
219
220
22/07/2019
108
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
close to our goals
221
working good ? Keep it
found something better ? Tell me
found something worse ? Don’t use it
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
not new
222
11g
221
222
22/07/2019
109
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
223
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| 1 | NESTED LOOPS | | 25 | 425 |
| 2 | NESTED LOOPS | | 25 | 425 |
|* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
|* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | |
| 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 |
-----------------------------------------------------------------------
select a.data, b.data
from tab1 a, tab2
where b.tab1_id = a.id
and a.code = :1
"capture" a baseline
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
224
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| 1 | NESTED LOOPS | | 25 | 425 |
| 2 | NESTED LOOPS | | 25 | 425 |
|* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
|* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | |
| 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 |
-----------------------------------------------------------------------
------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| * 1 | HASH JOIN | | 25 | 425 |
| * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
| 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 |
------------------------------------------------------------------------
select a.data, b.data
from tab1 a, tab2
where b.tab1_id = a.id
and a.code = :1
223
224
22/07/2019
110
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
225
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| 1 | NESTED LOOPS | | 25 | 425 |
| 2 | NESTED LOOPS | | 25 | 425 |
|* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
|* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | |
| 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 |
-----------------------------------------------------------------------
------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| * 1 | HASH JOIN | | 25 | 425 |
| * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
| 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 |
------------------------------------------------------------------------
select a.data, b.data
from tab1 a, tab2
where b.tab1_id = a.id
and a.code = :1
but we saved this !
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
maybe it is better ?
226
------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| * 1 | HASH JOIN | | 25 | 425 |
| * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
| 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 |
------------------------------------------------------------------------
225
226
22/07/2019
111
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
evolve a plan
227
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE
228
227
228
22/07/2019
112
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
229
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| 1 | NESTED LOOPS | | 25 | 425 |
| 2 | NESTED LOOPS | | 25 | 425 |
|* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
|* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | |
| 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 |
-----------------------------------------------------------------------
------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | |
| * 1 | HASH JOIN | | 25 | 425 |
| * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 |
| 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 |
------------------------------------------------------------------------
select a.data, b.data
from tab1 a, tab2
where b.tab1_id = a.id
and a.code = :1
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
a better "API"...
230
DBMS_SPM.I_LOVE_THIS_PLAN(sql_id)
DBMS_SPM. THIS_PLAN_SUCKS(sql_id)
DBMS_SPM. SWAP_THIS_PLAN_IN(sql_id)
DBMS_SPM. AUTO_SWAP_THIS_PLAN_IN(sql_id)
229
230
22/07/2019
113
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
so back to upgrade
231
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
2) your system is OK
232
how can I lower risk ?
231
232
22/07/2019
114
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> alter system set
2 optimizer_capture_sql_plan_baselines = true
233
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SQL> exec DBMS_SPM.PACK_STGTAB_BASELINE
(datapump)
SQL> exec DBMS_SPM.UNPACK_STGTAB_BASELINE
234
233
234
22/07/2019
115
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the same performance
235
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
236
235
236
22/07/2019
116
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
wrap up
237
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the optimizer is better
238
237
238
22/07/2019
117
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
the optimizer is different
239
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.2+ preferred
240
239
240
22/07/2019
118
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
12.1 ... backport
241
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
SPM is your friend
242
241
242
22/07/2019
119
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Thank you!
youtube bit.ly/youtube-connor
blog bit.ly/blog-connor
twitter bit.ly/twitter-connor
400+ posts mainly on database & development
250 technical videos, new uploads every week
rants and raves on tech and the world :-)
243

Weitere ähnliche Inhalte

Ähnlich wie OG Yatra - upgrading to the new 12c+ optimizer

Sangam 18 - The New Optimizer in Oracle 12c
Sangam 18 - The New Optimizer in Oracle 12cSangam 18 - The New Optimizer in Oracle 12c
Sangam 18 - The New Optimizer in Oracle 12cConnor McDonald
 
OG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsOG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsConnor McDonald
 
ITOUG 2019 - 18c, 19c features
ITOUG 2019 - 18c, 19c featuresITOUG 2019 - 18c, 19c features
ITOUG 2019 - 18c, 19c featuresConnor McDonald
 
Kscope19 - Understanding the basics of SQL processing
Kscope19 - Understanding the basics of SQL processingKscope19 - Understanding the basics of SQL processing
Kscope19 - Understanding the basics of SQL processingConnor McDonald
 
OG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersOG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersConnor McDonald
 
Melbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsMelbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsConnor McDonald
 
ITOUG 2019 - 25 years of hints and tips
ITOUG 2019 - 25 years of hints and tipsITOUG 2019 - 25 years of hints and tips
ITOUG 2019 - 25 years of hints and tipsConnor McDonald
 
OpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tipsOpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tipsConnor McDonald
 
ILOUG 2019 - 18c/19c features
ILOUG 2019 - 18c/19c featuresILOUG 2019 - 18c/19c features
ILOUG 2019 - 18c/19c featuresConnor McDonald
 
OpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 minsOpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 minsConnor McDonald
 
MySQL Performance Schema in MySQL 8.0
MySQL Performance Schema in MySQL 8.0MySQL Performance Schema in MySQL 8.0
MySQL Performance Schema in MySQL 8.0Mayank Prasad
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsConnor McDonald
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingConnor McDonald
 
ILOUG 2019 - 25 years of hints and tips
ILOUG 2019 - 25 years of hints and tipsILOUG 2019 - 25 years of hints and tips
ILOUG 2019 - 25 years of hints and tipsConnor McDonald
 
ILOUG 2019 - Autonomous, what does it mean for DBAs
ILOUG 2019 - Autonomous, what does it mean for DBAsILOUG 2019 - Autonomous, what does it mean for DBAs
ILOUG 2019 - Autonomous, what does it mean for DBAsConnor McDonald
 
18c and 19c features for DBAs
18c and 19c features for DBAs18c and 19c features for DBAs
18c and 19c features for DBAsConnor McDonald
 
Oracle Database features every developer should know about
Oracle Database features every developer should know aboutOracle Database features every developer should know about
Oracle Database features every developer should know aboutgvenzl
 
An Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problemAn Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problemJose Rodríguez
 
ILOUG 2019 - Flashback, the forgotten feature
ILOUG 2019 - Flashback, the forgotten featureILOUG 2019 - Flashback, the forgotten feature
ILOUG 2019 - Flashback, the forgotten featureConnor McDonald
 
Compile ahead of time. It's fine?
Compile ahead of time. It's fine?Compile ahead of time. It's fine?
Compile ahead of time. It's fine?Dmitry Chuyko
 

Ähnlich wie OG Yatra - upgrading to the new 12c+ optimizer (20)

Sangam 18 - The New Optimizer in Oracle 12c
Sangam 18 - The New Optimizer in Oracle 12cSangam 18 - The New Optimizer in Oracle 12c
Sangam 18 - The New Optimizer in Oracle 12c
 
OG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsOG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tips
 
ITOUG 2019 - 18c, 19c features
ITOUG 2019 - 18c, 19c featuresITOUG 2019 - 18c, 19c features
ITOUG 2019 - 18c, 19c features
 
Kscope19 - Understanding the basics of SQL processing
Kscope19 - Understanding the basics of SQL processingKscope19 - Understanding the basics of SQL processing
Kscope19 - Understanding the basics of SQL processing
 
OG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersOG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developers
 
Melbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsMelbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and Tips
 
ITOUG 2019 - 25 years of hints and tips
ITOUG 2019 - 25 years of hints and tipsITOUG 2019 - 25 years of hints and tips
ITOUG 2019 - 25 years of hints and tips
 
OpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tipsOpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tips
 
ILOUG 2019 - 18c/19c features
ILOUG 2019 - 18c/19c featuresILOUG 2019 - 18c/19c features
ILOUG 2019 - 18c/19c features
 
OpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 minsOpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 mins
 
MySQL Performance Schema in MySQL 8.0
MySQL Performance Schema in MySQL 8.0MySQL Performance Schema in MySQL 8.0
MySQL Performance Schema in MySQL 8.0
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAs
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matching
 
ILOUG 2019 - 25 years of hints and tips
ILOUG 2019 - 25 years of hints and tipsILOUG 2019 - 25 years of hints and tips
ILOUG 2019 - 25 years of hints and tips
 
ILOUG 2019 - Autonomous, what does it mean for DBAs
ILOUG 2019 - Autonomous, what does it mean for DBAsILOUG 2019 - Autonomous, what does it mean for DBAs
ILOUG 2019 - Autonomous, what does it mean for DBAs
 
18c and 19c features for DBAs
18c and 19c features for DBAs18c and 19c features for DBAs
18c and 19c features for DBAs
 
Oracle Database features every developer should know about
Oracle Database features every developer should know aboutOracle Database features every developer should know about
Oracle Database features every developer should know about
 
An Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problemAn Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problem
 
ILOUG 2019 - Flashback, the forgotten feature
ILOUG 2019 - Flashback, the forgotten featureILOUG 2019 - Flashback, the forgotten feature
ILOUG 2019 - Flashback, the forgotten feature
 
Compile ahead of time. It's fine?
Compile ahead of time. It's fine?Compile ahead of time. It's fine?
Compile ahead of time. It's fine?
 

Mehr von Connor McDonald

Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestConnor McDonald
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQLConnor McDonald
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsConnor McDonald
 
Sangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousSangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousConnor McDonald
 
Sangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesSangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesConnor McDonald
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresConnor McDonald
 
APEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousAPEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousConnor McDonald
 
APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne Connor McDonald
 
OOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsOOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsConnor McDonald
 
Latin America Tour 2019 - 18c and 19c featues
Latin America Tour 2019   - 18c and 19c featuesLatin America Tour 2019   - 18c and 19c featues
Latin America Tour 2019 - 18c and 19c featuesConnor McDonald
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - FlashbackConnor McDonald
 
Latin America Tour 2019 - 10 great sql features
Latin America Tour 2019  - 10 great sql featuresLatin America Tour 2019  - 10 great sql features
Latin America Tour 2019 - 10 great sql featuresConnor McDonald
 
Latin America Tour 2019 - slow data and sql processing
Latin America Tour 2019  - slow data and sql processingLatin America Tour 2019  - slow data and sql processing
Latin America Tour 2019 - slow data and sql processingConnor McDonald
 
APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101Connor McDonald
 
APEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLAPEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLConnor McDonald
 
APEX Connect 2019 - successful application development
APEX Connect 2019 - successful application developmentAPEX Connect 2019 - successful application development
APEX Connect 2019 - successful application developmentConnor McDonald
 
Pattern Matching with SQL - APEX World Rotterdam 2019
Pattern Matching with SQL - APEX World Rotterdam 2019Pattern Matching with SQL - APEX World Rotterdam 2019
Pattern Matching with SQL - APEX World Rotterdam 2019Connor McDonald
 

Mehr von Connor McDonald (20)

Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolest
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tips
 
Sangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousSangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on Autonomous
 
Sangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesSangam 2019 - The Latest Features
Sangam 2019 - The Latest Features
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL features
 
APEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousAPEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomous
 
APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne
 
OOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsOOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAs
 
Latin America Tour 2019 - 18c and 19c featues
Latin America Tour 2019   - 18c and 19c featuesLatin America Tour 2019   - 18c and 19c featues
Latin America Tour 2019 - 18c and 19c featues
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - Flashback
 
Latin America Tour 2019 - 10 great sql features
Latin America Tour 2019  - 10 great sql featuresLatin America Tour 2019  - 10 great sql features
Latin America Tour 2019 - 10 great sql features
 
Latin America Tour 2019 - slow data and sql processing
Latin America Tour 2019  - slow data and sql processingLatin America Tour 2019  - slow data and sql processing
Latin America Tour 2019 - slow data and sql processing
 
ANSI vs Oracle language
ANSI vs Oracle languageANSI vs Oracle language
ANSI vs Oracle language
 
KScope19 - SQL Features
KScope19 - SQL FeaturesKScope19 - SQL Features
KScope19 - SQL Features
 
APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101
 
APEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLAPEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQL
 
APEX Connect 2019 - successful application development
APEX Connect 2019 - successful application developmentAPEX Connect 2019 - successful application development
APEX Connect 2019 - successful application development
 
Pattern Matching with SQL - APEX World Rotterdam 2019
Pattern Matching with SQL - APEX World Rotterdam 2019Pattern Matching with SQL - APEX World Rotterdam 2019
Pattern Matching with SQL - APEX World Rotterdam 2019
 

Kürzlich hochgeladen

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Kürzlich hochgeladen (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

OG Yatra - upgrading to the new 12c+ optimizer

  • 1. 22/07/2019 1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Taming the beast The Optimizer in 12c and beyond Connor McDonald 1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Connor McDonald 1 2
  • 2. 22/07/2019 2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 4 3 4
  • 3. 22/07/2019 3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Stuff youtube bit.ly/youtube-connor blog bit.ly/blog-connor twitter bit.ly/twitter-connor 400+ posts mainly on database & development 250 technical videos, new uploads every week rants and raves on tech and the world :-) Copyright © 2018, Oracle and/or its affiliates. All rights reserved. etc... facebook bit.ly/facebook-connor linkedin bit.ly/linkedin-connor instagram bit.ly/instagram-connor slideshare bit.ly/slideshare-connor 5 6
  • 4. 22/07/2019 4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | In Memoriam Copyright © 2017, Oracle and/or its affiliates. All rights reserved. this session 8 7 8
  • 5. 22/07/2019 5 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3 things 9 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c(18c,19c) optimizer … better performance 10 9 10
  • 6. 22/07/2019 6 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c(18c,19c) optimizer … worse performance 11 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. worse performance … 12 11 12
  • 7. 22/07/2019 7 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14 13 14
  • 8. 22/07/2019 8 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. goal of the optimizer 15 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. simple 16 15 16
  • 9. 22/07/2019 9 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. good execution plan 17 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so how did we end up here ? 18 17 18
  • 10. 22/07/2019 10 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19 You will upgrade the database with the new optimizer features Copyright © 2017, Oracle and/or its affiliates. All rights reserved. LOL !LOL ! 20 19 20
  • 11. 22/07/2019 11 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. LMAOLMAO! 21 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. That dude is toast! That dude is toast! 22 21 22
  • 12. 22/07/2019 12 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Is this why I don't have a desk ? 23 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. lots of fear 24 23 24
  • 13. 22/07/2019 13 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. lots of mis-information 25 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. only 3 issues 26 25 26
  • 14. 22/07/2019 14 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ISSUE #1 27 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. poor statistics 28 27 28
  • 15. 22/07/2019 15 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. cardinality mis-estimates 29 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. for the optimizer... 30 29 30
  • 16. 22/07/2019 16 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ...cardinality is everything 31 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 32 31 32
  • 17. 22/07/2019 17 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 33 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 34 33 34
  • 18. 22/07/2019 18 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. better stats 35 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1) better histograms 36 35 36
  • 19. 22/07/2019 19 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2015 37 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 38 37 38
  • 20. 22/07/2019 20 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 39 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Histograms are so awesome. See how much they make me smile ! 40 39 40
  • 21. 22/07/2019 21 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 41 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. No … … honest :-) 42 41 42
  • 22. 22/07/2019 22 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the sucky part 43 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> create table T 2 as select * from dba_objects; Table created. SQL> exec dbms_stats.gather_table_stats('','T'); PL/SQL procedure successfully completed. 44 43 44
  • 23. 22/07/2019 23 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select COLUMN_NAME,NUM_DISTINCT, 2 ( select count(*) 3 from all_tab_histograms 4 where owner = a.owner 5 and table_name = a.table_name 6 and column_name = a.column_name ) 7 from all_tab_cols a 8 where table_name = 'T' 9 order by column_id; COLUMN_NAME NUM_DISTINCT HIST_CNT ------------------------------ ------------ ---------- OWNER 25 2 OBJECT_NAME 53000 2 SUBOBJECT_NAME 230 2 OBJECT_ID 90808 2 45 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 46 45 46
  • 24. 22/07/2019 24 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 47 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select intcol#,equality_preds, 2 range_preds,timestamp 3 from sys.col_usage$ 4 where obj# = (select object_id 5 from obj 6 where object_name = 'T' ); INTCOL# EQUALITY_PREDS RANGE_PREDS TIMESTAMP ---------- -------------- ----------- --------- 1 1 0 24-SEP-17 48 47 48
  • 25. 22/07/2019 25 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> exec dbms_stats.gather_table_stats('','T'); SQL> select COLUMN_NAME,NUM_DISTINCT, 2 ( select count(*) 3 from all_tab_histograms 4 where owner = a.owner 5 and table_name = a.table_name 6 and column_name = a.column_name ) 7 from all_tab_cols a 8 where table_name = 'T' 9 order by column_id; COLUMN_NAME NUM_DISTINCT HIST_CNT ------------------------------ ------------ ---------- OWNER 25 25 OBJECT_NAME 53000 2 SUBOBJECT_NAME 230 2 OBJECT_ID 90808 2 49 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. we don't know what your "app" is 50 49 50
  • 26. 22/07/2019 26 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. still requires care 51 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c improvements 52 51 52
  • 27. 22/07/2019 27 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select status, count(*) 2 from orders 3 group by status 4 order by 1; STATUS COUNT(*) -------------------- ---------- 1-New 1000 2-Assigned 10 3-InProgress 4000 4-Processed 3 5-Shipped 800 6-Received 5000 7-Completed 15000 8-Archived 20000 53 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. skewed ... 54 53 54
  • 28. 22/07/2019 28 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... need histogram 55 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 56 55 56
  • 29. 22/07/2019 29 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('','ORDERS', 3 method_opt=>'for all columns size 15'); 4 end; 5 / PL/SQL procedure successfully completed. SQL> select histogram from USER_TAB_COL_STATISTICS 2 where table_name = 'ORDERS'; HISTOGRAM --------------- FREQUENCY 57 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5), 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0) 8 from user_histograms 9 where table_name = 'ORDERS' 10 order by 1; ENDPOINT_NUMBER END_VAL FREQ --------------- -------------------- ---------- 1000 1-Nev 1000 1010 2-Ass 10 5010 3-InP 4000 5013 4-Pro 3 5813 5-Shi 800 10813 6-Rec 5000 25813 7-Com 15000 45813 8-Arc 20000 58 57 58
  • 30. 22/07/2019 30 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5), 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0) 8 from user_histograms 9 where table_name = 'ORDERS' 10 order by 1; ENDPOINT_NUMBER END_VAL FREQ --------------- -------------------- ---------- 1000 1-Nev 1000 1010 2-Ass 10 5010 3-InP 4000 5013 4-Pro 3 5813 5-Shi 800 10813 6-Rec 5000 25813 7-Com 15000 45813 8-Arc 20000 59 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5), 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0) 8 from user_histograms 9 where table_name = 'ORDERS' 10 order by 1; ENDPOINT_NUMBER END_VAL FREQ --------------- -------------------- ---------- 1000 1-Nev 1000 1010 2-Ass 10 5010 3-InP 4000 5013 4-Pro 3 5813 5-Shi 800 10813 6-Rec 5000 25813 7-Com 15000 45813 8-Arc 20000 60 59 60
  • 31. 22/07/2019 31 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from orders 2 where status = '5-Shipped'; ---------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ---------------------------------------------------- | 0 | SELECT STATEMENT | | 800 | 8800 | |* 1 | TABLE ACCESS FULL| ORDERS | 800 | 8800 | ---------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("STATUS"='5-Shipped') 61 true = 800 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so far … so good 62 61 62
  • 32. 22/07/2019 32 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. NDV > 254* 63 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('','ORDERS', 3 method_opt=>'for all columns size 6'); 4 end; 5 / PL/SQL procedure successfully completed. SQL> select histogram from USER_TAB_COL_STATISTICS 2 where table_name = 'ORDERS'; HISTOGRAM --------------- HEIGHT BALANCED 64 63 64
  • 33. 22/07/2019 33 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5), 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0) 8 from user_histograms 9 where table_name = 'ORDERS' 10 order by 1; ENDPOINT_NUMBER END_VAL BKTS --------------- -------------------- ---------- 0 1-Nev 0 1 6-Rec 1 3 7-Com 2 6 8-Arc 3 65 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 7-Complete 8-Archived 8-Archived 8-Archived 7-Complete 66 65 66
  • 34. 22/07/2019 34 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from orders 2 where status = '8-Archived'; ---------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ---------------------------------------------------- | 0 | SELECT STATEMENT | | 19089 | 205K| |* 1 | TABLE ACCESS FULL| ORDERS | 19089 | 205K| ---------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("STATUS"='8-Archived') true = 20000 67 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from orders 2 where status = '7-Completed'; ----------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------- | 0 | SELECT STATEMENT | | 15271 | 164K| |* 1 | TABLE ACCESS FULL| ORDERS | 15271 | 164K| ----------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("STATUS"='7-Completed') true = 10000 68 67 68
  • 35. 22/07/2019 35 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 69 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 7-Complete 8-Archived 8-Archived 8-Archived 7-Complete total ~ 45,000 ~7000 per bucket 70 69 70
  • 36. 22/07/2019 36 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 7-Complete 8-Archived 8-Archived 8-Archived 7-Complete total = 45,000 ~7000 per bucket 71 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from orders 2 where status = '2-Assigned'; ---------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ---------------------------------------------------- | 0 | SELECT STATEMENT | | 1273 | 14003 | |* 1 | TABLE ACCESS FULL| ORDERS | 1273 | 14003 | ---------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("STATUS"='2-Assigned') true = 3 72 71 72
  • 37. 22/07/2019 37 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. height balanced = risk 73 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g HEIGHT-BALANCED FREQUENCY 74 73 74
  • 38. 22/07/2019 38 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Footnote: 11g FOR ALL COLUMNS SIZE 254AUTO 75 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 76 75 76
  • 39. 22/07/2019 39 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('','ORDERS', 3 method_opt=>'for all columns size 15'); 4 end; 5 / PL/SQL procedure successfully completed. SQL> select histogram from USER_TAB_COL_STATISTICS 2 where table_name = 'ORDERS'; HISTOGRAM --------------- FREQUENCY 77 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('','ORDERS', 3 method_opt=>'for all columns size 6'); 4 end; 5 / PL/SQL procedure successfully completed. SQL> select histogram from USER_TAB_COL_STATISTICS 2 where table_name = 'ORDERS'; HISTOGRAM --------------- TOP-FREQUENCY 78 77 78
  • 40. 22/07/2019 40 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5) 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0) 8 from user_histograms 9 where table_name = 'ORDERS' 10 order by 1; ENDPOINT_NUMBER END_VAL FREQ --------------- -------------------- ---------- 1000 1-Nev 1000 5000 3-InP 4000 5800 5-Shi 800 10800 6-Rec 5000 25800 7-Com 15000 45800 8-Arc 20000 79 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 3-InProgress 5-Shipped 8-Archived 7-Completed 80 79 80
  • 41. 22/07/2019 41 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. other values ? 81 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select num_rows 2 from user_tables 3 where table_name = 'ORDERS'; NUM_ROWS ---------- 45813 SQL> select num_distinct 2 from user_tab_cols 3 where table_name = 'ORDERS'; NUM_DISTINCT ------------ 8 FREQ ---------- 800 1000 4000 5000 15000 20000 ======== 45800 ( 45813 - 45800 ) / ( 8 - 6 ) = 7 approx. 82 81 82
  • 42. 22/07/2019 42 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from orders 2 where status = '4-Processed'; ----------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------- | 0 | SELECT STATEMENT | | 7 | 77 | |* 1 | TABLE ACCESS FULL| ORDERS | 7 | 77 | ----------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("STATUS"='4-Processed') 83 true = 3 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. what defines "TOP" ? % top values > ((bkt-1)/bkt)* num rows eg 1000 rows/ 12 buckets 12 most frequent > 11/12 * 1000 ~920 rows 84 83 84
  • 43. 22/07/2019 43 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. what if TOP FREQ not possible ? 85 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert into orders select '1-New' 2 from dual connect by level <= 9000; SQL> insert into orders select '2-Assigned' 2 from dual connect by level <= 9500; SQL> insert into orders select '3-InProgress' 2 from dual connect by level <= 6000; SQL> insert into orders select '4-Processed' 2 from dual connect by level <= 9500; SQL> insert into orders select '5-Shipped' 2 from dual connect by level <= 8800; SQL> insert into orders select '6-Received' 2 from dual connect by level <= 4500; 86 85 86
  • 44. 22/07/2019 44 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select status, count(*) 2 from orders 3 group by status 4 order by 1; STATUS COUNT(*) -------------------- ---------- 1-New 10000 2-Assigned 9510 3-InProgress 10000 4-Processed 9503 5-Shipped 9600 6-Received 9500 7-Completed 15000 8-Archived 20000 87 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('','ORDERS', 3 method_opt=>'for all columns size 6'); 4 end; 5 / PL/SQL procedure successfully completed. SQL> select histogram from USER_TAB_COL_STATISTICS 2 where table_name = 'ORDERS'; HISTOGRAM --------------- HYBRID 88 87 88
  • 45. 22/07/2019 45 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 endpoint_number, 3 substr( 4 utl_raw.cast_to_varchar2( 5 hextoraw(to_char(endpoint_value,rpad('FM',65,'X')))),1,5), 6 endpoint_number - 7 nvl(lag(endpoint_number) over ( order by endpoint_number),0), 8 endpoint_repeat_count 9 from user_histograms 10 where table_name = 'ORDERS' 11 order by 1; ENDPOINT_NUMBER END_VAL FREQ ENDPOINT_REPEAT_COUNT --------------- -------------------- ---------- --------------------- 590 1-Nev 590 590 1743 3-InP 1153 626 2314 4-Pro 571 571 2895 5-Shi 581 581 3449 6-Rec 554 554 5547 8-Arc 2098 1213 89 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 3-InProgress 5-Shipped 8-Archived 4-Processed ENDPOINT_NUMBER END_VAL FREQ ENDPOINT_REPEAT_COUNT --------------- -------------------- ---------- --------------------- 590 1-Nev 590 590 1743 3-InP 1153 626 2314 4-Pro 571 571 2895 5-Shi 581 581 3449 6-Rec 554 554 5547 8-Arc 2098 1213 2-Assigned 590 1153 571 581 554 2098 7-Completed 90 89 90
  • 46. 22/07/2019 46 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 8-Archived END_VAL FREQ ENDPOINT_REPEAT_COUNT ---------------- -------- --------------------- 1-Nev 590 590 3-InP 1153 626 4-Pro 571 571 5-Shi 581 581 6-Rec 554 554 8-Arc 2098 1213 2098 7-Completed "Bucket 8-Arc has 2098 occurrences... 1213 of them are the end value 8-Arc... therefore 885 are > 6-Rec and < 8-Arc" 91 6-Received 554 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. key point 92 91 92
  • 47. 22/07/2019 47 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must use auto sample size 93 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. still need care 94 93 94
  • 48. 22/07/2019 48 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. representative values 95 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 96 95 96
  • 49. 22/07/2019 49 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "today's hot deals" 97 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. gather statistics SQL> select count(*) 2 from items 3 where type = 'HOT_DEAL' COUNT(*) ---------- 0 98 97 98
  • 50. 22/07/2019 50 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. activate "deals of the day" SQL> update items 2 set type = 'HOT_DEAL' 3 where ... SQL> select count(*) 2 from items 3 where type = 'HOT_DEAL' COUNT(*) ---------- 14 99 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. biggest day of the year ! SQL> select min(discount) 2 from items 3 where type = 'HOT_DEAL' 4 and ... where type = 'HOT_DEAL' SQL> select free_shipping 2 from items 3 where hot_deal = 'Y' 4 and ... SQL> select ... 2 from ... SQL> select ... 2 from ... SQL> select ... 2 from ... SQL> select ... 2 from ...SQL> select ... 2 from ... 100 99 100
  • 51. 22/07/2019 51 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 101 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. where type = 'HOT_DEAL' 102 101 102
  • 52. 22/07/2019 52 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ? 103 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select type, count(*) 2 from items 3 group by type; TYPE COUNT(*) ------ ---------- ORDERED 21353 ARCHIVED 4213 NORMAL 513234 SOLD 528291 HOT_DEAL 0 104 103 104
  • 53. 22/07/2019 53 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. normally... 105 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "ok... I got 1,000,000 total rows... and 7 distinct values. I got a frequency histogram... There's 4 popular values, adding up to 910,000 That's 3 values left to cover the rest 3 / ( 1,000,000 - 910,000) = density ... I'm done!" 106 105 106
  • 54. 22/07/2019 54 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for ITEMS[ITEMS] SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE kkecdn: Single Table Predicate:"ITEMS"."TYPE"='HOT_DEAL' Column (#2): NewDensity:0.001974, OldDensity:0.000000 BktCnt:1067091.000000, PopBktCnt:1067091.000000, PopValCnt:4, NDV:4 Column (#2): TYPE(VARCHAR2) AvgLen: 7 NDV: 4 Nulls: 0 Density: 0.001974 Histogram: Freq #Bkts: 4 UncompBkts: 1067091 EndPtVals: 4 Using density: 0.001974 of col #2 as selectivity of pred having unreasonably low value NDV / (1067091 - 1067091)"Aaggghhhh!" 107 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from items where type = 'HOT_DEAL'; ---------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ---------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2107 | 23177 | 672 (2)| |* 1 | TABLE ACCESS FULL| ITEMS | 2107 | 23177 | 672 (2)| ---------------------------------------------------------------- TYPE COUNT(*) ------ ---------- ORDERED 21353 ARCHIVED 4213 * 50% NORMAL 513234 SOLD 528291 108 107 108
  • 55. 22/07/2019 55 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. representative values ... at the right time 109 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19c can help 110 109 110
  • 56. 22/07/2019 56 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 111 TIME Gather Stale overnight Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 112 Conventional DML Real-time Statistics Gathered Statistics Lightweight Statistics 111 112
  • 57. 22/07/2019 57 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 113 real time statistics DML driven Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 114 cheap essential statistics high/low values row/column counts 113 114
  • 58. 22/07/2019 58 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 115 lightweight rapid gather resource manager Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) extensions 116 115 116
  • 59. 22/07/2019 59 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 117 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "extended" statistics 118 117 118
  • 60. 22/07/2019 60 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. aka "column groups" 119 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * 2 from ADDRESS 3 where CITY = 'Pune' 4 and COUNTRY = 'Australia'; 120 119 120
  • 61. 22/07/2019 61 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 121 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. automatic column groups 122 121 122
  • 62. 22/07/2019 62 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 123 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. expression tracking 124 123 124
  • 63. 22/07/2019 63 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select s.item_id, 2 s.category, 3 stddev(p.prod_list_price - p.prod_min_price) 4 from products p, 5 sales s 6 where ....; SQL> select expression_text, evaluation_count, fixed_cost 2 from user_expression_statistics 2 where table_name = 'PRODUCTS'; EXPRESSION_TEXT EVALUATION_COUNT FIXED_COST ------------------------------------------ ---------------- ---------- STDDEV("PROD_LIST_PRICE"-"PROD_MIN_PRICE") 766 .000041667 125 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ISSUE #2 126 125 126
  • 64. 22/07/2019 64 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing statistics 127 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3) online gather 128 127 128
  • 65. 22/07/2019 65 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the old problem 129 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert /*+ APPEND */ into MY_TABLE 2 select * 3 from MY_WHOPPING_GREAT_FAT_TABLE;MY_WHOPPING_GREAT_FAT_TABLE; 130 129 130
  • 66. 22/07/2019 66 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 131 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 132 131 132
  • 67. 22/07/2019 67 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 133 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 134 133 134
  • 68. 22/07/2019 68 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 135 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert /*+ APPEND */ into MY_TABLE 2 select * 3 from MY_WHOPPING_GREAT_FAT_TABLE 4 ... 7102984123 rows created. Elapsed: 06:12:34.00 136 135 136
  • 69. 22/07/2019 69 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. and then... 137 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select num_rows from user_tables 2 where table_name = 'MY_TABLE'; NUM_ROWS ---------- 138 137 138
  • 70. 22/07/2019 70 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so ... 139 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> begin 2 dbms_stats.gather_table_stats('', 3 tname=>'MY_TABLE' 4 ... 5 ... 6 end; 7 / Elapsed: a really long time 140 139 140
  • 71. 22/07/2019 71 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 141 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. on load 142 141 142
  • 72. 22/07/2019 72 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> create table T ( 2 ... 3 ... 4 ... ); Table created. SQL> select num_rows 2 from user_tables 3 where table_name = 'T'; NUM_ROWS ---------- SQL> insert /*+ APPEND */ into T 2 select * from dba_objects; 78876 rows created. SQL> commit; Commit complete. SQL> select num_rows 2 from user_tables 3 where table_name = 'T'; NUM_ROWS ---------- 78876 143 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. things to note 144 143 144
  • 73. 22/07/2019 73 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must be direct insert /*+ APPEND */ create table as select 145 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert into T select * from dba_objects; 78876 rows created. SQL> commit; SQL> select num_rows from user_tables 2 where table_name = 'T'; NUM_ROWS ---------- 146 145 146
  • 74. 22/07/2019 74 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must be empty 147 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert into T values (....) 1 row created. SQL> insert /*+ APPEND */ into T select * from dba_objects; 78876 rows created. SQL> commit; SQL> select num_rows from user_tables 2 where table_name = 'T'; NUM_ROWS ---------- 148 147 148
  • 75. 22/07/2019 75 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. execution plan 149 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> insert /*+ APPEND */ into T 2 select * from t_src; ------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ------------------------------------------------------------------------------ | 0 | INSERT STATEMENT | | 78877 | 9M| 428 (1)| | 1 | LOAD AS SELECT | T | | | | | 2 | OPTIMIZER STATISTICS GATHERING | | 78877 | 9M| 428 (1)| | 3 | TABLE ACCESS FULL | T_SRC| 78877 | 9M| 428 (1)| ------------------------------------------------------------------------------ 150 149 150
  • 76. 22/07/2019 76 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select column_name, notes 2 from user_tab_col_statistics 3 where table_name = 'T'; COLUMN_NAME NOTES ------------------------------ ------------- OWNER STATS_ON_LOAD OBJECT_NAME STATS_ON_LOAD SUBOBJECT_NAME STATS_ON_LOAD OBJECT_ID STATS_ON_LOAD DATA_OBJECT_ID STATS_ON_LOAD OBJECT_TYPE STATS_ON_LOAD CREATED STATS_ON_LOAD ... ... 151 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. note: no histograms 152 151 152
  • 77. 22/07/2019 77 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3) session GTT 153 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 154 153 154
  • 78. 22/07/2019 78 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> create global temporary table t ( x int, y int ) 2 on commit preserve rows; Table created. SQL> insert into t 2 select rownum, rownum 3 from dual 4 connect by level <= 10000; 10000 rows created. 155 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> exec dbms_stats.gather_table_stats('','T'); SQL> set autotrace traceonly explain SQL> select * from t; --------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)| | 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)| --------------------------------------------------------------- 156 155 156
  • 79. 22/07/2019 79 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. looks great until ... 157 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> connect scott/tiger Connected. SQL> select count(*) from t; COUNT(*) ---------- 0 SQL> set autotrace traceonly explain SQL> select * from t; --------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)| | 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)| --------------------------------------------------------------- 158 157 158
  • 80. 22/07/2019 80 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 159 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> create global temporary table t ( x int, y int ) 2 on commit preserve rows; Table created. SQL> insert into t 2 select rownum, rownum 3 from dual 4 connect by level <= 10000; 10000 rows created. 160 159 160
  • 81. 22/07/2019 81 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> exec dbms_stats.gather_table_stats('','T'); SQL> set autotrace traceonly explain SQL> select * from t; --------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)| | 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)| --------------------------------------------------------------- as before 161 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> connect scott/tiger Connected. SQL> insert into t 2 select rownum, rownum 3 from dual 4 connect by level <= 50; 50 rows created. SQL> exec dbms_stats.gather_table_stats('','T'); 162 161 162
  • 82. 22/07/2019 82 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> set autotrace traceonly explain SQL> select * from t; --------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------- | 0 | SELECT STATEMENT | | 50 | 300 | 2 (0)| | 1 | TABLE ACCESS FULL| T | 50 | 300 | 2 (0)| --------------------------------------------------------------- Note ----- - Global temporary table session private statistics used 163 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. back to session 1 164 163 164
  • 83. 22/07/2019 83 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> set autotrace traceonly explain SQL> select * from t; --------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10000 | 80000 | 7 (0)| | 1 | TABLE ACCESS FULL| T | 10000 | 80000 | 7 (0)| --------------------------------------------------------------- 165 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. note: default = private 166 165 166
  • 84. 22/07/2019 84 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so far ... 167 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing or poor statistics 168 167 168
  • 85. 22/07/2019 85 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. improved optimizer "inputs" histograms gather on load private GTT 169 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ISSUE #3 170 169 170
  • 86. 22/07/2019 86 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. statistics needed = "∞" 171 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. learn ... as we go 172 171 172
  • 87. 22/07/2019 87 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. learn ... from experiences 173 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Adaptive Query Optimization #1 174 learn as we go 173 174
  • 88. 22/07/2019 88 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> set autotrace traceonly explain SQL> select ... 2 from order_iterms o, 3 product_info p 4 where o.unit_price = 15 5 and o.quantity > 1 6 and o.product_id = p.product_id ----------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------- | 0 | SELECT STATEMENT | | 12 | 200 | | 1 | NESTED LOOPS | | 12 | 200 | | 2 | NESTED LOOPS | | 12 | 200 | | 3 | TABLE ACCESS FULL | ORDER_ITEMS | 12 | 800 | | 4 | INDEX UNIQUE SCAN | PRODUCT_PK | 1 | 40 | | 5 | TABLE ACCESS BY ROWID | PRODUCT_INFO | 1 | 10 | ----------------------------------------------------------------- 175 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 176 what if there's 5000 ? 4 where o.unit_price = 15 5 and o.quantity > 1 6 and o.product_id = p.product_id ----------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------- | 0 | SELECT STATEMENT | | 12 | 200 | | 1 | NESTED LOOPS | | 12 | 200 | | 2 | NESTED LOOPS | | 12 | 200 | | 3 | TABLE ACCESS FULL | ORDER_ITEMS | 12 | 800 | | 4 | INDEX UNIQUE SCAN | PRODUCT_PK | 1 | 40 | | 5 | TABLE ACCESS BY ROWID | PRODUCT_INFO | 1 | 10 | ----------------------------------------------------------------- 175 176
  • 89. 22/07/2019 89 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the runtime plan ? 177 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * from table( 2 dbms_xplan.display_cursor('...')); -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2000 | 16000 | 7 (0)| | 1 | HASH JOIN | | 2000 | 16000 | 7 (0)| | 2 | TABLE ACCESS FULL | ORDER_ITEMS | 10000 | 80000 | 7 (0)| | 3 | TABLE ACCESS FULL | PRODUCT_INFO | 10000 | 80000 | 7 (0)| -------------------------------------------------------------------------- 178 177 178
  • 90. 22/07/2019 90 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> desc V$SQL Name Null? Type ----------------------------------------- -------- ---------------------------- SQL_TEXT VARCHAR2(1000) SQL_FULLTEXT CLOB SQL_ID VARCHAR2(13) SHARABLE_MEM NUMBER PERSISTENT_MEM NUMBER ... IS_BIND_SENSITIVE VARCHAR2(1) IS_BIND_AWARE VARCHAR2(1) ... IS_REOPTIMIZABLE VARCHAR2(1) IS_RESOLVED_ADAPTIVE_PLAN VARCHAR2(1) ... SQL> 179 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. cool 180 "dodged a bullet" 179 180
  • 91. 22/07/2019 91 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Adaptive Query Optimization #2 181 learn from experience Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select /*+ gather_plan_statistics */ ... 2 from order_iterms o, 3 product_info p 4 where o.sales_date > date '2017-01-01' 5 and o.amt > 50 6 and o.product_id = p.product_id ------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | ------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 7500 | 250 | | 1 | HASH JOIN | | 1 | 7500 | 250 | | 2 | TABLE ACCESS FULL | ORDER_ITEMS | 1 | 7500 | 250 | | 3 | TABLE ACCESS FULL | PRODUCT_INFO | 1 | 200 | 212 | ------------------------------------------------------------------------- 182 181 182
  • 92. 22/07/2019 92 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> desc V$SQL Name Null? Type ----------------------------------------- -------- ---------------------------- SQL_TEXT VARCHAR2(1000) SQL_FULLTEXT CLOB SQL_ID VARCHAR2(13) SHARABLE_MEM NUMBER PERSISTENT_MEM NUMBER ... IS_BIND_SENSITIVE VARCHAR2(1) IS_BIND_AWARE VARCHAR2(1) ... IS_REOPTIMIZABLE VARCHAR2(1) IS_RESOLVED_ADAPTIVE_PLAN VARCHAR2(1) ... SQL> 183 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select /*+ gather_plan_statistics */ ... 2 from order_iterms o, 3 product_info p 4 where o.sales_date > date '2017-01-01' 5 and o.amt > 50 6 and o.product_id = p.product_id ------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | ------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 250 | 250 | | 1 | HASH JOIN | | 1 | 250 | 250 | | 2 | TABLE ACCESS FULL | ORDER_ITEMS | 1 | 250 | 250 | | 3 | TABLE ACCESS FULL | PRODUCT_INFO | 1 | 212 | 212 | ------------------------------------------------------------------------- Note: - statistics feedback used for this statement 184 183 184
  • 93. 22/07/2019 93 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. seems cool 185 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. we mightta got carried away :-( 186 185 186
  • 94. 22/07/2019 94 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select 2 directive_id, 3 type, 4 reason 5 from DBA_SQL_PLAN_DIRECTIVES; DIRECTIVE_ID TYPE REASON ---------------------- ----------------------- ------------------------------------ 14774885086507357845 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 3814111798494124542 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 14157928931483804012 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 3273885094090423666 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 17607835686132426105 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE 28719982731298412 DYNAMIC_SAMPLING_RESULT SINGLE TABLE CARDINALITY MISESTIMATE 1591495495394657516 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 162577689324691883 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 13736133022354002565 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE 7592221435558555884 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE ... ... 187 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select ... 2 from order_iterms o, 3 product_info p 4 where o.sales_date > date '2017-01-01' 5 and o.product_id = p.product_id Note: - 3 Sql Plan Directives used for this statement 188 187 188
  • 95. 22/07/2019 95 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. same SQL ... 189 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... different plans 190 189 190
  • 96. 22/07/2019 96 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... then more different plans 191 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... then even more different plans 192 191 192
  • 97. 22/07/2019 97 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. So ... this happens 193 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. upgrade to 12.1 194 193 194
  • 98. 22/07/2019 98 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "the plans keep changing" 199 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "um...stability?" 200 199 200
  • 99. 22/07/2019 99 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. just one switch 201 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 202 201 202
  • 100. 22/07/2019 100 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. options 203 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. optimizer_adaptive_features = false 204 203 204
  • 101. 22/07/2019 101 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 ... perhaps too aggressive 207 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 ... more control 208 207 208
  • 102. 22/07/2019 102 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. TRUE FALSE 209 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 patch 22652097 210 209 210
  • 103. 22/07/2019 103 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. which brings us to ... 211 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. upgrade scenarios 212 211 212
  • 104. 22/07/2019 104 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. there are only two 213 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1) your system sucks :-) 214 upgrade to target version (12.2+) explore full adaptive 213 214
  • 105. 22/07/2019 105 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) your system is OK 215 how can I lower risk ? Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL Plan Baselines 216 215 216
  • 106. 22/07/2019 106 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. apology #1 217 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 217 218
  • 107. 22/07/2019 107 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. rarely used 219 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. apology #2 220 evolve accepted repeatable unaccepted capture signature 219 220
  • 108. 22/07/2019 108 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. close to our goals 221 working good ? Keep it found something better ? Tell me found something worse ? Don’t use it Copyright © 2017, Oracle and/or its affiliates. All rights reserved. not new 222 11g 221 222
  • 109. 22/07/2019 109 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 223 ----------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | | 1 | NESTED LOOPS | | 25 | 425 | | 2 | NESTED LOOPS | | 25 | 425 | |* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 | |* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | | | 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 | ----------------------------------------------------------------------- select a.data, b.data from tab1 a, tab2 where b.tab1_id = a.id and a.code = :1 "capture" a baseline Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 224 ----------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | | 1 | NESTED LOOPS | | 25 | 425 | | 2 | NESTED LOOPS | | 25 | 425 | |* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 | |* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | | | 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 | ----------------------------------------------------------------------- ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | | * 1 | HASH JOIN | | 25 | 425 | | * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 | | 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 | ------------------------------------------------------------------------ select a.data, b.data from tab1 a, tab2 where b.tab1_id = a.id and a.code = :1 223 224
  • 110. 22/07/2019 110 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 225 ----------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | | 1 | NESTED LOOPS | | 25 | 425 | | 2 | NESTED LOOPS | | 25 | 425 | |* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 | |* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | | | 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 | ----------------------------------------------------------------------- ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | | * 1 | HASH JOIN | | 25 | 425 | | * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 | | 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 | ------------------------------------------------------------------------ select a.data, b.data from tab1 a, tab2 where b.tab1_id = a.id and a.code = :1 but we saved this ! Copyright © 2017, Oracle and/or its affiliates. All rights reserved. maybe it is better ? 226 ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | | * 1 | HASH JOIN | | 25 | 425 | | * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 | | 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 | ------------------------------------------------------------------------ 225 226
  • 111. 22/07/2019 111 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. evolve a plan 227 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE 228 227 228
  • 112. 22/07/2019 112 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 229 ----------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | ----------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | | 1 | NESTED LOOPS | | 25 | 425 | | 2 | NESTED LOOPS | | 25 | 425 | |* 3 | TABLE ACCESS FULL | TAB1 | 1 | 11 | |* 4 | INDEX RANGE SCAN | TAB2_TAB1_FKI | 25 | | | 5 | TABLE ACCESS BY INDEX ROWID| TAB2 | 25 | 150 | ----------------------------------------------------------------------- ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | | * 1 | HASH JOIN | | 25 | 425 | | * 5 | TABLE ACCESS FULL | TAB1 | 1 | 11 | | 8 | TABLE ACCESS FULL | TAB2 | 25 | 150 | ------------------------------------------------------------------------ select a.data, b.data from tab1 a, tab2 where b.tab1_id = a.id and a.code = :1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. a better "API"... 230 DBMS_SPM.I_LOVE_THIS_PLAN(sql_id) DBMS_SPM. THIS_PLAN_SUCKS(sql_id) DBMS_SPM. SWAP_THIS_PLAN_IN(sql_id) DBMS_SPM. AUTO_SWAP_THIS_PLAN_IN(sql_id) 229 230
  • 113. 22/07/2019 113 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so back to upgrade 231 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) your system is OK 232 how can I lower risk ? 231 232
  • 114. 22/07/2019 114 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> alter system set 2 optimizer_capture_sql_plan_baselines = true 233 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> exec DBMS_SPM.PACK_STGTAB_BASELINE (datapump) SQL> exec DBMS_SPM.UNPACK_STGTAB_BASELINE 234 233 234
  • 115. 22/07/2019 115 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the same performance 235 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 236 235 236
  • 116. 22/07/2019 116 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. wrap up 237 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the optimizer is better 238 237 238
  • 117. 22/07/2019 117 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the optimizer is different 239 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2+ preferred 240 239 240
  • 118. 22/07/2019 118 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 ... backport 241 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SPM is your friend 242 241 242
  • 119. 22/07/2019 119 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Thank you! youtube bit.ly/youtube-connor blog bit.ly/blog-connor twitter bit.ly/twitter-connor 400+ posts mainly on database & development 250 technical videos, new uploads every week rants and raves on tech and the world :-) 243