我必须使用laravel 5.2中的排队作业将两个表之间的belongsTo关系的600k数据库行导出到csv文件,当我不包含第二个表中的信息时,它可以正常工作,但它达到内存限制或超时我猜(当我包含第二个表时,输出没有错误,作业停止,文件阻塞在~8MB)。
这是我工作中的句柄功能:
public function handle()
{
$request = $this->request;
$ts = Carbon::now()->timestamp;
$file = "/path/file-" . $ts . ".csv";
$csv = CSV::createFromPath(public_path() . $file, "w");
$csv->insertOne(\Schema::getColumnListing('games'));
$queryBuilder = Game::query();
if(!empty($request['host'])) $queryBuilder->searchHost($request['host']);
if(!empty($request['customer_code'])) $queryBuilder->searchCustomerCode($request['customer_code']);
if(!empty($request['customer_first_name'])) $queryBuilder->searchCustomerFirstName($request['customer_first_name']);
if(!empty($request['customer_last_name'])) $queryBuilder->searchCustomerLastName($request['customer_last_name']);
if(!empty($request['check_1'])) $queryBuilder->searchCheckOne($request['check_1']);
if(!empty($request['check_2'])) $queryBuilder->searchCheckTwo($request['check_2']);
if(!empty($request['check_3'])) $queryBuilder->searchCheckThree($request['check_3']);
if(!empty($request['check_4'])) $queryBuilder->searchCheckFour($request['check_4']);
if(!empty($request['date_sync'])) $queryBuilder->SearchDate($request['date_sync']);
if(!empty($request['place'])) $queryBuilder->SearchPlace($request['place']);
$queryBuilder->chunk(100, function($games) use($request, $csv){
foreach ($games as $game) {
$csv->insertOne($game->toArray());
}
});
event(new GameFileReadyEvent($file));
}
此代码工作正常,但在foreach循环中,如果我这样做
$queryBuilder->chunk(100, function($games) use($request, $csv){
foreach ($games as $game) {
$csv->insertOne($game->id, $game->host->id, $game->check_first...);
}
});
它似乎在某处阻挡,我不知道为什么。
我正在使用league / csv来完成出口工作,laravel 5.2和用于开发的宅基地盒,以及用于生产的Ubuntu 16.04服务器。