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.92) (envelope-from ) id 1jBJ36-00032j-9H for pgsql-general@arkaria.postgresql.org; Mon, 09 Mar 2020 14:06:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jBJ35-0005nY-0X for pgsql-general@arkaria.postgresql.org; Mon, 09 Mar 2020 14:06:27 +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 1jBJ34-0005mv-Mu for pgsql-general@lists.postgresql.org; Mon, 09 Mar 2020 14:06:26 +0000 Received: from n3.nabble.com ([162.255.23.22]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jBJ2u-0003cM-Th for pgsql-general@postgresql.org; Mon, 09 Mar 2020 14:06:26 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 628E71B924161 for ; Mon, 9 Mar 2020 07:06:14 -0700 (MST) Date: Mon, 9 Mar 2020 07:06:14 -0700 (MST) From: greigwise To: pgsql-general@postgresql.org Message-ID: <1583762774401-0.post@n3.nabble.com> In-Reply-To: <10656.1583703114@sss.pgh.pa.us> References: <1583456933425-0.post@n3.nabble.com> <20200308204639.GE22585@hjp.at> <10656.1583703114@sss.pgh.pa.us> Subject: Re: Table with many NULLS for indexed column yields strange query plan MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Seqscans are not disabled. Also, this is PostgreSQL 10.11 if that helps. Costs are as follows: seq_page_cost --------------- 1 random_page_cost ------------------ 1.5 It is odd that it does not just do a seqscan on table3. It's a very small table... only like 36 rows. I'd think the plan *should* seq scan table3, get the id where number = 'xxxx', then use the index index_table2_on_table3_id on table2 to get the matching rows for that id. It does use that index when I specify that table3_id is not null, but not otherwise. table3_id is very selective into table2 for any non-null value, so I don't know why it would choose to scan that entire index in the case of the first query where the table3_id clearly can't be null due to the inner join. Check out this: select tablename, attname, inherited, null_frac, avg_width, n_distinct, most_common_vals, most_common_freqs from pg_stats where tablename = 'table2' and attname = 'table3_id'; tablename | attname | inherited | null_frac | avg_width | n_distinct | ---------------+------------------+-----------+-----------+-----------+------------+ table2 | table3_id | f | 0.996167 | 8 | 39 | most_common_vals: {985,363,990,991,992,45,81,8,126,307,378,739,855,993,994,190,338,366,369,537,663,805,846,155,277,803,870,988} most_common_freqs: {0.000233333,0.0002,0.0002,0.0002,0.0002,0.000166667,0.000166667,0.000133333,0.000133333,0.000133333,0.000133333,0.000133333,0.000133333,0.000133333,0.000133333,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,6.66667e-05,6.66667e-05,6.66667e-05,6.66667e-05,6.66667e-05} Thanks again for any help. Greig -- Sent from: https://www.postgresql-archive.org/PostgreSQL-general-f1843780.html