Successfully reported this slideshow.
Upcoming SlideShare
×

# How to estimate_oracle_cost

1,007 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### How to estimate_oracle_cost

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
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
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
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
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
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
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
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
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
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
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
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
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
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