Re: Optimising a two column OR check

From: Ivan Voras <ivoras(at)gmail(dot)com>
To: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Cc: postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Optimising a two column OR check
Date: 2019-10-13 13:29:19
Message-ID: CAF-QHFWjyEOGQq5M-3PZShZHFeC0idWpHw1WPq+iQ+ErdxYWAg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Sat, 12 Oct 2019 at 17:16, Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
wrote:

> >>>>> "Ivan" == Ivan Voras <ivoras(at)gmail(dot)com> writes:
>

> Ivan> SELECT user1_id,user2_id FROM friend WHERE user1_id=42 OR
> user2_id=42;
>
> To get friends of user 42:
>
> SELECT user1_id FROM friend WHERE user2_id=42
> UNION ALL
> SELECT user2_id FROM friend WHERE user1_id=42;
>
> assuming you create the (user2_id,user1_id) index, this should get you
> an Append of two index-only scans. We can use UNION ALL here rather than
> UNION because the table constraints ensure there are no duplicates.
>

Thanks! That's a more elegant solution for my query than what I had in mind!

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Ivan Voras 2019-10-13 13:38:49 Re: Optimising a two column OR check
Previous Message Andrew Gierth 2019-10-12 16:58:22 Re: Optimising a two column OR check