pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: andrew@pillette.com
To: pgsql-performance@postgresql.org
Subject: How to optimize a JOIN with BETWEEN?
Date: Sun, 19 Feb 2006 20:06:12 -0800
Message-ID: <200602200406.k1K46CB09448@pillette.com> (raw)

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.



view thread (2+ messages)  latest in thread

Message-ID: <200602200406.k1K46CB09448@pillette.com>
Permalink:  ../200602200406.k1K46CB09448@pillette.com/
Also on:    postgresql.org/message-id/200602200406.k1K46CB09448@pillette.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: andrew@pillette.com
  Subject: Re: How to optimize a JOIN with BETWEEN?
  In-Reply-To: <200602200406.k1K46CB09448@pillette.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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