Re: LIMIT OFFSET with DB view vs plain SQL

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Raj Gandhi <raj01gandhi(at)gmail(dot)com>
Cc: admin <pgsql-admin(at)postgresql(dot)org>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: LIMIT OFFSET with DB view vs plain SQL
Date: 2019-03-29 15:28:20
Message-ID: CAHyXU0zm0JPeXYtxgdj3E-wPK6ijy+TtPUJ3x-wb-a2fKKv2qw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-performance

On Thu, Mar 28, 2019 at 5:44 PM Raj Gandhi <raj01gandhi(at)gmail(dot)com> wrote:
>
> + pgsql-performance
>
> On Thu, Mar 28, 2019 at 6:41 PM Raj Gandhi <raj01gandhi(at)gmail(dot)com> wrote:
>>
>> Hi everyone,
>>
>>
>>
>> I’m using LIMIT offset with DB view. Looks like query planner is applying the LIMIT for DB view at the end after processing all rows.
>>
>> When running same SQL that was used to create the DB view, LIMIT is applied earlier so the query is much faster.
>>
>>
>>
>> Explain plan using DB view
>>
>> https://explain.depesz.com/s/gzjQ
>>
>>
>>
>> Explain plan using raw SQL
>>
>> https://explain.depesz.com/s/KgwO
>>
>>
>>
>> In both tests LIMIT was 100 with offset = 0.
>>
>> Is there any way to force DB view to apply limit earlier?

huh. OFFSET does indeed force a materialize plan. This is a widely
used tactic to hack the planner ('OFFSET 0').

Maybe try converting your query from something like:

SELECT * FROM foo LIMIT m OFFSET N;
to
WITH data AS
(
SELECT * FROM foo LIMIT m + n
)
SELECT * FROM foo OFFSET n;

I didn't try this, and it may not help, but it's worth a shot.

merlin

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Raj Gandhi 2019-03-29 23:38:09 Re: LIMIT OFFSET with DB view vs plain SQL
Previous Message Laurenz Albe 2019-03-29 13:53:10 Re: LIMIT OFFSET with DB view vs plain SQL

Browse pgsql-performance by date

  From Date Subject
Next Message Andreas Kretschmer 2019-03-29 16:00:19 Re: endless quere when upsert with ON CONFLICT clause
Previous Message Stephan Schmidt 2019-03-29 14:29:02 endless quere when upsert with ON CONFLICT clause