## Re: Strange heuristic in analyze.c

From: Greg Stark "" Strange heuristic in analyze.c 2010-01-29 16:38:10 407d949e1001290838t247fd8e6kac432453ed1d910a@mail.gmail.com (view raw)
```So I never realized the consequences of this little heuristic in
analyze.c in the handling of very low cardinality columns where we
want to just capture the complete list of values in the mcv and throw
away the histogram:

else if (toowide_cnt == 0 && nmultiple == ndistinct)
{
/*
* Every value in the sample appeared more than once.  Assume the
* column has just these values.
*/
}

The problem with this heuristic is that if the table is small enough
you might expect you can set the statistics target high and "sample"
the entire table and get a very accurate mcv covering all the values.
However if any of the values in the table appears only once this
heuristic will defeat you. The following code will then throw out of
the mcv any value which isn't 25% more common than "average". Leaving
you with a histogram for those values which often does very poorly if
the values don't fit any pattern and are just discrete arbitrary
values.

--
greg

```
From: Bruce Momjian Greg Stark "" Re: Strange heuristic in analyze.c 2010-02-05 20:53:57 201002052053.o15KrvG09347@momjian.us (view raw)
```Greg Stark wrote:
> So I never realized the consequences of this little heuristic in
> analyze.c in the handling of very low cardinality columns where we
> want to just capture the complete list of values in the mcv and throw
> away the histogram:
>
> 		else if (toowide_cnt == 0 && nmultiple == ndistinct)
> 		{
> 			/*
> 			 * Every value in the sample appeared more than once.  Assume the
> 			 * column has just these values.
> 			 */
> 		}
>
> The problem with this heuristic is that if the table is small enough
> you might expect you can set the statistics target high and "sample"
> the entire table and get a very accurate mcv covering all the values.
> However if any of the values in the table appears only once this
> heuristic will defeat you. The following code will then throw out of
> the mcv any value which isn't 25% more common than "average". Leaving
> you with a histogram for those values which often does very poorly if
> the values don't fit any pattern and are just discrete arbitrary
> values.

Do you want a C comment to document this problem?

--
Bruce Momjian  <bruce(at)momjian(dot)us>        http://momjian.us
EnterpriseDB                             http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

```
From: Greg Stark Bruce Momjian "" Re: Strange heuristic in analyze.c 2010-02-08 18:19:04 407d949e1002081019i3721686bh3ab61ce4eb7adfc9@mail.gmail.com (view raw)
```On Fri, Feb 5, 2010 at 8:53 PM, Bruce Momjian <bruce(at)momjian(dot)us> wrote:
> Do you want a C comment to document this problem?

Well I would rather a better heuristic :)

We really need some statistics nerds in this group who can pipe up
when these kinds of issues come up. There must be a good way to
estimate the probability that we've seen all distinct values.

--
greg

```