Re: Is there a way too speed up Limit with high OFFSET ?

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Georgi Ivanov <georgi(dot)r(dot)ivanov(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way too speed up Limit with high OFFSET ?
Date: 2010-08-12 07:49:31
Message-ID: AANLkTimBB=2ajOP=yW3tbxJeJaM-iqDSMMFL4BiAy4nH@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

2010/8/12 Georgi Ivanov <georgi(dot)r(dot)ivanov(at)gmail(dot)com>:
> Hi,
> I have query like this
> Select * from tabelname limit 10 OFFSET 10;
>
> If i increase the OFFSET to 1000 for example, the query runs slower . The
> bigger is OFFSET the slower is the query.
>

sure - database have to process all rows and skip a first n of result.
This method isn't very efective.

> This is standard pagination feature i use for my website.
> Actually the query is little bit more complex than this, but it is generally
> a
> select with a join.
>
> So i wander if there is a way to speed up this kind of query ?
>
> I'm now reading about windowing functions , but I'm not sure this is the way
> to go for this feature.
>

yes, you have to store a last primary key on page. Then if you wont to
show a next page, you have to select like

SELECT * FROM tablename WHERE id > last_primary_key_on_previous_page LIMIT 10.

When this method isn't possible, then you have to ensure the fast
processing of query without OFFSET. Maybe you have to add index,
increate a work_mem, ...

Regards

Pavel Stehule

>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sandeep Srinivasa 2010-08-12 07:53:46 Re: MySQL versus Postgres
Previous Message Guillaume Lelarge 2010-08-12 07:47:38 Re: Is there a way too speed up Limit with high OFFSET ?