Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 9785A9FBE34 for ; Wed, 30 May 2007 13:46:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 47256-04 for ; Wed, 30 May 2007 13:46:08 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from main2.mycybernet.net (main2.mycybernet.net [209.222.63.140]) by postgresql.org (Postfix) with ESMTP id 786CA9FBE0F for ; Wed, 30 May 2007 13:46:09 -0300 (ADT) Received: from 227-54-222-209.mycybernet.net ([209.222.54.227] helo=phlogiston.dydns.org) by main2.mycybernet.net with esmtp (Exim 4.62) (envelope-from ) id 1HtRJ8-0003Bq-E6 for pgsql-sql@postgresql.org; Wed, 30 May 2007 12:45:58 -0400 Received: by phlogiston.dydns.org (Postfix, from userid 1000) id 128DD3FAF; Wed, 30 May 2007 12:45:53 -0400 (EDT) Date: Wed, 30 May 2007 12:45:53 -0400 From: Andrew Sullivan To: pgsql-sql@postgresql.org Subject: Re: slow query execution Message-ID: <20070530164553.GK16260@phlogiston.dyndns.org> References: <543741.9839.qm@web52709.mail.re2.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <543741.9839.qm@web52709.mail.re2.yahoo.com> User-Agent: Mutt/1.5.9i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/193 X-Sequence-Number: 28473 On Wed, May 30, 2007 at 08:56:45AM -0700, Trigve Siver wrote: > Hi all, > > This query executes very slow: > > select (select count(*) from customer where id <= a.id) as row, id, > from customer as a order by id; So you are trying to get the ordinal position of every ID in the table? I'm not surprised it takes a long time -- you have to join the whole table to itself and then do a lot of counting. Are you just trying to get the "row number" for your query answer? You can do this with a temporary sequence, among other approaches, more cheaply. A -- Andrew Sullivan | ajs@crankycanuck.ca Users never remark, "Wow, this software may be buggy and hard to use, but at least there is a lot of code underneath." --Damien Katz