From: Pavel Luzanov <p.luzanov@postgrespro.ru>
To: pgsql-general@lists.postgresql.org
Subject: Re: n_distinct off by a factor of 1000
Date: Thu, 25 Jun 2020 16:27:08 +0300
Message-ID: <b750644a-9bad-d4fe-0b85-7ebaa6053f00@postgrespro.ru> (raw)
In-Reply-To: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com>
References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com>
Hello,
> I got my first hint of why this problem occurs when I looked at the
> statistics. For the column in question, "instrument_ref" the
> statistics claimed it to be:
>
> The default_statistics_target=500, and analyze has been run.
> select * from pg_stats where attname like 'instr%_ref'; -- Result:
> *40.000*
> select count(distinct instrumentid_ref) from bigtable -- Result: *33
> 385 922 (!!)*
>
> That is an astonishing difference of almost a 1000X.
> I have tried to increase the statistics target to 5000, and it helps,
> but it reduces the error to 100X. Still crazy high.
As far as I know, increasing default_statistics_target will not help. [1]
> I have considered these fixes:
> - hardcode the statistics to a particular ratio of the total number of
> rows
You can hardcode the percentage of distinct values:
ALTER TABLE bigtable ALTER COLUMN instrument_ref SET ( n_distinct=-0.06
); /* -1 * (33385922 / 500000000) */
[1]
https://www.postgresql.org/message-id/4136ffa0812111823u645b6ec9wdca60b3da4b00499%40mail.gmail.com
-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
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-general@postgresql.org
Cc: p.luzanov@postgrespro.ru
Subject: Re: n_distinct off by a factor of 1000
In-Reply-To: <b750644a-9bad-d4fe-0b85-7ebaa6053f00@postgrespro.ru>
References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> <b750644a-9bad-d4fe-0b85-7ebaa6053f00@postgrespro.ru>
* 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