From: | Merlin Moncure <mmoncure(at)gmail(dot)com> |
---|---|
To: | Craig Ringer <ringerc(at)ringerc(dot)id(dot)au> |
Cc: | David Johnston <polobo(at)yahoo(dot)com>, Robert James <srobertjames(at)gmail(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Controlling complexity in queries |
Date: | 2011-12-12 15:43:27 |
Message-ID: | CAHyXU0ybNOhruWCJXcvYE-MtJtW1fy47S75VM_aCTAg7hUf58A@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Sun, Dec 11, 2011 at 9:10 PM, Craig Ringer <ringerc(at)ringerc(dot)id(dot)au> wrote:
> On 12/12/2011 09:15 AM, David Johnston wrote:
>>
>> Use a WITH clause on the SELECT statement.
>
> Note that WITH is an optimisation fence, so if you're relying on Pg pushing
> WHERE clauses down into subqueries or anything like that you may find that
> your query runs a LOT slower when broken up as WITH expressions.
>
> There's been talk of a Pg extension that allows optimisation through WITH,
> but it's not currently possible.
>
> Another option is to wrap things up in SQL functions or views.
A note about that: abstracting via views vs functions is a completely
different approach. Views will not significantly change the way your
query works now -- they are inlined as macros and the final query is
going to be more or less the same as your hand rolled one.
Breaking your large queries into functions OTOH can make significant
changes to the plan, often to the worse. This is because functions,
especially complicated plpgsql set returning ones with procedural
logic, are black boxes to the sql optimizer. The upshot of this is
that functions tend to encourage nestloop style plans because the
function has to be serially executed.
Functions (also WITH) are great in that they can provide very high
levels of abstraction when composing complex queries, but there is a
price in the sense that you are taking away some of the database's
ability to plan and optimize the query. I prefer views unless there
is a good reason not to use them.
In the end, the performance of your queries is going to be directly
related to how well you map the problem into relational logic...the
database thinks relationally, so you (the OP) should learn to do so as
well.
merlin
From | Date | Subject | |
---|---|---|---|
Next Message | Adrian Klaver | 2011-12-12 15:49:05 | Re: initdb locale WIN1252 |
Previous Message | devrim | 2011-12-12 15:40:15 | Re: Problem installing PG9.1 using yum |