pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sven R. Kunze <srkunze@mail.de>
To: lfischer <l.fischer@ed.ac.uk>
To: pgsql-performance@postgresql.org
To: jeff.janes@gmail.com
To: ineyman@perceptron.com
To: b.n.madusudanan@gmail.com
Subject: Re: Multiple-Table-Spanning Joins with ORs in WHERE Clause
Date: Fri, 23 Sep 2016 08:35:09 +0200
Message-ID: <0e86e399-61dc-8e78-415e-c83db9592a80@mail.de> (raw)
In-Reply-To: <fc49b4e6-8912-2a8d-4f58-7d1ed455cb49@ed.ac.uk>
References: <97dbd6f9-a289-94d1-325f-997df87c672a@mail.de>
	<fc49b4e6-8912-2a8d-4f58-7d1ed455cb49@ed.ac.uk>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

Thanks a lot Madusudanan, Igor, Lutz and Jeff for your suggestions.

What I can confirm is that the UNION ideas runs extremely fast (don't 
have access to the db right now to test the subquery idea, but will 
check next week as I travel right now). Thanks again! :)


I was wondering: would it be possible for PostgreSQL to rewrite the 
query to generate the UNION (or subquery plan if it's also fast) on it's 
own?


Thanks,
Sven

On 22.09.2016 16:44, lfischer wrote:
> Hi Sven
>
> Why not do something like
>
> SELECT * FROM big_table
> WHERE
>      id in (SELECT big_table_id FROM table_a WHERE "table_a"."item_id" 
> IN (<handful of items>))
>     OR
>      id in (SELECT big_table_id FROM table_a WHERE "table_b"."item_id" 
> IN (<handful of items>))
>
> that way you don't need the "distinct" and therefore there should be 
> less comparison going on.
>
> Lutz
>
> On 22/09/16 14:24, Sven R. Kunze wrote:
>> Hi pgsql-performance list,
>>
>>
>> what is the recommended way of doing **multiple-table-spanning joins 
>> with ORs in the WHERE-clause**?
>>
>>
>> Until now, we've used the LEFT OUTER JOIN to filter big_table like so:
>>
>>
>> SELECT DISTINCT <fields of big_table>
>> FROM
>>     "big_table"
>>     LEFT OUTER JOIN "table_a" ON ("big_table"."id" = 
>> "table_a"."big_table_id")
>>     LEFT OUTER JOIN "table_b" ON ("big_table"."id" = 
>> "table_b"."big_table_id")
>> WHERE
>>     "table_a"."item_id" IN (<handful of items>)
>>     OR
>>     "table_b"."item_id" IN (<handful of items>);
>>
>>
>> However, this results in an awful slow plan (requiring to scan the 
>> complete big_table which obviously isn't optimal).
>> So, we decided (at least for now) to split up the query into two 
>> separate ones and merge/de-duplicate the result with application logic:
>>
>>
>> SELECT <fields of big_table>
>> FROM
>>     "big_table" INNER JOIN "table_a" ON ("big_table"."id" = 
>> "table_a"."big_table_id")
>> WHERE
>>     "table_a"."item_id" IN (<handful of items>);
>>
>>
>> SELECT <fields of big_table>
>> FROM
>>     "big_table" INNER JOIN "table_b" ON ("big_table"."id" = 
>> "table_b"."big_table_id")
>> WHERE
>>     "table_b"."item_id" IN (<handful of items>);
>>
>>
>> As you can imagine we would be very glad to solve this issue with a 
>> single query and without having to re-code existing logic of 
>> PostgreSQL. But how?
>>
>>
>> Best,
>> Sven
>>
>>
>> PS: if you require EXPLAIN ANALYZE, I can post them as well.
>>
>>
>
>



-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



view thread (17+ messages)  latest in thread

Message-ID: <0e86e399-61dc-8e78-415e-c83db9592a80@mail.de>
Permalink:  ../0e86e399-61dc-8e78-415e-c83db9592a80@mail.de/
Also on:    postgresql.org/message-id/0e86e399-61dc-8e78-415e-c83db9592a80@mail.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: srkunze@mail.de, l.fischer@ed.ac.uk, jeff.janes@gmail.com, ineyman@perceptron.com, b.n.madusudanan@gmail.com
  Subject: Re: Multiple-Table-Spanning Joins with ORs in WHERE Clause
  In-Reply-To: <0e86e399-61dc-8e78-415e-c83db9592a80@mail.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox