Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C98AA632B50 for ; Sat, 17 Oct 2009 00:21:53 -0300 (ADT) 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 13185-09 for ; Sat, 17 Oct 2009 03:21:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f218.google.com (mail-ew0-f218.google.com [209.85.219.218]) by mail.postgresql.org (Postfix) with ESMTP id C5CE1632849 for ; Sat, 17 Oct 2009 00:21:45 -0300 (ADT) Received: by ewy18 with SMTP id 18so2508016ewy.19 for ; Fri, 16 Oct 2009 20:21:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=Mq7pCtXWSM7af+zlBsY+iAevNPe8Rm9ajtu26J26XEw=; b=K8ayRler+6y/Mbow+Xhh8WluMABVkOO6fZNWV+rPvv0nH2TWEti/j7ICTaghgwmpZL VoEYlWllqSx963qC2xbJLUt0TA3QaY+YmW++BNbRtmU8sLW9fG82Jvvzwz6qbgWDpHKk K8TP0dEJbjmeOJOw9SH1B9mJcZBlmbH8AniUw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=RciF1/UQaUr2f4ne8N0myzmAdoomptN2LHVH6HPhYYQNl+x7uMCOCD8gioFt9kqwb1 OO66h81/Kn0rwRq7HInKyNgvYi3d0QCkiLzyvVUgwfAql3rtHHe+C2nqLj/uSypDvkld AJ3xZeGduOCTRKRMu0Ts2a1d6lF0/ml+zGatY= Received: by 10.211.160.4 with SMTP id m4mr2514700ebo.24.1255749704387; Fri, 16 Oct 2009 20:21:44 -0700 (PDT) Received: from ?192.168.0.2? (rsargent.dsl.xmission.com [199.104.125.126]) by mx.google.com with ESMTPS id 23sm2287583eya.36.2009.10.16.20.21.41 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 16 Oct 2009 20:21:43 -0700 (PDT) Message-ID: <4AD93837.7030500@gmail.com> Date: Fri, 16 Oct 2009 21:21:27 -0600 From: Rob Sargent User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Gary Chambers CC: pgsql-sql@postgresql.org Subject: Re: Efficiency in Multiple Queries of Same Table in a PL/PgSQL Function References: <302670f20910161927h688e13e7qd096e63e68b7ae6c@mail.gmail.com> In-Reply-To: <302670f20910161927h688e13e7qd096e63e68b7ae6c@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 200910/62 X-Sequence-Number: 33261 I don't see anything in the assignment statements (sigma_* :=) which would prevent one from doing all three of them within a single for loop. In fact, written as is there's some chance the values of the sigma_*s might change between repeated calls to the function since there is no explicit ordering of the rows returned from table blah. Putting all the assignments into a single select from blah would at least say that the sigma values are from the same dataset per run. As to efficiency in general, I would expect the entire table (~50 rows) would be entirely in memory after the first select, but you plan triples the time in the loop. This expense would likely only be noticeable if the function itself is called /lots/. Gary Chambers wrote: > All... > > In the poly_example function below, I am emulating an actual > requirement by querying the same table three (3) times in order to > derive a solution to a problem. Is this the best or most efficient > and effective way to implement this? The table (which consists of > only five (5) FLOAT8 columns) I'm querying contains less than 50 rows. > Thanks in advance for any insight or criticisms you offer. > > CREATE OR REPLACE FUNCTION poly_example() RETURNS SETOF FLOAT8 AS > $poly_example$ > DECLARE > term blah%ROWTYPE; > sigma_l FLOAT8 := 0.0; > sigma_b FLOAT8 := 0.0; > sigma_r FLOAT8 := 0.0; > > BEGIN > FOR term in SELECT * FROM blah LOOP > sigma_l := sigma_l + (RANDOM() * 100) * (term.i * term.i) + > RANDOM() * (term.j * term.j) + term.k; > END LOOP; > > FOR term in SELECT * FROM blah LOOP > sigma_b := sigma_b + (RANDOM() * 53) * (term.i * term.i) + > (RANDOM() * 5) * (term.j * term.j) + term.k; > END LOOP; > > FOR term in SELECT * FROM blah LOOP > sigma_r := sigma_r + 96.232234 * (term.i * term.i) + > 0.32322325 * (term.j * term.j) + term.k; > END LOOP; > > RETURN NEXT sigma_l + sigma_b + sigma_r; > END; > $poly_example$ LANGUAGE plpgsql STRICT IMMUTABLE; > > -- Gary Chambers > > /* Nothing fancy and nothing Microsoft! */ > >