Does not use index on query using "field is null"?

Started by Bjørn T Johansenover 15 years ago4 messagesgeneral
Jump to latest
#1Bjørn T Johansen
btj@havleik.no

I have a query that looks like this...:

select * from table where field is null

And when I run explain, it tells me that it uses seq scan... Is this because pg thinks that seq scan is as fast as using indexes or because using index on "is null" queries does
not work?

Regards,

BTJ

--
-----------------------------------------------------------------------------------------------
Bjørn T Johansen

btj@havleik.no
-----------------------------------------------------------------------------------------------
Someone wrote:
"I understand that if you play a Windows CD backwards you hear strange Satanic messages"
To which someone replied:
"It's even worse than that; play it forwards and it installs Windows"
-----------------------------------------------------------------------------------------------

#2Vick Khera
vivek@khera.org
In reply to: Bjørn T Johansen (#1)
Re: Does not use index on query using "field is null"?

2010/11/10 Bjørn T Johansen <btj@havleik.no>:

select * from table where field is null

And when I run explain, it tells me that it uses seq scan... Is this because pg thinks that seq scan is as fast as using indexes or because using index on "is null" queries does
not work?

Does the table have *lots* of NULLs in that column? If the index is
not very selective, it may choose to use a seq scan since it will
result in fewer I/O ops. This decision will be based on the relative
values of random_page_cost and seq_page_cost, and probably the
effective_cache_size as well.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bjørn T Johansen (#1)
Re: Does not use index on query using "field is null"?

=?ISO-8859-1?Q?Bj=F8rn?= T Johansen <btj@havleik.no> writes:

I have a query that looks like this...:
select * from table where field is null

And when I run explain, it tells me that it uses seq scan... Is this because pg thinks that seq scan is as fast as using indexes or because using index on "is null" queries does
not work?

What PG version? Releases before 8.3 do not think that IS NULL is an
indexable condition.

regards, tom lane

#4Bjørn T Johansen
btj@havleik.no
In reply to: Tom Lane (#3)
Re: Does not use index on query using "field is null"?

On Wed, 10 Nov 2010 10:00:43 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:

=?ISO-8859-1?Q?Bj=F8rn?= T Johansen <btj@havleik.no> writes:

I have a query that looks like this...:
select * from table where field is null

And when I run explain, it tells me that it uses seq scan... Is this because pg thinks that seq scan is as fast as using indexes or because using index on "is null" queries does
not work?

What PG version? Releases before 8.3 do not think that IS NULL is an
indexable condition.

regards, tom lane

We are using version 8.4.x...

BTJ