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 87D1B9FA5DC for ; Thu, 1 Jun 2006 10:53:15 -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 04183-10 for ; Thu, 1 Jun 2006 10:53:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from alvh.no-ip.org (201-221-200-231.bk11-dsl.surnet.cl [201.221.200.231]) by postgresql.org (Postfix) with ESMTP id D5E689FA040 for ; Thu, 1 Jun 2006 10:53:11 -0300 (ADT) Received: by alvh.no-ip.org (Postfix, from userid 1000) id 2916CC3A214; Thu, 1 Jun 2006 09:53:10 -0400 (CLT) Date: Thu, 1 Jun 2006 09:53:10 -0400 From: Alvaro Herrera To: Miroslav ?ulc Cc: pgsql-sql@postgresql.org Subject: Re: SELECT DISTINCT too slow Message-ID: <20060601135310.GB18513@surnet.cl> Mail-Followup-To: Miroslav ?ulc , pgsql-sql@postgresql.org References: <447EEAF1.7040305@startnet.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <447EEAF1.7040305@startnet.cz> User-Agent: Mutt/1.5.11+cvs20060403 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.122 tagged_above=0 required=5 tests=AWL, BAYES_00, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, FORGED_RCVD_HELO, RCVD_IN_SORBS_DUL X-Spam-Level: X-Archive-Number: 200606/4 X-Sequence-Number: 25240 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.