samedi 10 février 2018

Laravel 5.5 seeder really slow

I'm trying to seed a test database with a largish number of hits to test my dashboard metrics displays. So I want to generate the 25k hits, and associate them with a random Post from the database so that the data will be consistent.

I initially tried to grab a random Post in the HitFactory class, but this was really, really slow (like hours to generate several thousand hits). So then I moved the random part out into the seeder class, and made the call only once to minimize the DB hits, thinking this would speed it up considerably. But it didn't-- it still takes at least 5-10 seconds to create a single Hit object.

I'm not sure how this is possible-- is there an optimization I'm missing? Note that I can't just generate a random int between 1 - x and use that as the linked Post, since I'm using UUID style IDs for the posts table.

Here's the seeder that takes so long to run:

use Illuminate\Database\Seeder;

class HitsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $posts = App\Post::all();

        $i = 0;

        while ($i <= 25000) {
            $post = $posts->random();
            factory(App\Hit::class)->create(
                [
                    'post_key' => $post->post_key,
                    'subject_code' => $post->subject_code,
                    'subject_id' => $post->subject_id,
                ]
            );
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire