Exploring Pagination Techniques in Databases: Offset, Keyset, and Cursor Pagination

Exploring Pagination Techniques in Databases: Offset, Keyset, and Cursor Pagination

When working with large datasets, effective pagination is essential for a smooth user experience. The choice of pagination approach can significantly impact performance, especially in apps handling lots of real-time data. In this article, we’ll look at three common pagination techniques—Offset Pagination, Keyset Pagination, and Cursor Pagination—and compare them in terms of use cases, pros and cons, and support for the often-requested "Jump to Page" feature.


1. Offset Pagination

Offset pagination is a straightforward approach used in many applications. This method uses a starting point (or “offset”) and a page size to retrieve rows.

How It Works

Imagine a book with numbered pages; to go directly to page 3, you start counting from page 1 and skip the pages before it. Offset pagination works similarly: if we want to retrieve page 3 with a page size of 10, the offset would be 20, which skips the first 20 rows and retrieves the next 10.


API request:

GET /api/orders?offset=0&limit=10        

Example Query

SELECT *
FROM orders
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;        

When to Use It

  • Small or Medium Datasets: Works well when data volumes are moderate, so you’re not dealing with hundreds of pages.
  • Static Data: It’s a good choice when the data doesn’t change often, such as in a catalog or user list.

Pros

  • Easy to Implement: Offset pagination is intuitive and widely supported by database systems.
  • Direct Page Access: Users can skip directly to any page, making it ideal for “Jump to Page” features.

Cons

  • Performance Issues: The larger the offset, the longer it takes for the database to skip rows.
  • Inconsistent Results: Changes in data between requests can cause missing or duplicated rows.

Support for “Jump to Page”

Fully Supported. Offset pagination’s design makes it simple to jump to any page since you can calculate the offset directly. If your application needs "Jump to Page" navigation, this is an ideal solution.


2. Keyset Pagination

Keyset pagination, or “value-based pagination,” is a method often used for large datasets. Instead of relying on offsets, keyset pagination retrieves records based on a specific key, like a timestamp, which acts as the starting point for each page.

How It Works

With keyset pagination, each page is defined by the value of the key in the last row of the current page. For instance, if you're sorting by created_at, you retrieve rows with timestamps older than the last timestamp on the current page.

API request:

GET /api/orders?last_key=1729958600         

Example Query

SELECT *
FROM orders
WHERE created_at < last_page_last_row_created_at
ORDER BY created_at DESC
LIMIT 10;        

When to Use It

  • Large, Dynamic Datasets: Works well when data is constantly being added, like social feeds or real-time logs.
  • Chronological Feeds: Great for time-ordered data where each page naturally follows the previous.

Pros

  • High Performance: Keyset pagination avoids row-counting, making it faster for large datasets.
  • Stable Results: Since each page is based on a key rather than an offset, the data remains consistent even as new records are added.

Cons

Support for “Jump to Page”

Not Supported. Because keyset pagination depends on the value of the last row, it doesn’t support jumping to arbitrary pages. You’d need to sequentially retrieve each page until you reach the one you want.


3. Cursor (keyset) Pagination

Cursor pagination is similar to keyset pagination but with a unique twist: instead of a key, it uses a “cursor” to point to the current page. Each page gives a cursor reference for the next one.

How It Works

In cursor pagination, a unique cursor points to the last record on each page. When the user requests the next page, the app uses that cursor to fetch the following set of records.

API request:

GET /api/orders?cursor=eyJpZCI6MX0        

Example Query

SELECT *
FROM orders
WHERE id > last_cursor_id
ORDER BY id ASC
LIMIT 10;        

When to Use It

  • Real-Time Apps: Works well for applications where data changes frequently, such as social media feeds or transaction records.
  • Infinite Scroll: Ideal for infinite scroll interfaces where each page loads automatically as the user scrolls.

Pros

  • Efficiency: Cursor pagination is extremely fast, as it doesn’t require counting or skipping rows.
  • Consistency: Provides stable, predictable results with no duplicates or missing rows, even in high-churn data.

Cons

  • More Complex: Cursor pagination requires managing encoded cursors, which can be more challenging to implement.
  • No Random Access: It’s difficult to jump directly to a page, so it doesn’t support a “Jump to Page” feature.

Support for “Jump to Page”

Not Supported. Cursor pagination doesn’t allow jumping to a specific page since each page depends on the cursor from the previous page.


4. Page-Based Pagination

Page-based pagination involves using a "page" and "page_size" parameter to specify the desired page number.

How It Works

In page-based pagination, the dataset is divided into smaller, manageable chunks or "pages." Each page displays a fixed number of items, allowing users to navigate through large datasets efficiently.

  1. Total Items: The total number of records in the dataset is calculated.
  2. Items per Page: A predefined number of items is set for display on each page.
  3. Current Page: The user specifies which page they want to view.
  4. Calculating Page Range: The start and end indices for the items to display on the current page are calculated based on the current page number and the number of items per page.
  5. Fetching Data: The application retrieves only the items within the calculated range, reducing the amount of data loaded at once and improving performance.
  6. Navigation Controls: Users can navigate between pages using "Next" and "Previous" buttons, which allow them to move through the dataset easily.

This method enhances user experience by making it easy to find and access specific data without overwhelming the user with too much information at once.

API Request

GET /api/orders?page=2&page_size=20        

Example Query

SELECT *
FROM orders
ORDER BY id ASC
LIMIT page_size OFFSET (page - 1) * page_size;        

In this API request, we are requesting the second page, where each page contains 20 posts.


3. Windowed Pagination

Windowed pagination, also known as range-based pagination, allows for loading a specific window of records rather than fetching the entire dataset at once. This technique is especially useful for large datasets where only a subset of records is needed for display.

How It Works

In windowed pagination, the dataset is divided into "windows," each containing a set range of items. For instance, if a user wants to view a subset of records, the application can fetch a defined range of rows from the dataset, providing a better balance between performance and user experience.

API Request:

GET /api/orders?start=21&end=40        

Example Query:

SELECT *
FROM orders
WHERE id BETWEEN start_id AND end_id
ORDER BY id ASC;        

When to Use It

  • Large Datasets: Ideal for handling extensive datasets without overloading the user interface or application.
  • Dynamic Queries: Suitable when the dataset is frequently updated, and you want to limit the amount of data retrieved in a single query.

Pros

  • Performance Efficiency: Reduces the amount of data loaded into memory, making it quicker to fetch and display relevant records.
  • Less Data Transfer: By limiting the number of records retrieved, it minimizes the data sent over the network.

Cons

  • Complex Implementation: Requires careful handling of the start and end identifiers, which may complicate query logic.
  • Limited Navigation: Users may need to navigate through the pages sequentially, as random access to pages may not be straightforward.

Support for “Jump to Page” Partially Supported. While windowed pagination can allow for some form of jumping between records based on defined ranges, it typically lacks the straightforward access offered by offset pagination. Users may find it less intuitive to jump to arbitrary pages without additional logic to compute the appropriate range.


To view or add a comment, sign in

More articles by Nurmyrat Atamedov

Others also viewed

Explore content categories