Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 82274632BFA for ; Tue, 22 Dec 2009 20:10:16 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 00973-02 for ; Wed, 23 Dec 2009 00:10:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-out13.alice.it (smtp-out13.alice.it [85.33.2.18]) by mail.postgresql.org (Postfix) with ESMTP id B3C6C632B0C for ; Tue, 22 Dec 2009 20:10:08 -0400 (AST) Received: from FBCMMO04.fbc.local ([192.168.184.135]) by smtp-out13.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 01:10:05 +0100 Received: from FBCMCL01B08.fbc.local ([192.168.171.46]) by FBCMMO04.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 01:09:42 +0100 Received: from lan.webthatworks.it ([79.0.145.79]) by FBCMCL01B08.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 01:09:41 +0100 Received: from dawn.webthatworks.it (dawn.webthatworks.it [192.168.1.12]) by lan.webthatworks.it (Postfix) with ESMTP id 966F73005EF for ; Wed, 23 Dec 2009 01:09:40 +0100 (CET) Received: from dawn.webthatworks.it (localhost [127.0.0.1]) by dawn.webthatworks.it (Postfix) with ESMTP id 6627D3DA9B for ; Wed, 23 Dec 2009 01:09:40 +0100 (CET) Date: Wed, 23 Dec 2009 01:09:40 +0100 From: Ivan Sergio Borgonovo To: pgsql-sql@postgresql.org Subject: Re: short-cutting if sum()>constant Message-ID: <20091223010940.51c5c069@dawn.webthatworks.it> In-Reply-To: <20091223000031.54ad09b5@dawn.webthatworks.it> References: <20091222133527.3007bd96@dawn.webthatworks.it> <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> <20091223000031.54ad09b5@dawn.webthatworks.it> 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: 23 Dec 2009 00:09:41.0375 (UTC) FILETIME=[393D08F0:01CA8364] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/66 X-Sequence-Number: 33504 On Wed, 23 Dec 2009 00:00:31 +0100 Ivan Sergio Borgonovo wrote: > 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: as unexpected... > test=# create or replace function tano(a int, out b int) ^^^ should be anything but a > returns int as > $$ > declare > row record; > begin > b :=0; > for row in select a as _a from data where a>0 > loop > b := row._a + b; > if (b>=a) then > return; > end if; > end loop; > return; > end; > $$ language plpgsql; Making it longer to better appreciate the difference: 1M rows where a [0,2] select * from tano((1000000)::int); b --------- 1000001 (1 row) Time: 1235.243 ms select sum(x) from (select 1 as x,(a = generate_series(1,a))::int from data limit 1000000) s; sum --------- 1000000 (1 row) Time: 1309.441 ms Being fair once you add the where clause to the generate_series version the difference in performance is negligible and saying that the plpgsql version is faster would require some more serious benchmarking. Surprised! If the generate_series can compete with the plpgsql for loop... why is the plpgsql version so "slow"? -- Ivan Sergio Borgonovo http://www.webthatworks.it