Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 8A623632741 for ; Fri, 16 Oct 2009 23:27:37 -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 04875-08 for ; Sat, 17 Oct 2009 02:27:29 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f203.google.com (mail-qy0-f203.google.com [209.85.221.203]) by mail.postgresql.org (Postfix) with ESMTP id 943CE632452 for ; Fri, 16 Oct 2009 23:27:29 -0300 (ADT) Received: by qyk42 with SMTP id 42so1908491qyk.28 for ; Fri, 16 Oct 2009 19:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=CGAOVKm+NnxD4pblzGPA/5WLaEee3WzJT7mDxRNrut0=; b=iJ/KtkXYGEs2kELcRpy3GpQCV6x4DIi/dif7nhouJXoj1B9uoIft0zmqQLgSl5/oON PFC2AdmHYd6p7vquwHh27jYTVqcjMs0NUicXHj45FhLlFJEE+vvJh9Xy9l4mzOjW2K0b dNBemqEy9lcf06M5iM1ksaHE2tmwf/AYG1kZk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=xQJ5CbsPN+Skw3EczEJDZ5UtMYvVlM0MHjReWOOWbvFxoiY7ge4yfxtZJAfNuJk5HT p5ur5v88h1NHBN3hpoCj2BRbsea/NZV4QwLkbPfGvhNvKl+K7IDRncrFamezPV8ap+ih oFOJ7vy+voJwuTOonoezBD0SgpHp2UV5P+sNY= MIME-Version: 1.0 Received: by 10.229.28.16 with SMTP id k16mr317000qcc.70.1255746447791; Fri, 16 Oct 2009 19:27:27 -0700 (PDT) Date: Fri, 16 Oct 2009 22:27:27 -0400 Message-ID: <302670f20910161927h688e13e7qd096e63e68b7ae6c@mail.gmail.com> Subject: Efficiency in Multiple Queries of Same Table in a PL/PgSQL Function From: Gary Chambers To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 200910/61 X-Sequence-Number: 33260 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! */