Skip to content

BulkUpdate

Alexey Lapin edited this page Jan 22, 2023 · 3 revisions

Public API

You can see the public API in the BulkUpdateContract

Benchmarks

Environment

PHP: 8.1.14

MySQL: 8.0.28

Base test

/** @var GenerateEntityCollectionTestFeature $feature */
$feature = $this->app->make(GenerateEntityCollectionTestFeature::class);
/** @var SaveAndFillEntityCollectionTestFeature $saveFeature */
$saveFeature = $this->app->make(SaveAndFillEntityCollectionTestFeature::class);
/** @var BulkUpdate $bulkUpdate */
$bulkUpdate = $this->app
    ->make(BulkUpdate::class)
    ->chunk(100)
    ->onSaved(
        fn(Collection $collection) => null
    );

$times = [];
$numberOfQueries = [];
for ($i = 0; $i < 100; $i++) {
    // It generates models without saving
    $entities = $feature->handle(MySqlEntityWithAutoIncrement::class, 100);
    // It saves each model and fill in it the new data
    $saveFeature->handle($entities, 100);

    DB::connection('mysql')->enableQueryLog();
    $start = microtime(true);

    $bulkUpdate->update(MySqlEntityWithAutoIncrement::class, $entities);

    $times[] = microtime(true) - $start;
    $numberOfQueries[] = count(DB::connection('mysql')->getQueryLog());
    DB::connection('mysql')->disableQueryLog();
    DB::connection('mysql')->flushQueryLog();
}

echo 'time:' . PHP_EOL;
echo 'median = ' . round(collect($times)->median(), 4) . PHP_EOL;
echo 'avg = ' . round(collect($times)->avg(), 4) . PHP_EOL;
echo 'min = ' . round(collect($times)->min(), 4) . PHP_EOL;
echo 'max = ' . round(collect($times)->max(), 4) . PHP_EOL;

echo 'number of queries:' . PHP_EOL;
echo 'median = ' . round(collect($numberOfQueries)->median(), 4) . PHP_EOL;
echo 'avg = ' . round(collect($numberOfQueries)->avg(), 4) . PHP_EOL;
echo 'min = ' . round(collect($numberOfQueries)->min(), 4) . PHP_EOL;
echo 'max = ' . round(collect($numberOfQueries)->max(), 4) . PHP_EOL;

Output

time:
median = 0.1465
avg = 0.1494
min = 0.1393
max = 0.2452
number of queries:
median = 1
avg = 1
min = 1
max = 1
Clone this wiki locally