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

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 2

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

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

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

3年前 评论

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