pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to optimize a JOIN with BETWEEN?
2+ messages / 2 participants
[nested] [flat]
* How to optimize a JOIN with BETWEEN?
@ 2006-02-20 04:06 andrew@pillette.com
2006-02-22 00:14 ` Re: How to optimize a JOIN with BETWEEN? Jim C. Nasby <jnasby@pervasive.com>
0 siblings, 1 reply; 2+ messages in thread
From: andrew@pillette.com @ 2006-02-20 04:06 UTC (permalink / raw)
To: 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. 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.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: How to optimize a JOIN with BETWEEN?
2006-02-20 04:06 How to optimize a JOIN with BETWEEN? andrew@pillette.com
@ 2006-02-22 00:14 ` Jim C. Nasby <jnasby@pervasive.com>
0 siblings, 0 replies; 2+ messages in thread
From: Jim C. Nasby @ 2006-02-22 00:14 UTC (permalink / raw)
To: andrew@pillette.com; +Cc: pgsql-performance
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
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2006-02-22 00:14 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-20 04:06 How to optimize a JOIN with BETWEEN? andrew@pillette.com
2006-02-22 00:14 ` Jim C. Nasby <jnasby@pervasive.com>
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