Hello, I am exporting data with a total record of 15 lacks. It takes about 28-30 minutes. I want to optimize to reduce the time to export. I have used "avadim\FastExcelWriter\Excel"
public function exportExcelWithFastExcel()
{
set_time_limit(3000); //3000 seconds = 50 minutes
ini_set('memory_limit', -1);
$finance_details = Finance::query()
->with(['financeDetails' => function ($query) {
$query->select('id', 'finance_id', 'description','rate', 'qty', 'uplift');
}])
->take(1000000)
->get();
if (!empty($finance_details)) {
foreach ($finance_details as $finance_value) {
$grand_total = 0;
foreach($finance_value->financeDetails as $finance_details_data)
{
$line_total = 0;
$row_rate = $finance_details_data->rate ? $finance_details_data->rate : 0;
$row_qty = $finance_details_data->qty ? $finance_details_data->qty : 0;
$row_uplift = $finance_details_data->uplift ? $finance_details_data->uplift : 0;
if (!empty($row_uplift)) {
$line_total = (((float) $row_rate + (((float) $row_rate * $row_uplift) / 100)) * $row_qty);
} else {
$line_total = ((float) $row_rate * $row_qty);
}
$grand_total += $line_total;
}
$finance_arr = [];
$finance_arr['job_number'] = $finance_value->job_id ? $finance_value->job_id : '';
$finance_arr['doc_no'] = $finance_value->doc_no ? $finance_value->doc_no : '';
$finance_arr['app_no'] = $finance_value->app_no ? $finance_value->app_no : '';
$finance_arr['total_with_uplift'] = $grand_total;
$finance_arr['finalised'] = $finance_value->is_final ? $finance_value->is_final : '';
$collect_arr[] = $finance_arr;
}
}
# Create header style
$headStyle = [
'font' => [
'style' => 'bold',
'color' => 'ffffff',
],
'fill' => [
'background-color' => '#00008b'
],
// 'text-align' => 'center',
// 'vertical-align' => 'center',
'border' => 'thin',
];
# Create header title
$head = ['JOB NUMBER', 'DOC NO', 'APP NO', 'TOTAL WITH UPLIFT', 'FINALIZED'];
$excel = Excel::create(['Sheet1']);
$sheet = $excel->getSheet();
$sheet->writeHeader($head, $headStyle);
// Write data
foreach($collect_arr as $row_data) {
$sheet->writeRow(array_values($row_data));
}
# Add filter
$sheet->setAutofilter(1);
# Add Width
$sheet->setColWidths(['A' => 16, 'B' => 13, 'C' => 18, 'D' => 22, 'E' => 13]);
$excel->output('finance-details.xlsx');
}
Here are my suggestions for improving the process for the user:
Another possible solution can be adding indexing to the database, but as you are not filtering the data before export, I think the indexing will not improve the process.
Sometimes, there is time taking processes in the application, so we cannot do much of improvements there. Although by changing the processes as per the suggestions above, you can keep the user informed.
Thank you for suggestion
I want to pass that export file in apis response so for that I can not use this solution _Move the export logic to a background job
_
I have implemented chunk, but still taking more time.
public function exportExcelWithChunk()
{
$finance_details = Finance::query()
->with(['financeDetails' => function ($query) {
$query->select('id', 'finance_id', 'description','rate', 'qty', 'uplift');
}])
->take(100000)
->get();
if (!empty($finance_details)) {
foreach ($finance_details as $finance_value) {
$grand_total = 0;
foreach($finance_value->financeDetails as $finance_details_data)
{
$line_total = 0;
$row_rate = $finance_details_data->rate ? $finance_details_data->rate : 0;
$row_qty = $finance_details_data->qty ? $finance_details_data->qty : 0;
$row_uplift = $finance_details_data->uplift ? $finance_details_data->uplift : 0;
if (!empty($row_uplift)) {
$line_total = (((float) $row_rate + (((float) $row_rate * $row_uplift) / 100)) * $row_qty);
} else {
$line_total = ((float) $row_rate * $row_qty);
}
$grand_total += $line_total;
}
$finance_arr = [];
$finance_arr['job_number'] = $finance_value->job_id ? $finance_value->job_id : '';
$finance_arr['doc_no'] = $finance_value->doc_no ? $finance_value->doc_no : '';
$finance_arr['app_no'] = $finance_value->app_no ? $finance_value->app_no : '';
$finance_arr['total_with_uplift'] = $grand_total;
$finance_arr['finalised'] = $finance_value->is_final ? $finance_value->is_final : '';
$collect_arr[] = $finance_arr;
}
}
ini_set('max_execution_time',600);
ini_set('memory_limit',"4096");
Excel::create('Report', function ($excel) use ($collect_arr) {
$excel->sheet('report', function ($sheet) use ($collect_arr) {
$sheet->appendRow($this->columns());
$query->chunk(1000, function ($rows) use ($sheet) {
foreach ($rows as $row) {
$sheet->appendRow($this->rows($row));
}
});
});
})->download('xlsx');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community