Re: Weird CASE WHEN behaviour causing query to be suddenly very slow

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Kevin Viraud <kevin(dot)viraud(at)rocket-internet(dot)de>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Weird CASE WHEN behaviour causing query to be suddenly very slow
Date: 2015-03-31 17:03:10
Message-ID: CAKFQuwYTKZT7CyWEn1qorQzcEh2=O-AaEQTKyH7QuD1s7v_Bhw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, Mar 31, 2015 at 8:58 AM, Kevin Viraud <
kevin(dot)viraud(at)rocket-internet(dot)de> wrote:

> Touche ! Thanks a lot.
>
> Looking more at the data yes it goes very often to ELSE Clause. And
> therefore reaching the MAX_CACHED_RES.
>
> In there anyway to increase that value ?
>
> Basically, I have several tables containing millions of rows and let say 5
> columns. Those five columns, depending of their combination give me a 6th
> value.
> We have complex patterns to match and using simple LIKE / EQUAL and so on
> wouldn't be enough. This can be applied to N number of table so we
> refactored this process into a function that we can use in the SELECT
> statement, by giving only the 5 values each time.
>
> I wouldn't mind using a table and mapping it through a join if it were for
> my own use.
> But the final query has to be readable and usable for almost-non-initiated
> SQL user... So using a function with encapsulated case when seemed to be a
> good idea and so far worked nicely.
>
> But we might consider changing it if we have no other choice...
>
> Regards,
>
> Kevin
>
>
​Thoughts...​

​Memoization: http://en.wikipedia.org/wiki/Memoization

Rewrite the function in pl/perl​ and compare performance

​Hierarchy of CASE statements allowing you to reduce the number of
possibilities in exchange for manually pre-processing the batches on a
significantly less complicated condition probably using only 1 or 2 columns
instead of all five.​

​I'm not familiar with the caching constraint or the data so its hard to
make more specific suggestions.

David J.​

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tomas Vondra 2015-03-31 19:02:29 Re: PATCH: adaptive ndistinct estimator v4
Previous Message Kevin Viraud 2015-03-31 15:58:57 Re: Weird CASE WHEN behaviour causing query to be suddenly very slow