Courses

Better Eloquent Performance

Faster Seeds with Insert and Chunk

Previous: Query Result Caching: With or Without Package
avatar

hi, instead of chunk collection by 500 records and loop through each chunk, Would it be better if we insert directly 20000 models into database with the code below?

Employee::insert($employees->toArray());

This will produce only one query instead of 40 queries.

Thank you

avatar

Yes but insert() doesn't take care of timestamps fields and any Eloquent listeners. And 1 long query is not always faster than 40 shorter ones. You should actually test it out, I guess.

avatar
You can use Markdown
avatar
You can use Markdown