Introduction to Pagination | Shreya

editor-img
Shreya
Sept 26, 2022

Introduction to Pagination

Pagination APIs are required to retrieve small chunks of data from a large table so that FE doesn't have to wait a long time to load a vast amount of data. Hence, this results in faster responses and smaller payloads.

Two of the most commonly used paginations are:

  • Offset based pagination
  • Token based pagination

For demonstration purposes, suppose we have a posts table containing id, content, and created_at.

Offset based pagination

Let's say FE requested for 20posts after the 100th post of the posts table.

Here limit: 20 and offset: 100

SELECT * FROM posts OFFSET 100 LIMIT 20;

This will not be an issue when we have a small table but when the table is enormous and has thousands of entries- latency increases with the increasing value of the offset.

Why?

Because most databases prepare offset+limit number of rows for the query which means if the offset is say 100,000, the API would be forced to scan through 100,000 entries before discarding them - leading to higher execution time.

Token based Pagination

In token-based pagination, the FE sends a token and limit as parameters for the API to look for the next chunk of data from the API.

SELECT * FROM posts WHERE created_at >= 1662535267497 LIMIT 20;

Here, we can use the created_at attribute as a token. This requires much less latency.

Why?

This is because the query starts scanning the posts only from the point when the created_at of the post matches the token and it scans only limit number of posts.