Re: hashed subplan 5000x slower than two sequential operations

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Shrirang Chitnis <Shrirang(dot)Chitnis(at)hovservices(dot)com>, Bryce Nesbitt <bryce2(at)obviously(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: hashed subplan 5000x slower than two sequential operations
Date: 2010-12-08 20:25:40
Message-ID: AANLkTikQgD_ugqQOF4hQ0KgXXVgqfoAb7NjmZk+v0KfG@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

2010/12/8 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Shrirang Chitnis <Shrirang(dot)Chitnis(at)hovservices(dot)com> writes:
>> Bryce,
>> The two queries are different:
>
> I suspect the second one is a typo and not what he really wanted.
>
>> WHERE (contexts.parent_key = 392210
>>       OR contexts.context_key IN
>>          (SELECT collection_data.context_key
>>          FROM collection_data
>>           WHERE collection_data.collection_context_key = 392210)
>
> The only really effective way the planner knows to optimize an
> "IN (sub-SELECT)" is to turn it into a semi-join, which is not possible
> here because of the unrelated OR clause.  You might consider replacing
> this with a UNION of two scans of "contexts".  (And yes, I know it'd be
> nicer if the planner did that for you.)

I remeber a similar case - 9 years ago.

slow variant:

WHERE pk = C1 OR pk IN (SELECT .. FROM .. WHERE some = C2)

I had to rewrite to form

WHERE pk IN (SELECT .. FROM WHERE some = C2 UNION ALL SELECT C1)

Regards

Pavel Stehule

>
>                        regards, tom lane
>
> --
> Sent via pgsql-performance mailing list (pgsql-performance(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-performance
>

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Bryce Nesbitt 2010-12-08 20:31:53 Re: hashed subplan 5000x slower than two sequential operations
Previous Message Tom Lane 2010-12-08 20:12:26 Re: hashed subplan 5000x slower than two sequential operations