Re: Order by clause in 'select distinct'?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: James Kelty <jamesk(at)ashlandagency(dot)com>
Cc: <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Order by clause in 'select distinct'?
Date: 2002-03-14 01:18:32
Message-ID: 20020313171324.N90759-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Wed, 13 Mar 2002, James Kelty wrote:

> When I create a view by a query like the follow query:
>
> SELECT DISTINCT ON (o.ordergenkey) o.ordergenkey, lip.payment FROM Ordr o,
> LineItemPayment lip
> WHERE o.ordergenkey = lip.orderfkey
> and o.ordergenkey = ?
>
> The view is created with an ORDER BY clause on the o.ordergenkey...
>
> I think it might be an enforce rule for createing a view with a DISTINCT ON
> phrase.
>
> I would like to, if possible, get rid of the ORDER BY clause as it slow the
> query to
> a crawl.
>
> Any ideas?

There's not actually an order by, but IIRC distinct on is done with
unique-sort-... plan so the values get sorted before passed to the routine
that does the actual distincting. I don't think there's a way to get rid
of the sorting step. Perhaps raising sort_mem will make the query run
better.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Gill, Jeffrey L 2002-03-14 01:23:30 Optimization questions
Previous Message Adam Wyard 2002-03-13 23:49:00 Recover v6.5 database