Re: performance drop when function argument is evaluated in WHERE clause - Mailing list pgsql-performance

From Tom Lane
Subject Re: performance drop when function argument is evaluated in WHERE clause
Date
Msg-id 23396.1396965001@sss.pgh.pa.us
Whole thread Raw
In response to performance drop when function argument is evaluated in WHERE clause  (Gerardo Herzig <gherzig@fmed.uba.ar>)
Responses Re: performance drop when function argument is evaluated in WHERE clause  (Gerardo Herzig <gherzig@fmed.uba.ar>)
List pgsql-performance
Gerardo Herzig <gherzig@fmed.uba.ar> writes:
> Hi all. I have a function that uses a "simple" select between 3 tables. There is a function argument to help choose
howa WHERE clause applies. This is the code section: 
> select * from....
> [...]
> where case $3
>   when 'I' then [filter 1]
>   when 'E' then [filter 2]
>   when 'P' then [filter 3]
> else true end

> When the function is called with, say, parameter $3 = 'I', the funcion run in 250ms,
> but when there is no case involved, and i call directly "with [filter 1]" the function runs in 70ms.

> Looks like the CASE is doing something nasty.
> Any hints about this?

Don't do it like that.  You're preventing the optimizer from understanding
which filter applies.  Better to write three separate SQL commands
surrounded by an if/then/else construct.

(BTW, what PG version is that?  I would think recent versions would
realize that dynamically generating a plan each time would work around
this.  Of course, that approach isn't all that cheap either.  You'd
probably still be better off splitting it up manually.)

            regards, tom lane


pgsql-performance by date:

Previous
From: Shaun Thomas
Date:
Subject: Re: query against large table not using sensible index to find very small amount of data
Next
From: Tom Lane
Date:
Subject: Re: query against large table not using sensible index to find very small amount of data