Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 5F35E2E002E for ; Mon, 21 Apr 2008 20:29:57 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 79618-06 for ; Mon, 21 Apr 2008 20:29:54 -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 640A02E0044 for ; Mon, 21 Apr 2008 20:29:54 -0300 (ADT) Received: (qmail 26435 invoked by uid 503); 22 Apr 2008 01:14:54 -0000 Received: from gw2.ovh.net (HELO mail174.ha.ovh.net) (213.251.189.202) by 27.mail-out.ovh.net with SMTP; 22 Apr 2008 01:14:54 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 21 Apr 2008 23:29:52 -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; 21 Apr 2008 23:29:49 -0000 Date: Tue, 22 Apr 2008 01:34:40 +0200 To: "Francisco Reyes" Subject: Re: Group by more efficient than distinct? From: PFC Cc: "Gregory Stark" , "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: 7bit Message-ID: In-Reply-To: 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: 4327396293610309168 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.752 tagged_above=0 required=5 tests=AWL=0.752 X-Spam-Level: X-Archive-Number: 200804/307 X-Sequence-Number: 29800 On Sun, 20 Apr 2008 17:15:36 +0200, Francisco Reyes wrote: > PFC writes: > >> - If you process up to some percentage of your RAM worth of data, >> hashing is going to be a lot faster > > Thanks for the excellent breakdown and explanation. I will try and get > sizes of the tables in question and how much memory the machines have. Actually, the memory used by the hash depends on the number of distinct values, not the number of rows which are processed... Consider : SELECT a GROUP BY a SELECT a,count(*) GROUP BY a In both cases the hash only holds discinct values. So if you have 1 million rows to process but only 10 distinct values of "a", the hash will only contain those 10 values (and the counts), so it will be very small and fast, it will absorb a huge seq scan without problem. If however, you have (say) 100 million distinct values for a, using a hash would be a bad idea. As usual, divide the size of your RAM by the number of concurrent connections or something. Note that "a" could be a column, several columns, anything, the size of the hash will be proportional to the number of distinct values, ie. the number of rows returned by the query, not the number of rows processed (read) by the query. Same with hash joins etc, that's why when you join a very small table to a large one Postgres likes to use seq scan + hash join on the small table. >> - If you need DISTINCT ON, well, you're stuck with the Sort >> - So, for the time being, you can replace DISTINCT with GROUP BY... > > Have seen a few of those already on some code (new job..) so for those > it is a matter of having a good disk subsystem? Depends on your RAM, sorting in RAM is always faster than sorting on disk of course, unless you eat all the RAM and trash the other processes. Tradeoffs...