Re: Limit & offset effect on query plans

Lists: pgsql-performance
From: "Kevin Grittner" <kgrittn(at)mail(dot)com>
To: "Pavan Deolasee" <pavan(dot)deolasee(at)gmail(dot)com>,"Amitabh Kant" <amitabhkant(at)gmail(dot)com>
Cc: "pgsql-performance" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Limit & offset effect on query plans
Date: 2012-12-13 15:47:14
Message-ID: 20121213154714.80050@gmx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Pavan Deolasee wrote:
> Amitabh Kant <amitabhkant(at)gmail(dot)com> wrote:

>> Our scripts automatically add "LIMIT ALL" & "OFFSET 0" to every
>> select query if no values are passed on for these parameters. I
>> remember reading through the mailing list that it's better not
>> to pass them if they are not needed as they add a cost to the
>> query plan. Is this the case, or am i looking at a very minor
>> optimization.
>>
>
> I would tend to think that is the latter. While undoubtedly
> limit/offset clause will add another node during query planning
> and execution, AFAICS the OFFSET 0 and LIMIT ALL cases are
> optimized to a good extent. So the overhead of having them will
> not be significant.

I ran some quick tests on my i7 under Linux. Plan time was
increased by about 40 microseconds (based on EXPLAIN runtime) and
added a limit node to the plan. Execution time on a SELECT * FROM
tenk1 in the regression database went up by 1.35 ms on fully cached
runs.

-Kevin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Kevin Grittner" <kgrittn(at)mail(dot)com>
Cc: "Pavan Deolasee" <pavan(dot)deolasee(at)gmail(dot)com>, "Amitabh Kant" <amitabhkant(at)gmail(dot)com>, "pgsql-performance" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Limit & offset effect on query plans
Date: 2012-12-13 16:20:48
Message-ID: 6012.1355415648@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

"Kevin Grittner" <kgrittn(at)mail(dot)com> writes:
> Pavan Deolasee wrote:
>> I would tend to think that is the latter. While undoubtedly
>> limit/offset clause will add another node during query planning
>> and execution, AFAICS the OFFSET 0 and LIMIT ALL cases are
>> optimized to a good extent. So the overhead of having them will
>> not be significant.

> I ran some quick tests on my i7 under Linux. Plan time was
> increased by about 40 microseconds (based on EXPLAIN runtime) and
> added a limit node to the plan. Execution time on a SELECT * FROM
> tenk1 in the regression database went up by 1.35 ms on fully cached
> runs.

1.35ms out of what?

FWIW, I've been considering teaching the planner to not bother with
an actual Limit plan node if the limit clause is an obvious no-op.
I wasn't thinking about applications that blindly insert such clauses,
but rather about not penalizing subqueries when someone uses one of
these as an optimization fence. (The clauses would still work as an
opt fence, you'd just not see any Limit node in the final plan.)

regards, tom lane