Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id E78779FB694 for ; Fri, 16 Mar 2007 08:56:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 74476-04-2 for ; Fri, 16 Mar 2007 08:56:22 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 165239FB658 for ; Fri, 16 Mar 2007 08:56:23 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1HSB2Y-0002to-Uh for pgsql-sql@postgresql.org; Fri, 16 Mar 2007 12:56:10 +0100 Received: from cc503261-a.eelde1.dr.home.nl ([212.204.130.190]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 16 Mar 2007 12:56:10 +0100 Received: from halfgaar by cc503261-a.eelde1.dr.home.nl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 16 Mar 2007 12:56:10 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Wiebe Cazemier Subject: Re: Joins on many-to-many relations. Date: Fri, 16 Mar 2007 12:56:02 +0100 Lines: 31 Message-ID: References: <5.2.1.1.0.20070314133826.050002b0@pop6.sympatico.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: cc503261-a.eelde1.dr.home.nl User-Agent: KNode/0.10.4 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/222 X-Sequence-Number: 27860 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...