diff --git a/README.md b/README.md index ce6d607..affbe22 100755 --- a/README.md +++ b/README.md @@ -74,22 +74,34 @@ values (1001, 50, '2023-04-01'), (1003, 70, '2023-04-03'), ....; --limit 1.000 ``` +[MassInsert1](/app/Tasks/MassInsert/MassInsert1Task.php) + ### Result -| quantity | method | time seconds | performance | -|----------:|---------|-------------------:|------------:| -| 1.000 | Insert1 | 1,44s | - | -| 1.000 | Insert2 | 0,09s | 16x | -| 1.000 | Insert3 | 0,09s | 16x | -| 10.000 | Insert1 | 14,25s | - | -| 10.000 | Insert2 | 0,88s | 16x | -| 10.000 | Insert3 | 0,83s | 17x | -| 100.000 | Insert1 | (2m 38s) 158,41s | - | -| 100.000 | Insert2 | `error` | `error` | -| 100.000 | Insert3 | 8,25s | 19x | -| 1.000.000 | Insert1 | (31m 52s) 1952,32s | - | -| 1.000.000 | Insert2 | `error` | `error` | -| 1.000.000 | Insert3 | (1m 35s) 95s | 20x | +| quantity | method | time seconds | +|----------:|---------|-------------------:| +| 100 | Insert1 | 0,016s | +| 100 | Insert2 | 0,001s | +| 100 | Insert3 | 0,001s | +| 100 | Insert4 | 0,044s | +| 100 | Insert5 | 0,040s | +| 100 | Insert6 | 0,125s | +| 1.000 | Insert1 | 1,000s | +| 1.000 | Insert2 | 0,09s | +| 1.000 | Insert3 | 0,09s | +| 1.000 | Insert4 | 0,09s | +| 1.000 | Insert5 | 0,09s | +| 1.000 | Insert6 | 0,09s | +| 1.000 | Insert3 | 0,09s | +| 10.000 | Insert1 | 14,25s | +| 10.000 | Insert2 | 0,88s | +| 10.000 | Insert3 | 0,83s | +| 100.000 | Insert1 | (2m 38s) 158,41s | +| 100.000 | Insert2 | `error` | +| 100.000 | Insert3 | 8,25s | +| 1.000.000 | Insert1 | (31m 52s) 1952,32s | +| 1.000.000 | Insert2 | `error` | +| 1.000.000 | Insert3 | (1m 35s) 95s | # Analyse Update diff --git a/app/Console/Commands/MassInsertCommand.php b/app/Console/Commands/MassInsertCommand.php new file mode 100644 index 0000000..21023b7 --- /dev/null +++ b/app/Console/Commands/MassInsertCommand.php @@ -0,0 +1,60 @@ +option('count'); + + Bus::chain([ + (new MassInsert\MassInsert1Job($count)), + (new MassInsert\MassInsert2Job($count)), + (new MassInsert\MassInsert3Job($count)), + (new MassInsert\MassInsert4Job($count)), + (new MassInsert\MassInsert5Job($count)), + (new MassInsert\MassInsert6Job($count)), + ])->dispatch(); + } +} + + + + + + + + + + + + + + + + + + diff --git a/app/Console/Commands/MassUpdateCommand.php b/app/Console/Commands/MassUpdateCommand.php new file mode 100755 index 0000000..3f419f1 --- /dev/null +++ b/app/Console/Commands/MassUpdateCommand.php @@ -0,0 +1,70 @@ +group1(); + $this->group2(); + } + + private function group1(): void + { + Bus::chain([ + new Group1\MassUpdate1Job(), + new Group1\MassUpdate2Job(), + ])->dispatch(); + } + + private function group2() + { + Bus::chain([ + new Group2\MassUpdate3Job(), +// new Group2\MassUpdate4Job(), +// new Group2\MassUpdate5Job(), + new Group2\MassUpdate6Job(), + ])->dispatch(); + } +} + + + + + + + + + + + + + + + + + + diff --git a/app/Console/Commands/UpdateMassiveCommand.php b/app/Console/Commands/UpdateMassiveCommand.php deleted file mode 100755 index eb143b2..0000000 --- a/app/Console/Commands/UpdateMassiveCommand.php +++ /dev/null @@ -1,109 +0,0 @@ -dispatch(); - } -} - - - - - - - - - - - - - - - - - - diff --git a/app/Jobs/MassInsert/MassInsert1Job.php b/app/Jobs/MassInsert/MassInsert1Job.php new file mode 100644 index 0000000..64ae64a --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert1Job.php @@ -0,0 +1,39 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsert/MassInsert2Job.php b/app/Jobs/MassInsert/MassInsert2Job.php new file mode 100644 index 0000000..f61af7a --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert2Job.php @@ -0,0 +1,40 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsert/MassInsert3Job.php b/app/Jobs/MassInsert/MassInsert3Job.php new file mode 100644 index 0000000..9dd766a --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert3Job.php @@ -0,0 +1,41 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsert/MassInsert4Job.php b/app/Jobs/MassInsert/MassInsert4Job.php new file mode 100644 index 0000000..884f944 --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert4Job.php @@ -0,0 +1,42 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsert/MassInsert5Job.php b/app/Jobs/MassInsert/MassInsert5Job.php new file mode 100644 index 0000000..e76dd27 --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert5Job.php @@ -0,0 +1,43 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsert/MassInsert6Job.php b/app/Jobs/MassInsert/MassInsert6Job.php new file mode 100644 index 0000000..8df72b7 --- /dev/null +++ b/app/Jobs/MassInsert/MassInsert6Job.php @@ -0,0 +1,44 @@ +handle($this->count); + } +} diff --git a/app/Jobs/MassInsertRelationship/InsertMassive1Job.php b/app/Jobs/MassInsertRelationship/InsertMassive1Job.php new file mode 100644 index 0000000..824e551 --- /dev/null +++ b/app/Jobs/MassInsertRelationship/InsertMassive1Job.php @@ -0,0 +1,38 @@ +handle(); + } +} diff --git a/app/Jobs/UpdateMassive4Job.php b/app/Jobs/MassUpdate/Group1/MassUpdate1Job.php old mode 100644 new mode 100755 similarity index 78% rename from app/Jobs/UpdateMassive4Job.php rename to app/Jobs/MassUpdate/Group1/MassUpdate1Job.php index 1a654fe..be239ed --- a/app/Jobs/UpdateMassive4Job.php +++ b/app/Jobs/MassUpdate/Group1/MassUpdate1Job.php @@ -1,15 +1,15 @@ handle(); + (new MassUpdate1Task())->handle(); } } diff --git a/app/Jobs/UpdateMassive1Job.php b/app/Jobs/MassUpdate/Group1/MassUpdate2Job.php old mode 100755 new mode 100644 similarity index 78% rename from app/Jobs/UpdateMassive1Job.php rename to app/Jobs/MassUpdate/Group1/MassUpdate2Job.php index f38328d..73cb8b0 --- a/app/Jobs/UpdateMassive1Job.php +++ b/app/Jobs/MassUpdate/Group1/MassUpdate2Job.php @@ -1,15 +1,15 @@ handle(); + (new MassUpdate2Task())->handle(); } } diff --git a/app/Jobs/UpdateMassive2Job.php b/app/Jobs/MassUpdate/Group2/MassUpdate3Job.php similarity index 78% rename from app/Jobs/UpdateMassive2Job.php rename to app/Jobs/MassUpdate/Group2/MassUpdate3Job.php index b397541..71744b9 100644 --- a/app/Jobs/UpdateMassive2Job.php +++ b/app/Jobs/MassUpdate/Group2/MassUpdate3Job.php @@ -1,15 +1,15 @@ handle(); + (new MassUpdate3Task())->handle(); } } diff --git a/app/Jobs/UpdateMassive3Job.php b/app/Jobs/MassUpdate/Group2/MassUpdate4Job.php similarity index 78% rename from app/Jobs/UpdateMassive3Job.php rename to app/Jobs/MassUpdate/Group2/MassUpdate4Job.php index 324c4a7..cec7714 100644 --- a/app/Jobs/UpdateMassive3Job.php +++ b/app/Jobs/MassUpdate/Group2/MassUpdate4Job.php @@ -1,15 +1,15 @@ handle(); + (new MassUpdate4Task())->handle(); } } diff --git a/app/Jobs/MassUpdate/Group2/MassUpdate5Job.php b/app/Jobs/MassUpdate/Group2/MassUpdate5Job.php new file mode 100644 index 0000000..18ffaac --- /dev/null +++ b/app/Jobs/MassUpdate/Group2/MassUpdate5Job.php @@ -0,0 +1,38 @@ +handle(); + } +} diff --git a/app/Jobs/MassUpdate/Group2/MassUpdate6Job.php b/app/Jobs/MassUpdate/Group2/MassUpdate6Job.php new file mode 100644 index 0000000..e979fc1 --- /dev/null +++ b/app/Jobs/MassUpdate/Group2/MassUpdate6Job.php @@ -0,0 +1,38 @@ +handle(); + } +} diff --git a/app/Jobs/UpdateMassive5Job.php b/app/Jobs/UpdateMassive5Job.php deleted file mode 100644 index ff73ff8..0000000 --- a/app/Jobs/UpdateMassive5Job.php +++ /dev/null @@ -1,38 +0,0 @@ -handle(); - } -} diff --git a/app/Tasks/MassInsert/MassInsert1Task.php b/app/Tasks/MassInsert/MassInsert1Task.php new file mode 100644 index 0000000..4e31bc7 --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert1Task.php @@ -0,0 +1,30 @@ +create(); + } +} diff --git a/app/Tasks/MassInsert/MassInsert2Task.php b/app/Tasks/MassInsert/MassInsert2Task.php new file mode 100644 index 0000000..a84d36c --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert2Task.php @@ -0,0 +1,25 @@ +make(); + Transaction::query()->insert($transactions->toArray()); + } +} diff --git a/app/Tasks/MassInsert/MassInsert3Task.php b/app/Tasks/MassInsert/MassInsert3Task.php new file mode 100644 index 0000000..8eec123 --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert3Task.php @@ -0,0 +1,51 @@ + 0) { + + if ($count < $block) { + $block = $count; + } + + $transactions = Transaction::factory($block)->make(); + Transaction::query()->insert($transactions->toArray()); + + + $count -= $block; + } + } +} diff --git a/app/Tasks/MassInsert/MassInsert4Task.php b/app/Tasks/MassInsert/MassInsert4Task.php new file mode 100644 index 0000000..3b2728d --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert4Task.php @@ -0,0 +1,36 @@ +create(); + DB::commit(); + } catch (\Exception $exception) { + DB::rollBack(); + } + } +} diff --git a/app/Tasks/MassInsert/MassInsert5Task.php b/app/Tasks/MassInsert/MassInsert5Task.php new file mode 100644 index 0000000..8639e01 --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert5Task.php @@ -0,0 +1,51 @@ + 0) { + + if ($count < $block) { + $block = $count; + } + + DB::beginTransaction(); + try { + Transaction::factory($block)->create(); + + DB::commit(); + $count -= $block; + } catch (\Exception $exception) { + DB::rollBack(); + } + } + } +} diff --git a/app/Tasks/MassInsert/MassInsert6Task.php b/app/Tasks/MassInsert/MassInsert6Task.php new file mode 100644 index 0000000..dcc16d2 --- /dev/null +++ b/app/Tasks/MassInsert/MassInsert6Task.php @@ -0,0 +1,48 @@ +make(); + + foreach ($transactions as $transaction) { + $sql3 = vsprintf('EXECUTE transactions_plan5 (%s, \'%s\')', [ + (float)$transaction->value, + $transaction->date->format('Y-m-d H:i:s') + ]); + + DB::unprepared($sql3); + } + } +} diff --git a/app/Tasks/MassUpdate/Group1/MassUpdate1Task.php b/app/Tasks/MassUpdate/Group1/MassUpdate1Task.php new file mode 100755 index 0000000..236272a --- /dev/null +++ b/app/Tasks/MassUpdate/Group1/MassUpdate1Task.php @@ -0,0 +1,39 @@ +select([ + 'id', + ]) + ->where('id', '>', 0) + ->chunkById(1000, function ($transactions) { + foreach ($transactions as $transaction) { + Transaction::query() + ->where('id', '=', $transaction->id) + ->update([ + 'date' => Carbon::now(), + 'value' => 1, + ]); + } + }); + + } +} diff --git a/app/Tasks/UpdateMassive2Task.php b/app/Tasks/MassUpdate/Group1/MassUpdate2Task.php similarity index 60% rename from app/Tasks/UpdateMassive2Task.php rename to app/Tasks/MassUpdate/Group1/MassUpdate2Task.php index c971991..9dab16e 100644 --- a/app/Tasks/UpdateMassive2Task.php +++ b/app/Tasks/MassUpdate/Group1/MassUpdate2Task.php @@ -1,13 +1,21 @@ select([ + 'id', + 'date', + 'value', + ]) + ->where('id', '>', 0) + ->chunkById(1000, function ($transactions) { + + $values = []; + /** @var Collection $transactions */ + foreach ($transactions as $transaction) { + $transaction->date = Carbon::now()->subDays(random_int(1, 3)); + $transaction->value = 10 + random_int(0, 10); + + $values[] = [ + 'id' => $transaction->id, + 'date' => $transaction->date, + 'value' => $transaction->value, + ]; + } + + DB::table('transactions as t') + ->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id') + ->updateFrom([ + 'value' => DB::raw('m.value::decimal'), + 'date' => DB::raw('m.date::timestamp'), + ]); + }); + } + +} diff --git a/app/Tasks/MassUpdate/Group2/MassUpdate6Task.php b/app/Tasks/MassUpdate/Group2/MassUpdate6Task.php new file mode 100644 index 0000000..9262e14 --- /dev/null +++ b/app/Tasks/MassUpdate/Group2/MassUpdate6Task.php @@ -0,0 +1,66 @@ +select([ + 'id', + 'date', + 'value', + ]) + ->where('id', '>', 0) + ->chunkById(1000, function ($transactions) { + + $items = []; + /** @var Collection $transactions */ + foreach ($transactions as $transaction) { + $transaction->date = Carbon::now()->subDays(random_int(1, 3)); + $transaction->value = 10 + random_int(0, 10); + + $items[] = [ + 'id' => $transaction->id, + 'date' => $transaction->date, + 'value' => $transaction->value, + ]; + } + + $withs = []; + $unions = []; + foreach ($items as $key => $item) { + $withs[] = vsprintf('u%s as (update transactions set value = %s where id = %s returning null)', [ + $key, + $item['value'], + $item['id'], + ]); + + $unions[] = vsprintf('u%s', [ + $key + ]); + } + + $withs = implode(', ', $withs); + $unions = implode(', ', $unions); + + $sql = "with $withs, uall as (select * from $unions) select * from uall"; + + DB::statement($sql); + }); + } + +} diff --git a/app/Tasks/UpdateMassive1Task.php b/app/Tasks/UpdateMassive1Task.php deleted file mode 100755 index f4a0465..0000000 --- a/app/Tasks/UpdateMassive1Task.php +++ /dev/null @@ -1,40 +0,0 @@ -select([ - 'id', - ]) - ->where('id', '>', 0) - ->chunkById(1000, function ($transactions) { - -// DB::beginTransaction(); -// try { - foreach ($transactions as $transaction) { - Transaction::query() - ->where('id', '=', $transaction->id) - ->update([ - 'date' => Carbon::now(), - 'value' => 1, - ]); - } - DB::commit(); -// } catch (\Exception $exception) { -// DB::rollBack(); -// } - - }); - - } -} diff --git a/app/Tasks/UpdateMassive5Task.php b/app/Tasks/UpdateMassive5Task.php deleted file mode 100644 index d7e848f..0000000 --- a/app/Tasks/UpdateMassive5Task.php +++ /dev/null @@ -1,254 +0,0 @@ -select([ - 'id', - 'date', - 'value', - ]) - ->where('id', '>', 0) - ->chunkById(1000, function ($transactions) { - - $values = []; - /** @var Collection $transactions */ - foreach ($transactions as $transaction) { - $transaction->date = Carbon::now()->subDays(random_int(1, 3)); - $transaction->value = 10 + random_int(0, 10); - -// $values[] = [ -// 'primary_key' => $transaction->id, -// 'columns' => [ -// 'date' => "'$transaction->date'::timestamp", -// 'value' => $transaction->value, -// ] -// ]; - - $values[] = [ - 'id' => $transaction->id, - 'date' => $transaction->date, - 'value' => $transaction->value, - ]; - } - -// $values = [ -// [ -// 'id' => 1, -// 'date' => Carbon::now(), -// 'value' => 10, -// ], -// [ -// 'id' => 2, -// 'date' => '2023-01-02', -// 'value' => 20, -//// 'value' => DB::raw('t.value + 1'), -// ] -// ]; -// -// $values = [ -//// [ -//// 'id' => DB::raw('1::int8'), -//// 'date' => DB::raw('\'' . Carbon::now()->format('Y-m-d H:i:s') . '\'::timestamp'), -//// 'value' => DB::raw('10::numeric'), -//// ], -// [ -// 'id' => 2, -// 'date' => '2023-01-02', -// 'value' => 20, -//// 'value' => DB::raw('t.value + 1'), -// ] -// ]; - - - DB::enableQueryLog(); - -// DB::table('transactions') -// ->insert($values); - - $values = [ - [ - 'id' => 1, - 'value' => 20, -// 'day' => 2, - ], - [ - 'id' => 2, - 'value' => 30, -// 'day' => 5, - ], - [ - 'value' => 30, - 'id' => 3, -// 'day' => 3, - - ], - ]; - - - DB::table('transactions as t') - ->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id') - ->updateFrom([ - 'value' => DB::raw('m.value::decimal'), - ]); - - - DB::table('transactions as t') - ->massUpdate([ - [ - 'id' => 1, - 'value' => DB::raw('20::float'), - 'date' => '2023-01-01', - ], - [ - 'id' => 2, - 'value' => 30, - 'date' => '2023-01-01', - ], - [ - 'id' => 1, - 'value' => 30, - 'date' => '2023-01-01', - ], - ], 'id'); - - -// $properties = array_keys($values[0]); -// $x = []; -// $propertiesCount = count($properties); -// foreach ($values as $item) { -// foreach ($properties as $property) { -// if (count(array_keys($item)) !== $propertiesCount) { -// throw new \Exception('a quantidade de propriedades é diferente'); -// } -// -// $x [] = $item[$property]; -// } -// } -// -//// dd($x); -//// DB::table('transactions as t') -//// ->massUpdate($values, $uniqueKeys = ['id', 'value'], ); -// -// DB::table('transaction as t') -// ->whereIn('id', [1,2,3]) -// ->update([ -// 'value'=> DB::raw('value + (case id when 1 then 2.0 when 2 then 5.0 when 3 then 2.32 end)') -// ]); - - -// $table = DB::raw('(values (?, ?), (?, ?), (?, ?)) as "m" (' . implode(', ', $properties) . ')'); - - DB::enableQueryLog(); - DB::table('transactions as t') -// ->join($table, DB::raw('m.id::bigint'), '=', 't.id') -// ->addBinding($x) - ->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id') -// ->where('t.date', '>', Carbon::now()->subDays(5)) - ->updateFrom([ -// "value" => DB::raw('t.value + "m"."value"::decimal'), -// "date" => DB::raw('(t.date::timestamp + (interval \'1\' day * m.day::int))'), - "value" => DB::raw('m.value::decimal'), - ]); - - dd(DB::getQueryLog()); - dd('aa'); - -// t.value = m.value + 1; -// t.value = m.value + 1; - - - DB::table('transactions as t') - ->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id') - ->updateFrom([ - 'value' => DB::raw('m.value::decimal'), - ]); - dd(DB::getQueryLog()); - - $values = [ - [ - 'id' => 1, - 'date' => Carbon::now(), - 'value' => 20, - ], - [ - 'id' => 2, - 'date' => '2023-01-02', - 'value' => 30, - ], - ]; - - - DB::table('transactions as t') - ->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id') - ->updateFrom([ - 'value' => DB::raw('(m.value::decimal + 1)::decimal'), - 'date' => DB::raw('m.date::timestamp'), - 'type' => 2, - 'status' => DB::raw('case t.id when 1 then \'paid\' else t.status end'), - ]); - - -// DB::table('transactions as t') -// ->join(DB::raw('(values (1,\'2019-01-15 10:00:00\',10), (2,\'2019-01-15 10:00:00\', 20) ) as mu (id, value, date)'), 'mu.id', '=', 't.id') -// ->updateFrom([ -// 'value' => DB::raw('(mu.value::decimal + 1)::decimal'), -// 'date' => DB::raw('mu.date::timestamp'), -// 'type' => 2, -// 'status' => DB::raw('case t.id when 1 then \'paid\' else t.status end'), -// ]); - - dd(DB::getQueryLog()); - dd('aa'); -// DB::table('transactions as t') -// ->massUpdateWithFrom( -// values: [ -// [ -// 'id' => 1, -// 'date' => Carbon::now(), -// 'value' => 10, -// ] -// ], -//// valuesAlias: 'mu', -//// updateColumns: [ -//// 'date' => 'mu.date::timestamp', -//// 'value' => 'mu.value + 1', -//// 'type' => '2', -//// 'fee' => '(t.value / mu.value) * 0.1', -//// ] -// ); - -// $queryLog = DB::getQueryLog(); -// -// $sql = $queryLog[1]['query']; -// $bindings = $queryLog[1]['bindings']; -// -// foreach ($bindings as $i => $binding) { -// if ($binding instanceof \DateTimeInterface) { -// $bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); -// } else { -// $bindings[$i] = "'$binding'"; -// } -// } -// -// $sql = vsprintf(str_replace('?', '%s', $sql), $bindings); -// -// dd($sql); - -// -// dd('aa'); -// $updateMassive2 = new UpdateMassive5(); -// $updateMassive2->apply('transactions', 'id', $values); - }); - } - -} diff --git a/database/factories/TransactionFactory.php b/database/factories/TransactionFactory.php index 5e07dbd..fc601ac 100755 --- a/database/factories/TransactionFactory.php +++ b/database/factories/TransactionFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Str; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> @@ -17,6 +18,7 @@ class TransactionFactory extends Factory public function definition(): array { return [ +// 'id' => Str::uuid(), 'date' => fake()->dateTimeBetween('-30 days'), 'value' => fake()->numerify('####.##') ]; diff --git a/database/seeders/TransactionSeeder.php b/database/seeders/TransactionSeeder.php index e67985a..fb20f7b 100755 --- a/database/seeders/TransactionSeeder.php +++ b/database/seeders/TransactionSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Transaction; +use App\Tasks\MassInsert\MassInsert3Task; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; @@ -15,289 +16,6 @@ class TransactionSeeder extends Seeder { $count = config('update-massive.items_count'); - $type = 3; - - if ($type === 1) { - $this->type1($count); - } - - if ($type === 2) { - $this->type2($count); - } - - if ($type === 3) { - $this->type3($count); - } - - if ($type === 4) { - $this->type4($count); - } - - if ($type === 5) { - $this->type5($count); - } - - if ($type === 6) { - $this->type6($count); - } - - if ($type === 7) { - $this->type7($count); - } - } - - /** - * @param mixed $count - * @return void - */ - public function type1(mixed $count): void - { - /** - * insert unit - * 1000 - Database\Seeders\TransactionSeeder ...... 1,443.86 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ..... 14,256.24 ms DONE - * 100000 - Database\Seeders\TransactionSeeder .... 158,413.75 ms DONE - * 1000000 - - * - * 1. insert into transactions (id, value, date) values (1, 10, '2023-04-01'); - * - reconstroi os indices, verifica chave, view materializada - * - * 2. insert into transactions (id, value, date) values (2, 20, '2023-04-02'); - * - reconstroi os indices, verifica chave, view materializada - */ -// DB::enableQueryLog(); - Transaction::factory($count)->create(); -// dd(DB::getQueryLog()); - -// $result = DB::getQueryLog(); -// dump(collect($result)->sum('time')); - } - - /** - * @param mixed $count - */ - public function type2(mixed $count): void - { - /** - * insert multiple - * 1000 - Database\Seeders\TransactionSeeder ......... 96.11 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ........ 887.48 ms DONE - * 100000 - SQLSTATE[HY000]: General error: 7 number of parameters must be between 0 and 65535 - * 1000000 - SQLSTATE[HY000]: General error: 7 number of parameters must be between 0 and 65535 - */ - $transactions = Transaction::factory($count)->make(); - Transaction::query()->insert($transactions->toArray()); - } - - - /** - * Limit insert sql string - * insert multiple block - * - * 1000 - Database\Seeders\TransactionSeeder ......... 97.71 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ........ 833.27 ms DONE - * 100000 - Database\Seeders\TransactionSeeder ...... 8,256.29 ms DONE - * 1000000 - Database\Seeders\TransactionSeeder ..... 95,473.46 ms DONE - */ - private function type3(mixed $count) - { - - $block = 1000; - while ($count > 0) { - - if ($count < $block) { - $block = $count; - } - - $transactions = Transaction::factory($block)->make(); - Transaction::query()->insert($transactions->toArray()); - // insert into values ( ...), (....), ... 1000 - // commit - - // insert into values ( ...), (....), ... 1000 - // commit - - // insert into values ( ...), (....), ... 1000 - // commit - - $count -= $block; - } - - } - - /** - * insert unit - * 1000 - Database\Seeders\TransactionSeeder ...... 391.11 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ..... 3,741.94 ms DONE - * 100000 - Database\Seeders\TransactionSeeder .... 38,605.27 ms DONE - * 1000000 - - * - * 1. insert into transactions (id, value, date) values (1, 10, '2023-04-01'); - * 2. insert into transactions (id, value, date) values (2, 20, '2023-04-02'); - * .... 100.000 - * - * - reconstroi os indices, verifica chave, view materializada - */ - private function type4(int $count) - { - DB::beginTransaction(); - try { - Transaction::factory($count)->create(); - DB::commit(); - } catch (\Exception $exception) { - DB::rollBack(); - } - } - - /** - * insert unit - * 1000 - Database\Seeders\TransactionSeeder ...... 380.20 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ..... 3,716.59 ms DONE - * 100000 - Database\Seeders\TransactionSeeder .... 39,143.04 ms DONE - * 1000000 - - * - * 1. insert into transactions (id, value, date) values (1, 10, '2023-04-01'); - * 2. insert into transactions (id, value, date) values (2, 20, '2023-04-02'); - * .... 1.000 commit - * - reconstroi os indices, verifica chave, view materializada - * - * 1001. insert into transactions (id, value, date) values (1, 10, '2023-04-01'); - * 1002. insert into transactions (id, value, date) values (2, 20, '2023-04-02'); - * .... 1.000 commit - * - reconstroi os indices, verifica chave, view materializada - * - */ - private function type5(mixed $count) - { - $block = 1000; - while ($count > 0) { - - if ($count < $block) { - $block = $count; - } - - DB::beginTransaction(); - try { - Transaction::factory($block)->create(); - - DB::commit(); -// echo 'commit' . PHP_EOL; - $count -= $block; - } catch (\Exception $exception) { - DB::rollBack(); - } - } - } - - /** - * - * PDO - * Prepared statement insert verificar - * - * PREPARE transactions_plan (decimal, timestamp) AS - * INSERT INTO transactions (value, date) - * VALUES ($1, $2); - * - * EXECUTE transactions_plan(23.44, '2023-01-05 00:00:00'::timestamp); - * - * 1000 - Database\Seeders\TransactionSeeder ...... 1,341.07 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ..... 13,415.95 ms DONE - * 100000 - Database\Seeders\TransactionSeeder ............ - ms DONE - * 1000000 - Database\Seeders\TransactionSeeder ............ - ms DONE - * - * 1000 - Database\Seeders\TransactionSeeder ........ 173.91 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ...... 1,591.86 ms DONE - * 100000 - Database\Seeders\TransactionSeeder ......16,331.67 ms DONE - * 1000000 - Database\Seeders\TransactionSeeder ............ - ms DONE - * - * @param mixed $count - * @return void - */ - private function type6(mixed $count) - { -// DB::enableQueryLog(); - -// DB::beginTransaction(); -// -// try { - -// DB::enableQueryLog(); - - $sql1 = 'PREPARE transactions_plan5 (decimal, timestamp) AS INSERT INTO transactions (value, date) VALUES ($1::decimal, $2::timestamp)'; - DB::unprepared($sql1); - - $transactions = Transaction::factory($count)->make(); - -// $sql2 = 'EXECUTE transactions_plan5 (?, ?)'; - - foreach ($transactions as $transaction) { - $sql3 = vsprintf('EXECUTE transactions_plan5 (%s, \'%s\')', [ - (float)$transaction->value, - $transaction->date->format('Y-m-d H:i:s') - ]); - DB::unprepared($sql3); - -// $dateFormatted = $transaction->date->format('Y-m-d H:i:s'); -// $bindings = [ -// (float)$transaction->value, -// $dateFormatted, -// ]; - -// DB::statement($sql2, $bindings); - } - -// dd(DB::getQueryLog()); -// DB::commit(); -// } catch (\Exception $exception) { -// DB::rollBack(); -// } - -// $result = DB::getQueryLog(); -// dump(collect($result)->sum('time')); - } - - /** - * Limit insert sql string - * insert multiple block - * - * 1000 - Database\Seeders\TransactionSeeder ......... 97.71 ms DONE - * 10000 - Database\Seeders\TransactionSeeder ........ 858.70 ms DONE - * 100000 - Database\Seeders\TransactionSeeder ...... 8,819.44 ms DONE - * 1000000 - Database\Seeders\TransactionSeeder ..... 87,649.37 ms DONE - */ - private function type7(mixed $count) - { - - DB::beginTransaction(); - try { - - $block = 1000; - while ($count > 0) { - - if ($count < $block) { - $block = $count; - } - - $transactions = Transaction::factory($block)->make(); - Transaction::query()->insert($transactions->toArray()); - - // insert into values ( ...), (....), ... 1000 - // insert into values ( ...), (....), ... 1000 - // insert into values ( ...), (....), ... 1000 - - $count -= $block; - } - - DB::commit(); - // commit - } catch (\Exception $exception) { - DB::rollBack(); - } - - } - - private function champion($count) - { - $this->type3($count); + (new MassInsert3Task())->handle($count); } }