lundi 20 janvier 2020

Need opinion with relations count

Very long time I searching for solution for this problem:

Lets say we have 2 tables one table is Clients table and second table is ClientAssignment table: the ClientAssignment table is related to Clients table:

public function assignment()
{
    return $this->hasOne(ClientAssignment::class, 'client_id');
}

now when I want to count how many ClientAssignment has Clients and i do it like that:

$users =[1,2,3,4 .....]
$userAssignments = array();

foreach ($users as $user) {

    $user_assignments = Client::whereHas('assignment', function ($query) use ($user) {
     $query->where('assigned_id', $user);
    });

     $ua['user_id'] = $user;
     $ua['count'] = $user_assignments->count();
     array_push($userAssignments, $ua);
   }

The code works well but it hits the performance and query execution time ~20 + seconds on a relatively small table with 80k Clients, My question if can be another way to do the same thing but with minimum performance and query execution time hit ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire