March 2, 2021
Hot Topics:

Implementing Search Result Pagination in a Web Application

  • By Vlad Kofman
  • Send Email »
  • More Articles »

Middle-Tier-Driven Pagination Algorithm

Modern frameworks and database drivers allow efficient in-memory traversal of data on the application server level or middle-tier level. I would not recommend this approach for large data sets. However, because this approach could be combined with the caching solution on the application server, it should be considered for small to medium data sets. Depending on the nature of the business requirements, this solution could be used for "rigid" searches, where users have no control of the query parameters and are forced to use pre-defined criteria, such as a shopping category.

If many users can request an identical search query (and the result is not in the millions of rows or changes very often), it makes sense to select the whole result set from the database and pre-cache it in the application server's memory. Subsequent queries would not result in expansive database hits, and will be served very quickly from the application server's memory.

Here is the overview diagram of the second approach to the algorithm for pagination:

Click here for a larger image.

Front-End Pagination Algorithm

This approach is the third way to implement pagination, but it is very inefficient and should not be used unless some specific business rule demands it. Front-end pagination is done entirely on the front-end side, in the client's browser. Typically, it uses JavaScript or some other client-side technology. The entire result set is brought to the client via a response object, and manipulated visually to create clickable pages, by hiding and showing rows (or HTML elements representing them). Depending on the size of the result set, this approach's initial request will always take time equal to the time it takes to query for the entire set of data. But, subsequent page clicks will be fast, and depend on the client's hardware. Note that if the result is large, it can take a while to download the first time around; in the worst case, it can hang the browser.

Here is the overview diagram of the third approach:

Generating Page Links

After choosing the best approach to retrieve the result set and traverse it, the application needs to create the actual page links for users to click. Below is a generic function pseudo-code, which should work with any server-side technology. This logic should reside in your application, and will work with both database-driven and middle-tier pagination algorithms.

The function takes three (3) parameters and returns HTML representing page links.

The parameters are query text, staring location row number, and total number of the result set rows. The algorithm is clever enough to generate appropriate links based on where the user is in the navigation path.

Note: I set the default of 50 rows per page, and a page window to 10. This means that only 10 (or fewer) page links will be visible to the user.

Page 2 of 3

This article was originally published on August 24, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date