Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 43AC82E007A for ; Fri, 18 Apr 2008 06:36:27 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 55370-07 for ; Fri, 18 Apr 2008 06:36:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from oxford.xeocode.com (unknown [62.232.55.118]) by developer.postgresql.org (Postfix) with ESMTP id 8EDEB2E0066 for ; Fri, 18 Apr 2008 06:36:22 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.69) (envelope-from ) id 1Jmn0k-0001bY-Lq; Fri, 18 Apr 2008 10:36:02 +0100 To: "Francisco Reyes" Cc: "Pgsql performance" Subject: Re: Group by more efficient than distinct? In-Reply-To: (Francisco Reyes's message of "Thu\, 17 Apr 2008 23\:46\:08 -0400") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 5253) References: From: Gregory Stark Organization: EnterpriseDB Date: Fri, 18 Apr 2008 10:36:02 +0100 Message-ID: <87ve2f7bel.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/265 X-Sequence-Number: 29758 "Francisco Reyes" writes: > Is there any dissadvantage of using "group by" to obtain a unique list? > > On a small dataset the difference was about 20% percent. > > Group by > HashAggregate (cost=369.61..381.12 rows=1151 width=8) (actual > time=76.641..85.167 rows=2890 loops=1) HashAggregate needs to store more values in memory at the same time so it's not a good plan if you have a lot of distinct values. But the planner knows that and so as long as your work_mem is set to a reasonable size (keeping in mind each sort or other operation feels free to use that much itself even if there are several in the query) and the rows estimate is reasonable accurate -- here it's mediocre but not dangerously bad -- then if the planner is picking it it's probably a good idea. I'm not sure but I think there are cases where the DISTINCT method wins too. This is basically a bug, in an ideal world both queries would generate precisely the same plans since they're equivalent. It's just not a high priority since we have so many more interesting improvements competing for time. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Ask me about EnterpriseDB's PostGIS support!