Re: SELECT DISTINCT very slow

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-general(at)postgresql(dot)org
Cc: Ben Harper <rogojin(at)gmail(dot)com>
Subject: Re: SELECT DISTINCT very slow
Date: 2009-07-09 15:47:38
Message-ID: 200907091747.38337.andres@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thursday 09 July 2009 17:09:13 Ben Harper wrote:
> Hi,
> Can anybody explain this:
>
> Records: 600,000
> Field Width: Approximately 20 UTF8 characters - type is VARCHAR(25)
> Field is Indexed.
>
> SELECT DISTINCT field FROM table;
>
> Takes about 6 seconds. There are 111 distinct items.
>
> On Sqlite, and another place where I have a B+Tree, this query is
> faster than my eye can measure.
>
> Is this a well known issue?
Yes, I think so.

AFAIK the primary cause is that indexes in pg do not store visibility
information. That means you need to check for existence of the tuple on the
heap.
Possibly due to that PG has no special case code for DISTINCT to optimize such
a query using mostly the index. It would be possible that for each possible
value of 'field' you check the index only long enough to prove that there is at
least one such entry.

Taking that single field into its own table is not possible?

Andres

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2009-07-09 15:56:26 Re: SELECT DISTINCT very slow
Previous Message Bill Moran 2009-07-09 15:25:21 Re: SELECT DISTINCT very slow