Re: Query with correlated join having slow performance

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: saket bansal <saket(dot)tcs(at)gmail(dot)com>
Cc: Michael Lewis <mlewis(at)entrata(dot)com>, PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Query with correlated join having slow performance
Date: 2019-12-09 20:14:32
Message-ID: CAFj8pRAb2bdXHuGi2ZF0WAOyPW=KyRY-vK8pGzEWr4VnE96RcA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

po 9. 12. 2019 v 21:05 odesílatel saket bansal <saket(dot)tcs(at)gmail(dot)com> napsal:

> Thank you Michael. I re-wrote it and it does perform well. Modified query
> at:
>
>
> https://github.com/bansalsaket/PG_correlated_subquery_slowness/blob/master/Modified%20query%20-%20performs%20faster.txt
>
> Our app team is checking with their vendor whether this can be modified at
> source code level or not.
> But question remains somewhat valid. Data volume is not huge and original
> query wasn't very badly written either. Operating system level resources
> are similar.
> Do you know of any bugs associated with using co-related sub queries in
> postgres. In Oracle, it runs in a sec, while in postgres it does not give
> result at all , even after 8-9 hours.
> I understand both database engines work differently, but such drastic
> change in performance is a surprise!
> We have lot of migrations planned from oracle to postgres, this could be a
> show stopper. :(
> Any suggestions...
>

There was more times discussion about rewriting OR conditions to UNION -
but nobody did this work what I know. Usually Postgres process OR
conditions well due bitmap scans, but it doesn't work well in some special
cases. To this time this issue was fixed by manual query rewriting.

Regards

Pavel

> On Mon, Dec 9, 2019 at 1:49 PM Michael Lewis <mlewis(at)entrata(dot)com> wrote:
>
>> I'd suggest re-writing your query to avoid ORs whenever possible. Is this
>> generated by an ORM or subject to change with filters selected in
>> application or can you totally control it on DB side?
>>
>> It may be hugely more performant to simply rewrite this as (almost) the
>> same query twice UNION ALL'd together to separate the
>> productalt1_.alt_pdt_dbky OR productalt1_.orgnl_pdt_dbky conditions.
>>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message github kran 2019-12-09 22:37:10 Re: Upgrade PostgreSQL 9.6 to 10.6
Previous Message saket bansal 2019-12-09 20:04:54 Re: Query with correlated join having slow performance