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做为排序的条件的时候查询变得异常缓慢,请问有什么解决办法吗

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

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

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

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

2年前 评论

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