DEV Community

Debajyoti Das
Debajyoti Das

Posted on

Inserting dummy 200000 data using Faker

Browser could render 80,000 data but failed while trying to render 1 lakh.
2lakhs data could be inserted in MySQL under 20 secs with chunking.
3lakhs data insertion failed (Exception: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16777224 bytes))

        $faker = Faker\Factory::create();
        $dt = [];
        $i = 0;
        $st = microtime(true);
        for($i=1; $i<=200000; $i++)
        {

            $et= microtime(true) - $st;
            array_push($dt, ['id' => $i,'names' => $faker->name]);
            // $dt[$i]=[
            //     'id' => $i,'names' => $faker->name
            // ];
        }
        $c_i = collect($dt);
        $chunks = $c_i->chunk(500);
        $b = [];
        try{
            foreach($chunks as $key => $c)
            {
                // $b = $c[$key]['id'].$c[$key]['names'];
                // return $c->toArray();
                DB::table('names')->insert($c->toArray());
            }
            return 'Done';
        }
        catch(Exception $e1)
        {
            return $e1->getMessage();
        }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)