Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 5837E475FE8 for ; Wed, 13 Mar 2002 20:18:29 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id B0FB8D5F7; Wed, 13 Mar 2002 17:18:32 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id A674D5BFD; Wed, 13 Mar 2002 17:18:32 -0800 (PST) Date: Wed, 13 Mar 2002 17:18:32 -0800 (PST) From: Stephan Szabo To: James Kelty Cc: Subject: Re: Order by clause in 'select distinct'? In-Reply-To: Message-ID: <20020313171324.N90759-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200203/199 X-Sequence-Number: 3733 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.