diff --git a/app/Controller/PersonController.php b/app/Controller/PersonController.php index 7f4dc419..36e90f4e 100644 --- a/app/Controller/PersonController.php +++ b/app/Controller/PersonController.php @@ -60,11 +60,7 @@ public function search(RequestInterface $request, ResponseInterface $response): { $term = $request->getQueryParams()['t'] ?? null; if ($term) { - $list = Person::where(function (Builder $query) use ($term) { - $query->whereRaw("lower(name) like lower(\"%{$term}%\")") - ->orWhereRaw("lower(nick) like lower(\"%{$term}%\")") - ->orWhereRaw("lower(stack) like lower(\"%{$term}%\")"); - })->limit(50)->get(); + $list = Person::whereRaw('searchable like ?', [strtolower("%{$term}%")])->limit(50)->get(); return $response->json($list); } diff --git a/app/Model/Person.php b/app/Model/Person.php index b28dfd9f..36bc4c62 100644 --- a/app/Model/Person.php +++ b/app/Model/Person.php @@ -29,6 +29,11 @@ class Person extends Model 'stack', ]; + protected array $hidden = ['searchable']; + + /** + * The attributes that should be cast to native types. + */ protected array $casts = [ 'id' => 'string', 'stack' => 'array' diff --git a/app/Request/PersonRequest.php b/app/Request/PersonRequest.php index 049da998..15d80d4a 100644 --- a/app/Request/PersonRequest.php +++ b/app/Request/PersonRequest.php @@ -49,6 +49,7 @@ public function toPerson(): array 'name' => $data['nome'], 'birth' => $data['nascimento'], 'stack' => Json::encode($data['stack'] ?? null), + 'searchable' => strtolower(join(" ", [$data['apelido'], $data['nome'], join(" ", $data['stack'] ?? [])])) ]; } } diff --git a/config/autoload/databases.php b/config/autoload/databases.php index 08d3de6d..60531842 100644 --- a/config/autoload/databases.php +++ b/config/autoload/databases.php @@ -24,9 +24,9 @@ 'prefix' => env('DB_PREFIX', ''), 'pool' => [ 'min_connections' => 1, - 'max_connections' => 10, + 'max_connections' => 1000000, 'connect_timeout' => 10.0, - 'wait_timeout' => 3.0, + 'wait_timeout' => 10.0, 'heartbeat' => -1, 'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60), ], diff --git a/docker-compose.yml b/docker-compose.yml index 8d8bbd6d..0742182c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,8 +19,8 @@ services: deploy: resources: limits: - cpus: '0.5' - memory: '1.0GB' + cpus: '0.25' + memory: '0.7GB' api2: <<: *api @@ -40,8 +40,8 @@ services: deploy: resources: limits: - cpus: '0.15' - memory: '0.4GB' + cpus: '0.65' + memory: '1.0GB' cache: container_name: rinha-de-backend-cache diff --git a/migrations.sql b/migrations.sql index cde10ed0..91f554a8 100644 --- a/migrations.sql +++ b/migrations.sql @@ -2,6 +2,7 @@ create table person( id varchar(36) primary key unique, nick varchar(32) not null, name varchar(100) not null, + searchable longtext not null, birth date not null, stack json );