Re: Joins on many-to-many relations. - Mailing list pgsql-sql

From Wiebe Cazemier
Subject Re: Joins on many-to-many relations.
Date
Msg-id ete0ki$l9s$1@sea.gmane.org
Whole thread Raw
In response to Joins on many-to-many relations.  (Wiebe Cazemier <halfgaar@gmx.net>)
List 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...



pgsql-sql by date:

Previous
From: "Shavonne Marietta Wijesinghe"
Date:
Subject: Re: INSERT INTO
Next
From: Guillaume Lelarge
Date:
Subject: Re: INSERT INTO