Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 41BCE2E0035 for ; Mon, 21 Apr 2008 20:50:28 -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 63029-01 for ; Mon, 21 Apr 2008 20:50:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ironport2-out.teksavvy.com (ironport2-out.pppoe.ca [206.248.154.182]) by developer.postgresql.org (Postfix) with ESMTP id 0EF3B2E002E for ; Mon, 21 Apr 2008 20:50:24 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjsFAILFDEjO+Igp/2dsb2JhbACBUalJ X-IronPort-AV: E=Sophos;i="4.25,691,1199682000"; d="scan'208";a="18846216" Received: from smtp.pppoe.ca (HELO smtp.teksavvy.com) ([65.39.196.238]) by ironport2-out.teksavvy.com with ESMTP; 21 Apr 2008 19:50:22 -0400 Received: from [206.248.136.41] ([206.248.136.41]) by smtp.teksavvy.com (Internet Mail Server v1.0) with ESMTP id BBT33022; Mon, 21 Apr 2008 19:50:22 -0400 Message-ID: <480D283E.1060602@mark.mielke.cc> Date: Mon, 21 Apr 2008 19:50:22 -0400 From: Mark Mielke User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: PFC CC: Francisco Reyes , Gregory Stark , Pgsql performance Subject: Re: Group by more efficient than distinct? References: <87ve2f7bel.fsf@oxford.xeocode.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/308 X-Sequence-Number: 29801 PFC wrote: > 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. This surprises me - hash values are lossy, so it must still need to confirm against the real list of values, which at a minimum should require references to the rows to check against? Is PostgreSQL doing something beyond my imagination? :-) Cheers, mark -- Mark Mielke