5

So I am developing a laravel application and I am trying to get my seeds optimised so that they run faster.

http://bensmith.io/speeding-up-laravel-seeders

This guide helped a ton. According to this, I should minimise the number of SQL queries by doing mass insertions and it cut down the time to 10% of the original seeding time which is awesome.

So now I am doing something like:

$comments = [];
for ($i = 0; $i < 50; $i++) {
    $bar->advance();
    $comments[] = factory(Comment::class)->make([
        'created_at'      => Carbon\Carbon::now(),
        'updated_at'      => Carbon\Carbon::now(),
        'comment_type_id' => $comment_types->shuffle()->first()->id,
        'user_id'         => $users->shuffle()->first()->id,
        'commentable_id'  => $documents->shuffle()->first()->id,
    ])->toArray();
}
Comment::insert($comments);

This works like a charm. It gets the queries down to a single one.

But then I have other seeders where I to work with dumps and they are more complex:

$dump = file_get_contents(database_path('seeds/dumps/serverdump.txt'));
DB::table('server')->truncate();
DB::statement($dump);

$taxonomies = DB::table('server')->get();

foreach($taxonomies as $taxonomy){
    $bar->advance();
    $group = PatentClassGroup::create(['name' => $taxonomy->name]);

    preg_match_all('/([a-zA-Z0-9]+)/', $taxonomy->classes, $classes);

    foreach(array_pop($classes) as $key => $class){
        $type = strlen($class) == 4 ? 'GROUP' : 'MAIN';
        $inserted_taxonomies[] = PatentClassTaxonomy::where('name', $class)->get()->count()
            ? PatentClassTaxonomy::where('name', $class)->get()->first()
            : PatentClassTaxonomy::create(['name' => $class, 'type' => $type]);
    }

    foreach($inserted_taxonomies as $inserted_taxonomy){
        try{
            $group->taxonomies()->attach($inserted_taxonomy->id);
        }catch(\Exception $e){
            //
        }
    }
}

So here when I am attaching taxonomies to groups, I use the native eloquent code so taking the record and mass inserting is difficult. Yes, I can fiddle around and figure out a way to mass insert that too but my problem is that I have to write and optimise all seeds and every part of those seeds to mass insert.

Is there a way, where I can listen to DB queries laravel is trying to execute while seeding. I know I can do something like this:

DB::listen(function($query) {
    //
});

But it would still be executed right. What I would like to do is somehow catch the query in a variable, add it to a stack and then execute the whole stack when the seed is coming to an end. Or in between too since I might need some ids for some seeds. What is a good workaround this? And how to really optimise the seeds in laravel with a smart solution?

Rohan
  • 13,308
  • 21
  • 81
  • 154

0 Answers0