Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id CD6F79F9DAD for ; Wed, 20 Dec 2006 07:16:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 91017-05 for ; Wed, 20 Dec 2006 07:16:43 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.237]) by postgresql.org (Postfix) with ESMTP id ACC4F9F9C35 for ; Wed, 20 Dec 2006 07:16:42 -0400 (AST) Received: by wr-out-0506.google.com with SMTP id i23so987475wra for ; Wed, 20 Dec 2006 03:16:41 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=OZz+G9723lxwnKZchztE+BPI91lN2e5PczqTVSdZGfYU0Y4jSdQS0qBudWfUhzAmlQueT2P6QtWju5b2Fqx28Me6UW/AxCnnFucvcWm6D3i70znPkrlaTjVUHWHil6KSXQDXjbHvmQohrmWM2m38AqGEh3KpqAxwuELiAXfh6x0= Received: by 10.78.201.10 with SMTP id y10mr5012896huf.1166613400643; Wed, 20 Dec 2006 03:16:40 -0800 (PST) Received: by 10.78.107.14 with HTTP; Wed, 20 Dec 2006 03:16:40 -0800 (PST) Message-ID: Date: Wed, 20 Dec 2006 11:16:40 +0000 From: "Peter Childs" To: "Postgresql Performance" Subject: Re: GROUP BY vs DISTINCT In-Reply-To: <20061220105739.GB31739@uio.no> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20061220071940.17382.qmail@web52303.mail.yahoo.com> <20061220105739.GB31739@uio.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/487 X-Sequence-Number: 22543 On 20/12/06, Steinar H. Gunderson wrote: > On Tue, Dec 19, 2006 at 11:19:39PM -0800, Brian Herlihy wrote: > > Actually, I think I answered my own question already. But I want to > > confirm - Is the GROUP BY faster because it doesn't have to sort results, > > whereas DISTINCT must produce sorted results? This wasn't clear to me from > > the documentation. If it's true, then I could save considerable time by > > using GROUP BY where I have been using DISTINCT in the past. Usually I > > simply want a count of the distinct values, and there is no need to sort > > for that. > > You are right; at the moment, GROUP BY is more intelligent than DISTINCT, > even if they have to compare the same columns. This is, as always, something > that could be improved in a future release, TTBOMK. > > /* Steinar */ Oh so thats why group by is nearly always quicker than distinct. I always thought distinct was just short hand for "group by same columns as I've just selected" Is it actually in the sql spec to sort in a distinct or could we just get the parser to rewrite distinct into group by and hence remove the extra code a different way of doing it must mean.? Peter.