Thread: how to handle Pagination >

how to handle Pagination >

From
Madhavan
Date:
Hi,
I want to fetch records from the database and paginate in php script.
How this can be handled?

With Best Regards
Madhavan




Re: how to handle Pagination >

From
jesusthefrog
Date:
You will probably want to use OFFSET and LIMIT, which are the window size and page. Something like

SELECT .. FROM ... WHERE ... ORDER BY (...) OFFSET y LIMIT x

So say you want to get 100 records at a time. You would get page 1 with OFFSET 0 LIMIT 100, then page 2 with OFFSET 100, OFFSET 200, and so on.

That's just off the top of my head. There may be another solution which will work better in your case, but lacking details, this is what I would recommend.

--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT d+ s: a-- C++++(++) UL+++ P++++$ L+++>++++ E- W+ N o? K w--- O-
M- V? PS+++ PE(-) Y+ PGP->+++ t+>++ 5+++++ X(+) R>+ tv b+
DI++ D++>+++ G+ e* h! !r y**
------END GEEK CODE BLOCK------

Re: how to handle Pagination >

From
Vik Fearing
Date:
On 10/15/2013 05:01 PM, jesusthefrog wrote:
> You will probably want to use OFFSET and LIMIT, which are the window
> size and page. Something like
>
> SELECT .. FROM ... WHERE ... ORDER BY (...) OFFSET y LIMIT x
>
> So say you want to get 100 records at a time. You would get page 1
> with OFFSET 0 LIMIT 100, then page 2 with OFFSET 100, OFFSET 200, and
> so on.

You will most certainly NOT want to use that technique.

Instead, try these links:

https://wiki.postgresql.org/wiki/File:Pagination_Done_the_PostgreSQL_Way.pdf

http://microjet.ath.cx/WebWiki/ResultPaginationWithPostgresql.html

https://coderwall.com/p/lkcaag

http://www.depesz.com/2007/08/29/better-results-paging-in-postgresql-82/
--
Vik


Re: how to handle Pagination >

From
Kevin Hunter Kesling
Date:
At 10:52am -0400 Tue, 15 Oct 2013, Madhavan wrote:
>> I want to fetch records from the database and paginate in php
>> script. How this can be handled?

At 11:01am -0400 Tue, 15 Oct 2013, Jesusthefrog wrote:
> You will probably want to use OFFSET and LIMIT, which are the window
> size and page. Something like
>
> SELECT .. FROM ... WHERE ... ORDER BY (...) OFFSET y LIMIT x
>
> So say you want to get 100 records at a time. You would get page 1
> with OFFSET 0 LIMIT 100, then page 2 with OFFSET 100, OFFSET 200, and
> so on.
>
> That's just off the top of my head. There may be another solution
> which will work better in your case, but lacking details, this is
> what I would recommend.

The common wisdom (of which I'm aware, anyway) is that the limits of
this approach are in the atomicity of the 2+ pages at which a user
looks.  For instance, if the user looks at page 1 (say, records 1-100),
then the SQL would look something like:

      SELECT ... ORDER BY ... OFFSET 0 LIMIT 100;

Now, while the user (user A) spends time reading the results, another
user or process updates the table, say by removing records 40-49 (10
total records removed).  Now, user A loads the next page of results,
which begins at offset 100:

      SELECT ... ORDER BY ... OFFSET 100 LIMIT 100;

The problem is that this is a new query, so offset 100 refers to what
/was/ record 110, instead of what the user probably wanted, which is now
OFFSET 90.

See this for a better explanation:

      https://coderwall.com/p/lkcaag

Then note that the Postgres community has "solved" this problem:


https://wiki.postgresql.org/wiki/File:Pagination_Done_the_PostgreSQL_Way.pdf

Cheers,

Kevin


Re: how to handle Pagination >

From
Kevin Hunter Kesling
Date:
At 12:54pm -0400 Tue, 15 Oct 2013, Vik Fearing wrote:
> On 10/15/2013 05:01 PM, jesusthefrog wrote:
>> You will probably want to use OFFSET and LIMIT, which are the
>> window size and page. Something like
>>
>> SELECT .. FROM ... WHERE ... ORDER BY (...) OFFSET y LIMIT x
>>
>> So say you want to get 100 records at a time. You would get page 1
>> with OFFSET 0 LIMIT 100, then page 2 with OFFSET 100, OFFSET 200,
>> and so on.
>
> You will most certainly NOT want to use that technique.
>
> Instead, try these links:

Doh!  My previous email was late!  Vik wins this round!  :-)

Kevin