Compare statistics, good idea, Ok, here is the scenario
You have a large table where you have collected the statistics with sample of 10%, and due to some reasons you want to collect the statistics with auto or 100% sample size, but you are unsure of the difference it make. Note its not about to restoring the old statistics, but its about to compare the statistics changes over the period.
Oracle 11g (R1) comes up with three new procedures in dbms_stats package.
The DIFF_TABLE_STATS_
* statistics can be used to compare statistics for a table from two different sources. The statistics can be from:
two different user statistics tables
a single user statistics table containing two sets of statistics that can be identified using statids
a user statistics table and dictionary history
pending statistics
- The functions also compare the statistics of the dependent objects (indexes, columns, partitions). They displays statistics of the object(s) from both sources if the difference between those statistics exceeds a certain threshold (%). The threshold can be specified as an argument to the function, with a default of 10%. The statistics corresponding to the first source (
stattab1
or time1
) will be used as basis for computing the differential percentage.For example,
you table collected the stats at some estimate percent.
-- Gather statistics with a manually specified sample size
BEGIN dbms_stats.gather_table_stats('SH','SALES', estimate_percent=>'0.000001'); END; /
-- Copy statisitcs gathered with manually set sample size to
exec dbms_stats.export_table_stats ('SH','SALES',stattab=>'MY_STAT_TAB',statid=>'manually_set_sample_size'); END; /
-- delete existing statistics
BEGIN dbms_stats.delete_table_stats('SH','SALES'); END; /
-- Gather statistics with AUTO_SAMPLE_SIZE
BEGIN dbms_stats.gather_table_stats('SH','SALES'); END; /
Now using dbms_stats.diff_table_stats_in_stattab you can compare the existing statistics and the old statistics in the statistics table.
set long 99999999
set longchunksize 99999
Select * From table(dbms_stats.diff_table_stats_in_stattab('SH','SALES', 'MY_STAT_TAB', statid1=>'MANUALLY_SET_SAMPLE_SIZE')); --
Sample Output:-
Source:- https://blogs.oracle.com/optimizer/entry/how_do_i_compare_statistics
-Thanks
Geek DBA
Follow Me!!!