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 1i8SND-0000cE-4L for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 16:55:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8SNA-0005y1-Ov for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 16:55:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8SNA-0005xe-C7 for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 16:55:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i8SN3-0007Aq-No for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 16:55:07 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x8CGstJx003176; Thu, 12 Sep 2019 12:54:56 -0400 From: Tom Lane To: "Peter J. Holzer" cc: pgsql-general@lists.postgresql.org Subject: Re: Strange Behaviour with multicolumn indexes In-reply-to: <20190912164105.GA17616@hjp.at> References: <20190912164105.GA17616@hjp.at> Comments: In-reply-to "Peter J. Holzer" message dated "Thu, 12 Sep 2019 18:41:05 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3174.1568307295.1@sss.pgh.pa.us> Date: Thu, 12 Sep 2019 12:54:55 -0400 Message-ID: <3175.1568307295@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Peter J. Holzer" writes: > 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 > select c from t where a='A' and b='B'; > This uses t_b_idx, not - as I expected - t_a_b_idx. > 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 %. > 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? 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. > 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. 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. 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. Anyway I can't get excited about optimizing for a single non-null value. regards, tom lane