Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 66D5163339D for ; Tue, 22 Dec 2009 21:49:02 -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 13652-07 for ; Wed, 23 Dec 2009 01:48:54 +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 C8CEF632D2A for ; Tue, 22 Dec 2009 21:48:54 -0400 (AST) Received: from fbcmmo06.fbc.local ([192.168.184.137]) by smtp-out112.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 02:48:51 +0100 Received: from FBCMCL01B07.fbc.local ([192.168.171.45]) by fbcmmo06.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 02:46:36 +0100 Received: from lan.webthatworks.it ([79.0.145.79]) by FBCMCL01B07.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 02:46:36 +0100 Received: from dawn.webthatworks.it (dawn.webthatworks.it [192.168.1.12]) by lan.webthatworks.it (Postfix) with ESMTP id 6AFB030056A for ; Wed, 23 Dec 2009 02:46:35 +0100 (CET) Received: from dawn.webthatworks.it (localhost [127.0.0.1]) by dawn.webthatworks.it (Postfix) with ESMTP id 3EC773DA9B for ; Wed, 23 Dec 2009 02:46:35 +0100 (CET) Date: Wed, 23 Dec 2009 02:46:35 +0100 From: Ivan Sergio Borgonovo To: pgsql-sql@postgresql.org Subject: Re: short-cutting if sum()>constant Message-ID: <20091223024635.48d66554@dawn.webthatworks.it> In-Reply-To: <20091223010940.51c5c069@dawn.webthatworks.it> References: <20091222133527.3007bd96@dawn.webthatworks.it> <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> <20091223000031.54ad09b5@dawn.webthatworks.it> <20091223010940.51c5c069@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 01:46:36.0329 (UTC) FILETIME=[C3389990:01CA8371] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.11 tagged_above=-10 required=5 tests=BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 200912/67 X-Sequence-Number: 33505 On Wed, 23 Dec 2009 01:09:40 +0100 Ivan Sergio Borgonovo wrote: > 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... As even more unexpected... when all row are >0 and most of them are equal to 1 the generate_series performs appreciably better (roughly 15% faster). And I think your version can be further optimised: select count(*) from (select (generate_series(1,a))::int from data limit 90000000) s; This perform 30% faster. So what's so slow in the plpgsql version? Fortunately as expected when "enough" rows are >1 the for loop solution perform much better. -- Ivan Sergio Borgonovo http://www.webthatworks.it