简体   繁体   中英

Why Datatables is very slow with laravel?

I have a project with laravel in which he used "yajra / laravel-datatables" the problem is that it is very slow when it comes to fetching server data.

This project has a total of 11,000 records and I paginate it with Datatables of 10 in 10. To bring the first 10 it takes more than 3 seconds.

错误

We have another project that was done natively with PHP and Datatables and there are about 50,000 records and it works instantaneously by bringing the first 10 records plus it is the same computing capacity (It takes 100 milliseconds).

My infrastructure is in Amazon Web Services.

Because of the way we deliver the data to Datatables, I suspect that it is bringing all the data from the database and delivers 10 records to the interface only. However, it takes longer since it gets all the records in the database.

$query = DB :: select( 'QUERY SQL' );
return DataTables :: of( $query ) -> toJson();

Running the query in workbench takes about 200 milliseconds to bring all the information.

I'm not too sure how to create an entire Builder instance from a raw query, but what you definitely can do is using a subquery:

$builder = DB::query()->fromSub(
    DB::raw($theQueryString),
    'wrapped_query'
);

return datatable($builder)->toJson();

This has also the advantage that both sorting and searching will work flawlessly even with joined tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM