laravel 在laravel中使用块或游标优化代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45467100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optimizing code with chunk or cursor in laravel
提问by Nitish Kumar
I'm having Company
Model and Contact
Model defined in my Laravel 5.4
application, both of them have many to many relationship. So, for example contacts model has:
我在我的应用程序中定义了Company
模型和Contact
模型Laravel 5.4
,它们都有多对多的关系。因此,例如联系人模型具有:
public function company()
{
return $this
->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}
Now I've a data set where I want to pull all contacts data and there company details so I was using:
现在我有一个数据集,我想在其中提取所有联系人数据和公司详细信息,所以我正在使用:
public function getData()
{
$contacts = Contact::all();
foreach($contacts as $contact)
{
$getCompany = $contact->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first();
$getCompany->contacts = Company::find($getCompany->id)->contacts;
$contact->company = $getCompany;
$contact->companies_interested = json_decode($contact->companies_interested);
$companies = [];
if($contact->companies_interested)
{
foreach($contact->companies_interested as $companiesInterested)
{
$getCompany = Company::withTrashed()->find($companiesInterested);
$companies[] = array(
'value' => $getCompany->id,
'label' => $getCompany->name
);
}
$contact->companies_interested = json_encode($companies);
}
}
return response()->json([
'model' => $contacts
], 200);
}
This works perfectly fine for small data set, but while using large number of data it fails (approx 10,000 fields), I guess php memory fails
to load when it comes to large data set. I was going through Laravel
docs to find out the solution and came to know about chunk()
and cursor()
methods, Can someone guide me what can be done in this problem or what can be the approach to overcome this.
这对于小数据集非常有效,但是在使用大量数据时失败(大约 10,000 个字段),我想php memory fails
在涉及大数据集时加载。我正在浏览Laravel
文档以找出解决方案并了解chunk()
和了解cursor()
方法,有人可以指导我在这个问题中可以做些什么,或者有什么方法可以克服这个问题。
Thanks
谢谢
回答by Jean Marcos
I recommend you to test both methods for some quirkiness of your system.
我建议您测试这两种方法,以了解您系统的某些奇怪之处。
Chunk:
块:
It will "paginate" your query, this way you use less memory.
它将“分页”您的查询,这样您就可以使用更少的内存。
- Uses less memory
- It takes longer
- 使用更少的内存
- 需要更长的时间
`
`
public function getData() {
Contact::chunk(1000, function ($contacts) {
foreach ($contacts as $contact) {
//rest of your code...
}
});
}
`
`
Cursor:
光标:
You will use PHP Generators to search your query items one by one.
您将使用 PHP 生成器一项一项地搜索您的查询项。
- It takes less time
- Uses more memory
- 花更少的时间
- 使用更多内存
`
`
public function getData() {
foreach (Contact::cursor() as $contact) {
//rest of your code...
}
}
`
`
For a more detailed explanationsee this answer: What is the difference between laravel cursor and laravel chunk method?
有关更详细的解释,请参阅此答案:laravel cursor 和 laravel chunk method 之间的区别是什么?
For performance testingsee this post: https://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http%3A%2F%2Fqiita.com%2Fryo511%2Fitems%2Febcd1c1b2ad5addc5c9d
有关性能测试,请参阅此帖子:https: //translate.google.com/translate?hl=en&sl=auto&tl=en&u=http%3A%2F%2Fqiita.com%2Fryo511%2Fitems%2Febcd1c1b2ad5addc5c9d