pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: greigwise <greigwise@comcast.net>
To: pgsql-general@postgresql.org
Subject: Re: Table with many NULLS for indexed column yields strange query plan
Date: Mon, 9 Mar 2020 07:06:14 -0700 (MST)
Message-ID: <1583762774401-0.post@n3.nabble.com> (raw)
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>

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





view thread (4+ messages)

Message-ID: <1583762774401-0.post@n3.nabble.com>
Permalink:  ../1583762774401-0.post@n3.nabble.com/
Also on:    postgresql.org/message-id/1583762774401-0.post@n3.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: greigwise@comcast.net
  Subject: Re: Table with many NULLS for indexed column yields strange query plan
  In-Reply-To: <1583762774401-0.post@n3.nabble.com>
  References: <1583456933425-0.post@n3.nabble.com> <20200308204639.GE22585@hjp.at> <10656.1583703114@sss.pgh.pa.us> <1583762774401-0.post@n3.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox