Re: space for optimalization: DISTINCT without index

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Pavel Stehule <pavel(dot)stehule(at)hotmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: space for optimalization: DISTINCT without index
Date: 2005-12-12 21:48:23
Message-ID: 20051212214823.GA31847@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Dec 12, 2005 at 18:35:07 +0100,
Pavel Stehule <pavel(dot)stehule(at)hotmail(dot)com> wrote:
> Hello
>
> I did some test and I can see so DISTINCT works well on indexed columns,
> but is slow on derived tables without indexes. If I use without distinct
> group by I get much better times.
>
> SELECT DISTINCT a, b FROM tab
> SELECT a,b FROM tab GROUP BY a, b.
>
> Can You Explain it.

DISTINCT will require a sort step to remove duplicates. GROUP BY can use
either a sort or hash aggregate plan. If there are few distinct values,
the hash aggregate plan can be much faster.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2005-12-12 21:53:08 buildfarm off the air pro tem
Previous Message Jim C. Nasby 2005-12-12 21:36:41 Re: Reduce NUMERIC size by 2 bytes, reduce max length to 508 digits