0

I would like to get your recommend.

    // Solution one 
    $total_user=User::count();
    $active_user=User::where('active', 'active')->count();
    
    
    // Solution two 
    $user = User::all();
    $total_user=$user->count();
    $active_user=$user->where('active','active')->count();

Note : the number of users will become more then at least 1 million.
Thank all

2 Answers2

2

i think solution 1 is better

because in solution 2 you are getting all the data from database and then counting them and it costs much more time than just counting them in solution 1

so my choice is solution 1

1

1- First we pull all records with a single query

$users = User::select('id', 'active')->get();

Notice : With SELECET, we only bring the selected fields belonging to the user, if there is no need for other important user information for speed.

2- We filter

$active_users    = $users->filter(function ($row) {
   return $row->aktive == 'active';
});

3- Then we write(in view)

Total :{{ count($users) }}
Active :{{ count($active_users) }}
Yunus Kocabay
  • 133
  • 10