Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 110536327C4 for ; Tue, 22 Dec 2009 19:00:54 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 24058-05 for ; Tue, 22 Dec 2009 23:00:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-out112.alice.it (smtp-out112.alice.it [85.37.17.112]) by mail.postgresql.org (Postfix) with ESMTP id 8A2D763273A for ; Tue, 22 Dec 2009 19:00:41 -0400 (AST) Received: from FBCMMO02.fbc.local ([192.168.68.196]) by smtp-out112.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 00:00:38 +0100 Received: from FBCMCL01B02.fbc.local ([192.168.69.83]) by FBCMMO02.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 00:00:37 +0100 Received: from lan.webthatworks.it ([79.0.145.79]) by FBCMCL01B02.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 00:00:36 +0100 Received: from dawn.webthatworks.it (dawn.webthatworks.it [192.168.1.12]) by lan.webthatworks.it (Postfix) with ESMTP id 7BB983005EF for ; Wed, 23 Dec 2009 00:00:32 +0100 (CET) Received: from dawn.webthatworks.it (localhost [127.0.0.1]) by dawn.webthatworks.it (Postfix) with ESMTP id 39AE13DA9B for ; Wed, 23 Dec 2009 00:00:32 +0100 (CET) Date: Wed, 23 Dec 2009 00:00:31 +0100 From: Ivan Sergio Borgonovo To: pgsql-sql@postgresql.org Subject: Re: short-cutting if sum()>constant Message-ID: <20091223000031.54ad09b5@dawn.webthatworks.it> In-Reply-To: <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> References: <20091222133527.3007bd96@dawn.webthatworks.it> <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> Organization: http://www.webthatworks.it X-Mailer: Claws Mail 3.7.3 (GTK+ 2.18.5; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 22 Dec 2009 23:00:36.0720 (UTC) FILETIME=[92D4DF00:01CA835A] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.284 tagged_above=-10 required=5 tests=AWL=0.315, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/65 X-Sequence-Number: 33503 On Tue, 22 Dec 2009 20:47:18 +0100 Pavel Stehule wrote: > Hello > > I found one ugly trick. You can multiply lines and SUM > cons > could be replaced limit clause: The trick is really smart (and fun), kudos, really, it's always a pleasure to read your solutions, thanks. But as expected: In a table with 100000 rows with random values [0,9] test=# select sum(x) from (select 1 as x,(a = generate_series(1,a))::int from data limit 100000) s; sum -------- 100000 (1 row) Time: 71.423 ms test=# create or replace function tano(a int, out b int) returns int as $$ declare row record; begin b :=0; for row in select a as _a from data loop b := row._a + b; if (b>=a) then return; end if; end loop; return; end; $$ language plpgsql; test=# select * from tano(100000); b -------- 100000 (1 row) Time: 0.187 ms I run both several times to avoid simple caching issues... anyway I didn't really run a serious benchmark, but results were always in the same order of magnitude. I hope I didn't make any mistake. -- Ivan Sergio Borgonovo http://www.webthatworks.it