How to estimate_oracle_cost

816 views
736 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
816
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Bitmap index cost
  • Join Selectivity = 1/max( ndvA, ndvB)* cardA * cardB
  • C1 like :B1  { 0.05 | C1.Density }:Depending on whether the undocumented init.ora parameter _LIKE_WITH_BIND_AS_EQUALITY is false (the default) or true.
  • LVLS BLEVEL The height of the index b-tree #LB LEAF_BLOCKS The number of leaf blocks #DK DISTINCT_KEYS The number of distinct keys of the index LB/K AVG_LEAF_BLOCKS_PER_KEY The average number of leaf blocks per key DB/K AVG_DATA_BLOCKS_PER_KEY The average number of data blocks per key CLUF CLUSTERING_FACTOR The clustering factor of the index
  • http://blog.csdn.net/tianlesoftware/article/details/4668723Height-Balanced Histograms:
  • We have a range of 11—running from 1 to 12. The right-hand edge hits zero at month_no = 23(high value + 11); the left-hand edge hits zero at month_no = -10 (low value – 11).This will be bad news to some people. If you have sequence, or time-based, values in acommonly queried column and haven’t been keeping the statistics up to date, then queriesthat use an equality on that column will have been using a flat line to give you the right answeras time passes. Now, those same queries are going to give you cardinalities that keep droppingas time passes—until suddenly, the cardinality gets so low that plans may change dramatically.Cases 19 and 20 (ranges outside the recorded low/high range): The optimizer detectsthat the query falls outside the known range, and seems to return a selectivity, hencecardinality that would be correct for column = constant. (There is a nasty boundary case,though, that appears from 9i onwards when every row holds the same value.)
  • Using prorated density: 0.001420 of col #2 as selectvity of out-of-range/non-existent value pred
  • Don’t get overly exited over the 0 cost of a unique index lookup in an NL join. It would appear to make NL joins with this kind of index access, e.g. a fully qualified primary key join predicate, cost free.
  • (Prob+1)*round(1495/10.40)+round(171/10.40)=2*144
  • We Can open another topic for hash/SM/NL
  • Don’t get overly exited over the 0 cost of a unique index lookup in an NL join. It would appear to make NL joins with this kind of index access, e.g. a fully qualified primary key join predicate, cost free.
  • Full Table Scan: BLOCKS(table) / MBRC / parallelismdegree+ 1Index UniqueScan: (BLEVEL(index) + 1) * OPTIMIZER_INDEX_COST_ADJ / 100Index Unique Scan + Table Access by Index ROWID: (BLEVEL(index) + 2) * OPTIMIZER_INDEX_COST_ADJ / 100Index Full Scan: (BLEVEL(index) + LEAF_BLOCKS(index)) * OPTIMIZER_INDEX_COST_ADJ / 100Index Fast Full Scan: BLEVEL(index) + LEAF_BLOCKS(index) / MBRC / parallelismdegree+ 1Index Range Scan: (BLEVEL(index) + LEAF_BLOCKS(index) * FF1) / OPTIMIZER_INDEX_COST_ADJ * 100Index Range Scan+ Table Access by Index ROWID: (BLEVEL(index) + LEAF_BLOCKS(index) * FF1 + CLUSTERING_FACTOR(index) * FF2) * OPTIMIZER_INDEX_COST_ADJ / 100Index SkipScan: max(NUM_DISTINCT(skipped_columns)) * (BLEVEL(index) + LEAF_BLOCKS(index) * FF1) * OPTIMIZER_INDEX_COST_ADJ / 100Index SkipScan+ Table Access byIndex ROWID: max(NUM_DISTINCT(skipped_columns)) * (BLEVEL(index) + LEAF_BLOCKS(index) * FF1 + CLUSTERING_FACTOR(index) * FF2) * OPTIMIZER_INDEX_COST_ADJ / 100
  • exec DBMS_STATS.GATHER_SYSTEM_STATS('NOWORKLOAD');execute dbms_stats.gather_system_stats('start');execute dbms_stats.gather_system_stats('stop');
  • http://www.oaktable.net/category/tags/system-statistics
  • If _table_scan_cost_plus_one=true, need +1
  • We Can open another topic for hash/SM/NL
  • We Can open another topic for hash/SM/NL
  • We Can open another topic for hash/SM/NL
  • We Can open another topic for hash/SM/NL
  • We Can open another topic for hash/SM/NL
  • Using prorated density: 0.001420 of col #2 as selectvity of out-of-range/non-existent value pred
  • Don’t get overly exited over the 0 cost of a unique index lookup in an NL join. It would appear to make NL joins with this kind of index access, e.g. a fully qualified primary key join predicate, cost free.
  • (Prob+1)*round(1495/10.40)+round(171/10.40)=2*144+
  • (if _table_scan_cost_plus_one = true)
  • How to estimate_oracle_cost

    1. 1. How to estimate oracle costPhilip ZhongSina WeiBo:@离叔-菲利普Blog: Philipzhong.blogspot.com2013-2-18© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 1
    2. 2. • Important Statistics Values and Parameters• Cardinality and Selectivity• IO Cost model• New Cost model© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2
    3. 3. Table Statistics: Number of table blocks(BLOCKS) Number of table rows(NUM_ROWS)Index Statistics: Levels of the index(BLEVEL) Number of leaf blocks (LEAF_BLOCKS) Clustering factor(CLUSTERING_FACTOR) Number of distinct keys(DISTINCT_KEYS)Column Statistics: Number of distinct values(NUM_DISTINCT) Number of NULL values(NUM_NULLS) Density© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 3
    4. 4. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 4
    5. 5. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 5
    6. 6. • NUM_DISTINCT=number of distinct value• Base Cardinality = number of rows of the table• FF=filter factor of the condition=1 / NUM_DISTINCT(column)• Selectivity = FF= Estimated cardinality /Base Cardinality• Estimated Cardinality = Selectivity * Base Cardinality© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 6
    7. 7. • C1=value  1/NUM_DISTINCT• C1 like value  1/NUM_DISTINCT• C1>value  ((Hi-Value)/(Hi-Lo))• C1<value  ((Value-Lo)/(Hi-Lo))• C1>=value  ((Hi-Value)/(Hi-Lo))+1/NUM_DISTINCT• C1<=value  ((Value-Lo)/(Hi-Lo))+1/NUM_DISTINCT• C1 between value1 and value2  ((value2 – value1)/(Hi- Lo))+2/NUM_DISTINCT• Notice: if column is NULL Adjusted selectivity = Base selectivity * (1 - num_nulls/num_rows)© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 7
    8. 8. • C1=:B1  C1.Density• C1 like :B1  { 0.05 | C1.Density }• C1 { < | <= | > | >= } :B1 C1.Density• C1 between :B1 and :B2  C1.Density*C2. Density• Notice: if column is NULL Adjusted selectivity = C1.Density * ( 1 - num_nulls/ num_rows )© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 8
    9. 9. • C1 and C2  FF1 * FF2• C1 or C2  FF1 + FF2 - FF1 * FF2• Not C1  1-FF1© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 9
    10. 10. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 10
    11. 11. • Full Table Scan: Cost=( NBLKS/ k )+1 (if _table_scan_cost_plus_one = true)• Non-Index costing: Cost = blevel + (leaf_blocks * effective index selectivity) +(clustering_factor * effective table selectivity)• Unique-Index costing: Cost=blevel + min cost of index +(clustering_factor * effective table selectivity)• Join cost: NESTED LOOP JOIN cost = cost of accessing outer table + (cardinality of outer table * cost of accessing inner table ) SORT MERGE JOIN cost = (cost of accessing outer table + outer sort cost) + (cost of accessing inner table + inner sort cost) HASH JOIN cost = (cost of accessing outer table) + (cost of building hash table) + (cost of accessing inner table )© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 11
    12. 12. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 12
    13. 13. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 13
    14. 14. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 14
    15. 15. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 15
    16. 16. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 16
    17. 17. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 17
    18. 18. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 18
    19. 19. • Cost Calculation formula: Cost= (NBLKS/ k)+1 (if _table_scan_cost_plus_one = true) Case Name Estimate Cost Original Cost Case 1 171/10.40+1=17.44 18 Case 2 1495/10.40+1=144.75 145 Case 3 12151/10.40+1=1169.37 1170 Case 4 12151/6.59+1=1844.85 1846© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 19
    20. 20. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 20
    21. 21. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 21
    22. 22. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 22
    23. 23. Cost = blevel +( leaf_blocks * effective index selectivity + clustering_factor * effective table selectivity) Case Name Estimate Cost Original Cost Case 1 1+(190*1)=191 191 Case 2 1+1=2 2 Case 3 1+(264*1+91723* 0.000027)=267.48 268© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 23
    24. 24. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 24
    25. 25. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 25
    26. 26. Cost = blevel + ( leaf_blocks * effective index selectivity/k) Case Name Estimate Cost Original Cost Case 1 1+(190*1/10.40)=19.27 20 Case 2 1+(190*1/6.59)=29.83 30© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 26
    27. 27. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 27
    28. 28. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 28
    29. 29. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 29
    30. 30. • NewDensity = [ (BktCnt - PopBktCnt) / BktCnt ] / (NDV - PopValCnt)• NewDensity=((254-12)/254)/(717-6)=0.001340022© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 30
    31. 31. Cost = blevel + (leaf_blocks * effective index selectivity + clustering_factor * effective table selectivity) Case Name Estimate Cost Original Cost Case 1 1+(190*0.000285+84136*0.000285) 26 =25.03 Case 2 1+190*0.000285=1.05 2 Case 3 1+190*0.480315=92.25 93© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 31
    32. 32. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 32
    33. 33. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 33
    34. 34. Cost= blevel + min cost of index +(clustering_factor * effective table selectivity) Case Name Estimate Cost Original Cost Case 1 0+1=1 1 Case 2 0+5*1+12000*0.000417=10 10© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 34
    35. 35. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 35
    36. 36. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 36
    37. 37. Cost = blevel + ( leaf_blocks * effective index selectivity * max(NUM_DISTINCT(skipped columns)))+ clustering_factor * effective table selectivityNotice: need set _optimizer_skip_scan_enabled = true Case Name Estimate Cost Original Cost Case 1 1+(264*0.000027*36572+91723*0.0 268 00027)=264.16 Case 2 1+(110*0.000040*24724+39949*0.0 113 00040)=111.38© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 37
    38. 38. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 38
    39. 39. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 39
    40. 40. Cost=cost of accessing outer table + (cardinality of outer table * cost of accessing inner table )Nest Loop Join : Cost(T1)= 1+(21* 0.001721+9153*0.001721)=16.78 Cost(T2)=1+1 Cost(case1)=17+17*2=51 not equal 34 why? Adjusted Cost(case1)=17+17*1=34 Cost(case2)=1+(21*0.00142+9153*0.00142)+14.02*1=28.04© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 40
    41. 41. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 41
    42. 42. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 42
    43. 43. Cost = (cost of accessing outer table) + (cost of building hash table) + (cost of accessing inner table )Cost=( NBLKS/ k )+1 (if _table_scan_cost_plus_one = true)Cost =blevel +( leaf_blocks * effective index selectivity + clustering_factor * effective table selectivity)Cost(hash build)=(Probe passes+1)*(large table block/k)+(small table/k)Hash Join : Cost(T1)=1+ 171/10.40=17.44 Cost(T2)=1+ 1495/10.40=144.75 Cost(hash build)=2*(1495/16.06)+171/16.06= 196.8 Cost(case1)=18+145+ 197= 360 != 391 (k=13.8) Cost(T1)=1+ 171/10.40=17.44 Cost (T2)=1+ 191+ 1463=1655 Cost(case 2)=1655+18= 1673© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 43
    44. 44. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 44
    45. 45. cost = (cost of accessing outer table + outer sort cost) + (cost of accessing inner table + inner sort cost)Cost =blevel +( leaf_blocks * effective index selectivity + clustering_factor * effective table selectivity)Sort Merge Join : Cost(T1)= 1+(21* 0.001721+9153*0.001721)=16.78 Cost(T2)=1+(191*1)+1*1463 =192+1463=1655 Cost(sort outer)=120 Cost(sort inner)=0 (unique index) Cost(case1)=1655+120+0+17=1792© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 45
    46. 46. • What’s effect from “OPTIMIZER_INDEX_COST_ADJ”?• What’s effect from parallelism degree?• What’s effect from “Optimizer_Index_Caching”?© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 46
    47. 47. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 47
    48. 48. • Cost = IO Cost+ (#CPUCost/cpuspeed)/sreadtim/1000) OR (BLOCKS/MRBC)*(mreadtim/sreadtim)+((#CPUCost/cpuspeed)/s readtim/1000)• MRBC- number of multi block reads• sreadtim is set to ioseektim + db_block_size/iotrfrspeed• mreadtim is set to ioseektim + db_file_multiblock_read_count * db_block_size/iotftspeed• Notice: you can get these data from aux_stats$ table© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 48
    49. 49. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 49
    50. 50. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 50
    51. 51. Cost=(BLOCKS/MRBC)*(mreadtim/sreadtim)+((#CPUCost/cpuspee d)/sreadtim/1000) sreadtim is set to ioseektim + db_block_size/iotrfrspeedSreadtim=18+8192/21110=18.39Mreadtim=18+16*8192/21110=24.21 Case Name CPU Cost Estimate Cost Original Cost Case 1 round(3117766/(1414 0+ceil(ceil(171/16)*24. 16 *1000*18.38)=0 21/18.39)+1+0=16 Case 2 round(28085703/(141 0+ceil(ceil(1495/16)*2 126 4*1000*18.38)=1 4.21/18.39)+1+1=126© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 51
    52. 52. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 52
    53. 53. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 53
    54. 54. Cost = IO Cost+ (#CPUCost/cpuspeed)/sreadtim/1000)Sreadtim=18+8192/21110=18.39Mreadtim=18+16*8192/21110=24.21 Case Name CPU Cost Estimate Cost Original Cost Case 1 round(19717195/141 191*1+1=192 192 4/18.39/1000)=1 Case 2 round(14443/1414/18 1+1+0=2 2 .39/1000) =0© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 54
    55. 55. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 55
    56. 56. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 56
    57. 57. Cost=blevel+(BLOCKS/MRBC)*(mreadtim/sreadtim)+((#CPUCost/c puspeed)/sreadtim/1000)Sreadtim=18+8192/21110=18.39Mreadtim(1)=18+16*8192/21110=24.21Mreadtim(2)=18+8*8192/21110=21.1 Case Name CPU Cost Estimate Cost Original Cost Case 1 round(19278250/(141 1+12*(24.21/18.39)+1 18 4*18.39*1000))=1 =18 Case 2 round(19278250/(141 1+24*(21.1/18.39)+1= 30 4*18.39*1000))=1 30© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 57
    58. 58. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 58
    59. 59. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 59
    60. 60. Cost= IO Cost+ (#CPUCost/cpuspeed)/sreadtim/1000)Sreadtim=18+8192/21110=18.39Mreadtim=18+16*8192/21110=24.21 Case Name CPU Cost Estimate Cost Original Cost Case 1 round(202809 1+ceil((190*0.000288+ 27 /(1414*18.39*1000))= 84136*0.000288))+0= 0 26 Case 2 round(9169130 1+ceil(190*0.464567)+ 90 /(1414*18.39*1000))= 0=90 0© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 60
    61. 61. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 61
    62. 62. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 62
    63. 63. Cost = IO Cost+ (#CPUCost/cpuspeed)/sreadtim/1000) Case Name CPU Cost Estimate Cost Original Cost Case 1 round(8171/1414/18. 0+1 1 39/1000) =0 Case 2 round(108772 0+5+5=10 9 /1414/18.39/1000)=0© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 63
    64. 64. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 64
    65. 65. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 65
    66. 66. Cost= IO Cost+ (#CPUCost/cpuspeed)/sreadtim/1000)Sreadtim=18+8192/21110=18.39Mreadtim=18+16*8192/21110=24.21 Case Name CPU Cost Estimate Cost Original Cost Case 1 round(1909776 1+(264*0.000027*365 268 /(1414*18.39*1000))= 72+91723*0.000027)+ 0 0=264 Case 2 round(805543 1+((110*0.000040*247 113 /(1414*18.39*1000))= 24)+39949*0.000040) 0 +0=112© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 66
    67. 67. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 67
    68. 68. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 68
    69. 69. Cost=cost of accessing outer table + (cardinality of outer table * cost of accessing inner table )Nest Loop Join : Cost(CPU) =round(3418106/1414/18.39/1000)=0 Cost(T1)= ((171/16)*24.21/18.39)+1 = 15.07 Cost(T2)=1+0 Cost(case1)= 15.07 + 15.07 *1= 30.14 Cost(T1-CPU) =round(106010/1414/18.39/1000)=0 Cost(T2-CPU) =round(234970/1414/18.39/1000)=0 Cost(T1) =1+21*0.00142+9153*0.00142=14.02 Cost(case2)=14.02+14.02*1=28.04© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 69
    70. 70. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 70
    71. 71. © 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 71
    72. 72. Cost = (cost of accessing outer table) + (cost of building hash table) + (cost of accessing inner table )Cost=(BLOCKS/MRBC)*(mreadtim/sreadtim)+((#CPUCost/cpuspeed)/sreadtim/1000)+1 Cost(hash build)=(Probe passes+1)*(large table block/MRBC)+(small table/MRBC)Hash Join : Cost(T1-CPU)=round(3117766/(1414*18.39*1000))=0 Cost(T2-CPU)=round((28085703/(1414*18.39*1000)))=1 Cost(T1)=1+ (171/16)*24.21/18.39+0= 15.07, Cost(T2)=1+ (1495/16)*24.21/18.39+1=125 Cost(build Hash CPU)= 178640101/(1414*18.39*1000)= 6.87 Cost(case1)=15.07+125+ 6.87 = 146.94 Cost(T4-CPU)=round((30493428/(1414*18.39*1000)))=1 Cost(T2)=1+ (1495/16)*24.21/18.39+1=125 , Cost(T4)=1+ (1575/16)*24.21/18.39+1=131.59 Cost(build Hash CPU)= 128949682/(1414*18.39*1000)= 4.96 Cost(hash build)=2*(1575/16)+ (1495/16)=290 Cost(case2)=125+132+5+290= 552© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 72
    73. 73. • Wolfgang Breitling, A LOOK UNDER THE HOOD OF CBO: THE 10053 EVENT• Wolfgang Breitling, The Effects of optimizer_index_cost_adj and optimizer_index_caching on Access Plans• Jonathan Lewis , Cost-Based Oracle Fundamentals• Alberto DellEra, New Density calculation in 11g• Chris Antognini ,CBO – A Configuration Roadmap• Juliandyke,http://www.juliandyke.com/Diagnostics/Events/EventR eference.html• http://mikixiyou.iteye.com/blog/1709321• http://www.oaktable.net/category/tags/system-statistics© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 73
    74. 74. Thank you.© 2011 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 74

    ×