Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WCx58-0000xN-G4 for pgsql-performance@arkaria.postgresql.org; Mon, 10 Feb 2014 20:03:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WCx57-0002lA-Mm for pgsql-performance@arkaria.postgresql.org; Mon, 10 Feb 2014 20:03:53 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WCx56-0002l3-IA for pgsql-performance@postgresql.org; Mon, 10 Feb 2014 20:03:52 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WCx4x-00035T-C4 for pgsql-performance@postgresql.org; Mon, 10 Feb 2014 20:03:51 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 38207285E6; Mon, 10 Feb 2014 12:03:39 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 350374040C; Mon, 10 Feb 2014 12:03:39 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id AECB1C0063; Mon, 10 Feb 2014 12:03:38 -0800 (PST) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Mon, 10 Feb 2014 12:03:37 -0800 (PST) Message-ID: <52F93098.2090002@vmware.com> Date: Mon, 10 Feb 2014 22:03:36 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: M Putz CC: pgsql-performance@postgresql.org Subject: Re: Strange performance boost with random() References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -7.5 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 02/10/2014 09:52 PM, M Putz wrote: > > Hello, > > While analyzing performance, we encountered the following phenomenon, > > SELECT sum(pow(.5*generate_series,.5)) > FROM generate_series(1,1000000); > > is much much (a hundred times) slower than > > SELECT sum(pow(random()*generate_series,.5)) > FROM generate_series(1,1000000); > > and asymptotic difference is even more astounding. > This seems counter-intuitive, considering the cost of > an additional random() call instead of a constant factor. > What are the reasons for this strange performance boost? Different data type. The first uses numeric, which is pretty slow for doing calculations. random() returns a double, which makes the pow and sum to also use double, which is a lot faster. To see the effect, try these variants: SELECT sum(pow(.5::float8 * generate_series,.5)) FROM generate_series(1,1000000); SELECT sum(pow(random()::numeric * generate_series,.5)) FROM generate_series(1,1000000); - Heikki -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance