How to up speed for insert?

Posted 11 months ago by alex_storm

Hello! I have simple array for insert to db. It adds 50,000 entries at a time. In total in one table more than 2 million records which need to be entered in a database.

The question is how to speed up the introduction of this data.

I made tests for that.

  1. When I use next construction, I getting 23400 records per min.

// Inserting one record at a time
foreach ($records as $record)
{
DB::table($table)->insert($record);
}

  1. When I use next construction, I getting 40 records per min.

// Inserting 50000 records at a time
// I tried to reduce the number of records at a time, but the result is approximately the same
DB::table($table)->insert([$records]);

  1. When I use next construction, I getting 230000 records per min.

// Inserting 50000 records at a time
// The downside of this construction is that need to generate data for the insert yourself.
DB::insert('INSERT INTO '.$prefix.$table.' ('.$columns.') VALUES '.$values);

What other options can be used in Laravel system for mass data insertion with fast speed? Thanks.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.