laravel orm 使用withcount排序怎么优化加载速度

$data = ChildAccount::withCount(['phones as phones_month_count'=>function(Builder $query)use($start,$end){
      $query->whereDate('created_at','>=',$start)->whereDate('created_at','<=',$end);
},'apply as apply_count' =>function (Builder $query)use($start,$end) {
      $query->whereDate('created_at','>=',$start)->whereDate('created_at','<=',$end);
},'apply as apply_pass_count' =>function (Builder $query)use($start,$end) {
      $query->whereDate('created_at','>=',$start)->whereDate('created_at','<=',$end);
      $query->where('status',Apply::STATUS_PASS);
}])->where(function (Builder $query) use($locations,$location){
  if($location){
      $query->where('location',$location);
  }else{
      if($locations){
        $query->whereIn('location',explode(',',$locations));
      }
   }
 })->orderBy('phones_month_count','desc')->paginate($page_size);

在使用withCount做为排序的条件的时候查询变得异常缓慢,请问有什么解决办法吗

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

我覺得你要是不信orm,就看他是怎麽實現的,能不能優化。實在懶得看就自己寫原生sql。我屬於懶得看也懶得優化那總。 :joy:

3年前 评论
qizishuo (楼主) 3年前
风吹过有夏天的味道 3年前
openstrong

建议:phones_month_count 作为表字段存入维护。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!