X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AAD635292F for ; Wed, 1 Jun 2005 14:01:35 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41530-06 for ; Wed, 1 Jun 2005 17:01:34 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 92E795292D for ; Wed, 1 Jun 2005 14:01:32 -0300 (ADT) Received: (qmail 24880 invoked by uid 500); 1 Jun 2005 17:00:33 -0000 Date: Wed, 1 Jun 2005 12:00:32 -0500 From: Bruno Wolff III To: lucas@presserv.org Cc: pgsql-sql@postgresql.org Subject: Re: Sum() rows Message-ID: <20050601170032.GB20923@wolff.to> Mail-Followup-To: Bruno Wolff III , lucas@presserv.org, pgsql-sql@postgresql.org References: <20050531133648.cypdc5lnjtc0wsws@www.presserv.org> <20050531171708.GB5899@wolff.to> <20050601084900.duzlosra25wkwgsw@www.presserv.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050601084900.duzlosra25wkwgsw@www.presserv.org> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/8 X-Sequence-Number: 21609 On Wed, Jun 01, 2005 at 08:49:00 -0300, lucas@presserv.org wrote: > Yes, > I tried it. In this table the query works fine, but in a big table > (with aprox. > 200.000 records) the query performace is very bad. > I tried it (in the example table): > SELECT *,(select sum(value) from tb1 as tb1_2 where tb1_2.id<=tb1_1.id) as > subtot from tb1 as tb1_1 order by id; > > In a small table it works fine, but in a bigger table it works very slow. Not surprising, since this is probably O(n^2). > What is the better way??? Is there a sum() function that works how I want??? Having the application do the running sum is probably the best way to do it.