关于php:Laravel-Seeder-生成百万模拟数据

7次阅读

共计 1534 个字符,预计需要花费 4 分钟才能阅读完成。

Laravel 集成了 Faker 库,并提供了 Seeder 能够帮忙咱们轻松地生成模仿数据。

先书写数据仓库和数据填充代码

数据仓库代码

use App\Models\Topic;
use Faker\Generator as Faker;

$factory->define(Topic::class, function (Faker $faker) {$sentence = $faker->sentence();

    // 随机取一个月以内的工夫
    $updated_at = $faker->dateTimeThisMonth();

    // 传参为生成最大工夫不超过,因为创立工夫永远比更改工夫要早
    $created_at = $faker->dateTimeThisMonth($updated_at);

    return [
        'title' => $sentence,
        'body' => $faker->text(),
        'excerpt' => $sentence,
        'created_at' => $created_at,
        'updated_at' => $updated_at,
    ];
});

数据填充代码

class TopicsTableSeeder extends Seeder
{
   /**
    * Run the database seeds.
    *
    * @return void
    */
   public function run()
   {// 所有用户 ID 数组,如:[1,2,3,4]
       $user_ids = User::all()->pluck('id')->toArray();

       // 所有分类 ID 数组,如:[1,2,3,4]
       $category_ids = Category::all()->pluck('id')->toArray();

       // 获取 Faker 实例
       $faker = app(Faker\Generator::class);

       $topics = factory(Topic::class)
           ->times(1000)
           ->make()
           ->each(function ($topic, $index) use ($user_ids, $category_ids, $faker){
               // 从用户 ID 数组中随机取出一个并赋值
               $topic->user_id = $faker->randomElement($user_ids);

               // 话题分类,同上
               $topic->category_id = $faker->randomElement($category_ids);
           });

       // 将数据汇合转换为数组,并插入到数据库中
       Topic::insert($topics->toArray());
   }
}

咱们通过是 times() 设置了填充的次数,执行数据填充命令,能够将 1000 条数据填充至 topics 表中,这很不便。

php artisan db:seed --class=TopicsTableSeeder

如果咱们想要插入 100w 条数据,是不是把 times() 的参数改为 1000,000 就能够了?当你这样做之后,你会发现如下报错

General error: 1390 Prepared statement contains too many placeholders

这个问题是因为 mysql 默认反对的占位符最多为 65535(2^16-1) 个,写入数据为 m 列,n 行。m*n 必须小于 65535。

所以没法一次性插入大量数据,查了一下 php artisan db:seed 也没有提供执行次数的相干参数。

最初,决定应用 shell 脚本进行解决。

for ((i = 0; i < 1000; i++)); do
    /usr/local/bin/php artisan db:seed --class=TopicsTableSeeder
done

期待片刻,你会发现 100w 数据曾经生成结束!

PS:数据仓库和数据填充代码来自 larabbs

正文完
 0