Re: Joins on many-to-many relations.

From: Wiebe Cazemier <halfgaar(at)gmx(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Joins on many-to-many relations.
Date: 2007-03-16 11:56:02
Message-ID: ete0ki$l9s$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wednesday 14 March 2007 22:59, Wiebe Cazemier wrote:

> My question was mainly general;
> if there is a better way than using subselects to join two tables which
> are only connected to eachother through a join-table (containing only
> references to the two tables in question). Subselects are usually very
> slow, aren't they?

I fixed it. I now have a query with two outer joins, instead of the subselect
in the join condition:

SELECT DISTINCT ON (account.id) account.*
FROM trade.transaction_accounts AS account
INNER JOIN people.people AS owner
ON owner.id = account.owner_id
LEFT OUTER JOIN trade.transaction_account_co_owners acct_co_owner
ON account.id = acct_co_owner.account_id
LEFT OUTER JOIN people.people AS co_owner
ON acct_co_owner.co_owner_id = co_owner.id
WHERE upper(account.description) LIKE '%KOE%'
OR upper(owner.name) LIKE '%KOE%'
OR upper(owner.familiar_name) LIKE '%KOE%'
OR upper(owner.full_name) LIKE '%KOE%'
OR upper(co_owner.name) LIKE '%KOE%'
OR upper(co_owner.familiar_name) LIKE '%KOE%'
OR upper(co_owner.full_name) LIKE '%KOE%'

And now it executes in 1 ms.

This is what I was trying to do from the beginning, but because of some mental
block, was unable to think of the join condition...

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Guillaume Lelarge 2007-03-16 12:07:15 Re: INSERT INTO
Previous Message Shavonne Marietta Wijesinghe 2007-03-16 10:31:26 Re: INSERT INTO