X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A52D9329D58 for ; Sat, 2 Oct 2004 23:52:51 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 97466-04 for ; Sat, 2 Oct 2004 22:52:45 +0000 (GMT) Received: from mail.pws.com.au (mail.pws.com.au [210.23.138.139]) by svr1.postgresql.org (Postfix) with SMTP id 6DFC7329D4D for ; Sat, 2 Oct 2004 23:52:46 +0100 (BST) Received: (qmail 4249 invoked by uid 0); 3 Oct 2004 08:52:42 +1000 Received: from cpe-203-45-44-212.vic.bigpond.net.au (HELO wizzard.pws.com.au) (russell@pws.com.au@203.45.44.212) by mail.pws.com.au with SMTP; 3 Oct 2004 08:52:42 +1000 From: Russell Smith To: Ryan VanMiddlesworth Subject: Re: Query planner problem Date: Sun, 3 Oct 2004 08:50:28 +1000 User-Agent: KMail/1.7 Cc: pgsql-performance@postgresql.org References: <200410011706.54231.ryan@vanmiddlesworth.org> In-Reply-To: <200410011706.54231.ryan@vanmiddlesworth.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200410030850.28345.mr-russ@pws.com.au> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/19 X-Sequence-Number: 8495 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