Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 0518B2E002E for ; Tue, 6 May 2008 03:57:05 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 42642-04 for ; Tue, 6 May 2008 03:56:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from 27.mail-out.ovh.net (27.mail-out.ovh.net [91.121.30.210]) by developer.postgresql.org (Postfix) with SMTP id 9103C2E002D for ; Tue, 6 May 2008 03:57:01 -0300 (ADT) Received: (qmail 17221 invoked by uid 503); 6 May 2008 08:51:25 -0000 Received: from gw2.ovh.net (HELO mail170.ha.ovh.net) (213.251.189.202) by 27.mail-out.ovh.net with SMTP; 6 May 2008 08:51:25 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 6 May 2008 06:56:53 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO apollo13.peufeu.com) (88.161.102.87) by ns0.ovh.net with SMTP; 6 May 2008 06:56:52 -0000 Date: Tue, 06 May 2008 09:02:42 +0200 To: Justin , pgsql-performance@postgresql.org Subject: Re: need to speed up query From: PFC Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <481FBC0D.7010509@emproshunts.com> Content-Transfer-Encoding: Quoted-Printable Message-ID: In-Reply-To: <481FBC0D.7010509@emproshunts.com> User-Agent: Opera Mail/9.24 (Linux) X-Ovh-Tracer-Id: 86694293408319123 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.663 tagged_above=0 required=5 tests=AWL=0.663 X-Spam-Level: X-Archive-Number: 200805/71 X-Sequence-Number: 30039 > i've had to write queries to get trail balance values out of the GL = > transaction table and i'm not happy with its performance The table has= = > 76K rows growing about 1000 rows per working day so the performance is= = > not that great it takes about 20 to 30 seconds to get all the records = = > for the table and when we limit it to single accounting period it drop= s = > down to 2 seconds What is a "period" ? Is it a month, or something more "custom" ? Can = periods overlap ? > COALESCE(( SELECT sum(gltrans.gltrans_amount) AS sum > FROM gltrans > WHERE gltrans.gltrans_date < period.period_start > AND gltrans.gltrans_accnt_id =3D accnt.accnt_id > AND gltrans.gltrans_posted =3D true), 0.00)::text::money A= S = > beginbalance, Note that here you are scanning the entire table multiple times, the = complexity of this is basically (rows in gltrans)^2 which is something = you'd like to avoid.