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 1i8S9i-0000B0-K2 for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 16:41:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8S9g-0002Yh-Qp for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 16:41:12 +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 1i8S9g-0002YX-Gz for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 16:41:12 +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 1i8S9c-0007HS-6Y for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 16:41:10 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 67F79837; Thu, 12 Sep 2019 18:41:05 +0200 (CEST) Date: Thu, 12 Sep 2019 18:41:05 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Strange Behaviour with multicolumn indexes Message-ID: <20190912164105.GA17616@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="3MwIy2ne0vdjdPXF" 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 --3MwIy2ne0vdjdPXF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable [PostgreSQL 11.5 (Ubuntu 11.5-1.pgdg18.04+1) on x86_64-pc-linux-gnu] I have a table with many columns and many indexes (actually many tables with many columns and many indexes), but for the sake of this posting, we'll consider just three columns, which we unimaginatively call a, b, and c. There are also three indexes: 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 Nowe I have a query=20 select c from t where a=3D'A' and b=3D'B'; This uses t_b_idx, not - as I expected - t_a_b_idx. 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. 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 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? After all it would have to read only 1/1000 of the rows instead of 1/100. it would also have to scan much less of the index, so the fact the fact that the index is a bit larger shouldn't make a difference. Explain shows that the row estimates are spot on, but the cost for using t_a_b_idx is higher than for t_b_idx (which is in turn higher than for t_b_a_idx). 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 --3MwIy2ne0vdjdPXF Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl16dRsACgkQ8g5IURL+ KF2NChAAroi3SNlk3lAWv+R5H8td+btk0I6zymSyMzwNtniuKnybloHikA0DUim9 gCW2DEzZtJ9x64MZvPV7NnsElwoUV4sMMltoGi1Unw+xb1QPEO/P6KHZWdcpTkAu qNA9U7/kWq0a1JsAHu55Daz3BFfHa0HGuYervfmevprvPbAyZViaYHvwVRnGESXb C7SUum5/T31twpsLDedsagRRfaeHKDKd3/b2ETnQ9JGxRPlo66vevNl8yDrMHYn8 ankYtgyN/kKeFZo+spHwcnWleQWaVQPD1BI8/md/GzJCDru/lF9jsIih2TQcKPgK BeYmy7XP9ZAdiZxeho/8XMgR5pxRGPfaZIeWdHxcRvga4HLb0Kd7qAQuAvICRjvW OIjO9L0yXA94l65+VW4JrrxNdTQxXoyqFTU5hksJAXe81/nHRN2qGQzh14Go+CQT UvFPBiTb58IrhytftkTS7sMk7LSAsP1DVEAZ95O6bkyjqYREU8Y3GHvIII5NLg60 5Vz3IBZUQrLh1O1xOAWdwnrWSr1PMnE6uOGGOfkcfMWRrnI1YWe9/qO5PXNZ76kb g6KnKqHOGM5Zzhjp7FqYVuO131JD+Dgd0OWSpZSwLLt/qF/jpJmA4YSf+jX5Kqsf XqHZFEok5Ib2l+Apm/SO31oxMG910Ou0XJaR1Mtto3l675gB4tw= =oSsm -----END PGP SIGNATURE----- --3MwIy2ne0vdjdPXF--