Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 90EF1632D1B for ; Tue, 9 Feb 2010 18:38:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 85542-03 for ; Tue, 9 Feb 2010 22:38:19 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id BCCC665B046 for ; Tue, 9 Feb 2010 18:38:24 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o19McMC5025106; Tue, 9 Feb 2010 17:38:22 -0500 (EST) To: Dimi Paun cc: pgsql-performance Subject: Re: DISTINCT vs. GROUP BY In-reply-to: <1265751976.2513.34.camel@localhost> References: <1265751976.2513.34.camel@localhost> Comments: In-reply-to Dimi Paun message dated "Tue, 09 Feb 2010 16:46:16 -0500" Date: Tue, 09 Feb 2010 17:38:21 -0500 Message-ID: <25105.1265755101@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.465 tagged_above=-10 required=5 tests=AWL=0.134, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/144 X-Sequence-Number: 37565 Dimi Paun writes: >> From what I've read on the net, these should be very similar, > and should generate equivalent plans, in such cases: > SELECT DISTINCT x FROM mytable > SELECT x FROM mytable GROUP BY x > However, in my case (postgresql-server-8.1.18-2.el5_4.1), > they generated different results with quite different > execution times (73ms vs 40ms for DISTINCT and GROUP BY > respectively): The results certainly ought to be the same (although perhaps not with the same ordering) --- if they aren't, please provide a reproducible test case. As for efficiency, though, 8.1 didn't understand how to use hash aggregation for DISTINCT. Less-obsolete versions do know how to do that. regards, tom lane