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... ---------------------------(end of broadcast)--------------------------- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match