Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id EA4D32E0044 for ; Fri, 18 Apr 2008 07:30:38 -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 55643-10 for ; Fri, 18 Apr 2008 07:30:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from 27.mail-out.ovh.net (27.mail-out.ovh.net [91.121.30.210]) by developer.postgresql.org (Postfix) with SMTP id E46232E003D for ; Fri, 18 Apr 2008 07:30:30 -0300 (ADT) Received: (qmail 20870 invoked by uid 503); 18 Apr 2008 12:13:10 -0000 Received: from gw2.ovh.net (HELO mail174.ha.ovh.net) (213.251.189.202) by 27.mail-out.ovh.net with SMTP; 18 Apr 2008 12:13:10 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 18 Apr 2008 10:30:28 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO apollo13.peufeu.com) (88.161.102.87) by ns0.ovh.net with SMTP; 18 Apr 2008 10:30:26 -0000 Date: Fri, 18 Apr 2008 12:35:04 +0200 To: "Gregory Stark" , "Francisco Reyes" Subject: Re: Group by more efficient than distinct? From: PFC Cc: "Pgsql performance" Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <87ve2f7bel.fsf@oxford.xeocode.com> Content-Transfer-Encoding: Quoted-Printable Message-ID: In-Reply-To: <87ve2f7bel.fsf@oxford.xeocode.com> User-Agent: Opera Mail/9.24 (Linux) X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N X-Ovh-Tracer-Id: 10400500389946525232 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.754 tagged_above=0 required=5 tests=AWL=0.754 X-Spam-Level: X-Archive-Number: 200804/266 X-Sequence-Number: 29759 On Fri, 18 Apr 2008 11:36:02 +0200, Gregory Stark = wrote: > "Francisco Reyes" writes: > >> Is there any dissadvantage of using "group by" to obtain a unique lis= t? >> >> On a small dataset the difference was about 20% percent. >> >> Group by >> HashAggregate (cost=3D369.61..381.12 rows=3D1151 width=3D8) (actual >> time=3D76.641..85.167 rows=3D2890 loops=3D1) Basically : - If you process up to some percentage of your RAM worth of data, hashi= ng = is going to be a lot faster - If the size of the hash grows larger than your RAM, hashing will fail= = miserably and sorting will be much faster since PG's disksort is really = = good - GROUP BY knows this and acts accordingly - DISTINCT doesn't know this, it only knows sorting, so it sorts - If you need DISTINCT x ORDER BY x, sorting may be faster too (dependi= ng = on the % of distinct rows) - If you need DISTINCT ON, well, you're stuck with the Sort - So, for the time being, you can replace DISTINCT with GROUP BY...