pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Russell Smith <mr-russ@pws.com.au>
To: Ryan VanMiddlesworth <ryan@vanmiddlesworth.org>
Cc: pgsql-performance@postgresql.org
Subject: Re: Query planner problem
Date: Sun, 3 Oct 2004 08:50:28 +1000
Message-ID: <200410030850.28345.mr-russ@pws.com.au> (raw)
In-Reply-To: <200410011706.54231.ryan@vanmiddlesworth.org>
References: <200410011706.54231.ryan@vanmiddlesworth.org>

On Sat, 2 Oct 2004 08:06 am, Ryan VanMiddlesworth wrote:

[snip]
> 
> 
> Here is the query and EXPLAIN that runs quickly:
>   SELECT case_id FROM case_data 
>   WHERE case_filed_date > '2004-09-16' 
>     AND case_filed_date < '2004-09-20'
> 
>                        QUERY PLAN
> -------------------------------------------------------------
> Index Scan using case_data_case_filed_date on case_data  
> (cost=0.00..13790.52 rows=3614 width=18)
>   Index Cond: ((case_filed_date > '2004-09-16'::date) 
>            AND (case_filed_date < '2004-09-20'::date))
> 
> 
> And here is the query and EXPLAIN from the version that I believe the planner 
> should reduce to be logically equivalent:
>  SELECT case_id FROM case_data 
>  WHERE (('2004-09-16' IS NULL) OR (case_filed_date > '2004-09-16'))
>    AND (('2004-09-20' IS NULL) OR (case_filed_date < '2004-09-20'))
> 
>                        QUERY PLAN
> -------------------------------------------------------------
> Seq Scan on case_data  (cost=0.00..107422.02 rows=27509 width=18)
>   Filter: ((('2004-09-16' IS NULL) OR (case_filed_date > '2004-09-16'::date))
>        AND (('2004-09-20' IS NULL) OR (case_filed_date < '2004-09-20'::date)))
> 
> 
> I was hoping that the null comparisons would get folded out by the planner 
> relatively cheaply. But as you can see, the first query uses indexes and the 
> second one uses sequence scans, thereby taking much longer.  I guess my 
> question is - is there a better way to accomplish what I'm doing in SQL or am 
> I going to have to dynamically generate the statement based on supplied 
> parameters?
> 
The Index does not store NULL values, so you have to do a tables scan to find NULL values.
That means the second query cannot use an Index, even if it wanted to.

Regards

Russell Smith




view thread (4+ messages)  latest in thread

Message-ID: <200410030850.28345.mr-russ@pws.com.au>
Permalink:  ../200410030850.28345.mr-russ@pws.com.au/
Also on:    postgresql.org/message-id/200410030850.28345.mr-russ@pws.com.au

 · 

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: mr-russ@pws.com.au, ryan@vanmiddlesworth.org
  Subject: Re: Query planner problem
  In-Reply-To: <200410030850.28345.mr-russ@pws.com.au>

* 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