Blog Detail


Fast Implementation of Offset/Limit pagination for Laravel cover image

arrow_back Fast Implementation of Offset/Limit pagination for Laravel

This is a fast limit/offset pagination macro for Laravel. It can be used in place of the standard paginate methods.

This package uses a SQL method similar to a “deferred join” to achieve this speedup. A deferred join is a technique that defers access to requested columns until after the offset and limit have been applied.

In our case, we don’t actually do a join, but rather a wherein with a subquery. Using this technique we create a subquery that can be optimized with specific indexes for maximum speed and then use those results to fetch the full rows.

The SQL looks something like this:

select * from contacts              -- The full data that you want to show your users.
    where in (          -- The "deferred join" or subquery, in our case.
        select id from contacts     -- The pagination, accessing as little data as possible - ID only.
        limit 15 offset 150000      

You might get an error trying to run the query above! Something like This version of MySQL doesn’t yet support LIMIT & IN/ALL/ANY/SOME subquery. In this package, we run them as two separate queries to get around that!

The benefits can vary based on your dataset, but this method allows the database to examine as little data as possible to satisfy the user’s intent.

It’s unlikely that this method will ever perform worse than traditional offset/limit, although it is possible, so be sure to test on your data!


This package supports Laravel 8 and 9. (Laravel 8 must be 8.37 or higher.)

To install, require the package via composer:

composer require hammerstone/fast-paginate

There is nothing further you need to do. The service provider will be loaded automatically by Laravel.


Anywhere you would use Model::query()->paginate(), you can use Model::query()->fastPaginate()! That’s it! The method signature is the same.

Relationships are supported as well:


For more details, You can visit Github

Closing Notes

If you want to build any custom web application, CRM Software, SAAS Application, or eCommerce application then Codebrisk is here to help you. We have built an exquisite team of expert Laravel developers who share our passion. Our combined knowledge and experience ensure smooth onboarding, a quick start, and satisfying results. And it covers both small and large IT projects according to your needs. We are open to any type of contract and are always happy to explain any complexities. So if you have a great idea, please feel free to contact us or start a project with us.

Published at : 26-07-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project