X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 238A89DC860 for ; Tue, 21 Feb 2006 20:15:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 76207-05 for ; Tue, 21 Feb 2006 20:15:01 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 31B039DC857 for ; Tue, 21 Feb 2006 20:14:55 -0400 (AST) Received: by noel.decibel.org (Postfix, from userid 1001) id CD45656412; Tue, 21 Feb 2006 18:14:56 -0600 (CST) Received: (hashcash-sendmail, from uid 1001); Tue, 21 Feb 2006 18:14:56 -0600 Date: Tue, 21 Feb 2006 18:14:56 -0600 From: "Jim C. Nasby" To: andrew@pillette.com Cc: pgsql-performance@postgresql.org Subject: Re: How to optimize a JOIN with BETWEEN? Message-ID: <20060222001456.GZ77800@pervasive.com> References: <200602200406.k1K46CB09448@pillette.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200602200406.k1K46CB09448@pillette.com> X-Operating-System: FreeBSD 6.0-RELEASE-p4 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.11 X-Hashcash: 1:20:060222:andrew@pillette.com::vFm9cYU8FU+Nqn8Y:000000000000000000 00000000000000000000000026r1 X-Hashcash: 1:20:060222:pgsql-performance@postgresql.org::SQUlId0lXKQ2nKtg:00000 0000000000000000000000005YUY X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.104 required=5 tests=[AWL=0.104] X-Spam-Score: 0.104 X-Spam-Level: X-Archive-Number: 200602/335 X-Sequence-Number: 17321 Use a gist index. Easiest way would be to define a box with mindate at one corner and maxdate at the other corner, and then search for point(obsdate,obsdate) that lie with in the box. A more detailed explination is in the archives somewhere... On Sun, Feb 19, 2006 at 08:06:12PM -0800, andrew@pillette.com wrote: > Here's a simplified version of the schema: > > Table A has an ID field, an observation date, and other stuff. There are about 20K IDs and 3K observations per ID. Table B has a matching ID field, minimum and maximum dates, a code, and other stuff, about 0-50 records per ID. For a given ID, the dates in B never overlap. On A, the PK is (id, obsdate). On B, the PK is (id, mindate). I want > > SELECT a.id, b.code, AVG(other stuff) FROM A LEFT JOIN B ON a.id=b.id AND a.obsdate BETWEEN b.mindate AND b.maxdate GROUP BY 1,2; > > Is there a way to smarten the query to take advantage of the fact at most one record of B matches A? Also, I have a choice between using a LEFT JOIN or inserting dummy records into B to fill in the gaps in the covered dates, which would make exactly one matching record. Would this make a difference? > > Thanks. > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461