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 1jo27O-00074h-SE for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 09:54:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jo27N-0002Dp-Km for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 09:54:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jo27N-0002Dh-Ci for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 09:54:57 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jo27H-00064E-RH for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 09:54:56 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id AF28E3306; Wed, 24 Jun 2020 11:54:48 +0200 (CEST) Date: Wed, 24 Jun 2020 11:54:48 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: n_distinct off by a factor of 1000 Message-ID: <20200624095448.GA417@hjp.at> References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> <7879cb87-5a68-edec-9ec6-3f082d78ed86@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="oyUTqETQ0mS9luUI" 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 --oyUTqETQ0mS9luUI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-06-24 07:30:05 +0000, Klaudie Willis wrote: > show default_statistics_target; --> 500 > ALTER TABLE public.bigtable ALTER COLUMN instrumentid_ref SET STATISTICS = 5000; >=20 > Here is the output of the "ANALYZE VERBOSE bigtable;" > INFO: analyzing "public.bigtables" inheritance tree [...] > INFO: analyzing "public.bigtable_y2018" > INFO: "bigtable_y2018": scanned 1500000 of 10661013 pages, containing 28= 915115 > live rows and 12589 dead rows; 1500000 rows in sample, 205509611 estimated > total rows > INFO: analyzing "public.bigtable_y2019" > INFO: "bigtable_y2019": scanned 1500000 of 8911886 pages, containing 288= 88514 > live rows and 17778 dead rows; 1500000 rows in sample, 171634096 estimated > total rows > INFO: analyzing "public.bigtable_y2020" > INFO: "bigtable_y2020": scanned 1500000 of 6126616 pages, containing 294= 88967 > live rows and 6330 dead rows; 1500000 rows in sample, 120445051 estimated= total > rows > INFO: analyzing "public.bigtable_y2021" > INFO: "bigtable_y2021": scanned 1 of 1 pages, containing 8 live rows and= 0 > dead rows; 8 rows in sample, 8 estimated total rows So it scans 1500000 rows from each partition, but ... [...] > frac_MCV;n_distinct; n_mcv; n_hist;tablename > 0.9205394 122160 2140 5001 "bigtable" > 0.9203018 124312 1736 5001 "bigtable_y2018" > 0.9258158 113846 2107 5001 "bigtable_y2020" > 0.875 -0.375 2 "bigtable_y2021" > 0.92304045 118267 2204 5001 "bigtable_y2019" Estimates the number of distinct values in each partition as between 113846 and 124312. So it can have encountered at most that many different values, which means that it must have encountered each value about 12 or 13 times on average. My guess is that there are relatively few (less than 120000) distinct values which make up the bulk (over 90 %) of these tables and a lot (33 million) values which are very rare. Is this guess correct? hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --oyUTqETQ0mS9luUI Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl7zIuQACgkQ8g5IURL+ KF1NZw//UEduAE/sM3bdudndDZK7wzTbVmy9rANMrdnEdSr2Y1B7pSl5aEdIVf1q AzUuMB1mMOB8MrfXPXNVL30I47YeDpCyJE/H9610Zndk3czxhKFIWdTqu5RylTj0 gMpKq0IaGHd73liDzUwp7XZXurMAPAw+KPlvbq6DSviZ8w6dp9mUtP6jKs0kPMTo 1PIvBc4hpDyJKhkCO/lALQpYTofnm1YQD5W2oyXr6n2B5567uK1boJPvpgdIdBB2 wDbifZDKzamc8/ddQmTLm+++Nd9x5vtlHNw3V1u9xry8bQkOsvI6owroa58unuWF Q3Ba3KBcLEoAb7Jx+yD7LjdeZZ1009mM7PDbtd+eccgwSYtWV43/8M3Qx4y1h9N/ SAZOFqXkMbn5+q8PA3hphQWQ9f8cQkx/V6ygknKdiFIHufs/voWb6cW0qdsZ8WlO 6qlJ57NZ7P2fjfX2l/fvrV93GQix0D6V64Kb0Q0Vqrvo1LW5c/U3OBKuEOmsCo7R 6oMU39UlaA+bbChvC65i1IoEiDZaPtkS1mPxuCfK3iAF6PigrwARod8as6CBRhAw 7A6i6TWGcC/WwDC4WgV2qFlv/hA0fqRuIdSLsOz8F4IGO+R82sEUHX7EtXnFAJ1h R/WLJ/ajK5h+4eFSfF08MRNe0joLQ1ZzyCiKXQPTqRLew8Np1J8= =0LK9 -----END PGP SIGNATURE----- --oyUTqETQ0mS9luUI--