Hi everyone,
I have 90k retailers in retailers table.
I have 2.5 million retailer orders in retailer_order table.
While preparing some complex reports we were getting timeout errors. those reports involved another table with again over 2 million records.
Then, I thought of preparing a table to store values matching my report.
Then, I created below job.
Now my job is working but its slow and creating 20 records per minute thus taking close to 3 days to complete the job.
Can i improve the timing or follow any better approach. I am sure there would be better approaches to handle this situation.
public function handle()
{
// Determine the year range based on the month
if ($this->month <= 3) {
$year = ($this->year - 1) . '-' . $this->year;
} else {
$year = $this->year . '-' . ($this->year + 1);
}
// Calculate the sum of order values for the retailer for the specified month and year
$totalOrderValue = reatailerorder::where('retailer_id', $this->retailerId)
->whereYear('created_at', $this->year)
->whereMonth('created_at', $this->month)
->sum('orderValue');
// Convert month number to month name
$monthName = date('F', mktime(0, 0, 0, $this->month, 10));
$monthNameinCol = strtolower($monthName);
pobSale::updateOrCreate(
[
'retailer_id' => $this->retailerId,
'year' => $year,
],
[$monthNameinCol => $totalOrderValue]
);
}