Mar 1, 2024
0
Level 1
Efficient bulk data insertion (250K records) in Laravel without exceeding PHP time limit? (Queue, multi-tenancy)
I'm encountering a PHP execution time error while inserting approximately 250k records into a MySQL database using Laravel Eloquent. I'm aiming to achieve this without increasing the overall execution time, considering the following constraints:
- Frontend: Angular
- Multi-tenancy: Implemented using tenancy for laravel
- Attempted solutions: Laravel Queue
Below is my code.
Job File Code -
class MedicineData implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle(): void
{
foreach ($this->data as $key => $value) {
$medicine = new MedicineName();
$medicine->name = $value['name'];
$medicine->save();
}
}
}
Controller File Code
$chunks = array_chunk($data, 500);
$batch = Bus::batch([])->dispatch();
foreach ($chunks as $key => $value) {
$batch->add(new MedicineData($value));
}
I am getting an error of Maximum PHP Execution Time. How to resolve this.
Please or to participate in this conversation.