Re: subselects vs WITH in views

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Seref Arikan <serefarikan(at)gmail(dot)com>
Cc: Joe Van Dyk <joe(at)tanga(dot)com>, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: subselects vs WITH in views
Date: 2013-02-21 15:10:47
Message-ID: CAHyXU0wS286_qUV+_42OeQb_e6ZXva72e1kY3jcTeZ0FRE9hiA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Feb 21, 2013 at 4:31 AM, Seref Arikan <serefarikan(at)gmail(dot)com> wrote:
> Hi Merlin,
> So should I interpret this as: there is a potential gain from choosing
> subqueries over with WITHs ?

Well, potentially, yes. WITH is a mechanic to force iterative order
of evaluation on queries. This can be a good or bad thing naturally.
Subqueries can also do this, especially if you put them in the field
select list -- but WITH is more general. We also have an undocumented
hack that uses OFFSET 0 to force subquery evaluation. These are all
very dangerous tools because they tend to be very sensitive to data
inputs as you are bypassing database statistics effectively. The
other end of the spectrum is to use vanilla JOINs as much as possible
-- this releases the work of planning the query to the database.

Upcoming 9.3 LATERAL will remove one large class of cases where we
have to do this: joining against set returning functions with
non-constant inputs.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-02-21 15:57:45 Re: Trigram (pg_trgm) GIN index not used
Previous Message Merlin Moncure 2013-02-21 14:23:34 Re: Trigram (pg_trgm) GIN index not used