在 Laravel 6 中添加了一种新类型的集合: Lazy Collections。 如果需要处理非常大的数据集(数千或数百万行)而不会遇到内存限制,那么它们是非常棒的。
我最近的任务是在工作中的一个项目中重构 excel 导出。 问题是,由于数据集太大,Laravel 无法处理,导出无法再创建。 数据库查询返回了大约 300,000 个结果! 应用程序产生超时或一直内存不足。
一种天真的方法是增加超时时间或内存限制,并希望下次出现问题时,另一个人会处理这个问题。 但这不是我的工作方式。 我不喜欢创可贴。 我喜欢具体的、长期的解决方案。
laravel excel 扩展包已经相当灵活。 通过在使用 FromQuery
-concerns 时使用「chunks
」,它在减少数据库负载方面做得很好。 然而,我们的导出仍然很难处理大数据集。
我和我的同事讨论过,我们是否应该完全重写这个特性:将导出推送到队列中,并在导出结束时向用户发送通知。 然而,这个功能在这个应用程序中只是一个很小的东西。 对我们来说,仅仅为了一个简单的导出而增加如此多的开销是没有意义的。
那天晚些时候,我有一个小小的「我发现了」的时刻,因为我记得 Laravel 中有 LazyCollections 这个东东。
我重新编写了导出:它现在使用 FromCollection
-concern,而不是 FromQuery
。 我必须对 collection()
方法进行的惟一更改是将查询构建器链末尾的 get()
方法替换为 cursor()
。
下面是我们导出功能的简化版本。 Request
对象通过构造函数传递,因此我们可以根据用户在 UI 中选择的内容对查询进行调整。
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Http\Request;
class UsersExport implements FromCollection
{
use Exportable;
protected Request $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function collection()
{
return User::query()
->when($this->request->get('include_subscribed'), function ($q) {
return $q->where('is_subscribed', true);
})
->cursor(); // ← 重要的一点
}
}
我相信你在你的项目中遇到了内存问题。 你增加了内存限制,希望问题已经解决了 (我自己已经做过无数次了)。
如果是在 Laravel 项目中,我希望,我可以让你重新查看该代码并使用 LazyCollections 重写。
修复这个问题非常有趣,所以我做了一个小小的基准测试:我们的导出现在可以轻松地导出数百万行,而不会遇到内存限制。 太酷了! ?
sunrise beach resort v
https://times.coworkbooking.com/2019/11/21/52-fun-things-to-do-in-goa-india/
cancun hotels rooms
https://imp.world/explore/north-goa-vs-south-goa-a-2022-guide-to-the-two-amazing-sides-of-one-place/
five star hotel in cancun
packages to cancun
best hotels in cancun all inclusive
cancun things to do couples
vacations to cancun mexico
best cancun hotels for singles
gay friendly cancun all inclusive resorts
best nightlife all-inclusive resorts cancun
is there a casino in cancun mexico
vacation package to cancun all inclusive
top all inclusive family resorts
packages in cancun
cancun resorts for singles
classy cat tower
hotel deals in cancun
me cancun all inclusive package
cancun beach resort
cancun hotels and resorts