Re: Index on a NULL-value

От: Bruno Wolff III
Тема: Re: Index on a NULL-value
Дата: ,
Msg-id: 20050531031653.GA17723@wolff.to
(см: обсуждение, исходный текст)
Ответ на: Index on a NULL-value  (Tobias Brox)
Ответы: Re: Index on a NULL-value  (Tobias Brox)
Список: pgsql-performance

Скрыть дерево обсуждения

Index on a NULL-value  (Tobias Brox, )
 Re: Index on a NULL-value  (Bruno Wolff III, )
  Re: Index on a NULL-value  (Tobias Brox, )
   Re: Index on a NULL-value  (Bruno Wolff III, )
 Re: Index on a NULL-value  (Tobias Brox, )
  Re: Index on a NULL-value  (Bruno Wolff III, )
   Re: Index on a NULL-value  (Tobias Brox, )
    Re: Index on a NULL-value  (Greg Stark, )
     Re: Index on a NULL-value  (Tom Lane, )
   Re: Index on a NULL-value  (Tom Lane, )
    Re: Index on a NULL-value  (Tobias Brox, )
 Re: Index on a NULL-value  (Tobias Brox, )

On Tue, May 31, 2005 at 11:02:07 +0800,
  Tobias Brox <> wrote:
> I read in the manual today:
>
>   Indexes are not used for IS NULL clauses by default. The best way to use
>   indexes in such cases is to create a partial index using an IS NULL
>   predicate.
>
> This is from the documentation for PostgreSQL 8.  I did not find anything
> equivalent in the 7.4.8-documentation.
>
> I wasn't aware of this until it became an issue :-) Well, so I follow the
> tip but in vain.  Reduced and reproduced like this in PostgreSQL 7.4.7:
>
> test=# create table mock(a int, b int);
> CREATE TABLE
> test=# create index b_is_null on mock((b IS NULL));
> CREATE INDEX
> test=# insert into mock values (10,20);
> INSERT 70385040 1
> test=# insert into mock values (20,30);
> INSERT 70385041 1
> test=# insert into mock values (30, NULL);
> INSERT 70385042 1
> test=# set enable_seqscan=off;
> SET
> test=# explain select * from mock where b is NULL;
>                              QUERY PLAN
> --------------------------------------------------------------------
>  Seq Scan on mock (cost=100000000.00..100000020.00 rows=6 width=8)
>    Filter: (b IS NULL)
> (2 rows)
>
> vacuum analyze also didn't help to recognize the index ;-)

It isn't surprising that an index wasn't used since a sequential scan is
going to be faster in your test case.

If you want to test this out, you to want use realistically sized tables.


В списке pgsql-performance по дате сообщения:

От: Tobias Brox
Дата:
Сообщение: Re: Index on a NULL-value
От: "Praveen Raja"
Дата:
Сообщение: very large table