0

我想得到你的推荐。

    // 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();

注意:用户数量将超过至少 100 万。
谢谢大家

4

2 回答 2

2

我认为解决方案1更好

因为在解决方案 2 中,您从数据库中获取所有数据,然后对它们进行计数,这比仅在解决方案 1 中计算它们花费的时间要多得多

所以我的选择是解决方案 1

于 2021-01-19T11:28:11.407 回答
1

1-首先我们用一个查询提取所有记录

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

注意:使用 SELECET,我们只带上属于用户的选定字段,如果不需要其他重要的用户信息以提高速度。

2-我们过滤

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

3-然后我们写(在视图中)

Total :{{ count($users) }}
Active :{{ count($active_users) }}
于 2021-01-19T11:29:03.977 回答