Home> PHP Framework> Laravel> body text

Detailed explanation of how Laravel Seeder generates millions of simulation data

藏色散人
Release: 2021-03-30 09:14:06
forward
2710 people have browsed it

The following tutorial column will introduce you to Laravel Seeder to generate millions of simulated data. I hope it will be helpful to friends in need!

Laravel integrates the Faker library and provides Seeder to help us easily generate simulation data.

Detailed explanation of how Laravel Seeder generates millions of simulation dataFirst write the data warehouse and data filling code

Data warehouse code

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, ];});
Copy after login

Data filling code

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()); }}
Copy after login

We set the filling through times() Number of times, execute the data filling command to fill 1000 pieces of data into the topics table, which is very convenient.

php artisan db:seed --class=TopicsTableSeeder
Copy after login

If we want to insert 1 million pieces of data, can we just change the parameters of times() to 1000,000? When you do this, you will find the following error

General error: 1390 Prepared statement contains too many placeholders
Copy after login

This problem is because mysql supports a maximum of 65535 (2^16-1) placeholders by default, and the written data is m columns and n rows . m*n must be less than 65535.

So it is impossible to insert a large amount of data at one time. After checking

php artisan db:seed

, there is no relevant parameter for the number of executions.

Finally, I decided to use a shell script to solve it.

for (( i = 0; i < 1000; i++ )); do /usr/local/bin/php artisan db:seed --class=TopicsTableSeederdone
Copy after login
Wait for a moment, you will find that 1 million data has been generated!

PS: The data warehouse and data filling code comes from larabbs

Recommended:

The latest five Laravel video tutorials

The above is the detailed content of Detailed explanation of how Laravel Seeder generates millions of simulation data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!