Re: SELECT DISTINCT too slow

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Miroslav ?ulc <miroslav(dot)sulc(at)startnet(dot)cz>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SELECT DISTINCT too slow
Date: 2006-06-01 13:53:10
Message-ID: 20060601135310.GB18513@surnet.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Miroslav ?ulc wrote:
> Hello,
>
> I have a table with cca 400,000 rows. The table contains column "key" of
> varchar(20) type containing 10 distinct values. I want to get out what
> distinct values are present in the column. I use this simple query,
> which is very slow:
>
> SELECT DISTINCT Key FROM MRTPContactValue

You could get the universe of values from the table where this is a
primary key, and use an IN clause (which apparently is more efficient
than an EXISTS in some cases, but try that too) to search for values
that exist in the MRTPContactValue table.

I assume you do have the other table, don't you?

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Chille 2006-06-01 14:21:58 Re: distinct aggregate with complex type dont see the equality operator [solved]
Previous Message Rod Taylor 2006-06-01 13:41:56 Re: Table design question