2012-09-15 122 views
4

我試圖將〜20K記錄插入到我的數據庫時遇到問題。我注意到,儘管我在我的foreach循環中回顯,但我沒有在命令行中輸出任何內容。相反,在插入〜9440個記錄後,我得到一個與Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 91 bytes) in /Users/me/Sites/Laravel/database/connection.php on line 293有關的錯誤。Laravel DB插入錯誤:允許的內存大小用盡

這裏是我的代碼(試圖用兩個口才流利):

<?php 

class Process_Controller extends Base_Controller { 

    public function action_migrate() 
    { 
     $properties = DB::table('raw_properties')->get('id'); 
     $total = count($properties); 

     foreach($properties as $x => $p){ 
      $r = RawProperty::find($p->id); 
      $count = $x +1; 

      $prop_details = array(
       'column' => $r->field, 
       // Total of 21 fields 
      ); 

      DB::table('properties')->insert($prop_details); 

      echo "Created #$count of $total\n"; 
     } 
    } 

} 

回答

-2

此錯誤描述了你的PHP腳本已經耗盡內存限制,由於分配給腳本的內存不足。您需要使用ini_set函數 來增加memory_limit,例如ini_set('memory_limit','128M');

+3

我正好碰到這個問題,增加內存限制是速戰速決。更好的解決方案是關閉查詢日誌記錄,如@Erik所建議的,低於 – th3hamburgler

29

接受的答案是修復症狀而不是問題。問題是Laravel查詢日誌(在內存中)在執行如此大量的查詢時正在吃掉所有的RAM。看到這裏的答案:https://stackoverflow.com/a/18776710/221745

或者,簡單地說,通過關閉查詢日誌記錄:

DB::disableQueryLog() 

執行20K前查詢

+0

我同意。在表格中插入1M行時,2G是不夠的,但DB :: disableQueryLog()爲我解決了這個問題。 –

+0

謝謝我寧願修復問題而不是症狀 – malhal

+1

這是否必須再次啓用Querylogging或對於當前腳本是這樣嗎? –

相關問題