How to optimize a JOIN with BETWEEN? - Mailing list pgsql-performance

From andrew@pillette.com
Subject How to optimize a JOIN with BETWEEN?
Date
Msg-id 200602200406.k1K46CB09448@pillette.com
Whole thread Raw
Responses Re: How to optimize a JOIN with BETWEEN?
List pgsql-performance
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.
TableB has a matching ID field, minimum and maximum dates, a code, and other stuff, about 0-50 records per ID. For a
givenID, 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
GROUPBY 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
choicebetween using a LEFT JOIN or inserting dummy records into B to fill in the gaps in the covered dates, which would
makeexactly one matching record. Would this make a difference? 

Thanks.

pgsql-performance by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: Need pointers to "standard" pg database(s) for testing
Next
From: Christopher Kings-Lynne
Date:
Subject: Re: Need pointers to "standard" pg database(s) for