Laravel(Eloquent): chunk() vs cursor() (转载)

1、用seeder生成测试数据

<?php
// database/seeds/UsersSeeder.php

use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->delete();

        $faker = Faker\Factory::create('ja_JP');
        $total = 100000;
        $batchSize = 100;

        for ($i = 0, $limit = $total / $batchSize; $i < $limit; $i++) {
            DB::table('users')->insert($this->makeData($faker, $batchSize));
            printf("%d/%d\n", $i * $batchSize, $total);
        }
    }

    private function makeData(\Faker\Generator $faker, $batchSize)
    {
        $data = [];

        for ($i = 0; $i < $batchSize; $i++) {
            $data[] = [
                'name'     => $faker->userName,
                'email'    => mt_rand() . $faker->email,
                'password' => $faker->password(6),
            ];
        }

        return $data;
    }
}
复制代码
<?php

// database/seeds/DatabaseSeeder.php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersSeeder::class);
    }
}
复制代码
php artisan db:seed
复制代码

2、测试数据

php artisan make:command PerformanceTestCommand --command=performance:test
复制代码
<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;

class PerformanceTestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'performance:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = microtime(true);

//        $this->all();
//        $this->get();
//        $this->chunk(10000);
//        $this->chunk(1000);
//        $this->chunk(100);
//        $this->cursor();

        $time = microtime(true) - $start;
        $memory = memory_get_peak_usage(true) / 1024 / 1024;

        $this->output->writeln(sprintf('time: %f memory: %f MB', $time, $memory));
    }

    private function all()
    {
        foreach (User::all() as $user) {
            // do nothing
        }
    }

    private function get()
    {
        foreach (User::query()->get() as $user) {
            // do nothing
        }
    }

    private function chunk($count)
    {
        User::query()->chunk(
            $count,
            function ($users) {
                foreach ($users as $user) {
                    // do nothing
                }
            }
        );
    }

    private function cursor()
    {
        foreach (User::query()->cursor() as $user) {
            // do nothing
        }
    }
}
复制代码

3、测试结果

php artisan performance:test
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值