Re: query optimization differs between view and explicit - Mailing list pgsql-performance

From Stephan Szabo
Subject Re: query optimization differs between view and explicit
Date
Msg-id 20040129171201.I35951@megazone.bigpanda.com
Whole thread Raw
In response to query optimization differs between view and explicit query  (Reece Hart <reece@in-machina.com>)
Responses Re: query optimization differs between view and explicit
List pgsql-performance
On Thu, 29 Jan 2004, Reece Hart wrote:

> I have a large query which I would like to place in a view. The explicit
> query is sufficiently fast, but the same query as a view is much slower
> and uses a different plan. I would appreciate an explanation of why this
> is, and, more importantly whether/how I might coax the view to use a
> different plan.

Well, in general

select distinct on (A) A, B
 from table
 where B=10
 order by A,B;

is not always the same as

select * from
 (select distinct on (A) A, B
   from table order by A,B) foo
 where B=10;

If A is not unique, then given two rows of the
same A value one with B=10 and one with another B
value less than 10, the former is guaranteed to give
you an A,10 row, the latter will give no such row AFAICS.

If A is unique then the two queries are equivalent,
but then distinct on (A) isn't terribly meaningful.

pgsql-performance by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: query optimization differs between view and explicit
Next
From: Tom Lane
Date:
Subject: Re: query optimization differs between view and explicit