You are on page 1of 52

# Active

Statistics
Wolfgang Breitling
www.centrexcc.com
Copyright 2008 Centrex Consulting Corporation. Personal use of this material is
permitted. However, permission to reprint/republish this material for advertising
and promotional purposes or for creating new collective works for resale or
redistribution to servers or lists, or to reuse any copyrighted component of this
work in other works must be obtained from Centrex Consulting.

Who am I
Independent consultant since 1996
specializing in Oracle and Peoplesoft setup,
administration, and performance tuning
Member of the Oaktable Network
25+ years in database management
DL/1, IMS, ADABAS, SQL/DS, DB2, Oracle

Oracle since 1993 (7.0.12)
OCP certified DBA - 7, 8, 8i, 9i
Mathematics major from University of Stuttgart
2

© Wolfgang Breitling, Centrex Consulting Corporation

UKOUG - December 3, 2008

Focus
 Gather Statistics
 Gather Partition Statistics
 Gather Histograms

 Clone Statistics
 Modify or RYO Statistics
 prepare_column_stats

3

© Wolfgang Breitling, Centrex Consulting Corporation

UKOUG - December 3, 2008

Gather_Stats and the Stattab Table
 gather_object_stats(…, stattab=> , statid=> )
the current statistics in the dictionary get saved into
stattab under the given statid
new statistics are gathered into the dictionary

 gather_system_stats(…, stattab=> , statid=> )
the current statistics in the dictionary, if any, are
unaffected
new statistics are gathered directly into stattab under
the given statid

4

© Wolfgang Breitling, Centrex Consulting Corporation

UKOUG - December 3, 2008

Gather_System_Stats Strategy
1. Gather system stats into a stattab table using fixed
times and intervals with distinct statids e.g. schedule
hourly or at certain times each day:
gather_system_stats(interval=>60,
statid=>'S'||to_char(sysdate,'yyyymmddhh24miss'), … );

2. Process this data, e.g. using averages, and use those
values to set system statistics with
set_system_stats. Make sure mreadtm is > sreadtm!
select statid, n3 cpuspeed, n1 sreadtim, n2 mreadtim, n11 mbrc
from stats_table where type = 'S' and c4 = 'CPU_SERIO' order by statid

5

© Wolfgang Breitling, Centrex Consulting Corporation

UKOUG - December 3, 2008

spare4 from sys.AUTO_SAMPLE_SIZE NULL FOR ALL COLUMNS SIZE AUTO DBMS_STATS.AUTO_INVALIDATE AUTO AUTO APPROXIMATE_NDV TRUE PUBLISH STALE_PERCENT INCREMENTAL TRUE 10 FALSE INCREMENTA_INTERNAL_CONTROL TRUE © Wolfgang Breitling.AUTO_CASCADE DBMS_STATS. sval1.optstat_hist_control\$ SNAME SVAL1 SKIP_TIME TRACE DEBUG SYS_FLAGS STATS_RETENTION CASCADE ESTIMATE_PERCENT DEGREE METHOD_OPT NO_INVALIDATE GRANULARITY AUTOSTATS_TARGET 6 SPARE4 0 0 0 31 DBMS_STATS. Centrex Consulting Corporation UKOUG . 2008 .December 3.dbms_stats defaults select sname.

It is known that in some cases. which means DBMS_STATS decides in which columns a histogram may help to produce a better plan. 2008 . often  METHOD_OPT had a default of "FOR ALL COLUMNS SIZE 1" on 9i. Centrex Consulting Corporation UKOUG . Small sample sizes are known to produce poor number of distinct values NDV on columns with skewed data (which are common). 10g defaults to AUTO.AUTO_SAMPLE_SIZE for sample size. the effect of a histogram is adverse to the generation of a better plan. […]. which basically meant NO HISTOGRAMS.1.Words of Caution*  ESTIMATE_PERCENT had a default of 100% on 9i. plan * Metalink Note 465787. even if that means gather statistics less often. Emphasis all mine 7 © Wolfgang Breitling. common) thus generate sub-optimal plans. Use an estimate of 100% if your window maintenance can afford it.December 3. 10g defaults to DBMS_STATS.

972 0 0 0 0 139.208.598.9 01:22. cascade=>true) NAME 100 10 1 0.4 06:26.374 7.972 0 0 0 0 5 0 0 0 0 14 32 31 31 32 3.December 3.464 384.365 1.optimal 8 © Wolfgang Breitling.3 session logical reads 475.0 02:40.801 65.320.639 719.2.303 52.0 01:53.01 auto sql execute elapsed time DB CPU 22:55.method_opt=>'for all columns size 1'.0.550 0 161 1 4 259 1 4 259 1 4 259 1 4 259 2 6 259 3 72.039 279.3 01:36.948.1 0.5 00:58.3) gather_table_stats(….1 02:17.584 2.onepass workarea executions .934.5 00:47. 2008 .002.Gather Statistics Performance (10.490 1.1 04:42.961 0 0 0 33 index fast full scans (full) table fetch by rowid table scans (long tables) sorts (rows) physical reads direct temp tblsp physical writes workarea executions .871 139.1 00:59.9 02:14. Centrex Consulting Corporation UKOUG .

938 0 0 0 0 139.938 0 0 0 0 5 0 0 0 0 10 28 27 27 28 1.822 293.Gather Statistics Performance (11.1 00:42.225 0 155 1 4 215 1 4 215 1 4 215 1 4 215 2 4 313 1 72.4 00:43.1.1 01:07.556 0| 0 0 50 index fast full scans (full) table fetch by rowid table scans (long tables) sorts (rows) physical reads direct temp tblsp physical writes workarea executions .000. 2008 .December 3. cascade=>true) NAME 100 10 1 0.8 01:00.143 2. Centrex Consulting Corporation UKOUG .324 397.method_opt=>'for all columns size 1'.965.6 01:42.017 7.1 00:40.384 88.0.7 02:05.8 02:06.031 139.optimal 9 © Wolfgang Breitling.169 395.onepass workarea executions .680 1.5 06:40.3 session logical reads 489.623 1.258.01 auto sql execute elapsed time DB CPU 21:57.9 01:57.683 66.1 0.204.855.9 02:00.982.6) gather_table_stats(….

930 59.002.870 372.551 0 100.01 auto sql execute elapsed time DB CPU 19:43.December 3.223 716.358 4. 2008 .1 02:18.Gather Statistics Performance (10.505 100.3 02:34.8 01:57.456 46.method_opt=>'for all columns size 1'.6 00:35.817 273. Centrex Consulting Corporation UKOUG .2.6 01:18.optimal 10 © Wolfgang Breitling.406.3 04:15.3) gather_table_stats(….1 0.0.1 01:14.842 0 0 0 8 1.466 0 42 1 0 42 1 0 42 1 0 42 1 0 42 2 2 42 3 44.9 00:51.551 0 1 0 6 7 437.2 session logical reads 372.617 0 0 0 7 45.660.onepass workarea executions . cascade=>false) NAME 100 10 1 0.336 0 0 0 9 index fast full scans (full) table fetch by rowid table scans (long tables) sorts (rows) physical reads direct temp tblsp physical writes workarea executions .654 0 0 0 7 57.5 00:48.2 01:20.6 03:34.

2 01:28.9 00:34.01 auto sql execute elapsed time DB CPU 18:53. cascade=>false) NAME 100 10 1 0.1 0.512 0 100.2 01:31.043 0 0 0 3 42.512 0 1 0 2 3 439.086 60.3 00:50.1. 2008 .method_opt=>'for all columns size 1'.000. Centrex Consulting Corporation UKOUG .Gather Statistics Performance (11.450 389.635 0 60 1 0 50 1 0 50 1 0 52 1 0 50 2 0 67 1 44.067 100.6) gather_table_stats(….000 4.797 386.2 00:32.4 02:38.8 00:53.0.929 0 0 0 4 54 0 0 0 6 index fast full scans (full) table fetch by rowid table scans (long tables) sorts (rows) physical reads direct temp tblsp physical writes workarea executions .704 84.638 288.December 3.9 03:55.4 00:49.optimal 11 © Wolfgang Breitling.1 01:49.onepass workarea executions .3 01:31.1 session logical reads 386.820 0 0 0 6 60.390.

December 3. Centrex Consulting Corporation UKOUG . whether collecting histograms (for all columns size auto) or not (for all columns size 1).Gather Statistics  An estimate_percent < 100 does perform faster than a full compute.  The assertion that essentially all blocks are read as long as estimate_percent > 100/average rows per block is correct though. 12 © Wolfgang Breitling.  The difference comes from the sort(s) required to get the column stats. contrary to polular belief. 2008 . estimate_percent > 50 is not equivalent to a full compute.  That also means that.

786 3.687 3.524 375. 2008 .980 231 0.996.980 231 0.980 231 0.2.Table Statistics Accuracy (10.0.000 375.1% * error = abs(actual-estimate)/actual 13 © Wolfgang Breitling.4% 0.991.960 3.3) gather_table_stats(….000 blocks rowlen 4.000.000.870 375.1% 1 39.01 4.016.024 375.016 4.996.2% auto 451.980 232 0.1% 0.000 375.000 375.996.918 3. cascade=>true) est% sample num_rows 100 4.December 3.980 232 error* 10 399. Centrex Consulting Corporation UKOUG .980 232 0.1 4.method_opt=>'for all columns size 1'.

000 375.001.000.Table Statistics Accuracy (11.980 232 0.December 3.980 232 0. 2008 .980 232 error* 10 398.951.001 4.034.000.891 3.986.980 231 0.000 375.770 375.000 4.0% * error = abs(actual-estimate)/actual 14 © Wolfgang Breitling.000.349 4.01 4.000 375.900 375.980 231 1.6) gather_table_stats(…. Centrex Consulting Corporation UKOUG .000 blocks rowlen 4.method_opt=>'for all columns size 1'.980 231 0.2% auto 4.1.0% 0. cascade=>true) est% sample num_rows 100 4.3% 1 40.0.1 4.677 3.928 375.000.9% 0.

Column Distributions Name Null? C1 NOT NULL C2 NOT NULL C3 C4 C5 C6 FY AP D1 FILLER 15 Type VARCHAR2(8) VARCHAR2(8) VARCHAR2(16) VARCHAR2(30) VARCHAR2(8) VARCHAR2(30) NUMBER NUMBER DATE VARCHAR2(200) index T2_U © Wolfgang Breitling.December 3. Centrex Consulting Corporation column C1 C2 C3 C4 D1 FY AP index T2_I1 column FY AP C1 T2_I2 FY AP C3 T2_I3 FY AP C5 UKOUG . 2008 .

December 3. Centrex Consulting Corporation Log-Normal Distribution UKOUG .Column Distributions Normal Distribution 16 © Wolfgang Breitling. 2008 .

918 357.053 00314515 01252878 30.6% 0. 2008 .0% auto 451.786 309.6% 14.3 low high 100 4.8% 16.687 353.2.390 00361058 01127320 39.7% 40.1% 8.000.875 00380916 01231540 35.016 236.4% © Wolfgang Breitling.9% 21.1 4.0% 6.099 00440915 01202777 29.960 325.000 507.5% 35.5% 0.2% 0.December 3. Centrex Consulting Corporation UKOUG .Column Statistics Accuracy random-“uniform” distribution est% sample NDV low high NDV 10.1% 14.2% 10.0.993 00314515 01341929 10 17 399.01 4.6% 1 39.012 00426333 01145732 53.

5% 10.000 507.891 297.985 00356860 01175345 31.349 329.1 4.Column Statistics Accuracy random-“uniform” distribution.949 00348801 01226439 30.4% 26. est% sample NDV low high NDV 11.1% 0.001 346.0% See also Greg Rahn’s and the Optimizer Development Team’s blogs 18 © Wolfgang Breitling.December 3.1% auto 4.1.7% 15.0% 0. Centrex Consulting Corporation UKOUG .000.677 352.000.1% 26.9% 8.6% 1 40.5% 1.5% 12.993 00314515 01341929 10 398.0% 0.932 00397784 01328241 35.0.000 507.7% 13. 2008 .01 4.4% 0.6 low high 100 4.424 00314515 01341929 0.543 00398559 01139450 41.

0% 65.0.156 004379 025303 10 399.238 008090 021944 52.2.2% 84.7% 13.01 4.0% 7.8% 52.3 NDV low high NDV low high 100 4.3% auto 45.016 7.8% 20.0% 0.464 005288 024977 17.119 006657 023001 39.572 007231 022025 50.1 4.December 3. 2008 .687 12.786 7.0% 9.5% © Wolfgang Breitling.000 15.3% 1 39.229 9.Column Statistics Accuracy normal distribution est% 19 sample 10.000.1% 0.960 9.7% 87.286 008187 023403 38.8% 1.1% 13. Centrex Consulting Corporation UKOUG .

000 15.000 15.001 7.203 008073 022747 52.349 9.1 4.156 004379 025303 10 398.4% 0.1% 0.075 006892 022930 40.677 12.000.1% 57.0% 0.1.497 004379 024726 17.01 4. 2008 .9% 4.569 008104 021284 50.5% NDV low high 0.891 7.December 3.000.1% 15.0.0% 0.5% 84.Column Statistics Accuracy normal distribution est% NDV low high 100 4.0% 2.4% 9.6 © Wolfgang Breitling.3% 1 40.1% 85.156 004379 025303 auto 20 sample 11.4% 10. Centrex Consulting Corporation 0.0% UKOUG .

0% 0.0% 0.7% 0.491 770 00033746 09992981 90.994 00002527 09997860 21 10 399.033 00003581 09997860 62.7% 0. 2008 .0% 1 39.3 NDV low high 100 4.4% 0.9% 0.Column Statistics Accuracy lognormal distribution est% sample NDV low high 10.1% auto 4.2.December 3.0% 0.786 792 00003581 09995231 90.0.000 7.3% 0.0% UKOUG .365 00002527 09997860 7.4% 1235.0% © Wolfgang Breitling.016 718 00030383 09997860 91.1 4.0% 1102.687 7.1% 41.960 3.000. Centrex Consulting Corporation 41.01 4.

5% 538.1.001 730 00002527 09997860 90.December 3.0% 0.0% 0.6 NDV low high 100 4.994 00002527 09997860 0.000.0% 1 40.994 00002527 09997860 22 10 398. 2008 .0% 0.9% 0.0% 0.1 4.7% 0.4% 0.401 00002527 09997860 7.891 843 00016130 09995231 89.000.Column Statistics Accuracy lognormal distribution est% sample NDV low high 11.349 3.000 7.0.0% © Wolfgang Breitling.3% 41.3% 0.097 00003581 09997860 61.0% 0. Centrex Consulting Corporation UKOUG .677 7.0% 0.0% auto 4.000 7.01 4.

c2. 2008 . method_opt=>'for columns c4 size 254'). gather_table_stats(…. method_opt=>'for columns c3 size 254'). o gather_table_stats(…. method_opt=>'for columns c1. Centrex Consulting Corporation UKOUG . if any. p gather_table_stats(….c3. method_opt=>'for columns c1 size 254'). gather_table_stats(….c3. method_opt=>'for columns c2 size 254'). is the difference between the following ways of gathering histograms on multiple columns? n gather_table_stats(…. gather_table_stats(….c2.c4').Gathering Histograms What. method_opt=>'for columns size 254 c1. 23 © Wolfgang Breitling.December 3.c4 size 254').

161 session logical reads 1.0 1.051 table fetch by rowid 10.5 01:37.721.575.0 03:25.0 00:47.5 00:48.000 430.752 consistent gets db block gets 2.783 460.2 02:29.314 2.161 440.736 457.163 142.479.141 5.578.152 458.onepass 6 2 2 8 8 8 workarea executions .3 01:42. Centrex Consulting Corporation UKOUG .9 02:43.Gathering Histograms NAME n 100 o 100 p 100 n auto o auto p auto sql execute elapsed time 07:48.1 03:16.921 406.635 596.416 1.optimal 24 28 28 91 49 49 table scans (long tables) table scans (short tables) 24 © Wolfgang Breitling.469 458. 2008 .493 177 42 42 632 58 58 4 1 1 4 1 1 104 26 26 142 43 43 workarea executions .051 596.304 148.1 DB CPU 05:20.245 physical reads 1.219 147.241 407.0 02:26.December 3.642 1.350 6.7 06:07.869 140.

986 254 H H H H auto C1 1.999 254 H 507.847 254 H 1.236 75 H 377. 2008 .993 75 H 399.Gathering Histograms n o p NDV bkts NDV bkts NDV bkts 100 C1 7. Centrex Consulting Corporation UKOUG .994 254 C2 3.035 254 H 196 195 H 359.999 75 H 507.993 254 H 399.December 3.986 254 H 7.478 254 C4 377.153 254 H F H H col 25 © Wolfgang Breitling.050 254 C2 206 205 C3 366.993 254 C4 399.999 254 C3 507.990 254 H 377.364 254 H 844 75 F 174 75 H 362.994 75 H 3.986 254 H 7.994 254 H 3.

174 498.217 16. 'P1'.December 3. method_opt=>'for all columns size 1'.37 table_name P1 P1 P1 P1 P1 P1 P1 P1 P1 26 P1_2006_Q1 P1_2006_Q2 P1_2006_Q3 P1_2006_Q4 P1_2007_Q1 P1_2007_Q2 P1_2007_Q3 P1_2007_Q4 rows 4.217 © Wolfgang Breitling.736 16.000 500.705 blks empty avrow 129.731 498. estimate_percent=>100).Gather Partition Statistics gather_table_stats(user.217 16.000. Elapsed: 00:23:14.323 501.217 16.734 500. cascade=>true. 2008 .217 16.217 16.574 500.944 499.217 16. Centrex Consulting Corporation 0 0 0 0 0 0 0 0 0 224 225 224 224 224 224 225 224 225 last_analyzed 2008-01-27 2008-01-27 2008-01-27 2008-01-27 2008-01-27 2008-01-27 2008-01-27 2008-01-27 2008-01-27 12:16:36 11:54:05 11:54:18 11:54:29 11:54:43 11:54:54 11:55:05 11:55:16 11:55:27 UKOUG .217 16.815 499.

574 500.217 16.323 501.174 498.217 16.705 blks empty avrow 129.217 16.217 16. 'P1_2007_Q4'.944 499.000 500.217 16.000. 2008 .217 © Wolfgang Breitling.December 3.734 500.731 498. estimate_percent=>100).90 table_name P1 P1 P1_2006_Q1 P1 P1_2006_Q2 P1 P1_2006_Q3 P1 P1_2006_Q4 P1 P1_2007_Q1 P1 P1_2007_Q2 P1 P1_2007_Q3 P1 P1_2007_Q4 27 rows 4.Gather Partition Statistics gather_table_stats(user. P1_2007_Q4 cascade=>true.736 16.217 16. method_opt=>'for all columns size 1'. 'P1'.815 499.217 16. Centrex Consulting Corporation 0 0 0 0 0 0 0 0 0 224 225 224 224 224 224 225 224 225 last_analyzed 2008-01-27 13:40:24 2008-01-27 11:54:05 2008-01-27 11:54:18 2008-01-27 11:54:29 2008-01-27 11:54:43 2008-01-27 11:54:54 2008-01-27 11:55:05 2008-01-27 11:55:16 2008-01-27 13:20:25 UKOUG . Elapsed: 00:20:21.

000 500.217 16. cascade=>true.736 16. granularity=>'partition').174 498.815 499.705 blks empty avrow 129. 'P1_2007_Q4'.217 16.323 501. method_opt=>'for all columns size 1'. estimate_percent=>100.217 © Wolfgang Breitling.574 500.731 498.217 16. granularity=>'partition' Elapsed: 00:00:25. 2008 .December 3. Centrex Consulting Corporation 0 0 0 0 0 0 0 0 0 224 225 224 224 224 224 225 224 225 last_analyzed 2008-01-27 13:40:24 2008-01-27 11:54:05 2008-01-27 11:54:18 2008-01-27 11:54:29 2008-01-27 11:54:43 2008-01-27 11:54:54 2008-01-27 11:55:05 2008-01-27 11:55:16 2008-01-27 13:48:13 UKOUG . 'P1'.000.217 16.944 499.734 500.217 16.217 16.217 16.Gather Partition Statistics gather_table_stats(user.56 table_name P1 P1 P1_2006_Q1 P1 P1_2006_Q2 P1 P1_2006_Q3 P1 P1_2006_Q4 P1 P1_2007_Q1 P1 P1_2007_Q2 P1 P1_2007_Q3 P1 P1_2007_Q4 28 rows 4.

Centrex Consulting Corporation UKOUG . ap. c6. 2008 .December 3.---.------------------------------------0.----.'yyyy-mm-dd') d1 from p1 where fy = 2007 and ap = 10 and c1 = '00873‘ and c2 = '013912‘ and c3 = '000000740444‘ and c4 = '000000974796' ELAPSED CPU exec LIO PIO rows SQL_TEXT -------.036 0. to_char(d1. ap.013 1 14 2 1 select c5. fy.'yy | Id | Operation | Name | Rows | Pstart| Pstop | --------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | | | 1 | PARTITION RANGE SINGLE | | 1 | 8 | 8 | | 2 | TABLE ACCESS BY LOCAL INDEX ROWID| P1 | 1 | 8 | 8 | |* 3 | INDEX RANGE SCAN | P1_U | 1 | 8 | 8 | --------------------------------------------------------------------------3 .---.The CBO and Partition Pruning select c5.access("C1"='00873' AND "C2"='013912' AND "C3"='000000740444‘ AND "C4"='000000974796' AND "FY"=2007 AND "AP"=10) filter(("AP"=10 AND "FY"=2007)) 29 © Wolfgang Breitling. c6.----.-----. fy. to_char(d1.

Centrex Consulting Corporation UKOUG .264 0 225 2007-12-05 14:50:41 16.705 0 blks empty avrow last_analyzed 131. 2008 .217 0 224 2007-12-01 17:55:01 16.376 500.734 500.001.731 498.323 501.109 0 225 2007-12-01 17:56:11 0 0 0 2007-12-05 14:46:12 © Wolfgang Breitling.815 499.981 0 224 2007-12-01 17:54:18 16.4) gather_schema_stats(user.217 0 224 2007-12-01 17:53:57 16.574 500.217 0 224 2007-12-01 17:54:40 16.Partition Statistics – Add a Partition alter table p1 add partition "P1_2008_Q1" values less than (2008. table_name P1 P1 P1_2006_Q1 P1 P1_2006_Q2 P1 P1_2006_Q3 P1 P1_2006_Q4 P1 P1_2007_Q1 P1 P1_2007_Q2 P1 P1_2007_Q3 P1 P1_2007_Q4 P1 P1_2008_Q1 30 rows 4.options=>'GATHER AUTO').174 498.217 0 225 2007-12-01 17:55:26 16.944 499.217 0 225 2007-12-01 17:53:36 16.089 0 224 2007-12-01 17:55:48 17.December 3.

to_char(d1.---------------------------------1. c5. ap.'yyyy-mm-dd') d1 from p1 where fy = 2008 and ap = 1 and c1 = '00873‘ and c2 = '013912‘ and c3 = '000000740444‘ and c4 = '000000974796' ELAPSED CPU exec LIO PIO rows SQL_TEXT -------.----. | Id | Operation | Name | Rows | Pstart| Pstop | ---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | | | 1 | PARTITION RANGE SINGLE | | 1 | 9 | 9 | |* 2 | TABLE ACCESS BY LOCAL INDEX ROWID| P1 | 1 | 9 | 9 | |* 3 | INDEX RANGE SCAN | P1_I1 | 1 | 9 | 9 | ---------------------------------------------------------------------------2 . to_char(d1. 2008 .058 0. Centrex Consulting Corporation UKOUG . ap.December 3.filter(("C2"='016374' AND "C3"='000000809624' AND "C4"='000000719086')) 3 . c6.561 1.918 1 2.-----.-----. fy.access("FY"=2008 AND "AP"=1 AND "C1"='00797') filter("C1"='00797') 31 © Wolfgang Breitling.----.The CBO and Partition Pruning select c5.713 1 select fy.------. c6.

217 0 224 2007-12-01 17:53:57 16.944 499. Centrex Consulting Corporation UKOUG .'P1'. P1_2007_Q4 statid=>'P1_2007_Q4'.217 0 225 2007-12-01 17:53:36 16. dbms_stats.323 501.174 498.stattab=>'stats_table').734 500.'P1'.089 0 224 2007-12-01 17:55:48 17. 2008 . table_name P1 P1 P1_2006_Q1 P1 P1_2006_Q2 P1 P1_2006_Q3 P1 P1_2006_Q4 P1 P1_2007_Q1 P1 P1_2007_Q2 P1 P1_2007_Q3 P1 P1_2007_Q4 P1 P1_2008_Q1 32 rows 4.217 0 224 2007-12-01 17:54:40 16.217 0 224 2007-12-01 17:55:01 16.109 0 225 2007-12-01 17:56:11 0 0 0 2007-12-05 14:46:12 © Wolfgang Breitling.Cloning Statistics dbms_stats.'P1_2008_Q1'.December 3.705 0 blks empty avrow last_analyzed 131.stattab=>'stats_table').376 500.import_table_stats(user.217 0 225 2007-12-01 17:55:26 16.815 499.731 498.264 0 225 2007-12-05 14:50:41 16.'P1_2007_Q4'. P1_2008_Q1 statid=>'P1_2007_Q4'.981 0 224 2007-12-01 17:54:18 16.574 500.001.cascade=>true.export_table_stats(user.cascade=>true.

2008 .217 0 224 2007-12-01 17:55:01 16.731 498.981 0 224 2007-12-01 17:54:18 16.001.109 0 225 2007-12-01 17:56:11 © Wolfgang Breitling. P1_2008_Q1 statid=>'P1_2007_Q4'.December 3.705 499.217 0 224 2007-12-01 17:54:40 16. table_name P1 P1 P1_2006_Q1 P1 P1_2006_Q2 P1 P1_2006_Q3 P1 P1_2006_Q4 P1 P1_2007_Q1 P1 P1_2007_Q2 P1 P1_2007_Q3 P1 P1_2007_Q4 P1 P1_2008_Q1 33 rows 4.264 0 225 2007-12-05 14:50:41 16.217 0 225 2007-12-01 17:55:26 16. dbms_stats.734 500.323 501.705 blks empty avrow last_analyzed 131.217 0 225 2007-12-01 17:53:36 16.109 0 225 2007-12-01 17:56:11 17.import_table_stats(user.376 500. Centrex Consulting Corporation UKOUG .'P1'.217 0 224 2007-12-01 17:53:57 16.815 499.174 498.cascade=>true.089 0 224 2007-12-01 17:55:48 17.stattab=>'stats_table').944 499.574 500. commit.'P1_2008_Q1'.Cloning Statistics Update stats_table set c2 = 'P1_2008_Q1' P1_2008_Q1 where statid = 'P1_2007_Q4' and c2 = 'P1_2007_Q4'.

EAVS := 0. # SREC.MINVAL := NULL.NOVALS := DBMS_STATS.0.NULL. … 34 © Wolfgang Breitling.'"P1_2007_Q4"'.MAXVAL := NULL.'"P1_2007_Q4"'.NULL.2). END.2).file=clone. BEGIN SREC.0.0. 2008 .0. SREC. DBMS_STATS.'"P1"'.NULL.STATREC.December 3. SREC.srec.BKVALS := DBMS_STATS. END. exporting table P1 EXP-00091: Exporting questionable statistics.'"C1"'. P1_2007_Q4 0. clone. .SET_TABLE_STATS(NULL.NUMARRAY( 0.'"P1"'.NUMARRAY( 0.rows=no.1o ).0& ).tables=P1:P1_2007_Q4 .Cloning Statistics – Method “exp” exp scott/tiger.dmp: ANALSTATS TR "P1“ U BEGIN DBMS_STATS. SREC.0.CHVALS := NULL. SREC. SREC.0.SET_COLUMN_STATS(NULL.EPC := 2. P1_2007_Q4 NULL. ANALSTATS TR "P1“ ýÿw DECLARE SREC DBMS_STATS. Centrex Consulting Corporation UKOUG .

Cloned Statistics select c5. Centrex Consulting Corporation UKOUG . ap.-----.-----. to_char(d1.968 0.December 3.filter(("C3"='000000809624' AND "C4"='000000719086' AND "C2"='016374')) 3 .'yyyy-mm-dd') d1 from p1 where fy = 2008 and ap = 1 and c1 = '00873‘ and c2 = '013912‘ and c3 = '000000740444‘ and c4 = '000000974796' ELAPSED CPU exec LIO PIO rows SQL_TEXT -------.------. c5.access("FY"=2008 AND "AP"=1 AND "C1"='00797') filter("C1"='00797') 35 © Wolfgang Breitling.----.----.---------------------------------0. 2008 . to_char(d1. ap. c6. | Id | Operation | Name | Rows | Pstart| Pstop | ---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | | | 1 | PARTITION RANGE SINGLE | | 1 | 9 | 9 | |* 2 | TABLE ACCESS BY LOCAL INDEX ROWID| P1 | 1 | 9 | 9 | |* 3 | INDEX RANGE SCAN | P1_I1 | 1 | 9 | 9 | ---------------------------------------------------------------------------2 .724 1 select fy.968 1 2.564 1. fy. c6.

00 CLUF: 499675. 2008 .00 CLUF: 499675.00 DB/K: 1.00 DB/K: 1.00 Index Stats:: Index: P1_I1 Col#: 7 8 9 1 PARTITION [8] LVLS: 2 #LB: 2231 #DK: 296545 LB/K: 1.00 36 © Wolfgang Breitling.00 DB/K: 1.00 CLUF: 499667.00 CLUF: 499667.00 #Rows: 499705 #Blks: 17109 AvgRowLen: 225.00 Index: P1_U Col#: 1 2 3 4 9 7 8 PARTITION [8] LVLS: 2 #LB: 4462 #DK: 499705 LB/K: 1.Cloned Statistics BASE STATISTICAL INFORMATION *********************** Table Stats:: Table: P1 Alias: P1 Partition [8] #Rows: 499705 #Blks: 17109 AvgRowLen: 225.00 DB/K: 1.00 LVLS: 2 #LB: 4462 #DK: 499705 LB/K: 1.December 3.00 LVLS: 2 #LB: 2231 #DK: 296545 LB/K: 1. Centrex Consulting Corporation UKOUG .

00 vs.04 Degree: 1 37 © Wolfgang Breitling.0006e-06 of col #8 as selectivity of out-of-range value pred […] Access Path: index (skip-scan) SS sel: 2. index scan io: 1.December 3.Cloned Statistics Column (#8): AP(NUMBER) AvgLen: 3.00 NDV: 3 Nulls: 0 Density: 0.04 Resp: 4.00 Skip Scan rejected Access Path: index (RangeScan) Index: P1_I1 resc_io: 4.00 resc_cpu: 26374 ix_sel: 2. Centrex Consulting Corporation UKOUG .4629e-15 ANDV (#skips): 530 SS io: 530.33333 Min: 10 Max: 12 Using prorated density: 1. 2008 .4629e-15 Cost: 4.0024e-12 ix_sel_with_filters: 2.

00 Skip Scan rejected Access Path: index (RangeScan) Index: P1_U resc_io: 4.04 Card: 0.04 Resp: 4. Centrex Consulting Corporation UKOUG .00 vs.00 resc_cpu: 26374 ix_sel: 1.4833e-30 Cost: 4.04 Degree: 1 Resp: 4. index scan io: 1.December 3.04 Degree: 1 […] Best:: AccessPath: IndexRange Index: P1_I1 Cost: 4.Cloned Statistics Access Path: index (skip-scan) SS sel: 2.2402e-18 ix_sel_with_filters: 2.00 Bytes: 0 38 © Wolfgang Breitling.4833e-30 ANDV (#skips): 530 SS io: 530. 2008 .

CHVALS := NULL.'P1'.6). SREC. NUMARRAY BEGIN SREC.NOVALS).CHVALS := NULL.1.NUMARRAY(1. SREC.December 3.EPC := 2.'FY'.PREPARE_COLUMN_VALUES (SREC.NOVALS). NOVALS DBMS_STATS. SREC. Centrex Consulting Corporation UKOUG . NOVALS := DBMS_STATS.1.EPC := 2. 2008 . NUMARRAY SREC.1).'AP'. SREC.0.'P1'.3. DBMS_STATS. 39 © Wolfgang Breitling.Prepare and set Column Stats DECLARE SREC DBMS_STATS.srec.NUMARRAY(0. DBMS_STATS.EAVS := 0.NUMARRAY.4.EAVS := 0. SREC.NULL.3).SET_COLUMN_STATS(user.NUMARRAY(0. NUMARRAY SREC. NOVALS DBMS_STATS.SET_COLUMN_STATS(user.1/3.3.'P1_2008_Q1'.NULL. NOVALS := DBMS_STATS.srec.NUMARRAY(2008.2008).PREPARE_COLUMN_VALUES (SREC.NULL.BKVALS := DBMS_STATS.0.STATREC. NOVALS DBMS_STATS.NULL.6).BKVALS := DBMS_STATS. END.1).'P1_2008_Q1'.

3333E-01 1 2008 3 UKOUG .770 7.-------.Prepare and set Column Stats table partition column NDV density LOW -----.-----------. 2008 . Centrex Consulting Corporation 1.8309E-05 005680 HIGH -----01255 024231 […] FY AP 40 1 3 © Wolfgang Breitling.----------.------.-------P1 P1_2008_Q1 C1 813 1.0000E+00 2008 3.2300E-03 00336 C2 12.December 3.

December 3. fy.----. to_char(d1.----. Centrex Consulting Corporation UKOUG .031 0. | Id | Operation | Name | Rows | Pstart| Pstop | --------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | | | 1 | PARTITION RANGE SINGLE | | 1 | 9 | 9 | | 2 | TABLE ACCESS BY LOCAL INDEX ROWID| P1 | 1 | 9 | 9 | |* 3 | INDEX RANGE SCAN | P1_U | 1 | 9 | 9 | --------------------------------------------------------------------------3 .017 1 6 3 1 select fy. c6. to_char(d1. 2008 .----. c6. ap. c5.The CBO and Partition Pruning select c5. ap.---------------------------------0.access("C1"='00797' AND "C2"='016374' AND "C3"='000000809624' AND "C4"='000000719086' AND "FY"=2008 AND "AP"=1) filter(("AP"=1 AND "FY"=2008)) 41 © Wolfgang Breitling.-----.'yyyy-mm-dd') d1 from p1 where fy = 2008 and ap = 1 and c1 = '00873‘ and c2 = '013912‘ and c3 = '000000740444‘ and c4 = '000000974796' ELAPSED CPU exec LIO PIO rows SQL_TEXT -------.----.

DBMS_STATS.1). '1234'.DATEARRAY( DATEARRAY date '2007-10-01'. CHVALS := DBMS_STATS. SREC.EAVS := 0. CHARARRAY DTVALS DBMS_STATS.CHVALS := NULL. SREC.SET_COLUMN_STATS(…).PREPARE_COLUMN_VALUES (SREC. Centrex Consulting Corporation UKOUG .CHVALS).CHVALS := NULL.BKVALS := DBMS_STATS.More Prepare Column Stats DECLARE SREC DBMS_STATS.DTVALS). SREC. DTVALS := DBMS_STATS. DBMS_STATS. DBMS_STATS. SREC.'5678').December 3.NUMARRAY(0. DATEARRAY BEGIN SREC.EAVS := 0.EPC := 2.'5678' SREC.SET_COLUMN_STATS(…).CHARARRAY.EPC := 2.CHARARRAY( CHARARRAY '1234'. CHVALS DBMS_STATS.date '2007-12-31'). 42 © Wolfgang Breitling.PREPARE_COLUMN_VALUES (SREC. CHVALS DBMS_STATS.DATEARRAY.BKVALS := DBMS_STATS. END. SREC. 31' SREC.STATREC.1). 2008 .NUMARRAY(0.

December 3. 2852104.11414. Centrex Consulting Corporation UKOUG . END.BKVALS := DBMS_STATS.'I'.28).EPC := 5.PREPARE_COLUMN_VALUES (SREC.5993.'H'. CHARARRAY val count D 2852104 H 22 I 11414 N 5993 Z 28 BEGIN SREC.'H'.'Z').'N'.CHARARRAY( CHARARRAY 'D'.CHVALS := NULL. 'D'.5993.STATREC. 2008 .22.'I'.CHVALS). CHVALS := DBMS_STATS.SET_COLUMN_STATS(…).Create a Frequency Histogram DECLARE SREC DBMS_STATS.11414.CHARARRAY. CHVALS DBMS_STATS.28 DBMS_STATS. 43 © Wolfgang Breitling.EAVS := 0.'N'. CHVALS DBMS_STATS. SREC.'Z' SREC. SREC.22.NUMARRAY(2852104.

Centrex Consulting Corporation UKOUG .N2 BETWEEN 1400000000 AND 1550000000 ) AND L.798 4. T4 L1.N2 BETWEEN 1 AND 12 AND L1.798 SELECT L1.C1 = L.C3 <= L2. L2.978 95.0004E-05 1.C3 >= L2.N1 © Wolfgang Breitling. 2008 .C1 AND A.C7 = ' ' GROUP BY L1.9583E-06 1.N2 BETWEEN 1240000000 AND 1300000000 OR L1.C2 AND ( L2. L2.N1.N1. T4 L2.C2 AND ( L1.N2 BETWEEN 1750000000 AND 2000000000 AND A.N1 = 2005 AND A.Sampled Histograms table column T4 N1 N2 C1 C2 tbl T4 T4 T4 … T4 T4 T4 T4 T4 T4 T4 T4 T4 T4 44 NDV 29 99.N3) N3_SUM FROM T3 A.0522E-05 col EP value card N1 2325 0 2325 N1 3066 1 741 N1 3553 2 487 N1 N1 N1 N1 N1 N1 N1 N1 N1 N1 4782 4784 4786 4789 4791 4794 4795 4796 4797 4798 19 20 21 22 23 24 26 27 29 32 6 2 2 3 2 3 1 1 1 1 bkts 29 254 1 1 H F H N N sample 4.December 3.C6 = 'USD' AND A.042 density 4.N2 BETWEEN 1410000000 AND 1420000000 OR L1.798 4.N1 = 2 AND A.C4 <= L1.N1.057 95.N2 BETWEEN 1310000000 AND 1400000000 OR L1.0520E-05 1. L2 T5 L WHERE A.C1 AND A.C1 AND L.C4 >= L1.N1 = 26 AND A.798 4.N2 BETWEEN 1430000000 AND 1440000000 ) AND L2.N2 BETWEEN 1250000000 AND 1379999999 OR L2.N1 = 23 AND A. SUM (A.

099 3 1 NESTED LOOPS 1 60.074.December 3. 2008 .771 3.771 4 2 MERGE JOIN CARTESIAN 1 92 5AF 1 INDEX RANGE SCAN T4_U [L1] 1 4 6 24 BUFFER SORT 4 92 7AF 24 INDEX RANGE SCAN T4_U [L2] 1 23 8F 1 TABLE ACCESS BY INDEX ROWID T3 [A] 92 60.466 PIO 1.Sampled Histograms elapsed 5.920 LIO 1.146 cpu 763.616.283. Centrex Consulting Corporation UKOUG .534.240 rows 1 id card operation starts rows 0 SELECT STATEMENT ALL_ROWS 1 1 SORT GROUP BY NOSORT 1 1 2 1 NESTED LOOPS 1 3.099 45 © Wolfgang Breitling.771 9AF 1 INDEX RANGE SCAN T3_F [A] 92 1.204 10AF 1 INDEX RANGE SCAN T5_U [L] 60.

GET_COLUMN_STATS ( ownname => user.srec. nullcnt => l_nullcnt. avgclen => l_avgclen .EPC). l_prorate number.December 3. END. l_prorate := l_numrows/SREC. density => l_density.Pro-rate the Frequency H’gram DECLARE srec DBMS_STATS. nullcnt => l_nullcnt.SET_COLUMN_STATS ( ownname => user. distcnt => l_distcnt. 2008 .STATREC. distcnt => l_distcnt. srec => SREC ). END LOOP. 46 © Wolfgang Breitling. density => l_density. avgclen => l_avgclen. colname => 'N1'. tabname => 'T4'. FOR I in 1. select num_rows into l_numrows from user_tables where table_name = 'T4'.BKVALS(I) := round(SREC. l_density NUMBER.0). l_nullcnt NUMBER. tabname => 'T4'. srec => SREC. force => TRUE).BKVALS(SREC.epc LOOP SREC.BKVALS(I)*l_prorate. DBMS_STATS. l_avgclen NUMBER. BEGIN DBMS_STATS.. Centrex Consulting Corporation UKOUG . no_invalidate => FALSE. l_numrows NUMBER. colname => 'N1'. l_distcnt NUMBER.

Centrex Consulting Corporation UKOUG .Sampled Histograms table T4 T4 T4 … T4 T4 T4 T4 T4 T4 T4 T4 T4 T4 47 column N1 N1 N1 N1 N1 N1 N1 N1 N1 N1 N1 N1 N1 EP 48458 63902 74052 value 0 1 2 card 48458 15444 10150 99667 99708 99750 99812 99854 99917 99937 99958 99979 100000 19 20 21 22 23 24 26 27 29 32 126 41 42 62 42 63 20 21 21 21 © Wolfgang Breitling. 2008 .December 3.

551 18.409 383.790 cpu 296.December 3.Sampled Histograms elapsed 1.169 LIO 789.777 rows 1 id card operation starts 0 SELECT STATEMENT ALL_ROWS 1 1 SORT GROUP BY NOSORT 1 2 1 NESTED LOOPS 1 3 1 NESTED LOOPS 1 4 1 NESTED LOOPS 1 5AF 24 INDEX RANGE SCAN T4_U [L2] 1 6F 1 TABLE ACCESS BY INDEX ROWID T3 [A] 23 7AF 1 INDEX RANGE SCAN T3_F [A] 23 8AF 1 INDEX RANGE SCAN T5_U [L] 363.099 18.409 23 363.522 363.522 3.222 PIO 268.522 48 © Wolfgang Breitling.409 9AF 1 INDEX RANGE SCAN T4_U [L1] 18.099 UKOUG .316. Centrex Consulting Corporation ROWS 1 3. 2008 .

177 CLUF 23.140 index INDEX_B_U blks 1.141 VIEW [from\$_subquery\$_003] UNION-ALL 1 PARTITION RANGE SINGLE :4-4 1 TABLE ACCESS FULL TABLE_A [B]:4-4 72.141 HASH GROUP BY 72.Index Rebuild Danger xxx table TABLE_B table TABLE_B id 0 1A 2 3A 4 5F 6 7 8 9 10F 11 49 rows 72. Centrex Consulting Corporation UKOUG .140 empty av row 0 141 #LB 1.December 3.504 unique NDV U 72. 2008 .193 card operation SELECT STATEMENT ALL_ROWS 1 HASH JOIN SEMI 1 TABLE ACCESS BY INDEX ROWID TABLE_B [A] 1 INDEX RANGE SCAN INDEX_B_U [A] 72.140 INDEX FAST FULL SCAN TABLE_B [C] © Wolfgang Breitling.141 VIEW VW_NSO_1 [VW_NSO_1] FILTER 72.

749 card operation SELECT STATEMENT ALL_ROWS 1 TABLE ACCESS BY INDEX ROWID TABLE_B [A] 1 INDEX RANGE SCAN INDEX_B_U [A] FILTER 72.December 3.Index Rebuild Danger xxx table TABLE_B table TABLE_B id 0 1 2AF 3F 4 5 6 7 8F 9 50 rows 72.141 HASH GROUP BY 72.140 TABLE ACCESS FULL TABLE_B [C] © Wolfgang Breitling.141 VIEW [from\$_subquery\$_003] UNION-ALL 1 PARTITION RANGE SINGLE :6-6 1 TABLE ACCESS FULL TABLE_A [B]:6-6 72. 2008 .140 index INDEX_B_U unique U blks 1.807.924 #LB 45.804 CLUF 902. Centrex Consulting Corporation UKOUG .504 empty av row 0 141 NDV 2.

Available from http://structureddata.html.com/2008/01/ improvement-of-auto-sampling-statistics. Managing CBO Stats During an Upgrade from 9i to 10g Optimizer Development Group.com/technology/products/bi/db/11g/pdf/twp_bid w_dss_perf_11gr1.oracle.org/2007/09/17/ oracle-11g-enhancements-to-dbms_stats/ Seiler.References Jiang.blogspot. DSS Performance in Oracle Database 11g Available from http://www. Improvement of Auto Sampling Statistics Gathering Feature in Oracle 11g [Blog]. ISBN: 1590596366 Metalink 465787. Oracle 11g: Enhancements to Dbms_Stats [Blog]. L. Jonathan. Available from http://seilerwerks. Apress. Don. Cost-Based Oracle: Fundamentals.wordpress. Rahn. Centrex Consulting Corporation UKOUG . Available from http://optimizermagic. Dr.com/2007/08/17/dr-statslove-or-how-ilearned-to-stop-guessing-and-love-the-10053-trace/ 51 © Wolfgang Breitling. Greg.1. Statslove Or: How I Learned to Stop Guessing and Love the 10053 Trace [Blog].December 3.pdf Lewis. 2008 .

www.Wolfgang Breitling breitliw@centrexcc.com Centrex Consulting Corp.com .centrexcc.