I've wracked my brain about as much as possible on this one, and am still struggling! I have 2 tables of data that need to be exported with my applicaton: one is ~26k records, the other is ~60k records. I was first trying to handling this on the frontend using typical request/responses, even trying to use a streamed response as described in this artcle: https://medium.com/@barryvdh/streaming-large-csv-files-with-laravel-chunked-queries-4158e484a5a2
However, these approaches always lead to out of memory errors. I finally decided to try using a Job and Queue worker to process the export. While this is "working," it takes forever. The larger table takes about 6 minutes to finish. What other packages or services can help reduce the amount of time it takes to perform the export? Here is my code:
class ExportServiceHistory
{
public function fire($job, $data)
{
// Generate our file name
$filename="claims_export_" . date("YmdHis") . time() . ".csv";
// Set the file path to the temporary location
$handler = temp_path('export') . $filename;
// Open file handler for writing output
$file = fopen($handler, 'w');
/*
* Add headers
*/
$headers = [
'type',
'plan',
'component_name',
'claim_id',
'service_date',
'odometer',
'service_name',
'vin',
'year',
'make',
'model',
'claim_date',
'status',
'payout',
'distributorship'
];
fputcsv($file, $headers);
ServiceHistory::chunk(200, function ($records) use ($headers, $file) {
foreach ($records as $record) {
$row = [];
foreach ($headers as $column) {
$value = $record->{$column};
$row[] = $value;
}
fputcsv($file, $row);
}
});
fclose($file);
\Storage::disk('gcs')->put($handler, $file);
$job->delete();
}
public function failed($data)
{
// Called when the job is failing...
}
}
Also, as a note, the application is using Laravel 5.5 and cannot be upgraded at this time.
Any feedback is greatly appreciated, thanks!