Re: Optimizing `WHERE x IN` query

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Optimizing `WHERE x IN` query
Date: 2019-07-07 14:33:00
Message-ID: 86b8a755-2fa4-f7c2-8b0b-3290773feb97@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Omar Roth schrieb am 07.07.2019 um 15:43:
> Currently, the query I'm using to generate a user's feed is:
>
> ```
> SELECT * FROM channel_videos WHERE ucid IN (SELECT unnest(subscriptions) FROM
> users WHERE email = $1) ORDER BY published DESC;
> ```

You could try an EXISTS query without unnest:

select cv.*
from channel_videos cv
where exists ucid (select *
from users u
where cv.ucid = any(u.subscriptions)
and u.email = $1);

Did you try if a properly normalized model performs better?

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Thomas Munro 2019-07-07 23:57:37 Re: Bloom index cost model seems to be wrong
Previous Message Omar Roth 2019-07-07 13:43:14 Optimizing `WHERE x IN` query