X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A537B9FA5DC for ; Thu, 1 Jun 2006 12:20:35 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 18235-10 for ; Thu, 1 Jun 2006 12:20:31 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 9B6509FA040 for ; Thu, 1 Jun 2006 12:20:31 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k51FKUFE012555; Thu, 1 Jun 2006 11:20:30 -0400 (EDT) To: =?ISO-8859-2?Q?Miroslav_=A9ulc?= cc: pgsql-sql@postgresql.org Subject: Re: SELECT DISTINCT too slow In-reply-to: <447EEAF1.7040305@startnet.cz> References: <447EEAF1.7040305@startnet.cz> Comments: In-reply-to =?ISO-8859-2?Q?Miroslav_=A9ulc?= message dated "Thu, 01 Jun 2006 15:26:09 +0200" Date: Thu, 01 Jun 2006 11:20:30 -0400 Message-ID: <12554.1149175230@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/8 X-Sequence-Number: 25244 =?ISO-8859-2?Q?Miroslav_=A9ulc?= 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: > SELECT DISTINCT Key FROM MRTPContactValue 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