From: Nick Fankhauser <nickf@ontko.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-admin <pgsql-admin@postgresql.org>
Subject: Re: Problem with n_distinct being consistently inaccurate.
Date: Tue, 23 Sep 2003 15:03:57 -0500
Message-ID: <NEBBLAAHGLEEPCGOBHDGOEIOIJAA.nickf@ontko.com> (raw)
In-Reply-To: <20330.1064338636@sss.pgh.pa.us>
> AFAIK, estimating number of distinct values from a small sample is
> inherently an ill-conditioned problem.
If I had been getting estimates all over the map, I'd have been a bit more
unconcerned, but what I'm seeing is a very consistent number that also
increases and tends to be more consistent in proportion to the stats
"target" number. This makes me think that there is more at work here than
the inaccuracy likely to occur from small samples. It's as if the algorithm
and sample size (even at default) are pretty reasonable for returning
consistent results in this case, but a multiplier needs to be changed.
For instance, with the various values for statistics, if I do an analyze on
the table and then look at n_distinct 6 times, these are the results I get:
(actual number is 92,000)
set statistics = -1 (default):
13549
14268
14772
14518
13863
13526
mean = 14083
std dev = 518 or 3.7% of mean
set statistics = 100
22457
22598
22566
22580
22767
22490
mean = 22576
std dev = 108 or .5% of mean
set statistics = 500
39878
39984
40018
39977
39885
40070
mean = 39968
std dev = 75 or .2% of mean
set statistics = 1000
51428
51503
51486
51658
51625
51589
mean = 51548
std dev = 74 or .1% of mean
> You could try sticking the correct n_distinct into pg_statistic by hand
> just to see if it really does change the plan
OK... but I'm a bit confused on how to get to the right row in pg_statistic.
when I do a \d on pg_stats, it appears that pg_statistic.starelid matches up
with pg_class.oid, but apparently this is not the case. Is there a place I
can look to find which keys correspond among the pg_catalog tables?
> but I'd like to think
> that getting within a factor of 2 is good enough.
Probably so... but with the default stats, it is more like a factor of 6,
which seems significant to me, and if my conjecture is correct, it might be
an easy fix. (Easy for me to say, since I'm not a developer. <grin>)
-Nick
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: nickf@ontko.com, tgl@sss.pgh.pa.us
Subject: Re: Problem with n_distinct being consistently inaccurate.
In-Reply-To: <NEBBLAAHGLEEPCGOBHDGOEIOIJAA.nickf@ontko.com>
References: <20330.1064338636@sss.pgh.pa.us> <NEBBLAAHGLEEPCGOBHDGOEIOIJAA.nickf@ontko.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox