top of page
Logo of top retool developers and retool agency

Using Server-Side Pagination in Queries and Table Building

Are you drowning in data? As a product or engineering director, you know the struggle of managing large datasets efficiently. It's like trying to head into a tidal wave of information – chaotic and unmanageable. 


But what if you could control that flow, taking just the right amount of data you need?

Server-side pagination provides an effective way to handle extensive data, ensuring that applications perform optimally by limiting the amount of data transferred in each query. Retool pagination helps product and engineering teams build systems that streamline data handling and minimize performance bottlenecks.


This blog will cover how to implement server-side pagination within Retool for SQL and API requests. It will also discuss best practices, examples, and how to handle total row counts effectively using Retool’s Table component.


Let’s get started!


Importance of Server-Side Pagination



When managing large datasets, simply fetching all available data at once is inefficient and can lead to slow response times and high server resource usage. Server-side pagination offers a solution by returning data in manageable chunks, improving load times, and ensuring that only relevant data is transferred at any given time.


Here's why it matters:

  • Reduced load times: Your apps respond faster, keeping users happy.

  • Lower resource usage: Your servers breathe easier, handling requests more efficiently.

  • Improved user experience: No more endless scrolling or freezing interfaces.


By implementing server-side pagination, you're not just optimizing queries – you're significantly improving how your team interacts with data. You're paving the way for smoother operations and happier end-users.


This method is crucial for applications that involve frequent queries and large datasets, such as customer management systems, product databases, and real-time data applications. 


Now, let’s explore the different types of server-side pagination and how each can be implemented.


Implementing Server-Side Pagination in Queries


When implementing pagination, choosing the right method depends on the nature of your data and your system requirements. Each pagination type has distinct features, strengths, and limitations. Let's detail the three common types of pagination: Limit-offset, Cursor-based, and GraphQL Relay cursor. Understanding their features, pros, cons, and best use cases will help you select the right strategy for your specific needs.


1. Limit-Offset Pagination

Features:

  • In limit-offset pagination, the LIMIT clause specifies the maximum number of records to return, while the OFFSET clause tells the database to skip a specified number of records before starting to return data.

  • This is one of the simplest and most widely used pagination methods in SQL databases and APIs.

Pros:

  • Easy to implement: Many databases, including MySQL and PostgreSQL, have built-in support for limit-offset pagination, making it simple to implement.

  • Good for static data: Works well when data is relatively static or doesn't change frequently.

Cons:

  • Performance issues: As the offset increases, performance degrades significantly because the database has to scan through all the skipped rows.

  • Inconsistent results: In datasets that frequently change (e.g., rows being inserted or deleted), you may retrieve inconsistent or duplicate results between requests, as data can shift between queries.

Who Should Implement It:

  • Limit-offset pagination is ideal for static datasets or applications with smaller datasets where performance degradation is less of a concern. For example, simple admin dashboards or static blogs would benefit from this approach.

Example:

SELECT * FROM users

LIMIT 10 OFFSET 20;

This query retrieves 10 users, starting from the 21st user.

While limit-offset pagination is a simple solution, it might not be ideal for large or frequently updated datasets. In such cases, cursor-based pagination provides a more efficient alternative.


2. Cursor-Based Pagination

Features:

  • Cursor-based pagination, also known as keyset pagination, uses a "cursor" to keep track of the position in the data. Instead of using page numbers, it retrieves results starting after the last returned item, making it more efficient for large and dynamic datasets.

  • Typically, a unique field like a timestamp or an ID is used as the cursor.

Pros:

  • High performance: This method is much faster than limit-offset pagination for large datasets because it doesn’t require scanning through unnecessary rows.

  • Consistent results: Cursor-based pagination is less prone to missing or duplicating results in datasets that frequently change, making it ideal for dynamic data.

Cons:

  • Complexity: Cursor-based pagination is more complex to implement because it requires tracking the cursor or unique identifier between requests.

  • No jumping to specific pages: Unlike limit-offset pagination, this method doesn't allow easy navigation to a specific page number. You must start from the beginning and follow the cursor sequentially.

Who Should Implement It:

  • This method is best for large and frequently changing datasets, such as social media feeds, e-commerce product catalogs, or chat systems where the order and consistency of data are crucial.

Example:

GET /api/users?limit=10&cursor=5f2a3c4b2b3d

This request fetches 10 users starting after the cursor "5f2a3c4b2b3d," ensuring you don’t retrieve the same data twice.

Cursor-based pagination offers an efficient solution for dynamic data. However, for applications using GraphQL, GraphQL Relay cursor pagination provides a more standardized and optimized method.


3. GraphQL Relay Cursor Pagination

Features:

  • GraphQL Relay cursor-based pagination is an advanced method that implements the Relay specification for pagination in GraphQL APIs. It uses arguments such as first, last, after, and before to manage pagination and retrieve data.

  • It involves a pageInfo field that informs whether there are more pages (hasNextPage) or if it is the last page (hasPreviousPage).

Pros:

  • Standardized: The Relay specification provides a consistent, structured approach to pagination in GraphQL APIs.

  • Efficient: Relay’s cursor pagination is highly optimized for GraphQL, making it efficient for large-scale applications that require frequent data queries.

Cons:

  • Complexity: Implementing GraphQL Relay pagination requires additional setup and configuration, which can be more challenging for teams unfamiliar with GraphQL.

  • Limited backward navigation: Relay pagination excels in forward pagination but can have limitations when trying to paginate backward through large datasets.

Who Should Implement It:

  • This method is highly recommended for applications built with GraphQL, especially those with large and frequently changing datasets, such as real-time applications, content management systems, and large e-commerce platforms.

Example:

query {

  users(first: 10, after: "cursorValue") {

    edges {

      node {

        id

        name

      }

    }

    pageInfo {

      hasNextPage

      hasPreviousPage

    }

  }

}

In this example, the query retrieves 10 users starting after the given cursor, with pageInfo indicating if there are more pages.


By choosing the appropriate pagination strategy, you can significantly improve the efficiency and scalability of your applications, ensuring a better user experience and optimized data retrieval.


Once the appropriate pagination type is chosen, understanding how to implement it in specific systems such as SQL databases or API requests becomes critical.


Pagination in SQL Queries



In SQL queries, pagination allows for efficient data retrieval by limiting the number of returned records. Using the LIMIT and OFFSET clauses, developers can segment large datasets into smaller, more manageable chunks.

Best Practices:

  • Always include an ORDER BY clause to ensure consistent results across pages.

  • Consider database indexes to improve query performance, especially when using high offsets.

Example:

SELECT * FROM users

LIMIT 10 OFFSET 20;

This query retrieves 10 records starting from the 21st user, improving data retrieval efficiency.

By mastering SQL pagination, you're giving your team the power to retrieve exactly the data they need, when they need it. No more wading through irrelevant information or waiting for massive queries to complete.


Toolpioneers can help implement these solutions quickly and effectively, ensuring your team stays focused on what matters the most!



Along with SQL, APIs also benefit from pagination. Implementing pagination in API requests ensures efficient data transfer and minimal resource consumption.


Pagination in API Requests


APIs often handle large data exchanges, and pagination helps control the volume of data transferred in each request. There are two common approaches to API pagination: limit-offset and cursor-based pagination.

Limit-offset Pagination in APIs:

  • Use parameters like limit and page to define the number of items per request and the page number.

Example:

GET /api/users?limit=10&page=2

This request retrieves the second page of users with 10 users per page.

Cursor-Based Pagination in APIs:

  • Cursor-based pagination uses a cursor to specify the starting point for the next set of results.

Example:

GET /api/users?limit=10&after={{cursor}}

By implementing effective API pagination, you're ensuring your systems can handle high-volume data requests more efficiently. Your team can build more responsive, scalable applications that can grow with your business needs.


Accurate data handling also requires managing row counts efficiently. Handling total row counts ensures that developers have a comprehensive view of the dataset being paginated.


Handling Total Row Counts


Total row counts give developers insight into the full scope of the dataset. This can be retrieved either on the client side or server side, but server-side row counts offer better accuracy and performance.

Best Practices:

  • Ensure that the row count query uses the same filters as the main query to maintain consistency.

  • Consider using indexes to optimize the performance of count queries.

Example:


SELECT COUNT(*) FROM users WHERE status = 'active';

This query retrieves the total number of active users, providing insight into the data volume and the number of pages required.


With row counts in hand, Retool’s Table component can be leveraged for efficient server-side pagination.


Server-Side Pagination with the Table Component


Retool’s Table component is a versatile tool for displaying paginated data. By enabling server-side pagination in the Table settings and configuring queries to handle pagination parameters like LIMIT and OFFSET, data can be efficiently presented to users.

Steps for Setup:

  1. Enable server-side pagination in the Table settings.

  2. Modify queries to accept LIMIT and OFFSET or cursor-based pagination parameters.

  3. Update the table data based on pagination state changes, ensuring that data loads only as needed.

This approach gives you fine-grained control over data loading, improving performance and user experience. 


Toolpioneers can tailor these implementations to fit your unique needs, ensuring optimal performance at every stage!


Conclusion


Effective pagination is about understanding your data and your users' needs.

By mastering retool pagination, you've unlocked the power to:

  • Efficiently manage large datasets

  • Improve application performance

  • Enhance user experience

  • Scale your data operations seamlessly


In the world of big data, it's not about how much you can handle – it's about how smartly you can manage it. With efficient pagination within Retool, you're well on your way to smarter, more efficient data management.


Ready to put these skills into action? At Toolpioneers, we specialize in building custom software applications using Retool. Our team of experts can help you implement these pagination strategies and more, creating powerful internal tools that don't burden your engineering resources. Hire us and let's build something amazing together!



bottom of page