1
votes

I have a web app built in Laravel 4, I have a clients table in the database with more than 20000 records, everything working fine, but my hosting service told me that I need to improve my query to retrieve the clients data. I'm a little confused about this issue, I'm not sure what is wrong or how can I improve my query.

$data = Client::orderBy('created_at', 'desc')->paginate(50);

How is the best practice to retrieve data from a table with thousands records?

Thanks in advance!

1

1 Answers

1
votes

Your improvement is good.

If you paginate your queries, you don't get all the results of the query. If you have 20000 get all records take a lot of resources, and perhaps there is an error before you get all the records.

In your development environment you have a lot of resources but you need to think in these situations on your hosting service, you have less resources.

The same will occurs if your pagination number is too high. Don't have sense something like this

->paginate(10000)

Your pagination number must be a reasonable number.