mikelovely's avatar

Which pagination approach is more performant?

Which is more performant (in general)?:

    • Get all records from db table (no sorting, no limits)
    • Put in to a Collection and sort by user input
    • Put Collection in to a Paginator and return paginated records based on user input
    • Get all records from db table with user input using order and limit for pagination
    • Take db response and return

I appreciate this may depend on how big the table is, how many records are being returned, what the indexes are like etc.

But just as a general rule of thumb.

0 likes
6 replies
Punksolid's avatar

Hi @mikelovely The second one.

All the pagination should be do it by the database, if you bring all the records to your app someday it will hang out.

The fastest pagination available is not paginate but simplePaginate but you will not know in which page you are because it is based on the last id that you have and continue on the next id to take the next X rows

Look for the simplePaginate section (https://laravel.com/docs/5.8/pagination#paginating-query-builder-results)

1 like
jlrdw's avatar
Get all records from db table (no sorting, no limits)
  Put in to a Collection and sort by user input
 Put Collection in to a Paginator and return paginated records based on user input

Proper pagination only queries x number at a time. Do you think google would retrieve 125,000,000 records, put in a collection and paginate, no.

You should study up on how pagination works and Pagination using MySQL LIMIT, OFFSET.

With that you are only getting and querying 10 per page if that is the number you asked for.

In fact on large data, skipping index is used, because LIMIT, OFFSET isn't efficient.

But really study this and the math behind it. 1). Is never the way.

Snapey's avatar

In addition, you have no way of holding all the records in memory from one request to the next, so, whilst you may only send the first 20 of 200 to the client, come page 2, you will have to do it all again.

if the records are below 1000 (say 100kb) then you can get away with sending all to the client and letting it paginate locally. Datatables does this by default.

mikelovely's avatar

Good points. Especially the Google example. Looks like option 2 is the clear winner here

mikelovely's avatar

Thanks for the reply! I think everyone is in agreement here. For the record - I thought option 2 was probably best as well but I didn't want to influence potential responses.

mikelovely's avatar

Very good point. The more I think about it - the more Option 1 is a terrible idea.

Please or to participate in this conversation.