Published 3 months ago by mgmk
I have 35,000 over records in my table and I'm using datatables library to render all these records. After implemented with datatables, the query result took about 27 seconds. It is too long, right? Can you help me to check the way I implemented is correct or wrong?
Thanks in advance!
$products = $merchant->Product()->select(['id', 'name', 'price']) ->orderBy('created_at', 'desc') ->get(); return Datatables::of($products)->make();
Indeed as mentioned earlier you are first retrieving all records before you pass it to the datatables helper. I suppose you use https://datatables.yajrabox.com/
You should change your code to:
$products = $merchant->Product()->select(['id', 'name', 'price']) ->orderBy('created_at', 'desc'); return Datatables::of($products)->make();
As a reference copied from the site above:
use Yajra\Datatables\Facades\Datatables; // Using Eloquent return Datatables::eloquent(User::query())->make(true); // Using Query Builder return Datatables::queryBuilder(DB::table('users'))->make(true); // Using Collection return Datatables::collection(User::all())->make(true); // Using the Engine Factory return Datatables::of(User::query())->make(true); return Datatables::of(DB::table('users'))->make(true); return Datatables::of(User::all())->make(true);
@jlrdw you are quite rude to be honest. This kind of things maybe makes a lot of sense to you but not everybody understands this from the beginning. Instead of leaving replies like the one above you could add a short explanation why it is wrong instead of saying it is wrong and say: 'use google' because that won't help anybody if you do not know what exactly is wrong.
Any developer, by time they get to the point of using laravel, should fully realize large result sets need pagination.
If it makes you happy it's deleted.
But I still say any developer at laravel level should understand the importance of pagination in a large result set.
although it might come across as 'rude' to some people, I kinda agree with jlrdw on this one: If you have no knowledge (whatsoever) of handling large results, why would a person be building a system in a framework?
It's feels like: I have no idea about structures, materials or load bearing, but I really like the idea of building my own home instead of buying / renting one so I'll build myself a nice 2 floor apartment from any materials at hand without reading any books about the subject.
On subject: It's not your query that's taking ~30 seconds, but creating the page and sending it to the user. A query getting 35000 rows from a single table should not take more then a second, and even that would be slow.