Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dr24's avatar
Level 2

Pagination and query too slow on server

I have get all users method that shows all users from database with pagination. On local everything works fine and fast but thats probably because I don't have many users in database. But when I go to server it takes one minute to open up get all users page and when I scroll down it takes same amount of time to opet ?page=2 and etc. Query calculates distance from other users based on latitude and longitude of authenticated users and shows closest users to him. My query or pagination are probably too slow and I need help on how to optimize it.

0 likes
2 replies
wingly's avatar

Well for starters i don't see any pagination on the DB level that means you load all users in memory. Try using paginate() instead of get() at your query

dr24's avatar
Level 2

@wingly I can't get paginate instead of get because I need merge users and user with no distance in order to get results based on requirements. Is there any way to optimize this without paginate() method from laravel?

Please or to participate in this conversation.