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
It might be a good solution :-)
Thank you for your help.
Miroslav Šulc
Alvaro Herrera napsal(a):
Miroslav ?ulc wrote:
Well, key is not primary key from another table. It is just a column
in pair key = value.
The structure of the table is this:
Id (primary key)
MRTPContactId (id of
=?ISO-8859-2?Q?Miroslav_=A9ulc?= [EMAIL PROTECTED] writes:
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:
The GROUP BY is really fast :-)
Thank you.
Miroslav Šulc
Tom Lane napsal(a):
Try
SELECT Key FROM MRTPContactValue GROUP BY Key
The select distinct code is a bit old and crufty, GROUP BY is usually
smarter.
regards, tom lane
begin:vcard
Alvaro Herrera [EMAIL PROTECTED] writes:
Miroslav ?ulc wrote:
Well, key is not primary key from another table. It is just a column
in pair key = value.
The structure of the table is this:
Id (primary key)
MRTPContactId (id of contact from table MRTPContact)
Key (key from pair key
Greg Stark napsal(a):
Actually you could try the equivalent query:
SELECT Key FROM MRTPContactValue GROUP BY Key
This may or may not be faster because it can use a hash aggregate plan. I
would expect it to be faster here because there are few distinct keys and the
planner predicts that.
* Alvaro Herrera:
Miroslav ?ulc wrote:
The GROUP BY is really fast :-)
Doh! How does it do it?
It uses a hash table and can therefore discard duplicate rows more
quickly (essentially linear time in the number of rows if the number
of different rows is bounded).