Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8UOO-0005zW-1x for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 19:04:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8UOM-0001SL-K6 for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 19:04:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8UOM-0001SE-AF for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 19:04:30 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8UOJ-0000DZ-Bx for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 19:04:29 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 89EB8839; Thu, 12 Sep 2019 21:04:25 +0200 (CEST) Date: Thu, 12 Sep 2019 21:04:25 +0200 From: "Peter J. Holzer" To: Tom Lane Cc: "Peter J. Holzer" , pgsql-general@lists.postgresql.org Subject: Re: Strange Behaviour with multicolumn indexes Message-ID: <20190912190425.GA23651@hjp.at> References: <20190912164105.GA17616@hjp.at> <3175.1568307295@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="lrZ03NoBR/3+SXJZ" Content-Disposition: inline In-Reply-To: <3175.1568307295@sss.pgh.pa.us> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --lrZ03NoBR/3+SXJZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-09-12 12:54:55 -0400, Tom Lane wrote: > "Peter J. Holzer" writes: > > we'll consider just three columns, which we unimaginatively call a, b, > > and c. There are also three indexes: >=20 > > t_a_idx btree (a) WHERE a IS NOT NULL > > t_b_idx btree (b) WHERE b IS NOT NULL > > t_a_b_idx btree (a, b) WHERE a IS NOT NULL AND b IS NOT NULL >=20 > > Nowe I have a query=20 > > select c from t where a=3D'A' and b=3D'B'; Just in case somebody wants to play along at home: A narrow table with just these three columns and synthetic data shows the problem but doesn't use the flipped multicolumn index either. I guess you have to pad the table a bit to sufficiently change the costs. > > This uses t_b_idx, not - as I expected - t_a_b_idx. >=20 > > The distribution of values in columns a and b is quite different: a has > > 10 different values of similar frequency (and no null values). b has > > only a single non-null value which with a frequency of about 1 %.=20 >=20 > > So I definitely understand why it would prefer t_b_idx to t_a_idx, but > > certainly t_a_b_idx should be even better? >=20 > Not necessarily --- t_a_b_idx is (presumably) physically bigger than > t_b_idx, which makes it more expensive to search. The additional > selectivity gain apparently doesn't outweigh that. The index is about 20 % larger, but only 1/10 of it has to be searched, so it should take 1 - (1.2 / 10) =3D 88 % less time. > > If I create an index with the columns swapped: > > t_b_a_idx btree (b, a) WHERE b IS NOT NULL and a IS NOT NULL > > this index will be used. >=20 > Hmm. Probably that has something to do with a calculation about > the selectivity of the leading index column, ie do you have to > scan 10% of the index or 1% of the index. Does it use only the selectivity of the first column? That would explain it. The total selectivity of both columns is obviously the same. > It's not taking the partial-index filter into account in that, I > suspect, which skews the results in this case --- but that would be > hard to account for accurately. Hmm. Wouldn't that be a problem for partial indexes in general? They usually cover only a small portion of the table and if the selectivity is computed relative to the whole table the result may be way off. I think it should be possible to adjust for a "WHERE column IS NOT NULL" filter, because null_frac is in the statistics. For the general case you would need an estimate of the number of rows covered by the index, which I don't think we have. > Anyway I can't get excited about optimizing for a single non-null > value. That's certainly an extreme case, but I've seen bad plans for similar queries every now and then. At least now I have an idea what causes it and maybe a solution/workaround. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --lrZ03NoBR/3+SXJZ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl16lrMACgkQ8g5IURL+ KF2rzhAAlYlzc9vF+IiPNkWb8KULi4+5+JO3OFZftYGFMp4hX5Ya87R0hPNn6GtE 7KY/0m/20tjXagSlIOA3fB70SL2rfso6r19/bpkOIedlssR2H2w8DiWKhR5GsxCy iTMrPIeweItp+oIHAKcnqcIFQVzVrupfLxrseI2NHE0p/CCYqZTBgWyqedAiUQqB DQN7Lm8c9OpXaR8aJGmbo45Zb9LyGJbQIV471wys3MNncF7hmSMvTgZHkH19cIjW V+SIXX+xMS4lREGSyXOkh6DO41M9mTtWyxCldr+HBtVBumBaKPsNPtWBEiEhVp1/ 3nWvvQzjNVk/TPQOuIdmqZPXbREXsIppnv48Yo+jWi3qUZsuRzGDmS0El5hscS/2 H1/K/CYCBxM1gqFo5De7bRM8DGs2/XG1S/+T4Fp53ZiierNCiP4xdlw0ViWb81Xj 10BpWARmysn9o7kwVvpIV/3zN6pZVWTzl2Ftl3rx6fmn/PgBwsAC6sWUSc2IRPsz YdL5lKdHyQs0cKAtCK1dKk8SEgb+Bpx/0UU3jEDgqi/Bd5kReu7AaDi2gu/alSqP e4ZgiJNUGMH84luWxCztIrZuxHcRN3gKvMuD1CZUHdC6NYjk/VWh5B8LtHBIhCHR xQlSZ8qUBQ7XKAq8BHPy3X9OU1CNuPkIohPDJ9pgDcf4zxAc6Pg= =09Ch -----END PGP SIGNATURE----- --lrZ03NoBR/3+SXJZ--