Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sangam 18 - The New Optimizer in Oracle 12c

111 views

Published on

The 12c optimizer has a vast array of improvements, but of course, functionality changes means that your SQL plans might also change when you upgrade. This slidedeck covers what has changed, and how to ensure better more stable performance when you upgrade.

Published in: Technology
  • Be the first to comment

Sangam 18 - The New Optimizer in Oracle 12c

  1. 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Taming the beast The 12c Optimizer Connor McDonald 1
  2. 2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Connor McDonald
  3. 3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 3
  4. 4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 4
  5. 5. 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 :-)
  6. 6. 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
  7. 7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7https://asktom.oracle.com
  8. 8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. https://asktom.oracle.com/officehours
  9. 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 150 hours free access so far 9
  10. 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. this session 10
  11. 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3 things 11
  12. 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c optimizer … better performance 12
  13. 13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c optimizer … worse performance 13
  14. 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. worse performance … 14
  15. 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15
  16. 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16
  17. 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. goal of the optimizer 17
  18. 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. simple 18
  19. 19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. good execution plan 19
  20. 20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so how did we end up here ? 20
  21. 21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 21 You will upgrade us to 12c with its new optimizer
  22. 22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. LOL !LOL ! 22
  23. 23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. LMAOLMAO! 23
  24. 24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. That dude is toast! That dude is toast! 24
  25. 25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Is this why I don't have a desk ? 25
  26. 26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. lots of fear 26
  27. 27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. lots of mis-information 27
  28. 28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. only 2 issues 28
  29. 29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ISSUE #1 29
  30. 30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing or poor statistics 30
  31. 31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. cardinality mis-estimates 31
  32. 32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. cardinality is everything 32
  33. 33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 33
  34. 34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. better stats 34
  35. 35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing or poor statistics 35
  36. 36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1) better histograms 36
  37. 37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2015 37
  38. 38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 38
  39. 39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 39
  40. 40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Histograms are so awesome. See how much they make me smile ! 40
  41. 41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 41
  42. 42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. No … … honest :-) 42
  43. 43. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the sucky part 43
  44. 44. 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
  45. 45. 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
  46. 46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 46
  47. 47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 47
  48. 48. 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
  49. 49. 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
  50. 50. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. we don't know what your "app" is 50
  51. 51. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. still requires care 51
  52. 52. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c improvements 52
  53. 53. 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
  54. 54. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. skewed ... 54
  55. 55. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... need histogram 55
  56. 56. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 56
  57. 57. 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
  58. 58. 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
  59. 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 59
  60. 60. 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
  61. 61. 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
  62. 62. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so far … so good 62
  63. 63. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. NDV > 254* 63
  64. 64. 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
  65. 65. 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
  66. 66. 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
  67. 67. 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
  68. 68. 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
  69. 69. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 69
  70. 70. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 7-Complete 8-Archived 8-Archived 8-Archived 7-Completetotal ~ 45,000 ~7000 per bucket 70
  71. 71. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 7-Complete 8-Archived 8-Archived 8-Archived 7-Completetotal = 45,000 ~7000 per bucket 71
  72. 72. 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
  73. 73. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. height balanced means ... 73
  74. 74. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. bucket spanning = dramas 74
  75. 75. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. bucket swallowing = dramas 75
  76. 76. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g HEIGHT-BALANCED FREQUENCY 76
  77. 77. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Footnote: 11g FOR ALL COLUMNS SIZE 25AUTO 77
  78. 78. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 78
  79. 79. 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 79
  80. 80. 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 80
  81. 81. 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 81
  82. 82. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-New 6-Received 3-InProgress 5-Shipped 8-Archived 7-Completed 82
  83. 83. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. other values ? 83
  84. 84. 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 ) = 84
  85. 85. 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') 85 true = 3
  86. 86. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. what defines "TOP" % top values > ((bkt-1)/bkt)* num eg 1000 rows/ 12 bucket 12 most frequent > 11/12 * 1000 ~920 rows 86
  87. 87. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. what if TOP FREQ not possible ? 87
  88. 88. 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; 88
  89. 89. 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 89
  90. 90. 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 90
  91. 91. 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 91
  92. 92. 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 92
  93. 93. 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" 93 6-Received 554
  94. 94. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. no bucket spanning 94
  95. 95. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. key point 95
  96. 96. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must use auto sample size 96
  97. 97. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. still need care 97
  98. 98. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. representative values 98
  99. 99. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "today's hot deals" 99
  100. 100. 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 100
  101. 101. 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 101
  102. 102. 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 ... 102
  103. 103. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. where type = 'HOT_DEAL' 103
  104. 104. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 104
  105. 105. 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 105
  106. 106. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. normally... 106
  107. 107. 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!" 107
  108. 108. 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!" 108
  109. 109. 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 109
  110. 110. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. representative values ... at the right time 110
  111. 111. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) extensions 111
  112. 112. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 112
  113. 113. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "extended" statistics 113
  114. 114. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. aka "column groups" 114
  115. 115. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. solve GIGO 115
  116. 116. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select * 2 from ADDRESS 3 where CITY = 'Bangalore' 4 and COUNTRY = 'Australia'; 116
  117. 117. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 117
  118. 118. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. automatic column groups 118
  119. 119. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 119
  120. 120. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. expression tracking 120
  121. 121. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select s.item_id, 2 s.category, 3 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 ---------------------------------- ---------------- ---------- "PROD_LIST_PRICE"-"PROD_MIN_PRICE" 766 .000041667 121
  122. 122. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing or poor statistics 122
  123. 123. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3) online gather 123
  124. 124. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the old problem 124
  125. 125. 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; 125
  126. 126. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 126
  127. 127. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 127
  128. 128. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 128
  129. 129. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 129
  130. 130. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 130
  131. 131. 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 131
  132. 132. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. and then... 132
  133. 133. 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 ---------- 133
  134. 134. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so ... 134
  135. 135. 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 bloody long time 135
  136. 136. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 136
  137. 137. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. on load 137
  138. 138. 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 138
  139. 139. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. things to note 139
  140. 140. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must be direct insert /*+ APPEND */ create table as select 140
  141. 141. 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 ---------- 141
  142. 142. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. must be empty 142
  143. 143. 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 ---------- 143
  144. 144. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. execution plan 144
  145. 145. 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)| ------------------------------------------------------------------------------ 145
  146. 146. 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 ... ... 146
  147. 147. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. note: no histograms 147
  148. 148. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3) session GTT 148
  149. 149. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11g 149
  150. 150. 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. 150
  151. 151. 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)| --------------------------------------------------------------- 151
  152. 152. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. looks great until ... 152
  153. 153. 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)| --------------------------------------------------------------- 153
  154. 154. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 154
  155. 155. 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
  156. 156. 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 156
  157. 157. 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'); 157
  158. 158. 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 158
  159. 159. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. back to session 1 159
  160. 160. 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)| --------------------------------------------------------------- 160
  161. 161. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. note: default = private 161
  162. 162. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so far ... 162
  163. 163. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. missing or poor statistics 163
  164. 164. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. improved optimizer "inputs" histograms gather on load private GTT 164
  165. 165. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ISSUE #2 165
  166. 166. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. statistics needed = "∞" 166
  167. 167. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. learn ... as we go 167
  168. 168. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. learn ... from experiences 168
  169. 169. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Adaptive Query Optimization #1 169 learn as we go
  170. 170. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "Find all of the products with a unit price of 15 that we have sold more than 1 unit" 170
  171. 171. 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 | ----------------------------------------------------------------- 171
  172. 172. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 172 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 | -----------------------------------------------------------------
  173. 173. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the runtime plan ? 173
  174. 174. 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)| -------------------------------------------------------------------------- 174
  175. 175. 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> 175
  176. 176. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. cool 176 "dodged a bullet"
  177. 177. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Adaptive Query Optimization #2 177 learn from experience
  178. 178. 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 | ------------------------------------------------------------------------- 178
  179. 179. 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
  180. 180. 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 180
  181. 181. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. seems cool 181
  182. 182. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. we mightta got carried away :-( 182
  183. 183. 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 ... ... 183
  184. 184. 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 184
  185. 185. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. same SQL ... 185
  186. 186. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... different plans 186
  187. 187. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... then more different plans 187
  188. 188. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... then even more different plans 188
  189. 189. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. So ... this happens 189
  190. 190. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. upgrade to 12.1 190
  191. 191. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. new optimizations 191
  192. 192. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. new statistics 192
  193. 193. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. default = everything enabled 193
  194. 194. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. "the plans keep changing" 194
  195. 195. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. just one switch 195
  196. 196. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 196
  197. 197. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. options 197
  198. 198. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. optimizer_adaptive_features = false 198
  199. 199. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. optimizer_features_enable = 11.2.0.4 199
  200. 200. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. don't panic ... that's fine endorsed 200
  201. 201. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 ... perhaps too aggressive 201
  202. 202. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 ... more control 202
  203. 203. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 ... more passive 203
  204. 204. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. TRUE FALSE 204
  205. 205. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2 ... much better 205
  206. 206. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 patch 22652097 206
  207. 207. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. which brings us to ... 207
  208. 208. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. upgrade scenarios 208
  209. 209. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. there are only two 209
  210. 210. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1) your system sucks :-) 210 upgrade to 12.2 explore full adaptive
  211. 211. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) your system is OK 211 how can I lower risk ?
  212. 212. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL Plan Baselines 212
  213. 213. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. apology #1 213
  214. 214. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 214
  215. 215. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. but ultimately ... 215
  216. 216. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. ... just three things 216
  217. 217. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. try run stuff good 1 217
  218. 218. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. when it's running good … 2 218
  219. 219. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. … keep running it like that ! 219
  220. 220. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. If there is something better 220 3
  221. 221. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. then tell me 221
  222. 222. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 222
  223. 223. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 223
  224. 224. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. baselines do exactly this ! 224
  225. 225. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. rarely used 225
  226. 226. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. apology #2 226 evolve accepted repeatable unaccepted capture signature
  227. 227. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. close to our goals 227 working good ? Keep it found something better ? Tell me found something worse ? Don’t use it
  228. 228. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. not new 228 11g
  229. 229. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. (quick) terminology 229
  230. 230. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. signature 230
  231. 231. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> select sal from emp where empno = ... SQL> select SAL from EMP where EMPNO = ... SQL> SELECT /* comment */ SAL FROM EMP WHERE EMPNO = ... etc etc 231 signature
  232. 232. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. plan(s) for signature 232
  233. 233. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 233 SELECT A.DATA, B.DATA FROM TAB1 A, TAB2 WHERE B.TAB1_ID = A.ID AND A.CODE = :1 ----------------------------------------------------------------------- | 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
  234. 234. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 234 SELECT A.DATA, B.DATA FROM TAB1 A, TAB2 WHERE B.TAB1_ID = A.ID AND A.CODE = :1 ----------------------------------------------------------------------- | 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
  235. 235. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. when it's running good … 235 … keep running it like that !
  236. 236. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 236 SELECT A.DATA, B.DATA FROM TAB1 A, TAB2 WHERE B.TAB1_ID = A.ID AND A.CODE = :1 ----------------------------------------------------------------------- | 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 !
  237. 237. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. maybe it is better ? 237 ------------------------------------------------------------------------ | 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 | ------------------------------------------------------------------------
  238. 238. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. evolve a plan 238
  239. 239. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE 239
  240. 240. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 240 SELECT A.DATA, B.DATA FROM TAB1 A, TAB2 WHERE B.TAB1_ID = A.ID AND A.CODE = :1 ----------------------------------------------------------------------- | 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
  241. 241. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. unless … 241 … that makes it bad or there's something better
  242. 242. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. if only we had done ... 242 DBMS_SPM.I_LOVE_THIS_PLAN(sql_id) DBMS_SPM. THIS_PLAN_SUCKS(sql_id) DBMS_SPM. SWAP_THIS_PLAN_IN(sql_id)
  243. 243. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. so back to upgrade 243
  244. 244. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2) your system is OK 244 how can I lower risk ?
  245. 245. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SQL> alter system set 2 optimizer_capture_sql_plan_baselines = true 245
  246. 246. 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 246
  247. 247. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. the same performance 247
  248. 248. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 248
  249. 249. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. wrap up 249
  250. 250. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c optimizer is better 250
  251. 251. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12c optimizer is different 251
  252. 252. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.2+ preferred 252
  253. 253. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12.1 ... backport 253
  254. 254. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. SPM is your friend 254
  255. 255. 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 :-)

×