Re: Performance problems with DISTINCT ON

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Sgarbossa Domenico" <domenico(dot)sgarbossa(at)eniac(dot)it>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Performance problems with DISTINCT ON
Date: 2009-10-04 02:53:04
Message-ID: 29240.1254624784@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

"Sgarbossa Domenico" <domenico(dot)sgarbossa(at)eniac(dot)it> writes:
> I guess the right query is:

> select distinct on (articolo) articolo,data_ent,prezzo from listini_anagrafici order by articolo, data_ent desc

> but it seems that this query runs slowly... about 5/6 seconds.

> I've tried adding this index
> CREATE INDEX articolo_data_ent ON listini_anagrafici (articoli, data_ent)
> but it doesn't helps.

That index doesn't match the query ordering. You could do

select distinct on (articolo) articolo,data_ent,prezzo from listini_anagrafici order by articolo desc, data_ent desc

In more recent versions of Postgres you could make an index with one
column ascending and the other descending, but AFAIR 8.1 doesn't have
that.

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Marlowe 2009-10-04 05:31:11 Re: dump time increase by 1h with new kernel
Previous Message Karl Denninger 2009-10-04 02:35:26 Re: Best suiting OS