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.

Introduction to oracle optimizer

923 views

Published on

Published in: Technology
  • Be the first to comment

Introduction to oracle optimizer

  1. 1. Introduction to Oracle Optimizer Heribertus Bramundito
  2. 2. Agenda  Our Environment  Heap Table  Histogram  Oracle Optimizer  Cost  Selectivity & Cardinality  Clustering Factor  Access Method  Partitioning  Join Method  Sub-query  Hint
  3. 3. Our Environment Heap Table  This is default table when we issue the CREATE TABLE statement  Data is stored in random fashion, no specific sort of order (best fit algorithm) 3
  4. 4. Our Environment Heap Table (result from table access) 4
  5. 5. Our Environment Heap Table (result from index access) 5
  6. 6. Our Environment IOT Table (a comparison) 6
  7. 7. Our Environment Histogram  Collection of information about data distribution in specific column  Oracle maintains 2 types of histogram: frequency and height-balanced  Oracle use histogram as additional information when deciding whether to use index scan or table scan 7
  8. 8. Our Environment Histogram (column without histogram) 8
  9. 9. Our Environment Histogram (create histogram) 9
  10. 10. Our Environment Histogram (column with histogram) 10
  11. 11. Agenda  Our Environment  Heap Table  Histogram  Oracle Optimizer  Cost  Selectivity & Cardinality  Clustering Factor  Access Method  Partitioning  Join Method  Sub-query  Hint
  12. 12. Cost  Jonathan Lewis: “The cost represents the optimizer‟s best estimate of the time it will take to execute the statement”  A result of the calculation performed by optimizer  Few conditions that make CBO produces wrong result”  No statistics on the underlying objects or statistics are obsolete  Performance characteristics of hardware or current workloads are not known  Bug  From Oracle Performance Tuning Guide and Reference Cost = (#SRds * sreadtim + #MRds * mreadtim + #CPUCycles / cpuspeed) / sreadtim #SRds: number of single data block reads #MRds: number of multi data block reads #CPUCycle: number of CPU cycles sreadtim: single block read time mreadtim: multi block read time cpuspeed: CPU cycles per second 12
  13. 13. Cost (the calculation)  #SRds = blevel + #leaf_block * idx_sel + clustering_factor * tbl_sel (index     scan) #MRds = #tbl_blocks under HWM / mbrc (full table/ index scan) sreadtim = ioseektim + block_size * iotfrspeed (SREADTIM) mreadtim = ioseektim + mbrc * block_size * iotfrspeed (MREADTIM) mbrc = db_file_multiblock_read_count (MBRC)  Going back to slide #4 (let‟s try to calculate cost of table scan)  Cost = (24 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 1.5 * 42 / 12 = 5.25  Slide #5 (index scan only)  Cost = 0 + ceil(1 * 0.0588) = ceil(0.0588) = 1  Slide #10 (index scan with access to the table)  Cost = 0 + 1 + ceil((4 / 17) * 20) = 1 + ceil(4.705) = 1 + 5 = 6 13
  14. 14. Selectivity & Cardinality  Selectivity is what fraction of row the predicate is supposed to fetch/ return Selectivity = 1 / num_distinct (no histogram) Selectivity = density (histogram)  Cardinality is total number of row the predicate is supposed to return Cardinality = num_rows * density  Going back to slide #4  Cardinality = num_rows = 20  Slide #5  Selectivity = (number of bucket with data / number of total bucket) = 17 / 17 = 1  Cardinality = selectivity * num_rows = 1 * 20 = 20  Slide #10  14 Selectivity = 4 / 17 = 0.235  Cardinality = 0.235 * 20 = 4.7
  15. 15. Clustering Factor  Represents the degree to which data is randomly distributed through a table  Number of blocks <= clustering factor <= number of rows  Index has better selectivity if clustering factor is close to number of data block, means Oracle can do multi block read on the table for several index‟s key 15
  16. 16. Clustering Factor (block_id from rowid)  Going back to NORMAL_HASH table example, due to the anomaly configuration of pctfree and pctused, every block contains single row only 16
  17. 17. Access Method  There are 2 access methods: table and index access  Index access can be: Fast Full Scan, Full Scan, Unique Scan, Range Scan, Range Scan (MIN/MAX) and Skip Scan 17
  18. 18. Access Method Full Table Scan  Oracle is reading all rows from the table  Not suitable for OLTP system with high volume of transaction and usually only access small fraction of data  Suitable for DSS system with batch reporting query  Not good for Nested Loop (NL) for the outer table (huge table)  Usually we see it in Hash Join (HJ) 18
  19. 19. Access Method Index Fast Full Scan  Oracle is reading all rows from the index to get the result (doesn‟t required table access since the index contains all columns required to resolve the query)  To be able to use Index FFS, the column should be defined as NOT NULL or at least one column in a composite index is NOT NULL, the reason is that NULL values are not included in the index creation, so when the column is defined as NOT NULL Oracle knows that all values are available in the index  Index FFS will be available as an option if we put “IS NOT NULL” in the WHERE clause explicitly  Cost will be only for accessing the index 19
  20. 20. Access Method Index Fast Full Scan (forced by hint) 20
  21. 21. Access Method Index Fast Full Scan (cost calculation)  If we remove the HINT, we will have Index Full Scan  Cost for Index Fast Full Scan  Cost = (8 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 0.5 * 32 / 12 = ceil(1.333) = 2  Cost for Index Full Scan  Cost = 0 + ceil(1 * 1 / 14) = ceil(0.0714) = 1 21
  22. 22. Access Method Index Fast Full Scan (building the example)  Another figure from index with more leaf block (another anomaly configuration in the pctfree of the index) 22
  23. 23. Access Method Index Fast Full Scan (the result) 23
  24. 24. Access Method Index Full Scan  Oracle is reading all rows from the index, and may be accessing these rows in the underlying table Without table access 24
  25. 25. Access Method Index Full Scan (with table access) Unique index 25
  26. 26. Access Method Index Unique Scan  Oracle is reading 0 or 1 rows from the index, only on unique index  Equality operator in the predicate (=), ay be seen in AND, OR, IS NULL operator 26
  27. 27. Access Method Index Unique Scan (the result) 27
  28. 28. Access Method Index Range Scan  Oracle is reading 0 or more contiguous rows from the index  Non unique index with range operator in the predicate (>, <, >=, <=) 28
  29. 29. Access Method Index Range Scan (MIN/MAX)  Oracle is identifying 0 or more contiguous rows in the index, but is reading only one (the first or the last) in order to satisfy a MIN or MAX aggregate function 29
  30. 30. Access Method Index Skip Scan  Oracle is reading 0 or more rows from different parts of the index (composite index), and may be accessing these rows in the underlying table  Skip Scan will be happened when we access a composite index with second column in the index‟s order. It will not be happened for the third column, forth, etc.  It will works only if first column in the index has low cardinality (few distinct value) otherwise full table scan will be better in most of the case 30
  31. 31. Access Method Index Skip Scan (building the example) 31
  32. 32. Access Method Index Skip Scan (the result) First index‟s column with low cardinality 32
  33. 33. Access Method Index Skip Scan (a comparison) First index‟s column with high cardinality 33
  34. 34. Partitioning  There are some consideration when we are working with partition table. One of its is regarding access path. We introduce partitioning on the table usually to reduce the number of rows which will affected by any query, since we know that not all of those rows are being used in the query.  There are 4 kinds of access method for partitioned table: Partition Range Single, Partition Range Iterator, Partition Range All and Partition Range Sub-query 34
  35. 35. Partitioning (building the example) 35
  36. 36. Partitioning Partition Range Single  Exactly only single partition of the table involves in the query. Access path on this partition depends on the query, can be Table Scan or any Index Scan 36
  37. 37. Partitioning Partition Range Iterator  We will see this kind of access path whenever we have several partitions in the query 37
  38. 38. Partitioning Partition Range All  In this kind of access method, all partitions in the table will be scanned. This is a bad example of table design  (create a partition table without taking any benefit of it) 38
  39. 39. Partitioning Partition Range Sub-query (building an example)  This method is new in 10g. If the partitioned table is bigger compare to the other join table and the expected number of the records (result) is significantly less, Oracle will perform dynamic partition pruning using sub-query  The partitioned table will be having 200,000 blocks and the other join only 200 blocks 39
  40. 40. Partitioning Partition Range Sub-query (the result) Only 7 rows compare to 1000 rows from partitioned table 40
  41. 41. Partitioning Partition Range Sub-query (a comparison) 41
  42. 42. Join Method  There are 3 join methods: Nested Loop (NL), Hash Join (HJ) and Sort Merge Join (SM)  Most of the time we see only „standard‟ join between 2 tables, but in rare case we will see Anti-Join and Semi-Join variation for all above 3 methods. Anti-Join will be appear when we are working with NOT IN clause while Semi-Join will be appear when we are working with EXISTS clause 42
  43. 43. Join Method Nested Loop  The Nested means an iteration. Pseudo-code for this kind of join will be like below: for x in (select [col] from outer_table) loop for y in (select [col] from inner_table where outer_table.join_col = inner_table.join_col) loop return the rows from outer and inner table end loop; end loop;  Suitable for small “size” for the outer (driving) table. For the inner table, it should be accessed using index scan  Starting from 9i, Oracle introduces new „table prefetching‟ method which will reduce logical I/O 43
  44. 44. Join Method Nested Loop (building the example) 44
  45. 45. Join Method Nested Loop (table prefetching method) note down the logical I/O 45
  46. 46. Join Method Nested Loop (legacy method) recreate with UNIQUE index 46
  47. 47. Join Method Nested Loop (the result) 47
  48. 48. Join Method Hash Join  In this method, first Oracle will choose 1 dataset (build table – this is outer table in Nested Loop), and then create hash table in memory using generated hash-key from join column. Once completed, second table (probe table – this is inner table in Nested Loop) will be scanned using the same hash function (probing the hash table)  Applicable for join with equality operator (=)  There are 3 level of effectiveness: optimal, one-pass and multi-pass. Optimal when the size of tables is matched with hash_area_size. One-pass or Multipass when the tables is not enough to be hash-ed in the memory (requires disk operation)  Event 10104 for tracing Hash Join operation 48
  49. 49. Join Method Hash Join (cont.)  Check hash_area_size and workarea_size_policy database parameter  Check v$sysstat for relevant system statistics SELECT name, value, case when sum(value) over() = 0 then 0 else round(value*100/sum(value) over(),2) end as pct FROM v$sysstat WHERE name LIKE 'workarea executions%' 49
  50. 50. Join Method Hash Join (components) 50
  51. 51. Join Method Hash Join (build the example) 51
  52. 52. Join Method Hash Join (the result – optimal) workarea_size_policy = AUTO test on SMALL table 52
  53. 53. Join Method Hash Join (the result – optimal, part 2) workarea_size_policy = AUTO test on BIG table 53
  54. 54. Join Method Hash Join (with index scan on the tables) 54
  55. 55. Join Method Hash Join (event 10104) 55
  56. 56. Join Method Sort Merge  There are 2 operation in this method: sort and merge. So     56 it is application for any query which requires sorting (on the join column): Order By clause, Group By clause, Set operation, Distinct operator, Analytical function, Index creation, Connect By query and etc Similar to Hash Join, there are 3 level of effectiveness for sorting operation: optimal, one-pass and multi-pass. Optimal when the size sort_area_size is enough to handle sort operation. One-pass or Multi-pass when Oracle requires disk operation for the sorting Event 10032 for tracing sort operation and 10033 for tracing sort I/O operation Check sort_area_size and workarea_size_policy database parameter Check v$sysstat for relevant system statistics and v$tempstat for sorting statistics
  57. 57. Join Method Sort Merge (cont.)  Merging part can be one of the following possibilities: 57
  58. 58. Join Method Sort Merge (build the example) 58
  59. 59. Join Method Sort Merge (sorting only example) 59
  60. 60. Join Method Sort Merge (sorting only example – cont.) 60
  61. 61. Join Method Sort Merge (the example) 61
  62. 62. Join Method Sort Merge (Merge Join Cartesian) 62
  63. 63. Join Method Sort Merge (event 10032) 63
  64. 64. Sub-query  There 2 main types of sub-query: Nested Sub- query and Correlated Sub-query  Nested sub-query when the sub-query (inner query) need to be completed first and then the result will be passed to the main query  Correlated sub-query when the main query should be executed first in order to execute the inner query  In some cases we can rewrite sub-query into join form for performance improvement 64
  65. 65. Sub-query (building the example) 65
  66. 66. Sub-query Nested  The select statement against “sub_q” is executed first and the outputs will be used by main query 66
  67. 67. Sub-query Correlated  We can see from Predicate Information, “sub_q” is executed for every value from “main_q” 67
  68. 68. Sub-query Correlated – rewrite into join 68
  69. 69. Sub-query Correlated – comparing the statistics Check logical I/O 69
  70. 70. Hint  Some of famous Oracle hints  PARALLEL, PARALLEL_INDEX  FULL  INDEX, INDEX_SS, INDEX_FFS  LEADING  ORDERED  DRIVING_SITE  USE_NL, USE_HASH, USE_MERGE  APPEND  USE_CONCAT  Other hints  MERGE_AJ, HASH_AJ  MERGE_SJ, HASH_SJ  UNNEST, NO_UNNEST 70
  71. 71. References  Jonathan Lewis, Cost-Based Oracle     71  Fundamentals Thomas Kyte, Expert Oracle Database Architecture – 9i and 10g Programming Techniques and Solutions http://www.orafaq.com/tuningguide/ http://oraclerandolf.blogspot.com/2011/10/paralleldowngrade.html http://asktom.oracle.com/pls/asktom/f?p=100:1:0:: NO:: http://jonathanlewis.wordpress.com/

×