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

  • k as in knife

    Many moons ago my earlier version of my blog had this list I use when spelling things for people over the phone. I managed to find it at archive.org and thought I’d repost… a as in aisle (or aye) b as in bdellium  c as in czar d as in djibouti e as in eight…

  • Open Source TechFinder

    Inspired by the AUTM conference, I got inspired to look at some of the common processes techtransfer folks do. Main idea was to try to develop something relatively ‘standalone’ that might address a use case I learned about, so I decided on building a web-based open source techfinder tool to publish licensable technologies. The notion…

  • 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>…

  • 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…

Leave a Reply

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