Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id A23A8475B4A for ; Wed, 27 Feb 2002 16:45:17 -0500 (EST) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.5.2) with HTTP id 812107; Wed, 27 Feb 2002 13:45:16 -0800 From: "Josh Berkus" Subject: Re: How slow is DISTINCT? To: wweng@kencast.com, pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.5.2 Date: Wed, 27 Feb 2002 13:45:16 -0800 Message-ID: In-Reply-To: <3C7D252A.3030608@kencast.com> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200202/441 X-Sequence-Number: 6862 Wei Wang, > How exactly slow is DISTINCT being processed in SQL engines? (not > limited to postgresql, though comments on postgresql would be most > relevant) I can only give you a relative result, based exlusively on my anecdotal experience with 7.1: Fast: SELECT ... Slower: SELECT ... GROUP BY x,y,z or: SELECT DISCTINCT ON (x) ... (Postgres non-standard extension) SLowest: SELECT DISTINCT ... The reason for this is that SELECT DISTINCT is effectively a GROUP BY on all result fields of the query, and if a few of the aren't indexed that requires a seq scan. If performance is an issue, you may wish to consider restructuring your queries and/or data model to eliminate the actual duplicate rows. -Josh