Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jpfHr-0005hk-Pm for pgsql-general@arkaria.postgresql.org; Sun, 28 Jun 2020 21:56:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jpfHJ-0006Uh-5d for pgsql-general@arkaria.postgresql.org; Sun, 28 Jun 2020 21:55:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jpfHI-0006UZ-Tk for pgsql-general@lists.postgresql.org; Sun, 28 Jun 2020 21:55:56 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jpfHE-0004e8-SU for pgsql-general@lists.postgresql.org; Sun, 28 Jun 2020 21:55:56 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 286BC3353; Sun, 28 Jun 2020 23:55:50 +0200 (CEST) Date: Sun, 28 Jun 2020 23:55:50 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: n_distinct off by a factor of 1000 Message-ID: <20200628215550.GA17947@hjp.at> References: <20200624203519.GA24048@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="mP3DRpeJDSE+ciuQ" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --mP3DRpeJDSE+ciuQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-06-24 16:27:35 -0600, Michael Lewis wrote: > On Wed, Jun 24, 2020, 2:35 PM Peter J. Holzer wrote: >=20 > Yes, estimating the number of distinct values from a relatively small > sample is hard when you don't know the underlying distribution. It mi= ght > be possible to analyze the sample to find the distribution and get a > better estimate. But I'm not sure how useful that would really be: If > a few values are very common and most very rare you are probably also > much more likely to use the common values in a query: And for those y= ou > you would massively underestimate their frequency if you had an accur= ate > n_distinct value. That might be just as bad or even worse. >=20 >=20 >=20 > This would only be true for values that are "common" but not in the MCVs = list, > right? Yes, but if you have 33 million values there are likely to be a lot of them "common but not in the MCVs list", even for a very biased distribution. > If we could increase the sampling ratio beyond the hard coded 300x to get= a > more representative sample I thought of that but abandoned it since I don't think a better estimate for n_distinct will help (see above for the reason). The problem is that the distribution is biased and the planner has no idea whether the value it is searching for is common or rare if it isn't in the MCV list. Unless ... As I understood Klaudie, the values are ids, and ids have no inherent meaning, the common values are probably scattered randomly. But it might be possible to change that. Group the ids by frequency. Ids < 1E12 occur at most 10 times, Ids >=3D 1E12 <2E12 occur at most 100 times and so on.=20 This may mean that ids aren't long time stable - they may change as their frequency changes. But if an id always changes by a multiple of 1E12, the last 12 decimal digits are stable.=20 The advantage is that then the planner can use the histogram to get a pretty good estimate of how frequent a value is. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --mP3DRpeJDSE+ciuQ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl75EeEACgkQ8g5IURL+ KF3SHg//TrYQE87MznK/B1VDtICGETiSCeBjtLCF+gnu3Aj4c5/ZN+mtVGdr0jV6 /w5RsB3hg7i3RDtD5Jk9e2QF8AKuppYiqfkP8Ehjv41pWVR2R0jYkLiqI7wwnm+W y9+AIJZdnQiPavevpe8IM3OER/iNyj/z6PR7k2Ijrtv7YeD9GXD8lSzBUcFXLkIi xTwv5z8woMuVIq92B88Gsngyb6ufVpXnx0cXqlu5Sgf9EbP1m+qVUnsY8OC9O4q9 yDJNTZ2RwIicYxu01PPUnX3B+FH6Yyt/9m/LPfp5mL0RQLo+UNYzRNPqHOutvK+9 mgfG2pj6sQ81KzHS4639Fssbl9aGjjCrtJwJ5BugNv67OCNvgZEudmzksuRBQH5m Ov2NSHlgxnpCYcocoP7sh+KNhejcTd4v7mHNlJq6NsgAVfjnZsCB1LiseQIYtWDS doSv9vfc3EHvL1uwRrPvxSGoN4ArCD/XJaM/x962hq2F2EVN7/GRkBy9vQSQFV48 LehF9zitFxppxBu8G2KKep1Knvhvjri+ZqQzkvnxcxrZFJKDPbDRVEqwbAgfgX21 87mJYPgOvVN7PyRwj0mh+gVpJA9ck59eSWspkxB/0jQzSpR+z3Z8129eupgPvJvs ZVVfRwQPb4+7kNPidfAmbBAqUc6GzeanfNh8ajpq5VQIV+Yus2g= =Dxh0 -----END PGP SIGNATURE----- --mP3DRpeJDSE+ciuQ--