pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Childs <peterachilds@gmail.com>
To: Postgresql Performance <pgsql-performance@postgresql.org>
Subject: Re: GROUP BY vs DISTINCT
Date: Wed, 20 Dec 2006 11:16:40 +0000
Message-ID: <a2de01dd0612200316g92cc189jf3369ccedf1b8c12@mail.gmail.com> (raw)
In-Reply-To: <20061220105739.GB31739@uio.no>
References: <20061220071940.17382.qmail@web52303.mail.yahoo.com>
	<20061220105739.GB31739@uio.no>

On 20/12/06, Steinar H. Gunderson <sgunderson@bigfoot.com> 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.



view thread (4+ messages)  latest in thread

Message-ID: <a2de01dd0612200316g92cc189jf3369ccedf1b8c12@mail.gmail.com>
Permalink:  ../a2de01dd0612200316g92cc189jf3369ccedf1b8c12@mail.gmail.com/
Also on:    postgresql.org/message-id/a2de01dd0612200316g92cc189jf3369ccedf1b8c12@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: peterachilds@gmail.com
  Subject: Re: GROUP BY vs DISTINCT
  In-Reply-To: <a2de01dd0612200316g92cc189jf3369ccedf1b8c12@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox