pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Robert Haas <robertmhaas@gmail.com>
To: Uwe Bartels <uwe.bartels@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: big distinct clause vs. group by
Date: Mon, 18 Apr 2011 12:19:34 -0400
Message-ID: <BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com> (raw)
In-Reply-To: <AANLkTi=SwWco0mJcU549tzMXJ0kmBwCsNHabH8OUjg1M@mail.gmail.com>
References: <AANLkTi=SwWco0mJcU549tzMXJ0kmBwCsNHabH8OUjg1M@mail.gmail.com>

On Wed, Mar 16, 2011 at 4:45 AM, Uwe Bartels <uwe.bartels@gmail.com> wrote:
> I'm having trouble with some sql statements which use an expression with
> many columns and distinct in the column list of the select.
> select distinct col1,col2,.....col20,col21
> from table1 left join table2 on <join condition>,...
> where
>  <other expressions>;
>
> The negative result is a big sort with teporary files.
>               ->  Sort  (cost=5813649.93..5853067.63 rows=15767078 width=80)
> (actual time=79027.079..81556.059 rows=12076838 loops=1)
>                     Sort Method:  external sort  Disk: 1086096kB
> By the way - for this query I have a work_mem of 1 GB - so raising this
> further is not generally possible - also not for one special command, due to
> parallelism.
>
> How do I get around this?

Hmm.  It seems to me that there's no way to work out the distinct
values without either sorting or hashing the output, which will
necessarily be slow if you have a lot of data.

> I have one idea and like to know if there any other approaches or an even
> known better solution to that problem. By using group by I don't need the
> big sort for the distinct - I reduce it (theoreticly) to the key columns.
>
> select <list of key columns>,<non key column>
> from tables1left join table2 on <join condition>,...
> where
>  <other conditions>
> group by <list of key columns>

You might try SELECT DISTINCT ON (key columns) <key columns> <non-key
columns> FROM ...

> Another question would be what's the aggregate function which needs as less
> as possible resources (time).

Not sure I follow this part.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



view thread (11+ messages)  latest in thread

Message-ID: <BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com>
Permalink:  ../BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com/
Also on:    postgresql.org/message-id/BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: robertmhaas@gmail.com, uwe.bartels@gmail.com
  Subject: Re: big distinct clause vs. group by
  In-Reply-To: <BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com>
  References: <AANLkTi=SwWco0mJcU549tzMXJ0kmBwCsNHabH8OUjg1M@mail.gmail.com> <BANLkTimuvQr3Lvyh6T6fbKDs1v34Bi5=aQ@mail.gmail.com>

* 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