When one has a large database that needs to be sent to the user and one decides to send it in chunks rather than sending the entire database at a go, there comes the role of pagination. It is quite obvious that the concept of pagination is used by almost all the sites that are visited daily. Before getting into the depth of pagination, one must know about the two different types of paginations.
Two types of paginations can be implemented on the database:
Cursor based pagination is considered to be one of the most effective methods of pagination and must be integrated wherever possible.
The advantages of Cursor based pagination compared to offset pagination are:
Everything has its pros and cons. The cursor-based pagination too has its disadvantages. The disadvantages of cursor-based pagination as compared to offset based pagination are:
Let us assume that there is a task to sort a user table based on the first name and last name. This can become an issue for cursors since the cursors require the implementation of the unique sequential column to sort. Most of the cursor implementation is based on the timestamp column. Thus, if the requirement is to sort then cursors are not the best choice. One can try different techniques to achieve the same but it will slow down the process and make it even slower than offset.
Cursors may not be very difficult to implement but offsets are generally easy to implement. Thus, if one is looking to implement pagination quickly then offset is the call. This is especially beneficial if the data is static. Thus, the complications can be avoided wherever necessary.
Although this is not primarily a cursor issue as offset pagination used in the backend can even implement an infinite scroll. However, extensive use of cursor for real-time application has made it more convenient to use. Infinite scroll is considered a convenient user interface for the users where they can see a lot of data at once. However, this takes away the user’s conscious decisive power to view the next page and thus can lead to a very addictive experience.
While retrieving data for an offset pagination one generally requires two parameters in the query: a limit and an offset. This becomes slower when there is an increase in the database items. This happens as the database still needs to be read up to the offset number of rows to identify where it needs to start the selection of data.
It generally has a worst-case complexity of o(n). Also, those large databases are generally not static which shall require frequent writes. The resulting window will often be inaccurate across the various pages.
If one needs to get the first page of the newest posts from the database, the following query must be used
The offset that we can include to know the results of the second page is:
However, this process becomes lengthy when the dataset increases.
The cursor pagination as stated above is a unique pointer to a particular record. with the help of cursors, one gets rid of the need to read all the rows which have already been seen with the help of the WHERE clause. Thus, this makes its complexity o(1). The issue of inaccurate results is dealt with after reading a particular row rather than relying on the fact that the position of records is identical.
The same example can be implemented with the help of the following query using cursor pagination:
To use the cursor, the results need to be returned from the first page in addition to the cursor that has been used for the last item in the set of results. This is a good usage of the cursor to move forward in the result set however if the fetching direction is changed the complexity to retrieve the records increases.
The most widely used pagination for real-time applications is cursor pagination. This is primarily because of the increased frequency of new records that are added to the database. The cursor approach is always recommended as it eliminates the duplicated items and also skips no item. A pointer keeps the track of where the data is and which item needs to be fetched next. There are different situations in which both cursor and offset have their advantages. The usage primarily depends upon the data being dealt with.
However, when static data is being queried the cost of performance may not be enough to use a cursor since the complexity increases in such a situation. It is quite obvious that cursor-based pagination is a better choice as compared to offset pagination but the decision to go for the right approach depends entirely upon the use case and the effect pagination shall have on the product.
Learn more about eChannelHub with a free demo, tailored for your unique retail business.
Request A DemoLearn more about eChannelHub with a free demo, tailored for your unique online business