Oracle FAQ Your Portal to the Oracle Knowledge Grid

Home -> Community -> Mailing Lists -> Oracle-L -> RE: Any quick way to remove histograms?

RE: Any quick way to remove histograms?

From: Powell, Mark D <>
Date: Tue, 27 Mar 2007 13:41:36 -0400
Message-ID: <>


One advantage of spooling a file and executing the file is that you can have the execution file also create a spool file with the commands and results in the output. This allows verification that the task did run successfully. Script files also allow copying and pasting of individual commands when only a partial update is desired or some other manipulation of the parameters is desired for a specific statement. In another word, flexibility.

-----Original Message-----
[] On Behalf Of
Sent: Tuesday, March 27, 2007 11:17 AM
To:; Cc:
Subject: RE: Any quick way to remove histograms?

Why indeed?


Joel Patterson
Database Administrator
904 727-2546

-----Original Message-----
[] On Behalf Of Wolfgang Breitling Sent: Tuesday, March 27, 2007 9:20 AM
Subject: RE: Any quick way to remove histograms?

Why spool, capture and then execute when you can do it all in one:


   l_ndv number;
   l_dummy number;
   l_srec dbms_stats.statrec;


   for r in (select owner, table_name, column_name, null partition_name from DBA_TAB_COL_STATISTICS

         histogram <> 'NONE' [ and possibly other criteria ]
       union all
       select owner, table_name, column_name, partition_name from
         histogram <> 'NONE' [ and possibly other criteria ]
     ) loop
     dbms_stats.get_column_stats (
       ownname => r.owner,
       tabname => r.table_name,
       colname => r.column_name,
       partname => r.partition_name,
       stattab => null,
       statid => null,
       distcnt => l_ndv,
       density => l_dummy,
       nullcnt => l_dummy,
       srec => l_srec,
       avgclen => l_dummy);
     dbms_stats.set_column_stats (
       ownname => r.owner,
       tabname => r.table_name,
       colname => r.column_name,
       partname => r.partition_name,
       distcnt => l_ndv);

   end loop;

and no fighting with quotes :-)

I believe if you have subpartitions you will need to export the statistics to a stattab table, manipulate the statistics there and then re-import as there is no set_column_stats for subpartitions.

At 06:23 PM 3/26/2007, Allen, Brandon wrote:
>At first I was thinking that Alberto's procedure would still be
>for retrieving the current stats and then resetting just the distcnt
>value. However, after thinking it through for a while, I realized what

>you were probably both thinking the whole time, which is that with this

>simplified method, you can just use a simple SELECT statement on
>all_tab_col_statistics to retrieve the current num_distinct value and
>plug it into the set_column_stats procedure for all columns that
>currently have histograms, thereby creating a script that can be used
>very quickly remove all existing histograms. It took me a while to get

>all the single quotes just right, but here is the working version for
>anyone else that might find it helpful:
>select 'execute dbms_stats.set_column_stats(''' || owner || ''',''' ||
>table_name || ''',''' || column_name || ''', distcnt=>''' ||
>num_distinct || ''');' from all_tab_col_statistics where histogram <>
>Sample Output:
>execute dbms_stats.set_column_stats('SCOTT','T','C1', distcnt=>'2421');

>execute dbms_stats.set_column_stats('SCOTT','T','C2', distcnt=>'2425');

>execute dbms_stats.set_column_stats('SCOTT','T','C3', distcnt=>'2421');
>Thanks again to Wolfgang and Alberto!
>-----Original Message-----
>From: Alberto Dell'Era []
>Sent: Friday, March 16, 2007 4:43 PM
>Cc: Allen, Brandon;
>Subject: Re: Any quick way to remove histograms?
>Much simpler :) - interesting.
>On 3/16/07, Wolfgang Breitling <> wrote:
> >
> > I also did a quick check and just using
> >
> > exec
> >

> > istcnt=>
> > <num_distinct>);
> >
> > will remove the histogram without removing the low_value and
> >
> >
> >
> > At 01:40 PM 3/16/2007, Alberto Dell'Era wrote:
> > >On 3/16/07, Allen, Brandon <> wrote:
> > >>Is there any faster way to remove histograms other than
> > >>the table? I want to keep the existing table, index & column
> > >>but with only 1 bucket (i.e. no histograms).
> > >
> > >You might try the attached script, that reads the stats using
> > >dbms_stats.get_column_stats and re-sets them, minus the histogram,
> > >using dbms_stats.set_column_stats.
> > >
> > >I haven't fully tested it - it's only 10 minutes old, even if I
> > >slightly modified for you another script I've used for quite some
> > >time - and the spool on seems to confirmthat the histogram

> > >is, indeed, removed, while all the other statistics are preserved.
> > >have also reset density to 1/num_distinct, that is the value you
> > >if no histogram is collected.
>Privileged/Confidential Information may be contained in this message or

>attachments hereto. Please advise immediately if you or your employer
>do not consent to Internet email for messages of this kind.
>Opinions, conclusions and other information in this message that do not

>relate to the official business of this company shall be understood as
>neither given nor endorsed by it.


Wolfgang Breitling
Centrex Consulting Corporation

This email has been scanned by the MessageLabs Email Security System. For more information please visit


Received on Tue Mar 27 2007 - 12:41:36 CDT

Original text of this message