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 9B7815397E for ; Sat, 2 Apr 2005 08:01:41 +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 01642-01 for ; Sat, 2 Apr 2005 07:01:36 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 83FEE53823 for ; Sat, 2 Apr 2005 08:01:34 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j3271WbK006147 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 2 Apr 2005 00:01:34 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j3271WSV010590; Sat, 2 Apr 2005 00:01:32 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j3271VoZ010589; Sat, 2 Apr 2005 00:01:31 -0700 (MST) (envelope-from mfuhr) Date: Sat, 2 Apr 2005 00:01:31 -0700 From: Michael Fuhr To: Mischa Cc: "H.J. Sanders" , pgsql-performance@postgresql.org Subject: Re: date - range Message-ID: <20050402070131.GA10532@winnie.fuhr.org> References: <1112421584.424e34d0d2d63@webmail.telus.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1112421584.424e34d0d2d63@webmail.telus.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/29 X-Sequence-Number: 11426 On Fri, Apr 01, 2005 at 09:59:44PM -0800, Mischa wrote: > > > > select ....... where first_date <= today and last_date >= today > > > > Whatever index we create system always does a sequential scan (which I can > > understand). Has someone a smarter solution? > > Yep, standard SQL problem. The answer is sort of a hand-rolled GIST index. That might not be necessary in this case. CREATE TABLE foo ( id serial PRIMARY KEY, first_date date NOT NULL, last_date date NOT NULL, CONSTRAINT check_date CHECK (last_date >= first_date) ); /* populate table */ CREATE INDEX foo_date_idx ON foo (first_date, last_date); ANALYZE foo; EXPLAIN SELECT * FROM foo WHERE first_date <= current_date AND last_date >= current_date; QUERY PLAN -------------------------------------------------------------------------------------------- Index Scan using foo_date_idx on foo (cost=0.01..15.55 rows=97 width=12) Index Cond: ((first_date <= ('now'::text)::date) AND (last_date >= ('now'::text)::date)) (2 rows) -- Michael Fuhr http://www.fuhr.org/~mfuhr/