diff --git a/README.md b/README.md index affbe22..5466e35 100755 --- a/README.md +++ b/README.md @@ -27,81 +27,64 @@ php artisan migrate:fresh --seed # Analyse Insert -`Insert1` - -Insere um registro por vez. - -```sql -insert into transactions (id, value, date) -values (1, 10, '2023-04-01'); - -insert into transactions (id, value, date) -values (2, 20, '2023-04-02'); - -insert into transactions (id, value, date) -values (3, 30, '2023-04-03'); +```shell +php artisan app:mass-insert --count=100 +php artisan queue:work --queue=default ``` -`Insert2` - -Realiza a inserção de forma multipla com todos os registros sendo passados no `values` de uma unica vez. - -```sql -insert into transactions (id, value, date) -values (1, 10, '2023-04-01'), - (2, 20, '2023-04-02'), - (3, 30, '2023-04-03'), ....; -``` - -> Nesse modelo, existe uma limitação: -> -> SQLSTATE[HY000]: General error: 7 number of parameters must be between 0 and 65535 - -`Insert3` - -Realiza a inserção de forma multipla com todos os registros sendo passados no `values` mas em blocos de `1.000` -registros. - -```sql -insert into transactions (id, value, date) -values (1, 10, '2023-04-01'), - (2, 20, '2023-04-02'), - (3, 30, '2023-04-03'), ....; --limit 1.000 - -insert into transactions (id, value, date) -values (1001, 50, '2023-04-01'), - (1002, 60, '2023-04-02'), - (1003, 70, '2023-04-03'), ....; --limit 1.000 -``` - -[MassInsert1](/app/Tasks/MassInsert/MassInsert1Task.php) +[MassInsert1](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert1Task.php), +[MassInsert2](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert2Task.php), +[MassInsert3](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert3Task.php), +[MassInsert4](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert4Task.php), +[MassInsert5](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert5Task.php), +[MassInsert6](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert6Task.php) ### Result -| 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 | +| quantity | method | time seconds | +|---------:|---------|-------------:| +| 100 | Insert1 | 0,166 | +| 100 | Insert2 | 0,015 | +| 100 | Insert3 | 0,015 | +| 100 | Insert4 | 0,044 | +| 100 | Insert5 | 0,040 | +| 100 | Insert6 | 0,125 | + +| quantity | method | time seconds | +|---------:|---------|-------------:| +| 1.000 | Insert1 | 1,000 | +| 1.000 | Insert2 | 0,087 | +| 1.000 | Insert3 | 0,087 | +| 1.000 | Insert4 | 0,352 | +| 1.000 | Insert5 | 0,343 | +| 1.000 | Insert6 | 1,000 | + +| quantity | method | time seconds | +|---------:|---------|-------------:| +| 10.000 | Insert1 | 13,000 | +| 10.000 | Insert2 | 0,789 | +| 10.000 | Insert3 | 0,795 | +| 10.000 | Insert4 | 3,000 | +| 10.000 | Insert5 | 3,000 | +| 10.000 | Insert6 | 11,000 | + +| quantity | method | time seconds | +|---------:|---------|-------------:| +| 100.000 | Insert1 | `2m 43s` 163 | +| 100.000 | Insert2 | `error` | +| 100.000 | Insert3 | 8 | +| 100.000 | Insert4 | 35 | +| 100.000 | Insert5 | 34 | +| 100.000 | Insert6 | `2m 27s` 147 | + +| quantity | method | time second | +|----------:|---------|------------:| +| 1.000.000 | Insert1 | 1952,320 | +| 1.000.000 | Insert2 | `error` | +| 1.000.000 | Insert3 | 950,000 | +| 1.000.000 | Insert4 | 950,000 | +| 1.000.000 | Insert5 | - | +| 1.000.000 | Insert6 | - | # Analyse Update diff --git a/app/Tasks/MassInsert/MassInsert2Task.php b/app/Tasks/MassInsert/MassInsert2Task.php index a84d36c..907aeda 100644 --- a/app/Tasks/MassInsert/MassInsert2Task.php +++ b/app/Tasks/MassInsert/MassInsert2Task.php @@ -5,6 +5,7 @@ namespace App\Tasks\MassInsert; use App\Models\Transaction; use Carbon\Carbon; use Illuminate\Support\Facades\DB; +use PHPUnit\Event\Runtime\PHP; class MassInsert2Task { @@ -19,6 +20,12 @@ class MassInsert2Task */ public function handle(int $count): void { + if ($count >= 65535) { + echo 'QLSTATE[HY000]: General error: 7 number of parameters must be between 0 and 65535' . PHP_EOL; + + return; + } + $transactions = Transaction::factory($count)->make(); Transaction::query()->insert($transactions->toArray()); } diff --git a/config/app.php b/config/app.php index 6415a10..a30b028 100755 --- a/config/app.php +++ b/config/app.php @@ -188,4 +188,6 @@ return [ // 'Example' => App\Facades\Example::class, ])->toArray(), + + 'items_count' => env('ITEMS_COUNT', 1000) ]; diff --git a/config/update-massive.php b/config/update-massive.php deleted file mode 100644 index a410cab..0000000 --- a/config/update-massive.php +++ /dev/null @@ -1,20 +0,0 @@ - env('ITEMS_COUNT', 1000) -]; diff --git a/database/seeders/TransactionSeeder.php b/database/seeders/TransactionSeeder.php index fb20f7b..eb8f930 100755 --- a/database/seeders/TransactionSeeder.php +++ b/database/seeders/TransactionSeeder.php @@ -14,7 +14,11 @@ class TransactionSeeder extends Seeder */ public function run(): void { - $count = config('update-massive.items_count'); + $count = config('app.items_count'); + + if ($count === 0) { + return; + } (new MassInsert3Task())->handle($count); }