X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 7B2BFD1B520 for ; Thu, 2 Oct 2003 23:53:49 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 02465-07 for ; Thu, 2 Oct 2003 20:53:05 -0300 (ADT) Received: from smtp.noos.fr (nan-smtp-15.noos.net [212.198.2.123]) by svr1.postgresql.org (Postfix) with ESMTP id 30B62D1B50D for ; Thu, 2 Oct 2003 20:53:02 -0300 (ADT) Received: (qmail 24773 invoked by uid 0); 2 Oct 2003 23:53:05 -0000 Received: from unknown (HELO bigfoot.com) ([::ffff:212.198.37.110]) (envelope-sender ) by ::ffff:212.198.2.123 (qmail-ldap-1.03) with SMTP for ; 2 Oct 2003 23:53:05 -0000 Message-ID: <3F7CBA4E.6020302@bigfoot.com> Date: Fri, 03 Oct 2003 01:52:46 +0200 From: Gaetano Mendola User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5b) Gecko/20030901 Thunderbird/0.2 X-Accept-Language: en-us, en MIME-Version: 1.0 To: "pgsql-performance@postgresql.org" Cc: Andriy Tkachuk Subject: Re: runtime of the same query in function differs on 2 degree! References: <20031002163005.N53420-100000@pool.imt.com.ua> In-Reply-To: <20031002163005.N53420-100000@pool.imt.com.ua> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/71 X-Sequence-Number: 3819 Andriy Tkachuk wrote: > Hi folks. > > What's wrong with planner that executes my query in function?: > (i mean no explanation but runtime) > > > tele=# EXPLAIN analyze select calc_total(6916799, 1062363600, 1064955599); > QUERY PLAN > ------------------------------------------------------------------------------------------ > Result (cost=0.00..0.01 rows=1 width=0) (actual time=36919.37..36919.37 rows=1 loops=1) > Total runtime: 36919.40 msec > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > tele=# \df+ calc_total > ... > declare > usr alias for $1; > d1 alias for $2; > d2 alias for $3; > res integer; > begin > select sum(cost) into res > from bills where > (parent(user_id) = usr or user_id = usr) > and dat >= d1 and dat < d2; > if res is not null then > return res; > else > return 0; > end if; > end; You didn't wrote the type of d1 and d2, I had your same problem: declare a_user alias for $1; res INTEGER; begin select cost into res from my_table where login = a_user; ...... end; the problem was that login was a VARCHAR and a_user was a TEXT so the index was not used, was enough cast a_user::varchar; I believe that your dat, d1, d2 are not "index" comparable. Gaetano