laravel bulk imports

Small note, mostly for myself, as a reminder that when doing large imports of data, you may not need each block to be processed as an Eloquent model.

<?php
//
$processedData = ['name'=>'foobar', 'age'=39];
User::create($processedData);
//

That code above would generally be faster as

<?php
//
$processedData = ['name'=>'foobar', 'age'=39];
DB::table('users')->insert($processedData);
//

Obviously there are even faster ways – preparing your data in some text format, and using your DB cli tools to do a bulk/mass import.

Most recently, I’ve been given a large dump of data which I need to suck in to a different application (built with laravel).

Initial import process I wrote was the top style, and with the amount of data in the dump, it took around 12-14 minutes locally. Using the bottom style, it’s closer to 6 minutes.

On the public testing server, the process took around 9 minutes, and is now around 4.

This approach isn’t always suitable for every case, but it might be worth trying out if you’re hitting speed snags. In my case, this is re-importing sample data after a build/push, and waiting 9-10 minutes is … annoying. Waiting 4 minutes is less annoying.

Similar Posts

  • A bit of feedback…

    A small bit of feedback… that’s often what a mobile user is looking for. Haptic feedback – a quick device vibration – is great. It’s subtle, quick, doesn’t interrupt, but gives an actual *feeling* that something happened. And… on iOS, it’s harder to do without building a full ‘native mobile app’. iOS Safari doesn’t support…

  • Identity and habits

    “Your present identity should not constrain your future habits”. For this quote from the audio book “Hello, Habits“. Obviously the book is about ‘habits’, but the phrase could easily have been “Your present identity should not constrain your future self”. In either reading of this, it’s been stuck in my head for while now.  How…

  • PHP Quality Tools

    Curious about checking out the quality of your PHP project, but don’t know where to start? https://github.com/jakzal/phpqa is a project providing docker images of various tools to help measure aspects of your PHP code. will run the phploc tool on your current folder But… you can alias the tool, then simply run $ phpqa <toolname>…

  • CyclopsMonitor

    I’ve been posting more about this new service over on linkedin, but haven’t posted much here. CyclopsMonitor is a web monitoring service – checking if a web address is up, how fast it responds, if specific content is available, when SSL/TLS certs expire, when domain name expires, and… sending you notifications when problems occur. Currently,…

  • Laravel down migrations

    I get an email newsletter from Martin Joo every week or so. The newsletters generally have some useful tips around the Laravel framework or sometimes just general development tips. I’ve learned a couple of neat tricks here and there, and will continue to receive. This morning I received an email with a Laravel ‘tip’ regarding…

Leave a Reply

Your email address will not be published. Required fields are marked *