Re: JOIN on set of rows?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Peter Fein <pfein(at)pobox(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: JOIN on set of rows?
Date: 2005-05-11 15:47:47
Message-ID: 42822923.1080309@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Peter Fein wrote:
>>>
>>>SELECT t1.symbol AS app_name, t2.outside_key AS app_id
>>>FROM t2 LEFT JOIN t1 ON t1.t2_id=t2.id AS my_join
>>>LEFT JOIN rows of arbitrary (app_name, app_id) ON
>>>my_join.app_name=rows.app_name AND my_join.app_id=rows.app_id

> Sorry, I kinda wrote that wrong. ;) What I really want is:
>
> SELECT rows of known, app-generated (app_name, app_id)
> INTERSECT
> SELECT t1.symbol AS app_name, t2.outside_key AS app_id
> FROM t2 LEFT JOIN t1 ON t1.t2_id=t2.id
>
> There are around a max of 50 rows in the first select and
> perhaps up to 1 million in the second.

Personally, I'd just generate the SQL clause on the fly:

SELECT
t1.symbol AS app_name,
t2.outside_key AS app_id
FROM
t1, t2
WHERE
t1.t2_id = t2.id
AND (
(t1.symbol='<name-val-01>' AND t2.outside_key=<id-val-01>)
OR
(t1.symbol='<name-val-01>' AND t2.outside_key=<id-val-01>)
OR
...
)
;

I'm assuming you don't really want a LEFT JOIN between t2/t1 since that
would mean you had null app_name's which the application-generated
values couldn't match anyway.

If you find the performance unacceptable, try inserting the 50 rows into
a temporary (or perhaps even permanent) table and joining against that.
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2005-05-11 15:53:15 Re: Disabling Triggers
Previous Message Tom Lane 2005-05-11 15:43:32 Re: Disabling Triggers