Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 71C65C83CAD for ; Tue, 10 Jul 2012 06:30:54 -0300 (ADT) Received: from gw03.mail.saunalahti.fi ([195.197.172.111]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SoWmT-000378-0k for pgsql-performance@postgresql.org; Tue, 10 Jul 2012 09:30:54 +0000 Received: from cfo-10.mail.saunalahti.fi (cfo-10.mail.saunalahti.fi [62.142.5.104]) by gw03.mail.saunalahti.fi (Postfix) with ESMTP id 28D32216B28 for ; Tue, 10 Jul 2012 12:30:39 +0300 (EEST) Received: from cfo-10.mail.saunalahti.fi (localhost [127.0.0.1]) by cfo-10.mail.saunalahti.fi (Postfix) with SMTP id 144EF54094 for ; Tue, 10 Jul 2012 12:30:39 +0300 (EEST) X-Spam-Checker-Version: SpamAssassin 3.3.1-saunamods_5.20 (2010-03-16) on cfo-10.mail.saunalahti.fi X-Spam-Level: X-Spam-Status: No, score=0.0 required=7.0 tests=none shortcircuit=no autolearn=no version=3.3.1-saunamods_5.20 Received: from castor.webmail.wippies.com (castor.webmail.wippies.com [195.197.55.110]) by cfo-10.mail.saunalahti.fi (Postfix) with ESMTP id C433454097 for ; Tue, 10 Jul 2012 12:30:38 +0300 (EEST) Date: Tue, 10 Jul 2012 12:30:38 +0300 (EEST) From: Pena Kupen To: pgsql-performance@postgresql.org Message-ID: <29875759.2181341912638615.JavaMail.kupen@wippies.fi> Subject: Re: Custom function in where clause MIME-Version: 1.0 Content-Type: text/plain; Charset=iso-8859-1; Format=Flowed Content-Transfer-Encoding: quoted-printable X-Mailer: Saunalahti webmail - http://saunalahti.fi X-Originating-IP: 87.100.242.156 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/117 X-Sequence-Number: 47298 Hi and thank's guys! First trying this Brendan's recommendation. It seems only a small difference between sql and PL/pgSQL. from 40-->37. No= t so good yet. I will try Maxim's little later and you all know. -- kupen Brendan Jurd [direvus@gmail.com] kirjoitti:=20 > On 10 July 2012 18:36, Pena Kupen wrote: > > Hi, > > > > I have searched solution to my problem a few days. On my query, there i= s big > > performance problem. > > It seems to me, that problem is on where-part of sql and it's function. > > >=20 > > How should I handle this situation and use function? > > >=20 > I would start by rewriting your function in plain SQL rather than > PL/pgSQL. As a general rule, don't write a function in PL/pgSQL > unless you really need procedurality. This function does not. >=20 > For example: >=20 > CREATE OR REPLACE FUNCTION get_level_value(_user integer, _id, _area) > RETURNS integer > AS $$ > -- Return 3 if there are matching records in table_o, otherwise return = 1. > SELECT CASE WHEN EXISTS ( > SELECT id > FROM table_o > WHERE userid=3D_user AND > id=3D_id AND > area=3D_area > ) THEN 3 ELSE 1 END; > $$ > LANGUAGE sql STABLE; >=20 > Cheers, > BJ >=20 --=20 Wippies-vallankumous on t=E4=E4ll=E4! Varmista paikkasi vallankumouksen etu= rintamassa ja liity Wippiesiin heti! http://www.wippies.com/