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 1joC7B-0000rX-OP for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 20:35:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joC79-00040Y-9H for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 20:35:23 +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 1joC79-00040Q-1H for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 20:35:23 +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 1joC76-0002FV-Ge for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 20:35:22 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 97232330D; Wed, 24 Jun 2020 22:35:19 +0200 (CEST) Date: Wed, 24 Jun 2020 22:35:19 +0200 From: "Peter J. Holzer" To: Klaudie Willis Cc: pgsql-general@lists.postgresql.org Subject: Re: n_distinct off by a factor of 1000 Message-ID: <20200624203519.GA24048@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="vkogqOf2sHV7VnPd" Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --vkogqOf2sHV7VnPd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable [Please keep replies on the list] On 2020-06-24 11:02:22 +0000, Klaudie Willis wrote: > Holzer, thanks for your feedback. Yes, your guess is very good. The > data consists of millions of instruments that occur a handful of cases > (rare), and instruments that are very common. >=20 > I am still a little surprised that it is so hard to sample data and > estimate distinct values within a 1000X. My intuition misleads me into > thinking this should be simpler, but I understand that this is no > simple task at all. To your information, it seems like SQL server > 2016 makes the same "mistake" when calculating distincts (or 1/density > as they call it). I have similar data there that I have looked into, > and it seems "fooled" as well. Yes, estimating the number of distinct values from a relatively small sample is hard when you don't know the underlying distribution. It might 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 you you would massively underestimate their frequency if you had an accurate n_distinct value. That might be just as bad or even worse. I'm wondering whether it would make sense to use a range instead of a single value in cost calculations. Then the planner could prefer plans which had reasonable cost over the whole range over plans which are good at one end of the range but horrible at the other. I'm a afraid that would lead to combinatorial explosion, though. Even with a small number of ranges there would be a large number of possible combinations to calculate. So one would probably have to resort to monte carlo simulation or soemthing like that. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --vkogqOf2sHV7VnPd Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl7zuQIACgkQ8g5IURL+ KF2QTBAAmCPdN5X8pVJTpvyIA6eRwfUIL70F7XKTXWIty392hxfxtwrwOKOogqno sNYPw8uZWyfwz/APcZqITrdnESVJ9BrPlZ5GllMgeP+egsgHmf/gbUbZ8+kl1R6s nc7ajKo+O1TNkS7FuD/e/RIP89TaNKiIvhAij3uTfLLla94ZH96XAzGhW4c3pbxO vJbsCVz2aTNpa3oZgSXfhLMA9zkUyg5FUB7Aa17TBDuU7i1caMGti79dPhDz1oL3 8O0/BCJRD/74YD81WbeKuKSmGoK6cI949JrSIScTCpWyuNOtxPPQQnc2aFFAiS3M EjfNDDg/3Wad/s0fCMtnAKSOoYa0u41bZ1VJjXlzDyPom2A+pIbKOlM77ERTDEGD tSaLN7V045R+GScPcBRTkTojHqOf/lf/CC9Fbi00JGWRTyLFSMHTSl63FGAVHi4s D7Ig69k2vIAhNPmp63YpScMm56WMtmnyDewmtdEFZ2A+rTVgcgBf6Q0zbrnu0+Nw nTuKPYBpC9iQO2tN9dmJ1IipZRk6Bp9idOHtr6dNhz7TlQcx0fJDOu+X+qdMwdKP 3SFkzzMP3wLLNT9AVl83Jv2ogDU3oUN97CUohfoY9ZNCSRjF+24EAH8cOJbNGYps u65vYzLPNmtiw7zH0ipl5o7Hnc8PRebfqYPmVqHzq/iynH3RXlk= =Kcy8 -----END PGP SIGNATURE----- --vkogqOf2sHV7VnPd--