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做为排序的条件的时候查询变得异常缓慢,请问有什么解决办法吗
我覺得你要是不信orm,就看他是怎麽實現的,能不能優化。實在懶得看就自己寫原生sql。我屬於懶得看也懶得優化那總。 :joy:
建议:
phones_month_count
作为表字段存入维护。