pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tv@fuzzy.cz>
To: Andrus <kobruleht2@hot.ee>
Cc: pgsql-performance@postgresql.org
Subject: Re: Using index for IS NULL query
Date: Tue, 11 Nov 2008 22:01:58 +0100
Message-ID: <4919F2C6.4000303@fuzzy.cz> (raw)
In-Reply-To: <gfcprb$m40$1@news.hub.org>
References: <gfcprb$m40$1@news.hub.org>

> Index is not used for
> 
> is null
> 
> condition:
> 
> create index  makse_dokumnr_idx on makse(dokumnr);
> explain select
>     sum( summa)
>   from MAKSE
>   where  dokumnr is null
> 
> "Aggregate  (cost=131927.95..131927.96 rows=1 width=10)"
> "  ->  Seq Scan on makse  (cost=0.00..131927.94 rows=1 width=10)"
> "        Filter: (dokumnr IS NULL)"
 >
 >
> Table makse contains 1200000 rows and about 800 rows with dokumnr is 
> null so using index is much faster that seq scan.
> How to fix ?

Yes, NULL values are not stored in the index, but you may create 
functional index on

(CASE WHEN dokumnr IS NULL THEN -1 ELSE dokumnr END)

and then use the same expression in the WHERE clause. You may replace 
the '-1' value by something that's not used in the dokumnr column.

regards
Tomas



view thread (9+ messages)  latest in thread

Message-ID: <4919F2C6.4000303@fuzzy.cz>
Permalink:  ../4919F2C6.4000303@fuzzy.cz/
Also on:    postgresql.org/message-id/4919F2C6.4000303@fuzzy.cz

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tv@fuzzy.cz, kobruleht2@hot.ee
  Subject: Re: Using index for IS NULL query
  In-Reply-To: <4919F2C6.4000303@fuzzy.cz>

* 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