hillcow's avatar

Laravel performance: How to use pagination with index?

When using Laravel's paginate(20) method, the MySQL query looks like this at the end:

limit 20 offset 0;

To my understanding, therefore Laravel has to scan the entire table instead of using an index. The right way to go would be to apply the limit in the where clause somehow.

How do you handle this? Is there a package or something that takes care of that? I don't really want to build my own paginator. Thanks!

0 likes
2 replies
Sinnbeck's avatar

How would you set a limit in the where clause? I see no way of getting exactly 20 records (unless you never delete a row, and it wouldnt be sortable)

hillcow's avatar

Hm, good question, I don't have the answer. I just don't want to believe that you have to scan the entire table. Kind of destroys the whole purpose of pagination for me.

Please or to participate in this conversation.