Re: WHERE ... IN condition and multiple columns in subquery

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: WHERE ... IN condition and multiple columns in subquery
Date: 2016-10-28 14:06:48
Message-ID: 14473.1477663608@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alexander Farber <alexander(dot)farber(at)gmail(dot)com> writes:
> is it please possible to rewrite the SQL query

> SELECT DISTINCT ON (uid)
> uid,
> female,
> given,
> photo,
> place
> FROM words_social
> WHERE uid IN (SELECT player1 FROM games)
> OR uid IN (SELECT player2 FROM games)
> ORDER BY uid, stamp DESC

You could do

WHERE uid IN (SELECT player1 FROM games UNION SELECT player2 FROM games)

(or possibly UNION ALL would be a trifle faster). This still reads the
"games" CTE twice; but since you're evaluating that CTE elsewhere in the
query, I think the additional follower node isn't worth trying to get
rid of.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steve Clark 2016-10-28 14:15:27 Re: deadlock error - version 8.4 on CentOS 6
Previous Message Tom Lane 2016-10-28 13:48:14 Re: deadlock error - version 8.4 on CentOS 6