在Laravel中使用代码块或游标优化代码 [英] Optimizing code with chunk or cursor in laravel

查看:60
本文介绍了在Laravel中使用代码块或游标优化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5.4应用程序中定义了Company模型和Contact模型,它们两者之间有许多关系.因此,例如联系人模型具有:

public function company()
{
    return $this
        ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}

现在我有一个数据集,我想在其中提取所有联系人数据以及公司详细信息,所以我在使用:

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);
}

这对于小型数据集非常适用,但是在使用大量数据时却失败了(大约10,000个字段),我想php memory fails在涉及大型数据集时会加载.我正在浏览Laravel文档以找出解决方案,并了解了chunk()cursor()方法.有人可以指导我解决该问题的方法吗?或者可以采用什么方法来解决此问题.

谢谢

解决方案

我建议您对这两种方法进行一些系统古怪的测试.

块:

它将分页"您的查询,这样您就可以使用更少的内存.

  • 使用更少的内存
  • 需要更长的时间

`

public function getData() {
    Contact::chunk(1000, function ($contacts) {
        foreach ($contacts as $contact) {
            //rest of your code...
        }
    });
}

`

光标:

您将使用PHP生成器逐一搜索查询项.

  • 花费更少的时间
  • 使用更多的内存

`

public function getData() {
    foreach (Contact::cursor() as $contact) {
        //rest of your code...
    }
}

`

有关更详细的解释,请参见以下答案: 解决方案

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.

  • It takes less time
  • Uses more memory

`

public function getData() {
    foreach (Contact::cursor() as $contact) {
        //rest of your code...
    }
}

`

For a more detailed explanation see this answer: What is the difference between laravel cursor and laravel chunk method?

For performance testing see this post: https://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http%3A%2F%2Fqiita.com%2Fryo511%2Fitems%2Febcd1c1b2ad5addc5c9d

这篇关于在Laravel中使用代码块或游标优化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆