简体   繁体   English

为什么Laravel Datatables非常慢?

[英]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. 我有一个与laravel合作的项目,其中他使用了“ yajra / laravel-datatables”,问题是,在获取服务器数据时非常慢。

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. 该项目总共有11,000条记录,我用10中的10的数据表对它进行分页。要获得前10条,需要3秒钟以上。

错误

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). 我们还有另一个项目是用PHP和Datatables本机完成的,大约有50,000条记录,并且通过引入前10条记录以及相同的计算能力(花费100毫秒)可以立即工作。

My infrastructure is in Amazon Web Services. 我的基础设施位于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. 由于我们将数据传递到Datatables的方式,我怀疑它是从数据库中带走所有数据,并且仅将10条记录传递到接口。 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. 在工作台中运行查询大约需要200毫秒才能带出所有信息。

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实例,但是您绝对可以使用子查询来做:

$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. 这还有一个优点,即使使用联接的表,排序和搜索都可以完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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