main
Johnathan Douglas 2023-07-17 10:22:04 -03:00
parent dfb4a9a1da
commit 711d7247f2
5 changed files with 67 additions and 91 deletions

117
README.md
View File

@ -27,81 +27,64 @@ php artisan migrate:fresh --seed
# Analyse Insert # Analyse Insert
`Insert1` ```shell
php artisan app:mass-insert --count=100
Insere um registro por vez. php artisan queue:work --queue=default
```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');
``` ```
`Insert2` [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),
Realiza a inserção de forma multipla com todos os registros sendo passados no `values` de uma unica vez. [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),
```sql [MassInsert5](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert5Task.php),
insert into transactions (id, value, date) [MassInsert6](https://gitea.nteia.com/johdougss/example-update-massive/src/branch/main/app/Tasks/MassInsert/MassInsert6Task.php)
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)
### Result ### Result
| quantity | method | time seconds | | quantity | method | time seconds |
|----------:|---------|-------------------:| |---------:|---------|-------------:|
| 100 | Insert1 | 0,016s | | 100 | Insert1 | 0,166 |
| 100 | Insert2 | 0,001s | | 100 | Insert2 | 0,015 |
| 100 | Insert3 | 0,001s | | 100 | Insert3 | 0,015 |
| 100 | Insert4 | 0,044s | | 100 | Insert4 | 0,044 |
| 100 | Insert5 | 0,040s | | 100 | Insert5 | 0,040 |
| 100 | Insert6 | 0,125s | | 100 | Insert6 | 0,125 |
| 1.000 | Insert1 | 1,000s |
| 1.000 | Insert2 | 0,09s | | quantity | method | time seconds |
| 1.000 | Insert3 | 0,09s | |---------:|---------|-------------:|
| 1.000 | Insert4 | 0,09s | | 1.000 | Insert1 | 1,000 |
| 1.000 | Insert5 | 0,09s | | 1.000 | Insert2 | 0,087 |
| 1.000 | Insert6 | 0,09s | | 1.000 | Insert3 | 0,087 |
| 1.000 | Insert3 | 0,09s | | 1.000 | Insert4 | 0,352 |
| 10.000 | Insert1 | 14,25s | | 1.000 | Insert5 | 0,343 |
| 10.000 | Insert2 | 0,88s | | 1.000 | Insert6 | 1,000 |
| 10.000 | Insert3 | 0,83s |
| 100.000 | Insert1 | (2m 38s) 158,41s | | 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 | Insert2 | `error` |
| 100.000 | Insert3 | 8,25s | | 100.000 | Insert3 | 8 |
| 1.000.000 | Insert1 | (31m 52s) 1952,32s | | 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 | Insert2 | `error` |
| 1.000.000 | Insert3 | (1m 35s) 95s | | 1.000.000 | Insert3 | 950,000 |
| 1.000.000 | Insert4 | 950,000 |
| 1.000.000 | Insert5 | - |
| 1.000.000 | Insert6 | - |
# Analyse Update # Analyse Update

View File

@ -5,6 +5,7 @@ namespace App\Tasks\MassInsert;
use App\Models\Transaction; use App\Models\Transaction;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use PHPUnit\Event\Runtime\PHP;
class MassInsert2Task class MassInsert2Task
{ {
@ -19,6 +20,12 @@ class MassInsert2Task
*/ */
public function handle(int $count): void 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(); $transactions = Transaction::factory($count)->make();
Transaction::query()->insert($transactions->toArray()); Transaction::query()->insert($transactions->toArray());
} }

View File

@ -188,4 +188,6 @@ return [
// 'Example' => App\Facades\Example::class, // 'Example' => App\Facades\Example::class,
])->toArray(), ])->toArray(),
'items_count' => env('ITEMS_COUNT', 1000)
]; ];

View File

@ -1,20 +0,0 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
| "memcached", "redis", "dynamodb", "array"
|
*/
'items_count' => env('ITEMS_COUNT', 1000)
];

View File

@ -14,7 +14,11 @@ class TransactionSeeder extends Seeder
*/ */
public function run(): void public function run(): void
{ {
$count = config('update-massive.items_count'); $count = config('app.items_count');
if ($count === 0) {
return;
}
(new MassInsert3Task())->handle($count); (new MassInsert3Task())->handle($count);
} }