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