pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron Mayer <rm_pg@cheapcomplexdevices.com>
To: pgsql-performance@postgresql.org
Subject: Why are distinct and group by choosing different plans?
Date: Thu, 02 Aug 2007 14:32:14 -0700
Message-ID: <f8tigg$4b6$1@news.hub.org> (raw)

I notice that I get different plans when I run the
following two queries that I thought would be
identical.

  select distinct test_col from mytable;
  select test_col from mytable group by test_col;

Any reason why it favors one in one case but not the other?



d=# explain analyze select distinct test_col from mytable;
                                                                         QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=0.00..14927.69 rows=27731 width=4) (actual time=0.144..915.214 rows=208701 loops=1)
   ->  Index Scan using "mytable(test_col)" on mytable  (cost=0.00..14160.38 rows=306925 width=4) (actual time=0.140..575.580 rows=306925 loops=1)
 Total runtime: 1013.657 ms
(3 rows)

d=# explain analyze select test_col from mytable group by test_col;
                                                       QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=7241.56..7518.87 rows=27731 width=4) (actual time=609.058..745.295 rows=208701 loops=1)
   ->  Seq Scan on mytable  (cost=0.00..6474.25 rows=306925 width=4) (actual time=0.063..280.000 rows=306925 loops=1)
 Total runtime: 840.321 ms
(3 rows)


d=# select version();
                                                    version
----------------------------------------------------------------------------------------------------------------
 PostgreSQL 8.2.1 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
(1 row)




view thread (3+ messages)  latest in thread

Message-ID: <f8tigg$4b6$1@news.hub.org>
Permalink:  ../f8tigg$4b6$1@news.hub.org/
Also on:    postgresql.org/message-id/f8tigg$4b6$1@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: rm_pg@cheapcomplexdevices.com
  Subject: Re: Why are distinct and group by choosing different plans?
  In-Reply-To: <f8tigg$4b6$1@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox