2017-09-15 92 views
0

我在Laravel的日程安排中遇到问题。允许的内存大小为8589934592字节穷尽

我看到有一个关于此错误重复的问题,但这些不解决我的问题

[email protected]:/var/www/vhosts/trafficshield.tools/httpdocs# /opt/plesk/php/7.1/bin/php artisan schedule:run Running scheduled command: Closure ^[[15~PHP Fatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 4096 bytes) in /var/www/vhosts/trafficshield.tools/httpdocs/vendor/laravel/framework/src/Illum inate/Database/Eloquent/Model.php on line 279 PHP

此计划目的是依靠我们服务的所有campains的一切来访。

$schedule->call(function() { 
     $campaigns = Campaign::all(); 
     foreach ($campaigns as $campaign) { 
      $campaign->denied_visits = $campaign->visitsDenied->count(); 
      $campaign->allowed_visits = $campaign->visitsAllowed->count(); 
      $campaign->save(); 
     } 
    })->everyFiveMinutes(); 

如何更改PHP代码避免此问题?

配置:memory_limit : 8G

感谢的提前对您有所帮助。

+2

要改变'memory_limit'在数据量增加时会暂时修复问题,所以我建议您避免使用Elloquent来完成这项工作,因为Elloquent的内存效率很差。我会给你一个方法,坚持 – aaron0207

+0

只是一种思想:如果被困在无限循环中,你经常会用尽内存...... – Greg

+0

@Greg我相信没有无限循环。 – Pixel

回答

0
Allowed memory size of 8589934592 bytes exhausted 

这种错误是由内存中的数据的数量较大造成的,所以解决的方式是写一个不太沉重的记忆脚本。通过更改memory_limit,我们只能得到暂时修复,因为当我们的数据增长时,它会回来。

$campaigns = Campaign::all(); //at this point you are pulling the whole Campaigns table to memory.. and you pull some extra rows after that increasing even more the memory use 

正如我所说,Elloquent差效率做这种任务让我们获取数据库行作为mysqli的用来做什么,一个接一个:

$db = DB::connection()->getPdo(); //get a database connection instance 

    $main_query_sql = "SELECT * FROM Campaigns"; //write our query 
    $main_query = $db->prepare($main_query_sql); //prepare it to get executed 
    $main_query->execute(); //execute our query 


    $visits_denied_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 1 AND CAMPAIGN_ID ="; //let's prepare our aggregate queries 

    $visits_allowed_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 0 AND CAMPAIGN_ID ="; //I just assumed them so change it as you need 

    $visits_denied = null; 
    $visits_allowed = null; 

    while($campaign = $main_query->fetch()){ //fetch our rows one by one 
     //now we are getting an associative array from the db instead of a model so we cannot use it as a Laravel Model (we can't use ->save() method or eager loading) 
     $visits_denied = $db->prepare($visits_denied_sql.$campaign['id']); 
     $visits_denied = $visits_denied->execute(); 
     $denied_visits = $visits_denied->fetch(); 

     $visits_allowed= $db->prepare($visits_allowed_sql.$campaign['id']); 
     $visits_allowed= $visits_allowed->execute(); 
     $allowed_visits = $visits_allowed->fetch(); 

     $campaign['denied_visits'] = $denied_visits['total']; 
     $campaign['allowed_visits'] = $allowed_visits['total'] ; 

     //edit with the correct update sentence: 
     $insert_query_sql = "UPDATE CAMPAIGNS SET allowed_visits = :visits_allowed, denied_visits = :visits_denied WHERE id = :id"; 
     $insert_query = $db->prepare($insert_query_sql); 
     $insert_query->bindParam(':visits_allowed', $campaign['allowed_visits']); 
     $insert_query->bindParam(':visits_denied', $campaign['denied_visits']); 
     $insert_query->bindParam(':id', $campaign['id']); 
     $insert_query->execute(); 

    } 

试试这个您的日程安排中,让我知道如果工作!

+0

我有一个错误: https://puu.sh/xAgsa/f248b8b553.png,我使用Laravel 5.3和PostgreSQL – Pixel

+0

你有没有像这样导入? '使用Illuminate \ Support \ Facades \ DB;' – aaron0207

+0

是的,我输入了! – Pixel

相关问题