Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 24BD4633FA3 for ; Tue, 9 Feb 2010 18:12:24 -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 80753-08 for ; Tue, 9 Feb 2010 22:12:08 +0000 (UTC) X-Greylist: delayed 00:25:38.007345 by SQLgrey-1.7.6 Received: from ulysses.lattica.com (lattica.com [204.14.19.114]) by mail.postgresql.org (Postfix) with ESMTP id 672C2634A91 for ; Tue, 9 Feb 2010 18:12:13 -0400 (AST) Received: from localhost (ulysses.lattica.com [127.0.0.1]) by ulysses.lattica.com (Postfix) with ESMTP id CFEB8EA4002 for ; Tue, 9 Feb 2010 16:46:32 -0500 (EST) X-Virus-Scanned: amavisd-new at lattica.com Received: from ulysses.lattica.com ([127.0.0.1]) by localhost (ulysses.lattica.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fcq0N3fEiSBd for ; Tue, 9 Feb 2010 16:46:17 -0500 (EST) Received: from [192.168.3.11] (CPE00222d5a2ad0-CM00222d5a2acd.cpe.net.cable.rogers.com [99.230.157.51]) by ulysses.lattica.com (Postfix) with ESMTP id 35FCEEA4001 for ; Tue, 9 Feb 2010 16:46:17 -0500 (EST) Subject: DISTINCT vs. GROUP BY From: Dimi Paun To: pgsql-performance Content-Type: text/plain; charset="UTF-8" Organization: Lattica, Inc. Date: Tue, 09 Feb 2010 16:46:16 -0500 Message-ID: <1265751976.2513.34.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.28.2 (2.28.2-1.fc12) Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.11 tagged_above=-10 required=5 tests=BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 201002/142 X-Sequence-Number: 37563 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): tts_server_db=# EXPLAIN ANALYZE select userdata from tagrecord where clientRmaInId = 'CPC-RMA-00110' group by userdata; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=775.68..775.69 rows=1 width=146) (actual time=40.058..40.058 rows=0 loops=1) -> Bitmap Heap Scan on tagrecord (cost=4.00..774.96 rows=286 width=146) (actual time=40.055..40.055 rows=0 loops=1) Recheck Cond: ((clientrmainid)::text = 'CPC-RMA-00110'::text) -> Bitmap Index Scan on idx_tagdata_clientrmainid (cost=0.00..4.00 rows=286 width=0) (actual time=40.050..40.050 rows=0 loops=1) Index Cond: ((clientrmainid)::text = 'CPC-RMA-00110'::text) Total runtime: 40.121 ms tts_server_db=# EXPLAIN ANALYZE select distinct userdata from tagrecord where clientRmaInId = 'CPC-RMA-00109'; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- Unique (cost=786.63..788.06 rows=1 width=146) (actual time=73.018..73.018 rows=0 loops=1) -> Sort (cost=786.63..787.34 rows=286 width=146) (actual time=73.016..73.016 rows=0 loops=1) Sort Key: userdata -> Bitmap Heap Scan on tagrecord (cost=4.00..774.96 rows=286 width=146) (actual time=72.940..72.940 rows=0 loops=1) Recheck Cond: ((clientrmainid)::text = 'CPC-RMA-00109'::text) -> Bitmap Index Scan on idx_tagdata_clientrmainid (cost=0.00..4.00 rows=286 width=0) (actual time=72.936..72.936 rows=0 loops=1) Index Cond: ((clientrmainid)::text = 'CPC-RMA-00109'::text) Total runtime: 73.144 ms What gives? -- Dimi Paun Lattica, Inc.