Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXWPn-0008Q5-RY for pgsql-sql@arkaria.postgresql.org; Tue, 08 Apr 2014 13:50:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WXWPn-0004zQ-9Q for pgsql-sql@arkaria.postgresql.org; Tue, 08 Apr 2014 13:50:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXWPl-0004xJ-NT; Tue, 08 Apr 2014 13:50:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXWPj-0008MU-6m; Tue, 08 Apr 2014 13:50:13 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s38Do1q3023397; Tue, 8 Apr 2014 09:50:01 -0400 From: Tom Lane To: Gerardo Herzig cc: pgsql-performance@postgresql.org, "pgsql-sql " Subject: Re: [PERFORM] performance drop when function argument is evaluated in WHERE clause In-reply-to: <731037499.293695.1396958021532.JavaMail.root@fmed.uba.ar> References: <731037499.293695.1396958021532.JavaMail.root@fmed.uba.ar> Comments: In-reply-to Gerardo Herzig message dated "Tue, 08 Apr 2014 08:53:41 -0300" Date: Tue, 08 Apr 2014 09:50:01 -0400 Message-ID: <23396.1396965001@sss.pgh.pa.us> X-Pg-Spam-Score: -2.2 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Gerardo Herzig writes: > Hi all. I have a function that uses a "simple" select between 3 tables. There is a function argument to help choose how a 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 -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql