Re: serious under-estimation of n_distinct for clustered distributions - Mailing list pgsql-performance

From Claudio Freire
Subject Re: serious under-estimation of n_distinct for clustered distributions
Date
Msg-id CAGTBQpbjdoKob-7M9oO_q9=OJ6Kt0G8dVK1VoZEpxtxCKLbgdw@mail.gmail.com
Whole thread Raw
In response to serious under-estimation of n_distinct for clustered distributions  (Stefan Andreatta <s.andreatta@synedra.com>)
List pgsql-performance
On Sat, Dec 29, 2012 at 5:57 PM, Stefan Andreatta
<s.andreatta@synedra.com> wrote:
>  n*d / (n - f1 + f1*n/N)
>
> where f1 is the number of values that occurred only once in the sample. n is
> the number of rows sampled, d the number of distincts found and N the total
> number of rows in the table.
>
...
>
> When the number of values that are found only once in the sample (f1)
> becomes zero, the whole term equals d, that is, n_distinct is estimated to
> be just the number of distincts found in the sample.

I think the problem lies in the assumption that if there are no
singly-sampled values, then the sample must have included all distinct
values. This is clearly not true even on a fully random sample, it
only means those sampled distinct values appear frequently enough to
be excluded from the sample.

In the clustered case, the error would be evident if the
randomly-sampled pages were split into two samples, and considered
separately. The distinct set in one would not match the distinct set
in the other, and the intersection's size would say something about
the real number of distinct values in the whole population.


pgsql-performance by date:

Previous
From: Stefan Andreatta
Date:
Subject: Re: serious under-estimation of n_distinct for clustered distributions
Next
From: John Kasarda
Date:
Subject: Slow connections on Win 7